Skip to content

Commit 574192d

Browse files
committed
feat: Update templates for hardware model v2
Updated the board templates for "zmk keyboard new" to support Zephyr's hardware model v2. Also updated links to Zephyr's documentation to point to Zephyr 4.1 to match the ZMK update that brings in hardware model v2.
1 parent 39f419f commit 574192d

34 files changed

+143
-74
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ Run `zmk keyboard list` to print a list of supported keyboard hardware.
157157

158158
If ZMK doesn't support your keyboard yet, you can run `zmk keyboard new` to create a new keyboard from a template.
159159

160-
This won't walk you through all of the details of adding support for a new keyboard, but it will generate most of the boilerplate for you. See the [New Keyboard Shield](https://zmk.dev/docs/development/hardware-integration/new-shield) guide for how to finish writing the keyboard files.
160+
Note that this will generate most of the boilerplate for you, but you will still need to manually edit the files to match your keyboard. See the [New Keyboard Shield](https://zmk.dev/docs/development/hardware-integration/new-shield) guide for how to finish writing the keyboard files.
161161

162162
## Module Management
163163

164-
[Zephyr modules](https://docs.zephyrproject.org/3.5.0/develop/modules.html) can add support for new keyboards, behaviors, and other features to ZMK. Use the `zmk module` command to install modules into your repo:
164+
[Zephyr modules](https://docs.zephyrproject.org/4.1.0/develop/modules.html) can add support for new keyboards, behaviors, and other features to ZMK. Use the `zmk module` command to install modules into your repo:
165165

166166
```sh
167167
zmk module add # Add a module

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ disable = [
5858
"too-many-instance-attributes",
5959
"too-many-locals",
6060
"too-many-positional-arguments",
61+
"too-many-statements",
6162
]
6263

6364
[tool.setuptools]

zmk/commands/keyboard/new.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def keyboard_new(
117117
callback=_short_name_callback,
118118
),
119119
] = None,
120+
vendor: Annotated[
121+
str | None, typer.Option("--vendor", "-v", help="Keyboard vendor ID")
122+
] = None,
120123
keyboard_type: Annotated[
121124
KeyboardType | None,
122125
typer.Option(
@@ -129,6 +132,7 @@ def keyboard_new(
129132
KeyboardPlatform | None,
130133
typer.Option(
131134
"--platform",
135+
"--soc",
132136
"-p",
133137
help="If creating a board, the platform/SoC on which it is based.",
134138
),
@@ -171,6 +175,11 @@ def keyboard_new(
171175
if not keyboard_type:
172176
keyboard_type = _prompt_keyboard_type()
173177

178+
if not vendor and keyboard_type == KeyboardType.BOARD:
179+
print("Boards are typically organized into folders by vendor name.")
180+
print("(You may leave this blank to not create a vendor folder.)")
181+
vendor = VendorPrompt.ask()
182+
174183
if not interconnect_id and keyboard_type == KeyboardType.SHIELD:
175184
interconnect = _prompt_interconnect(repo)
176185
else:
@@ -193,6 +202,7 @@ def keyboard_new(
193202
short_name=short_name,
194203
keyboard_id=keyboard_id,
195204
interconnect=interconnect,
205+
vendor=vendor,
196206
)
197207

198208
dest = board_root / template.dest
@@ -357,6 +367,22 @@ def ask( # pyright: ignore[reportIncompatibleMethodOverride]
357367
return result
358368

359369

370+
class VendorPrompt(NamePromptBase):
371+
"""Prompt for a vendor identifier."""
372+
373+
@classmethod
374+
def validate(cls, value: str) -> None:
375+
if not value:
376+
# Vendor is allowed to be blank
377+
return
378+
379+
_validate_id(value)
380+
381+
@classmethod
382+
def ask(cls) -> str: # pyright: ignore[reportIncompatibleMethodOverride]
383+
return super().ask("Enter an ID for the vendor")
384+
385+
360386
_DEFAULT_ARCH = "arm"
361387
_PLATFORM_ARCH: dict[KeyboardPlatform, str] = {
362388
KeyboardPlatform.NRF52840: "arm",
@@ -376,15 +402,20 @@ def _get_template(
376402
short_name: str,
377403
keyboard_id: str,
378404
interconnect: Interconnect | None = None,
405+
vendor: str | None = None,
379406
):
380407
template = TemplateData()
381408
template.data["id"] = keyboard_id
382409
template.data["name"] = keyboard_name
383410
template.data["shortname"] = short_name
411+
template.data["vendor"] = vendor or ""
384412
template.data["keyboard_type"] = str(keyboard_type)
385413
template.data["interconnect"] = ""
386414
template.data["arch"] = ""
387415
template.data["gpio"] = _DEFAULT_GPIO
416+
template.data["soc"] = (
417+
"" if keyboard_platform == KeyboardPlatform.OTHER else str(keyboard_platform)
418+
)
388419

389420
match keyboard_type:
390421
case KeyboardType.SHIELD:
@@ -404,7 +435,7 @@ def _get_template(
404435
template.data["gpio"] = _PLATFORM_GPIO.get(keyboard_platform, _DEFAULT_GPIO)
405436

406437
template.folder = f"board/{keyboard_platform}/"
407-
template.dest = f"{arch}/{keyboard_id}"
438+
template.dest = f"{vendor}/{keyboard_id}" if vendor else keyboard_id
408439

409440
match keyboard_layout:
410441
case KeyboardLayout.UNIBODY:

zmk/templates/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ def get_template_files(
3939
template_path = _ROOT_PATH / folder
4040

4141
for file in template_path.rglob("*"):
42-
template_name = str(file.relative_to(_ROOT_PATH))
43-
template = lookup.get_template(template_name)
42+
name_template = Template(text=file.name, strict_undefined=True)
43+
name = cast(str, name_template.render_unicode(**data))
4444

45-
file_name = Template(text=file.name, strict_undefined=True)
45+
# Board identifiers can contain forward slashes. File names cannot.
46+
name = name.replace("/", "_")
4647

47-
yield (
48-
cast(str, file_name.render_unicode(**data)),
49-
_ensure_trailing_newline(cast(str, template.render_unicode(**data))),
50-
)
48+
file_template_name = str(file.relative_to(_ROOT_PATH))
49+
file_template = lookup.get_template(file_template_name)
50+
text = _ensure_trailing_newline(cast(str, file_template.render_unicode(**data)))
51+
52+
yield name, text
5153

5254

5355
# Matches <%tag ...>, </%tag ...>, and <%tag ... />. Group 1 is the tag name.
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
# This file was generated from a template. Edit it to match your keyboard.
2-
# See https://docs.zephyrproject.org/3.5.0/hardware/porting/board_porting.html#write-kconfig-files
2+
# See https://docs.zephyrproject.org/4.1.0/hardware/porting/board_porting.html#write-kconfig-files
33
# for more information.
44

55
if ${self.guard() | trim}
66

7-
config BOARD_ENABLE_DCDC
8-
bool "DCDC mode"
9-
select SOC_DCDC_NRF52X
10-
default y
11-
12-
# Uncomment this block if you are powering the SoC from the VDDH pin and have
13-
# an inductor between VDD and DCCH. Otherwise, delete this block.
14-
# WARNING: uncommenting this without the correct inductor will brick the device
15-
# and require reprogramming it with a J-Link or similar debug probe!
16-
17-
# config BOARD_ENABLE_DCDC_HV
18-
# bool "High Voltage DCDC converter"
19-
# select SOC_DCDC_NRF52X_HV
20-
# default y
7+
# Define any board-specific Kconfig options here.
8+
# If there are none, you may delete this file.
219

2210
endif # ${self.guard() | trim}

zmk/templates/board/nrf52840/Kconfig.defconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was generated from a template. Edit it to match your keyboard.
2-
# See https://docs.zephyrproject.org/3.5.0/hardware/porting/board_porting.html#write-kconfig-files
2+
# See https://docs.zephyrproject.org/4.1.0/hardware/porting/board_porting.html#write-kconfig-files
33
# for more information.
44

55
<%block name="before" />

zmk/templates/board/nrf52840/board.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was generated from a template. Edit it to match your keyboard.
2-
# See https://docs.zephyrproject.org/3.5.0/develop/test/twister.html#board-configuration
2+
# See https://docs.zephyrproject.org/4.1.0/develop/test/twister.html#board-configuration
33
# for more information.
44

55
identifier: ${id}

zmk/templates/board/nrf52840/board_defconfig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# This file was generated from a template. Edit it to match your keyboard.
2-
# See https://docs.zephyrproject.org/3.5.0/hardware/porting/board_porting.html#write-kconfig-files
2+
# See https://docs.zephyrproject.org/4.1.0/hardware/porting/board_porting.html#write-kconfig-files
33
# for more information.
44

5-
CONFIG_SOC_SERIES_NRF52X=y
6-
CONFIG_SOC_NRF52840_QIAA=y
7-
CONFIG_BOARD_${id.upper()}=y
8-
95
# Enable MPU
106
CONFIG_ARM_MPU=y
117

zmk/templates/board/nrf52840/common_outer.dtsi

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Uncomment this to switch from the LDO to the DC/DC regulator on REG0.
2+
// Do not enable the DC/DC regulator without an LC filter connected to the DCCH
3+
// pin, or the keyboard will not boot until an LC filter is connected.
4+
// &reg0 {
5+
// status = "okay";
6+
// };
7+
8+
// Uncomment this to switch from the LDO to the DC/DC regulator on REG1.
9+
// Do not enable the DC/DC regulator without an LC filter connected to the DCC
10+
// pin, or the keyboard will not boot until an LC filter is connected.
11+
// &reg1 {
12+
// regulator-initial-mode = <NRF5X_REG_MODE_DCDC>;
13+
// };
14+
115
&adc {
216
status = "okay";
317
};
@@ -22,13 +36,18 @@
2236
pinctrl-names = "default", "sleep";
2337
};
2438

39+
// If you use the NFC pins as GPIOs, uncomment this. Otherwise, remove it.
40+
// &uicr {
41+
// nfct-pins-as-gpios;
42+
// };
43+
2544
zephyr_udc0: &usbd {
2645
status = "okay";
2746
};
2847

2948
&flash0 {
3049
// Adjust this flash map as is necessary for your board. For more information, see
31-
// https://docs.zephyrproject.org/3.5.0/services/storage/flash_map/flash_map.html#relationship-with-devicetree
50+
// https://docs.zephyrproject.org/4.1.0/services/storage/flash_map/flash_map.html#relationship-with-devicetree
3251
partitions {
3352
compatible = "fixed-partitions";
3453
#address-cells = <1>;

zmk/templates/board/nrf52840/pinctrl.dtsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file was generated from a template. Edit it to match your keyboard.
2-
// See https://docs.zephyrproject.org/3.5.0/hardware/porting/board_porting.html
3-
// and https://docs.zephyrproject.org/3.5.0/hardware/pinctrl/index.html for
2+
// See https://docs.zephyrproject.org/4.1.0/hardware/porting/board_porting.html
3+
// and https://docs.zephyrproject.org/4.1.0/hardware/pinctrl/index.html for
44
// more instructions.
55

66
#include <dt-bindings/pinctrl/nrf-pinctrl.h>

0 commit comments

Comments
 (0)