Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
input_kbd_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_
8#define ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_
9
17#include <zephyr/device.h>
18#include <zephyr/kernel.h>
19#include <zephyr/pm/device.h>
20#include <zephyr/sys/atomic.h>
21#include <zephyr/sys/util.h>
22#include <zephyr/sys_clock.h>
23#include <zephyr/toolchain.h>
24
26#define INPUT_KBD_MATRIX_COLUMN_DRIVE_NONE -1
27
29#define INPUT_KBD_MATRIX_COLUMN_DRIVE_ALL -2
30
32#define INPUT_KBD_MATRIX_SCAN_OCURRENCES 30U
33
35#if CONFIG_INPUT_KBD_MATRIX_16_BIT_ROW
36typedef uint16_t kbd_row_t;
37#define PRIkbdrow "04" PRIx16
38#else
40#define PRIkbdrow "02" PRIx8
41#endif
42
43#if defined(CONFIG_INPUT_KBD_ACTUAL_KEY_MASK_DYNAMIC) || defined(__DOXYGEN__)
44#define INPUT_KBD_ACTUAL_KEY_MASK_CONST
64 uint8_t row, uint8_t col, bool enabled);
65#else
66#define INPUT_KBD_ACTUAL_KEY_MASK_CONST const
67#endif
68
70#define INPUT_KBD_MATRIX_ROW_BITS NUM_BITS(kbd_row_t)
71
86 void (*drive_column)(const struct device *dev, int col);
92 kbd_row_t (*read_row)(const struct device *dev);
103 void (*set_detect_mode)(const struct device *dev, bool enabled);
104};
105
131
132#define INPUT_KBD_MATRIX_DATA_NAME(node_id, name) \
133 _CONCAT(__input_kbd_matrix_, \
134 _CONCAT(name, DEVICE_DT_NAME_GET(node_id)))
135
140#define INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(node_id, _row_size, _col_size) \
141 BUILD_ASSERT(IN_RANGE(_row_size, 1, INPUT_KBD_MATRIX_ROW_BITS), "invalid row-size"); \
142 BUILD_ASSERT(IN_RANGE(_col_size, 1, UINT8_MAX), "invalid col-size"); \
143 IF_ENABLED(DT_NODE_HAS_PROP(node_id, actual_key_mask), ( \
144 BUILD_ASSERT(DT_PROP_LEN(node_id, actual_key_mask) == _col_size, \
145 "actual-key-mask size does not match the number of columns"); \
146 static INPUT_KBD_ACTUAL_KEY_MASK_CONST kbd_row_t \
147 INPUT_KBD_MATRIX_DATA_NAME(node_id, actual_key_mask)[_col_size] = \
148 DT_PROP(node_id, actual_key_mask); \
149 )) \
150 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \
151 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \
152 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \
153 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \
154 static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx)[_row_size * _col_size];
155
159#define INPUT_KBD_MATRIX_DT_DEFINE(node_id) \
160 INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL( \
161 node_id, DT_PROP(node_id, row_size), DT_PROP(node_id, col_size))
162
171#define INPUT_KBD_MATRIX_DT_INST_DEFINE_ROW_COL(inst, row_size, col_size) \
172 INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(DT_DRV_INST(inst), row_size, col_size)
173
179#define INPUT_KBD_MATRIX_DT_INST_DEFINE(inst) \
180 INPUT_KBD_MATRIX_DT_DEFINE(DT_DRV_INST(inst))
181
190#define INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL(node_id, _api, _row_size, _col_size) \
191 { \
192 .api = _api, \
193 .row_size = _row_size, \
194 .col_size = _col_size, \
195 .poll_period_us = DT_PROP(node_id, poll_period_ms) * USEC_PER_MSEC, \
196 .stable_poll_period_us = DT_PROP_OR(node_id, stable_poll_period_ms, \
197 DT_PROP(node_id, poll_period_ms)) * \
198 USEC_PER_MSEC, \
199 .poll_timeout_ms = DT_PROP(node_id, poll_timeout_ms), \
200 .debounce_down_us = DT_PROP(node_id, debounce_down_ms) * USEC_PER_MSEC, \
201 .debounce_up_us = DT_PROP(node_id, debounce_up_ms) * USEC_PER_MSEC, \
202 .settle_time_us = DT_PROP(node_id, settle_time_us), \
203 .ghostkey_check = !DT_PROP(node_id, no_ghostkey_check), \
204 IF_ENABLED(DT_NODE_HAS_PROP(node_id, actual_key_mask), ( \
205 .actual_key_mask = INPUT_KBD_MATRIX_DATA_NAME(node_id, actual_key_mask), \
206 )) \
207 \
208 .matrix_stable_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state), \
209 .matrix_unstable_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state), \
210 .matrix_previous_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state), \
211 .matrix_new_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state), \
212 .scan_cycle_idx = INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx), \
213 }
214
221#define INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT(node_id, api) \
222 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL( \
223 node_id, api, DT_PROP(node_id, row_size), DT_PROP(node_id, col_size))
224
234#define INPUT_KBD_MATRIX_DT_INST_COMMON_CONFIG_INIT_ROW_COL(inst, api, row_size, col_size) \
235 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL(DT_DRV_INST(inst), api, row_size, col_size)
236
243#define INPUT_KBD_MATRIX_DT_INST_COMMON_CONFIG_INIT(inst, api) \
244 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst), api)
245
252 /* Track previous cycles, used for debouncing. */
255
256 struct k_sem poll_lock;
257#ifdef CONFIG_PM_DEVICE
258 atomic_t suspended;
259#endif
260
262
264 CONFIG_INPUT_KBD_MATRIX_THREAD_STACK_SIZE);
265};
266
273#define INPUT_KBD_STRUCT_CHECK(config, data) \
274 BUILD_ASSERT(offsetof(config, common) == 0, \
275 "struct input_kbd_matrix_common_config must be placed first"); \
276 BUILD_ASSERT(offsetof(data, common) == 0, \
277 "struct input_kbd_matrix_common_data must be placed first")
278
287void input_kbd_matrix_poll_start(const struct device *dev);
288
289#if defined(CONFIG_INPUT_KBD_DRIVE_COLUMN_HOOK) || defined(__DOXYGEN__)
302void input_kbd_matrix_drive_column_hook(const struct device *dev, int col);
303#endif
304
316
317#ifdef CONFIG_PM_DEVICE
327int input_kbd_matrix_pm_action(const struct device *dev,
328 enum pm_device_action action);
329#endif
330
333#endif /* ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_ */
long atomic_t
Definition atomic_types.h:15
void input_kbd_matrix_drive_column_hook(const struct device *dev, int col)
Drive column hook.
#define INPUT_KBD_MATRIX_SCAN_OCURRENCES
Number of tracked scan cycles.
Definition input_kbd_matrix.h:32
int input_kbd_matrix_common_init(const struct device *dev)
Common function to initialize a keyboard matrix device at init time.
int input_kbd_matrix_actual_key_mask_set(const struct device *dev, uint8_t row, uint8_t col, bool enabled)
Enables or disables a specific row, column combination in the actual key mask.
uint8_t kbd_row_t
Row entry data type.
Definition input_kbd_matrix.h:39
#define INPUT_KBD_ACTUAL_KEY_MASK_CONST
Definition input_kbd_matrix.h:44
void input_kbd_matrix_poll_start(const struct device *dev)
Start scanning the keyboard matrix.
pm_device_action
Device PM actions.
Definition device.h:90
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:411
Keyboard matrix internal APIs.
Definition input_kbd_matrix.h:75
void(* set_detect_mode)(const struct device *dev, bool enabled)
Request to put the matrix in detection mode.
Definition input_kbd_matrix.h:103
kbd_row_t(* read_row)(const struct device *dev)
Read the matrix row.
Definition input_kbd_matrix.h:92
void(* drive_column)(const struct device *dev, int col)
Request to drive a specific column.
Definition input_kbd_matrix.h:86
Common keyboard matrix config.
Definition input_kbd_matrix.h:111
uint32_t debounce_down_us
Definition input_kbd_matrix.h:118
uint8_t * scan_cycle_idx
Definition input_kbd_matrix.h:129
uint32_t settle_time_us
Definition input_kbd_matrix.h:120
bool ghostkey_check
Definition input_kbd_matrix.h:121
uint32_t poll_period_us
Definition input_kbd_matrix.h:115
kbd_row_t * matrix_previous_state
Definition input_kbd_matrix.h:127
kbd_row_t * matrix_unstable_state
Definition input_kbd_matrix.h:126
uint32_t debounce_up_us
Definition input_kbd_matrix.h:119
kbd_row_t * matrix_new_state
Definition input_kbd_matrix.h:128
uint32_t poll_timeout_ms
Definition input_kbd_matrix.h:117
kbd_row_t * matrix_stable_state
Definition input_kbd_matrix.h:125
kbd_row_t * actual_key_mask
Definition input_kbd_matrix.h:122
uint32_t stable_poll_period_us
Definition input_kbd_matrix.h:116
const struct input_kbd_matrix_api * api
Definition input_kbd_matrix.h:112
uint8_t col_size
Definition input_kbd_matrix.h:114
uint8_t row_size
Definition input_kbd_matrix.h:113
Common keyboard matrix data.
Definition input_kbd_matrix.h:251
struct k_thread thread
Definition input_kbd_matrix.h:261
uint8_t scan_cycles_idx
Definition input_kbd_matrix.h:254
uint32_t scan_clk_cycle[30U]
Definition input_kbd_matrix.h:253
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_INPUT_KBD_MATRIX_THREAD_STACK_SIZE)
struct k_sem poll_lock
Definition input_kbd_matrix.h:256
Thread Structure.
Definition thread.h:259
Misc utilities.
Variables needed for system clock.
Macros to abstract toolchain specific capabilities.