Browse Source

feat(kscan): Use PORT events for kscan matrix interrupts

* Lower power usage compared to regular interrupts on nrf52.
xmkb
Pete Johanson 4 years ago
parent
commit
9be566603e
  1. 4
      app/boards/arm/nice_nano/nice_nano.dts
  2. 16
      app/drivers/zephyr/kscan_gpio_matrix.c

4
app/boards/arm/nice_nano/nice_nano.dts

@ -31,6 +31,10 @@
}; };
&gpiote {
status = "okay";
};
&gpio0 { &gpio0 {
status = "okay"; status = "okay";
}; };

16
app/drivers/zephyr/kscan_gpio_matrix.c

@ -64,6 +64,7 @@ static int kscan_gpio_config_interrupts(struct device **devices,
struct kscan_gpio_irq_callback_##n { \ struct kscan_gpio_irq_callback_##n { \
struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) * work; \ struct COND_CODE_0(DT_INST_PROP(n, debounce_period), (k_work), (k_delayed_work)) * work; \
struct gpio_callback callback; \ struct gpio_callback callback; \
struct device *dev; \
}; \ }; \
static struct kscan_gpio_irq_callback_##n irq_callbacks_##n[INST_INPUT_LEN(n)]; \ static struct kscan_gpio_irq_callback_##n irq_callbacks_##n[INST_INPUT_LEN(n)]; \
struct kscan_gpio_config_##n { \ struct kscan_gpio_config_##n { \
@ -105,7 +106,7 @@ static int kscan_gpio_config_interrupts(struct device **devices,
static int kscan_gpio_enable_interrupts_##n(struct device *dev) { \ static int kscan_gpio_enable_interrupts_##n(struct device *dev) { \
return kscan_gpio_config_interrupts( \ return kscan_gpio_config_interrupts( \
kscan_gpio_input_devices_##n(dev), kscan_gpio_input_configs_##n(dev), \ kscan_gpio_input_devices_##n(dev), kscan_gpio_input_configs_##n(dev), \
INST_INPUT_LEN(n), GPIO_INT_DEBOUNCE | GPIO_INT_EDGE_BOTH); \ INST_INPUT_LEN(n), GPIO_INT_LEVEL_ACTIVE); \
} static int kscan_gpio_disable_interrupts_##n(struct device *dev) { \ } static int kscan_gpio_disable_interrupts_##n(struct device *dev) { \
return kscan_gpio_config_interrupts(kscan_gpio_input_devices_##n(dev), \ return kscan_gpio_config_interrupts(kscan_gpio_input_devices_##n(dev), \
kscan_gpio_input_configs_##n(dev), \ kscan_gpio_input_configs_##n(dev), \
@ -113,10 +114,13 @@ static int kscan_gpio_config_interrupts(struct device **devices,
}), \ }), \
()) \ ()) \
static void kscan_gpio_set_output_state_##n(struct device *dev, int value) { \ static void kscan_gpio_set_output_state_##n(struct device *dev, int value) { \
int err; \
for (int i = 0; i < INST_OUTPUT_LEN(n); i++) { \ for (int i = 0; i < INST_OUTPUT_LEN(n); i++) { \
struct device *in_dev = kscan_gpio_output_devices_##n(dev)[i]; \ struct device *in_dev = kscan_gpio_output_devices_##n(dev)[i]; \
const struct kscan_gpio_item_config *cfg = &kscan_gpio_output_configs_##n(dev)[i]; \ const struct kscan_gpio_item_config *cfg = &kscan_gpio_output_configs_##n(dev)[i]; \
gpio_pin_set(in_dev, cfg->pin, value); \ if ((err = gpio_pin_set(in_dev, cfg->pin, value))) { \
LOG_DBG("FAILED TO SET OUTPUT %d to %d", cfg->pin, err); \
} \
} \ } \
} \ } \
static void kscan_gpio_set_matrix_state_##n( \ static void kscan_gpio_set_matrix_state_##n( \
@ -134,8 +138,6 @@ static int kscan_gpio_config_interrupts(struct device **devices,
/* Disable our interrupts temporarily while we scan, to avoid */ \ /* Disable our interrupts temporarily while we scan, to avoid */ \
/* re-entry while we iterate columns and set them active one by one */ \ /* re-entry while we iterate columns and set them active one by one */ \
/* to get pressed state for each matrix cell. */ \ /* to get pressed state for each matrix cell. */ \
COND_CODE_0(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (kscan_gpio_disable_interrupts_##n(dev);), \
()) \
kscan_gpio_set_output_state_##n(dev, 0); \ kscan_gpio_set_output_state_##n(dev, 0); \
for (int o = 0; o < INST_OUTPUT_LEN(n); o++) { \ for (int o = 0; o < INST_OUTPUT_LEN(n); o++) { \
struct device *out_dev = kscan_gpio_output_devices_##n(dev)[o]; \ struct device *out_dev = kscan_gpio_output_devices_##n(dev)[o]; \
@ -152,8 +154,6 @@ static int kscan_gpio_config_interrupts(struct device **devices,
} \ } \
/* Set all our outputs as active again. */ \ /* Set all our outputs as active again. */ \
kscan_gpio_set_output_state_##n(dev, 1); \ kscan_gpio_set_output_state_##n(dev, 1); \
/*Re-enable interrupts so that they can be triggered again for future press/release*/ \
COND_CODE_0(CONFIG_ZMK_KSCAN_MATRIX_POLLING, (kscan_gpio_enable_interrupts_##n(dev);), ()) \
for (int r = 0; r < INST_MATRIX_ROWS(n); r++) { \ for (int r = 0; r < INST_MATRIX_ROWS(n); r++) { \
for (int c = 0; c < INST_MATRIX_COLS(n); c++) { \ for (int c = 0; c < INST_MATRIX_COLS(n); c++) { \
bool pressed = read_state[r][c]; \ bool pressed = read_state[r][c]; \
@ -172,6 +172,8 @@ static int kscan_gpio_config_interrupts(struct device **devices,
k_delayed_work_cancel(&data->work); \ k_delayed_work_cancel(&data->work); \
k_delayed_work_submit(&data->work, K_MSEC(5)); \ k_delayed_work_submit(&data->work, K_MSEC(5)); \
})) \ })) \
} else { \
kscan_gpio_enable_interrupts_##n(dev); \
} \ } \
return 0; \ return 0; \
} \ } \
@ -183,6 +185,7 @@ static int kscan_gpio_config_interrupts(struct device **devices,
gpio_port_pins_t pin) { \ gpio_port_pins_t pin) { \
struct kscan_gpio_irq_callback_##n *data = \ struct kscan_gpio_irq_callback_##n *data = \
CONTAINER_OF(cb, struct kscan_gpio_irq_callback_##n, callback); \ CONTAINER_OF(cb, struct kscan_gpio_irq_callback_##n, callback); \
kscan_gpio_disable_interrupts_##n(data->dev); \
COND_CODE_0(DT_INST_PROP(n, debounce_period), ({ k_work_submit(data->work); }), ({ \ COND_CODE_0(DT_INST_PROP(n, debounce_period), ({ k_work_submit(data->work); }), ({ \
k_delayed_work_cancel(data->work); \ k_delayed_work_cancel(data->work); \
k_delayed_work_submit(data->work, \ k_delayed_work_submit(data->work, \
@ -238,6 +241,7 @@ static int kscan_gpio_config_interrupts(struct device **devices,
LOG_DBG("Configured pin %d on %s for input", in_cfg->pin, in_cfg->label); \ LOG_DBG("Configured pin %d on %s for input", in_cfg->pin, in_cfg->label); \
} \ } \
irq_callbacks_##n[i].work = &data->work; \ irq_callbacks_##n[i].work = &data->work; \
irq_callbacks_##n[i].dev = dev; \
gpio_init_callback(&irq_callbacks_##n[i].callback, \ gpio_init_callback(&irq_callbacks_##n[i].callback, \
kscan_gpio_irq_callback_handler_##n, BIT(in_cfg->pin)); \ kscan_gpio_irq_callback_handler_##n, BIT(in_cfg->pin)); \
err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \ err = gpio_add_callback(input_devices[i], &irq_callbacks_##n[i].callback); \

Loading…
Cancel
Save