diff --git a/.gitignore b/.gitignore index a2beb18a..e02cca0e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ -.west/ -bootloader/ -modules/ -tools/ -zephyr/ -build/ +/.west +/bootloader +/modules +/tools +/zephyr +/build diff --git a/app/boards/arm/planck/planck_rev6.dts b/app/boards/arm/planck/planck_rev6.dts index ef23bfd8..ae002cdd 100644 --- a/app/boards/arm/planck/planck_rev6.dts +++ b/app/boards/arm/planck/planck_rev6.dts @@ -55,7 +55,7 @@ }; kscan { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "Keyscan Matrix"; row-gpios = <&gpioa 10 GPIO_ACTIVE_HIGH>, <&gpioa 9 GPIO_ACTIVE_HIGH>, diff --git a/app/boards/arm/proton_c/board.cmake b/app/boards/arm/proton_c/board.cmake index 4843c415..9da8ea91 100644 --- a/app/boards/arm/proton_c/board.cmake +++ b/app/boards/arm/proton_c/board.cmake @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT -board_runner_args(jlink "--device=STM32F303VC" "--speed=4000") +board_runner_args(dfu-util "--pid=0483:df11" "--alt=0" "--dfuse") +board_runner_args(jlink "--device=STM32F303CC" "--speed=4000") -include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake) +include(${ZEPHYR_BASE}/boards/common/dfu-util.board.cmake) include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) diff --git a/app/boards/shields/clueboard_california/Kconfig.defconfig b/app/boards/shields/clueboard_california/Kconfig.defconfig new file mode 100644 index 00000000..2408f9ff --- /dev/null +++ b/app/boards/shields/clueboard_california/Kconfig.defconfig @@ -0,0 +1,14 @@ + +if SHIELD_CLUEBOARD_CALIFORNIA + +config ZMK_KEYBOARD_NAME + default "Clueboard California Macropad" + +# Unable to use interrupts as the same pin number is used +# across A & B controllers, and STM32F303CCT6 can't enable +# interrutps for multiple controllers for the same "line" +# for the external interrupts. +config ZMK_KSCAN_GPIO_POLLING + default y + +endif diff --git a/app/boards/shields/clueboard_california/Kconfig.shield b/app/boards/shields/clueboard_california/Kconfig.shield new file mode 100644 index 00000000..eca025d1 --- /dev/null +++ b/app/boards/shields/clueboard_california/Kconfig.shield @@ -0,0 +1,5 @@ +# Copyright (c) 2020 Pete Johanson +# SPDX-License-Identifier: MIT + +config SHIELD_CLUEBOARD_CALIFORNIA + def_bool $(shields_list_contains,clueboard_california) diff --git a/app/boards/shields/clueboard_california/clueboard_california.conf b/app/boards/shields/clueboard_california/clueboard_california.conf new file mode 100644 index 00000000..e69de29b diff --git a/app/boards/shields/clueboard_california/clueboard_california.overlay b/app/boards/shields/clueboard_california/clueboard_california.overlay new file mode 100644 index 00000000..9f52d0d3 --- /dev/null +++ b/app/boards/shields/clueboard_california/clueboard_california.overlay @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 Pete Johanson + * + * SPDX-License-Identifier: MIT + */ + +/ { + chosen { + zmk,kscan = &kscan0; + }; + + kscan0: kscan_0 { + compatible = "zmk,kscan-gpio-direct"; + + label = "KSCAN"; + + input-gpios + = <&gpioa 10 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpioa 9 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpioa 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpiob 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpiob 11 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpiob 9 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpioa 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpioa 7 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpiob 1 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + , <&gpiob 2 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + ; + + }; + + // TODO: Per-key LED node(s) +}; + diff --git a/app/boards/shields/clueboard_california/keymap/keymap.overlay b/app/boards/shields/clueboard_california/keymap/keymap.overlay new file mode 100644 index 00000000..f2f49ea4 --- /dev/null +++ b/app/boards/shields/clueboard_california/keymap/keymap.overlay @@ -0,0 +1,24 @@ +#include +#include +#include + +/ { + chosen { + zmk,keymap = &keymap0; + }; + + keymap0: keymap { + compatible = "zmk,keymap"; + label ="Default Kyria Keymap"; + layers = <&default>; + }; + + layers { + compatible = "zmk,layers"; + + default: layer_0 { + label = "DEFAULT"; + keys = ; + }; + }; +}; diff --git a/app/boards/shields/clueboard_california/readme.md b/app/boards/shields/clueboard_california/readme.md new file mode 100644 index 00000000..0255eff7 --- /dev/null +++ b/app/boards/shields/clueboard_california/readme.md @@ -0,0 +1,24 @@ +# [Clueboard California Macropad](https://clueboard.co/parts/clueboard-california-macropad-kit) + +A small 10-key macropad in the shape of the US state of California. + +## Features + +- Per key LED. +- Proton-C only, including space for the piezo speaker. +- Direct GPIO wiring, no GPIO matrix at all. + +## Hardware Notes + +| Switch | Switch Pin | LED Pin | +| ------------ | ---------- | ------- | +| shasta1 | a10 | a2 | +| modoc1 | a9 | b5 | +| mendocino1 | a0 | a1 | +| lassen1 | b8 | b4 | +| eldorado1 | b11 | b12 | +| big_sur1 | b9 | b10 | +| sierra1 | a8 | a15 | +| los_padres1 | a7 | a6 | +| mojave1 | b1 | b0 | +| joshua_tree1 | b2 | b3 | diff --git a/app/boards/shields/kyria/kyria.overlay b/app/boards/shields/kyria/kyria.overlay index af4e591c..e9074f6f 100644 --- a/app/boards/shields/kyria/kyria.overlay +++ b/app/boards/shields/kyria/kyria.overlay @@ -29,7 +29,7 @@ }; kscan_left: kscan_left { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "KSCAN_LEFT"; diode-direction = "row2col"; @@ -49,7 +49,7 @@ }; kscan_right: kscan_right { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "KSCAN_RIGHT"; diode-direction = "row2col"; diff --git a/app/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay b/app/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay index d65b3b1d..95b08354 100644 --- a/app/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay +++ b/app/boards/shields/petejohanson_handwire/petejohanson_handwire.overlay @@ -21,7 +21,7 @@ }; left_hand: kscan_1 { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "KSCAN_LEFT"; diode-direction = "row2col"; @@ -32,7 +32,7 @@ }; right_hand: kscan_2 { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "KSCAN_RIGHT"; diode-direction = "row2col"; diff --git a/app/boards/shields/petejohanson_proton_handwire/petejohanson_proton_handwire.overlay b/app/boards/shields/petejohanson_proton_handwire/petejohanson_proton_handwire.overlay index 5deaa424..65753fd5 100644 --- a/app/boards/shields/petejohanson_proton_handwire/petejohanson_proton_handwire.overlay +++ b/app/boards/shields/petejohanson_proton_handwire/petejohanson_proton_handwire.overlay @@ -5,7 +5,7 @@ }; kscan0: kscan { - compatible = "gpio-kscan"; + compatible = "zmk,kscan-gpio-matrix"; label = "KSCAN"; diode-direction = "row2col"; diff --git a/app/drivers/zephyr/CMakeLists.txt b/app/drivers/zephyr/CMakeLists.txt index 03a97c6e..983828f5 100644 --- a/app/drivers/zephyr/CMakeLists.txt +++ b/app/drivers/zephyr/CMakeLists.txt @@ -1,9 +1,9 @@ if(CONFIG_ZMK_KSCAN_GPIO_DRIVER) - # Add hello_world_driver.h to the set of global include paths. zephyr_include_directories(.) zephyr_library() zephyr_library_sources( - kscan_gpio.c + kscan_gpio_matrix.c + kscan_gpio_direct.c ) endif() diff --git a/app/drivers/zephyr/Kconfig b/app/drivers/zephyr/Kconfig index 2dcb3a82..cd526e86 100644 --- a/app/drivers/zephyr/Kconfig +++ b/app/drivers/zephyr/Kconfig @@ -3,6 +3,14 @@ config ZMK_KSCAN_GPIO_DRIVER default y select GPIO +if ZMK_KSCAN_GPIO_DRIVER + +config ZMK_KSCAN_GPIO_POLLING + bool "Poll for key event triggers instead of using interrupts" + default n + +endif + config ZMK_KSCAN_INIT_PRIORITY int "Keyboard scan driver init priority" default 40 diff --git a/app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-direct.yaml b/app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-direct.yaml new file mode 100644 index 00000000..09a9b6c0 --- /dev/null +++ b/app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-direct.yaml @@ -0,0 +1,16 @@ +# Copyright (c) 2020, Pete Johanson +# SPDX-License-Identifier: MIT + +description: Direct GPIO keyboard KSCAN controller + +compatible: "zmk,kscan-gpio-direct" + +include: kscan.yaml + +properties: + input-gpios: + type: phandle-array + required: true + debounce-period: + type: int + default: 5 diff --git a/app/drivers/zephyr/dts/bindings/gpio-kscan.yaml b/app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-matrix.yaml similarity index 91% rename from app/drivers/zephyr/dts/bindings/gpio-kscan.yaml rename to app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-matrix.yaml index 748d3fa1..5ebcbdd3 100644 --- a/app/drivers/zephyr/dts/bindings/gpio-kscan.yaml +++ b/app/drivers/zephyr/dts/bindings/zmk,kscan-gpio-matrix.yaml @@ -3,7 +3,7 @@ description: GPIO keyboard matrix controller -compatible: "gpio-kscan" +compatible: "zmk,kscan-gpio-matrix" include: kscan.yaml diff --git a/app/drivers/zephyr/kscan_gpio_direct.c b/app/drivers/zephyr/kscan_gpio_direct.c new file mode 100644 index 00000000..6e0beb01 --- /dev/null +++ b/app/drivers/zephyr/kscan_gpio_direct.c @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_kscan_gpio_direct + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +struct kscan_gpio_item_config +{ + char *label; + gpio_pin_t pin; + gpio_flags_t flags; +}; + +union work_reference { + struct k_delayed_work delayed; + struct k_work direct; +}; + +struct kscan_gpio_config +{ + u8_t num_of_inputs; + u8_t debounce_period; + struct kscan_gpio_item_config inputs[]; +}; + +struct kscan_gpio_data +{ +#if defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) + struct k_timer poll_timer; +#endif /* defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ + kscan_callback_t callback; + union work_reference work; + struct device *dev; + u32_t pin_state; + struct device *inputs[]; +}; + +static struct device **kscan_gpio_input_devices(struct device *dev) +{ + struct kscan_gpio_data *data = dev->driver_data; + return data->inputs; +} + +static const struct kscan_gpio_item_config *kscan_gpio_input_configs(struct device *dev) +{ + const struct kscan_gpio_config *cfg = dev->config_info; + return cfg->inputs; +} + +#if !defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) + +struct kscan_gpio_irq_callback +{ + union work_reference *work; + u8_t debounce_period; + struct gpio_callback callback; +}; + +static int kscan_gpio_config_interrupts(struct device *dev, gpio_flags_t flags) +{ + const struct kscan_gpio_config *cfg = dev->config_info; + struct device **devices = kscan_gpio_input_devices(dev); + const struct kscan_gpio_item_config *configs = kscan_gpio_input_configs(dev); + + for (int i = 0; i < cfg->num_of_inputs; i++) + { + struct device *dev = devices[i]; + const struct kscan_gpio_item_config *cfg = &configs[i]; + + int err = gpio_pin_interrupt_configure(dev, cfg->pin, flags); + + if (err) + { + LOG_ERR("Unable to enable matrix GPIO interrupt"); + return err; + } + } + + return 0; +} + +static int kscan_gpio_direct_enable(struct device *dev) +{ + return kscan_gpio_config_interrupts(dev, + GPIO_INT_DEBOUNCE | GPIO_INT_EDGE_BOTH); +} +static int kscan_gpio_direct_disable(struct device *dev) +{ + return kscan_gpio_config_interrupts(dev, + GPIO_INT_DISABLE); +} + +static void kscan_gpio_irq_callback_handler(struct device *dev, + struct gpio_callback *cb, gpio_port_pins_t pin) +{ + struct kscan_gpio_irq_callback *data = + CONTAINER_OF(cb, struct kscan_gpio_irq_callback, callback); + + if (data->debounce_period > 0) { + k_delayed_work_cancel(&data->work->delayed); + k_delayed_work_submit(&data->work->delayed, K_MSEC(data->debounce_period)); + } else { + k_work_submit(&data->work->direct); + } +} + +#else /* !defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ + +static void kscan_gpio_timer_handler(struct k_timer *timer) +{ + struct kscan_gpio_data *data = + CONTAINER_OF(timer, struct kscan_gpio_data, poll_timer); + + k_work_submit(&data->work.direct); +} + +static int kscan_gpio_direct_enable(struct device *dev) +{ + struct kscan_gpio_data *data = dev->driver_data; + k_timer_start(&data->poll_timer, K_MSEC(10), K_MSEC(10)); + return 0; +} +static int kscan_gpio_direct_disable(struct device *dev) +{ + struct kscan_gpio_data *data = dev->driver_data; + k_timer_stop(&data->poll_timer); + return 0; +} + +#endif /* defined(CONFIG_ZMK_KSCAN_GPIO_POLLING) */ + +static int kscan_gpio_direct_configure(struct device *dev, kscan_callback_t callback) +{ + struct kscan_gpio_data *data = dev->driver_data; + if (!callback) + { + return -EINVAL; + } + data->callback = callback; + return 0; +} + +static int kscan_gpio_read(struct device *dev) +{ + struct kscan_gpio_data *data = dev->driver_data; + const struct kscan_gpio_config *cfg = dev->config_info; + u32_t read_state = data->pin_state; + LOG_DBG("Scanning the pins for updated state"); + for (int i = 0; i < cfg->num_of_inputs; i++) + { + struct device *in_dev = kscan_gpio_input_devices(dev)[i]; + const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; + WRITE_BIT(read_state, i, gpio_pin_get(in_dev, in_cfg->pin) > 0); + } + for (int i = 0; i < cfg->num_of_inputs; i++) + { + bool pressed = BIT(i) & read_state; + if (pressed != (BIT(i) & data->pin_state)) + { + LOG_DBG("Sending event at %d,%d state %s", + 0, i, (pressed ? "on" : "off")); + WRITE_BIT(data->pin_state, i, pressed); + data->callback(dev, 0, i, pressed); + } + } + return 0; +} + +static void kscan_gpio_work_handler(struct k_work *work) +{ + struct kscan_gpio_data *data = + CONTAINER_OF(work, struct kscan_gpio_data, work); + kscan_gpio_read(data->dev); +} + +static const struct kscan_driver_api gpio_driver_api = { + .config = kscan_gpio_direct_configure, + .enable_callback = kscan_gpio_direct_enable, + .disable_callback = kscan_gpio_direct_disable, +}; + +#define KSCAN_DIRECT_INPUT_ITEM(i,n) \ + { \ + .label = DT_INST_GPIO_LABEL_BY_IDX(n, input_gpios, i), \ + .pin = DT_INST_GPIO_PIN_BY_IDX(n, input_gpios, i), \ + .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, input_gpios, i), \ + }, + +#define INST_INPUT_LEN(n) DT_INST_PROP_LEN(n, input_gpios) + +#define GPIO_INST_INIT(n) \ + COND_CODE_0(CONFIG_ZMK_KSCAN_GPIO_POLLING, (static struct kscan_gpio_irq_callback \ + irq_callbacks_##n[INST_INPUT_LEN(n)];), ()) \ + static struct kscan_gpio_data kscan_gpio_data_##n = { \ + .inputs = { [INST_INPUT_LEN(n)-1] = NULL } \ + }; \ + static int kscan_gpio_init_##n(struct device *dev) \ + { \ + struct kscan_gpio_data *data = dev->driver_data; \ + const struct kscan_gpio_config *cfg = dev->config_info; \ + int err; \ + struct device **input_devices = kscan_gpio_input_devices(dev); \ + for (int i = 0; i < cfg->num_of_inputs; i++) \ + { \ + const struct kscan_gpio_item_config *in_cfg = &kscan_gpio_input_configs(dev)[i]; \ + input_devices[i] = device_get_binding(in_cfg->label); \ + if (!input_devices[i]) \ + { \ + LOG_ERR("Unable to find input GPIO device"); \ + return -EINVAL; \ + } \ + err = gpio_pin_configure(input_devices[i], in_cfg->pin, GPIO_INPUT | in_cfg->flags); \ + if (err) \ + { \ + LOG_ERR("Unable to configure pin %d on %s for input", in_cfg->pin, in_cfg->label); \ + return err; \ + } \ + COND_CODE_0(CONFIG_ZMK_KSCAN_GPIO_POLLING, \ + ( \ + irq_callbacks_##n[i].work = &data->work; \ + irq_callbacks_##n[i].debounce_period = cfg->debounce_period; \ + gpio_init_callback(&irq_callbacks_##n[i].callback, kscan_gpio_irq_callback_handler, BIT(in_cfg->pin)); \ + err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ + if (err) \ + { \ + LOG_ERR("Error adding the callback to the column device"); \ + return err; \ + } \ + ), ()) \ + } \ + data->dev = dev; \ + COND_CODE_1(CONFIG_ZMK_KSCAN_GPIO_POLLING, (k_timer_init(&data->poll_timer, kscan_gpio_timer_handler, NULL);), ( )) \ + if (cfg->debounce_period > 0) { \ + k_delayed_work_init(&data->work.delayed, kscan_gpio_work_handler); \ + } else { \ + k_work_init(&data->work.direct, kscan_gpio_work_handler); \ + } \ + return 0; \ + } \ + static const struct kscan_gpio_config kscan_gpio_config_##n = { \ + .inputs = { UTIL_LISTIFY(INST_INPUT_LEN(n), KSCAN_DIRECT_INPUT_ITEM, n) }, \ + .num_of_inputs = INST_INPUT_LEN(n), \ + .debounce_period = DT_INST_PROP(n, debounce_period) \ + }; \ + DEVICE_AND_API_INIT(kscan_gpio_##n, DT_INST_LABEL(n), kscan_gpio_init_##n, \ + &kscan_gpio_data_##n, &kscan_gpio_config_##n, \ + POST_KERNEL, CONFIG_ZMK_KSCAN_INIT_PRIORITY, \ + &gpio_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT) + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file diff --git a/app/drivers/zephyr/kscan_gpio.c b/app/drivers/zephyr/kscan_gpio_matrix.c similarity index 99% rename from app/drivers/zephyr/kscan_gpio.c rename to app/drivers/zephyr/kscan_gpio_matrix.c index 76606d8b..22217eff 100644 --- a/app/drivers/zephyr/kscan_gpio.c +++ b/app/drivers/zephyr/kscan_gpio_matrix.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT */ -#define DT_DRV_COMPAT gpio_kscan +#define DT_DRV_COMPAT zmk_kscan_gpio_matrix #include #include @@ -13,6 +13,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + struct kscan_gpio_item_config { char *label; @@ -254,3 +256,5 @@ static int kscan_gpio_config_interrupts(struct device **devices, &gpio_driver_api_##n); DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT) + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/include/zmk/matrix.h b/app/include/zmk/matrix.h index 783c98e2..5397cfb4 100644 --- a/app/include/zmk/matrix.h +++ b/app/include/zmk/matrix.h @@ -5,6 +5,9 @@ #if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,row_gpios) #define ZMK_MATRIX_ROWS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,row_gpios) #define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,col_gpios) +#elif DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,input_gpios) +#define ZMK_MATRIX_ROWS 1 +#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,input_gpios) #else #define ZMK_MATRIX_ROWS DT_PROP(ZMK_MATRIX_NODE_ID,rows) #define ZMK_MATRIX_COLS DT_PROP(ZMK_MATRIX_NODE_ID,columns)