Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kscan cleanup #1269

Merged
merged 2 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/Kconfig
Expand Up @@ -461,12 +461,6 @@ config ZMK_KSCAN_EVENT_QUEUE_SIZE
int "Size of the event queue for KSCAN events to buffer events"
default 4

config ZMK_KSCAN_MOCK_DRIVER
bool "Enable mock kscan driver to simulate key presses"

config ZMK_KSCAN_COMPOSITE_DRIVER
bool "Enable composite kscan driver to combine kscan devices"

#KSCAN Settings
endmenu

Expand Down
3 changes: 0 additions & 3 deletions app/boards/arm/ferris/Kconfig.defconfig
Expand Up @@ -17,7 +17,4 @@ config ZMK_USB
config ZMK_KSCAN_MATRIX_POLLING
default y

config ZMK_KSCAN_COMPOSITE_DRIVER
default y

endif # BOARD_FERRIS
2 changes: 0 additions & 2 deletions app/boards/arm/ferris/ferris_rev02_defconfig
Expand Up @@ -17,8 +17,6 @@ CONFIG_I2C=y

# ZMK Settings
CONFIG_ZMK_USB=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=y
CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER=y
CONFIG_ZMK_KSCAN_MATRIX_POLLING=y
CONFIG_USB_SELF_POWERED=n

Expand Down
3 changes: 0 additions & 3 deletions app/boards/native_posix.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=n
CONFIG_ZMK_BLE=n
CONFIG_LOG=y
Expand Down
3 changes: 0 additions & 3 deletions app/boards/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=n
# Enable to have the native posix build expose USBIP device(s)
# CONFIG_ZMK_USB=y
Expand Down
6 changes: 3 additions & 3 deletions app/drivers/kscan/CMakeLists.txt
Expand Up @@ -5,8 +5,8 @@ zephyr_library_named(zmk__drivers__kscan)
zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)

zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER debounce.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_matrix.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_direct.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_demux.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_MATRIX kscan_gpio_matrix.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DIRECT kscan_gpio_direct.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DEMUX kscan_gpio_demux.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER kscan_mock.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER kscan_composite.c)
32 changes: 30 additions & 2 deletions app/drivers/kscan/Kconfig
@@ -1,11 +1,39 @@
# Copyright (c) 2020 The ZMK Contributors
# SPDX-License-Identifier: MIT

DT_COMPAT_ZMK_KSCAN_COMPOSITE := zmk,kscan-composite
DT_COMPAT_ZMK_KSCAN_GPIO_DEMUX := zmk,kscan-gpio-demux
DT_COMPAT_ZMK_KSCAN_GPIO_DIRECT := zmk,kscan-gpio-direct
DT_COMPAT_ZMK_KSCAN_GPIO_MATRIX := zmk,kscan-gpio-matrix
DT_COMPAT_ZMK_KSCAN_MOCK := zmk,kscan-mock

config ZMK_KSCAN_COMPOSITE_DRIVER
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_KSCAN_COMPOSITE))

config ZMK_KSCAN_GPIO_DRIVER
bool "Enable GPIO kscan driver to detect key presses"
default y
bool
select GPIO

config ZMK_KSCAN_GPIO_DEMUX
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_KSCAN_GPIO_DEMUX))
select ZMK_KSCAN_GPIO_DRIVER

config ZMK_KSCAN_GPIO_DIRECT
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_KSCAN_GPIO_DIRECT))
select ZMK_KSCAN_GPIO_DRIVER

config ZMK_KSCAN_GPIO_MATRIX
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_KSCAN_GPIO_MATRIX))
select ZMK_KSCAN_GPIO_DRIVER

config ZMK_KSCAN_MOCK_DRIVER
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_KSCAN_MOCK))

if ZMK_KSCAN_GPIO_DRIVER

config ZMK_KSCAN_MATRIX_POLLING
Expand Down
4 changes: 0 additions & 4 deletions app/drivers/kscan/kscan_gpio_demux.c
Expand Up @@ -13,8 +13,6 @@

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;
Expand Down Expand Up @@ -251,5 +249,3 @@ struct kscan_gpio_item_config {
&gpio_driver_api_##n);

DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT)

#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
4 changes: 0 additions & 4 deletions app/drivers/kscan/kscan_gpio_direct.c
Expand Up @@ -13,8 +13,6 @@

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;
Expand Down Expand Up @@ -242,5 +240,3 @@ static const struct kscan_driver_api gpio_driver_api = {
&gpio_driver_api);

DT_INST_FOREACH_STATUS_OKAY(GPIO_INST_INIT)

#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
65 changes: 21 additions & 44 deletions app/drivers/kscan/kscan_gpio_matrix.c
Expand Up @@ -19,8 +19,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#define DT_DRV_COMPAT zmk_kscan_gpio_matrix

#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

#define INST_DIODE_DIR(n) DT_ENUM_IDX(DT_DRV_INST(n), diode_direction)
#define COND_DIODE_DIR(n, row2col_code, col2row_code) \
COND_CODE_0(INST_DIODE_DIR(n), row2col_code, col2row_code)
Expand Down Expand Up @@ -51,29 +49,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define COND_POLL_OR_INTERRUPTS(pollcode, intcode) \
COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, pollcode, intcode)

// TODO (Zephr 2.6): replace the following
// kscan_gpio_dt_spec -> gpio_dt_spec
// KSCAN_GPIO_DT_SPEC_GET_BY_IDX -> GPIO_DT_SPEC_GET_BY_IDX
// gpio_pin_get -> gpio_pin_get_dt
// gpio_pin_set -> gpio_pin_set_dt
// gpio_pin_interrupt_configure -> gpio_pin_interrupt_configure_dt
struct kscan_gpio_dt_spec {
const struct device *port;
gpio_pin_t pin;
gpio_dt_flags_t dt_flags;
};

#define KSCAN_GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
{ \
.port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)), \
.pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
.dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
}

#define KSCAN_GPIO_ROW_CFG_INIT(idx, inst_idx) \
KSCAN_GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx),
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx),
#define KSCAN_GPIO_COL_CFG_INIT(idx, inst_idx) \
KSCAN_GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx),
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx),

enum kscan_diode_direction {
KSCAN_ROW2COL,
Expand Down Expand Up @@ -103,7 +82,7 @@ struct kscan_matrix_data {
};

struct kscan_gpio_list {
const struct kscan_gpio_dt_spec *gpios;
const struct gpio_dt_spec *gpios;
size_t len;
};

Expand Down Expand Up @@ -146,9 +125,9 @@ static int kscan_matrix_set_all_outputs(const struct device *dev, const int valu
const struct kscan_matrix_config *config = dev->config;

for (int i = 0; i < config->outputs.len; i++) {
const struct kscan_gpio_dt_spec *gpio = &config->outputs.gpios[i];
const struct gpio_dt_spec *gpio = &config->outputs.gpios[i];

int err = gpio_pin_set(gpio->port, gpio->pin, value);
int err = gpio_pin_set_dt(gpio, value);
if (err) {
LOG_ERR("Failed to set output %i to %i: %i", i, value, err);
return err;
Expand All @@ -163,9 +142,9 @@ static int kscan_matrix_interrupt_configure(const struct device *dev, const gpio
const struct kscan_matrix_config *config = dev->config;

for (int i = 0; i < config->inputs.len; i++) {
const struct kscan_gpio_dt_spec *gpio = &config->inputs.gpios[i];
const struct gpio_dt_spec *gpio = &config->inputs.gpios[i];

int err = gpio_pin_interrupt_configure(gpio->port, gpio->pin, flags);
int err = gpio_pin_interrupt_configure_dt(gpio, flags);
if (err) {
LOG_ERR("Unable to configure interrupt for pin %u on %s", gpio->pin, gpio->port->name);
return err;
Expand Down Expand Up @@ -248,25 +227,25 @@ static int kscan_matrix_read(const struct device *dev) {

// Scan the matrix.
for (int o = 0; o < config->outputs.len; o++) {
const struct kscan_gpio_dt_spec *out_gpio = &config->outputs.gpios[o];
const struct gpio_dt_spec *out_gpio = &config->outputs.gpios[o];

int err = gpio_pin_set(out_gpio->port, out_gpio->pin, 1);
int err = gpio_pin_set_dt(out_gpio, 1);
if (err) {
LOG_ERR("Failed to set output %i active: %i", o, err);
return err;
}

for (int i = 0; i < config->inputs.len; i++) {
const struct kscan_gpio_dt_spec *in_gpio = &config->inputs.gpios[i];
const struct gpio_dt_spec *in_gpio = &config->inputs.gpios[i];

const int index = state_index_io(config, i, o);
const bool active = gpio_pin_get(in_gpio->port, in_gpio->pin);
const bool active = gpio_pin_get_dt(in_gpio);

debounce_update(&data->matrix_state[index], active, config->debounce_scan_period_ms,
&config->debounce_config);
}

err = gpio_pin_set(out_gpio->port, out_gpio->pin, 0);
err = gpio_pin_set_dt(out_gpio, 0);
if (err) {
LOG_ERR("Failed to set output %i inactive: %i", o, err);
return err;
Expand Down Expand Up @@ -342,14 +321,14 @@ static int kscan_matrix_disable(const struct device *dev) {
#endif
}

static int kscan_matrix_init_input_inst(const struct device *dev,
const struct kscan_gpio_dt_spec *gpio, const int index) {
static int kscan_matrix_init_input_inst(const struct device *dev, const struct gpio_dt_spec *gpio,
const int index) {
if (!device_is_ready(gpio->port)) {
LOG_ERR("GPIO is not ready: %s", gpio->port->name);
return -ENODEV;
}

int err = gpio_pin_configure(gpio->port, gpio->pin, GPIO_INPUT | gpio->dt_flags);
int err = gpio_pin_configure_dt(gpio, GPIO_INPUT);
if (err) {
LOG_ERR("Unable to configure pin %u on %s for input", gpio->pin, gpio->port->name);
return err;
Expand Down Expand Up @@ -377,7 +356,7 @@ static int kscan_matrix_init_inputs(const struct device *dev) {
const struct kscan_matrix_config *config = dev->config;

for (int i = 0; i < config->inputs.len; i++) {
const struct kscan_gpio_dt_spec *gpio = &config->inputs.gpios[i];
const struct gpio_dt_spec *gpio = &config->inputs.gpios[i];
int err = kscan_matrix_init_input_inst(dev, gpio, i);
if (err) {
return err;
Expand All @@ -388,13 +367,13 @@ static int kscan_matrix_init_inputs(const struct device *dev) {
}

static int kscan_matrix_init_output_inst(const struct device *dev,
const struct kscan_gpio_dt_spec *gpio) {
const struct gpio_dt_spec *gpio) {
if (!device_is_ready(gpio->port)) {
LOG_ERR("GPIO is not ready: %s", gpio->port->name);
return -ENODEV;
}

int err = gpio_pin_configure(gpio->port, gpio->pin, GPIO_OUTPUT | gpio->dt_flags);
int err = gpio_pin_configure_dt(gpio, GPIO_OUTPUT);
if (err) {
LOG_ERR("Unable to configure pin %u on %s for output", gpio->pin, gpio->port->name);
return err;
Expand All @@ -409,7 +388,7 @@ static int kscan_matrix_init_outputs(const struct device *dev) {
const struct kscan_matrix_config *config = dev->config;

for (int i = 0; i < config->outputs.len; i++) {
const struct kscan_gpio_dt_spec *gpio = &config->outputs.gpios[i];
const struct gpio_dt_spec *gpio = &config->outputs.gpios[i];
int err = kscan_matrix_init_output_inst(dev, gpio);
if (err) {
return err;
Expand Down Expand Up @@ -445,10 +424,10 @@ static const struct kscan_driver_api kscan_matrix_api = {
BUILD_ASSERT(INST_DEBOUNCE_RELEASE_MS(index) <= DEBOUNCE_COUNTER_MAX, \
"ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \
\
static const struct kscan_gpio_dt_spec kscan_matrix_rows_##index[] = { \
static const struct gpio_dt_spec kscan_matrix_rows_##index[] = { \
UTIL_LISTIFY(INST_ROWS_LEN(index), KSCAN_GPIO_ROW_CFG_INIT, index)}; \
\
static const struct kscan_gpio_dt_spec kscan_matrix_cols_##index[] = { \
static const struct gpio_dt_spec kscan_matrix_cols_##index[] = { \
UTIL_LISTIFY(INST_COLS_LEN(index), KSCAN_GPIO_COL_CFG_INIT, index)}; \
\
static struct debounce_state kscan_matrix_state_##index[INST_MATRIX_LEN(index)]; \
Expand Down Expand Up @@ -482,5 +461,3 @@ static const struct kscan_driver_api kscan_matrix_api = {
CONFIG_APPLICATION_INIT_PRIORITY, &kscan_matrix_api);

DT_INST_FOREACH_STATUS_OKAY(KSCAN_MATRIX_INIT);

#endif // DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
3 changes: 0 additions & 3 deletions app/tests/backlight/basic/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/backlight/config-brt/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/backlight/config-on/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/backlight/config-step/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/backlight/cycle/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/backlight/low-brightness/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=y
CONFIG_GPIO_EMUL=y
CONFIG_ZMK_BLE=n
Expand Down
3 changes: 0 additions & 3 deletions app/tests/wpm/1-single_keypress/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=n
CONFIG_ZMK_BLE=n
CONFIG_LOG=y
Expand Down
3 changes: 0 additions & 3 deletions app/tests/wpm/2-multiple_keypress/native_posix_64.conf
@@ -1,6 +1,3 @@
CONFIG_KSCAN=n
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n
CONFIG_GPIO=n
CONFIG_ZMK_BLE=n
CONFIG_LOG=y
Expand Down