Zephyr API Documentation 4.4.0-rc1
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
12
13#ifndef ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_
14#define ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_
15
21
22#include <zephyr/device.h>
23#include <zephyr/kernel.h>
24#include <zephyr/pm/device.h>
25#include <zephyr/sys/atomic.h>
26#include <zephyr/sys/util.h>
27#include <zephyr/sys_clock.h>
28#include <zephyr/toolchain.h>
29
31#define INPUT_KBD_MATRIX_COLUMN_DRIVE_NONE -1
32
34#define INPUT_KBD_MATRIX_COLUMN_DRIVE_ALL -2
35
37#define INPUT_KBD_MATRIX_SCAN_OCURRENCES 30U
38
40#if CONFIG_INPUT_KBD_MATRIX_16_BIT_ROW
41typedef uint16_t kbd_row_t;
42#define PRIkbdrow "04" PRIx16
43#else
45#define PRIkbdrow "02" PRIx8
46#endif
47
48#if defined(CONFIG_INPUT_KBD_ACTUAL_KEY_MASK_DYNAMIC) || defined(__DOXYGEN__)
49#define INPUT_KBD_ACTUAL_KEY_MASK_CONST
69 uint8_t row, uint8_t col, bool enabled);
70#else
71#define INPUT_KBD_ACTUAL_KEY_MASK_CONST const
72#endif
73
75#define INPUT_KBD_MATRIX_ROW_BITS NUM_BITS(kbd_row_t)
76
91 void (*drive_column)(const struct device *dev, int col);
97 kbd_row_t (*read_row)(const struct device *dev);
108 void (*set_detect_mode)(const struct device *dev, bool enabled);
109};
110
136
137#define INPUT_KBD_MATRIX_DATA_NAME(node_id, name) \
138 _CONCAT(__input_kbd_matrix_, \
139 _CONCAT(name, DEVICE_DT_NAME_GET(node_id)))
140
145#define INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(node_id, _row_size, _col_size) \
146 BUILD_ASSERT(IN_RANGE(_row_size, 1, INPUT_KBD_MATRIX_ROW_BITS), "invalid row-size"); \
147 BUILD_ASSERT(IN_RANGE(_col_size, 1, UINT8_MAX), "invalid col-size"); \
148 IF_ENABLED(DT_NODE_HAS_PROP(node_id, actual_key_mask), ( \
149 BUILD_ASSERT(DT_PROP_LEN(node_id, actual_key_mask) == _col_size, \
150 "actual-key-mask size does not match the number of columns"); \
151 static INPUT_KBD_ACTUAL_KEY_MASK_CONST kbd_row_t \
152 INPUT_KBD_MATRIX_DATA_NAME(node_id, actual_key_mask)[_col_size] = \
153 DT_PROP(node_id, actual_key_mask); \
154 )) \
155 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \
156 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \
157 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \
158 static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \
159 static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx)[_row_size * _col_size];
160
164#define INPUT_KBD_MATRIX_DT_DEFINE(node_id) \
165 INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL( \
166 node_id, DT_PROP(node_id, row_size), DT_PROP(node_id, col_size))
167
176#define INPUT_KBD_MATRIX_DT_INST_DEFINE_ROW_COL(inst, row_size, col_size) \
177 INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(DT_DRV_INST(inst), row_size, col_size)
178
184#define INPUT_KBD_MATRIX_DT_INST_DEFINE(inst) \
185 INPUT_KBD_MATRIX_DT_DEFINE(DT_DRV_INST(inst))
186
195#define INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL(node_id, _api, _row_size, _col_size) \
196 { \
197 .api = _api, \
198 .row_size = _row_size, \
199 .col_size = _col_size, \
200 .poll_period_us = DT_PROP(node_id, poll_period_us), \
201 .stable_poll_period_us = DT_PROP_OR(node_id, stable_poll_period_us, \
202 DT_PROP(node_id, poll_period_us)), \
203 .poll_timeout_ms = DT_PROP(node_id, poll_timeout_ms), \
204 .debounce_down_us = DT_PROP(node_id, debounce_down_ms) * USEC_PER_MSEC, \
205 .debounce_up_us = DT_PROP(node_id, debounce_up_ms) * USEC_PER_MSEC, \
206 .settle_time_us = DT_PROP(node_id, settle_time_us), \
207 .ghostkey_check = !DT_PROP(node_id, no_ghostkey_check), \
208 IF_ENABLED(DT_NODE_HAS_PROP(node_id, actual_key_mask), ( \
209 .actual_key_mask = INPUT_KBD_MATRIX_DATA_NAME(node_id, actual_key_mask), \
210 )) \
211 \
212 .matrix_stable_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state), \
213 .matrix_unstable_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state), \
214 .matrix_previous_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state), \
215 .matrix_new_state = INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state), \
216 .scan_cycle_idx = INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx), \
217 }
218
225#define INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT(node_id, api) \
226 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL( \
227 node_id, api, DT_PROP(node_id, row_size), DT_PROP(node_id, col_size))
228
238#define INPUT_KBD_MATRIX_DT_INST_COMMON_CONFIG_INIT_ROW_COL(inst, api, row_size, col_size) \
239 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT_ROW_COL(DT_DRV_INST(inst), api, row_size, col_size)
240
247#define INPUT_KBD_MATRIX_DT_INST_COMMON_CONFIG_INIT(inst, api) \
248 INPUT_KBD_MATRIX_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst), api)
249
256 /* Track previous cycles, used for debouncing. */
259
261#ifdef CONFIG_PM_DEVICE
262 atomic_t suspended;
263#endif
264
266
268 CONFIG_INPUT_KBD_MATRIX_THREAD_STACK_SIZE);
269};
270
277#define INPUT_KBD_STRUCT_CHECK(config, data) \
278 BUILD_ASSERT(offsetof(config, common) == 0, \
279 "struct input_kbd_matrix_common_config must be placed first"); \
280 BUILD_ASSERT(offsetof(data, common) == 0, \
281 "struct input_kbd_matrix_common_data must be placed first")
282
291void input_kbd_matrix_poll_start(const struct device *dev);
292
293#if defined(CONFIG_INPUT_KBD_DRIVE_COLUMN_HOOK) || defined(__DOXYGEN__)
306void input_kbd_matrix_drive_column_hook(const struct device *dev, int col);
307#endif
308
320
321#ifdef CONFIG_PM_DEVICE
331int input_kbd_matrix_pm_action(const struct device *dev,
332 enum pm_device_action action);
333#endif
334
343bool input_kbd_matrix_ghosting(const struct device *dev);
344
355void input_kbd_matrix_update_state(const struct device *dev);
356
358
359#endif /* ZEPHYR_INCLUDE_INPUT_KBD_MATRIX_H_ */
long atomic_t
Definition atomic_types.h:15
void input_kbd_matrix_update_state(const struct device *dev)
Update the keyboard matrix state and apply debouncing.
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:37
bool input_kbd_matrix_ghosting(const struct device *dev)
Check whether the current keyboard matrix state has ghost keys.
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:44
#define INPUT_KBD_ACTUAL_KEY_MASK_CONST
Definition input_kbd_matrix.h:49
void input_kbd_matrix_poll_start(const struct device *dev)
Start scanning the keyboard matrix.
pm_device_action
Device PM actions.
Definition device.h:144
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:513
Keyboard matrix internal APIs.
Definition input_kbd_matrix.h:80
void(* set_detect_mode)(const struct device *dev, bool enabled)
Request to put the matrix in detection mode.
Definition input_kbd_matrix.h:108
kbd_row_t(* read_row)(const struct device *dev)
Read the matrix row.
Definition input_kbd_matrix.h:97
void(* drive_column)(const struct device *dev, int col)
Request to drive a specific column.
Definition input_kbd_matrix.h:91
Common keyboard matrix config.
Definition input_kbd_matrix.h:116
uint32_t debounce_down_us
Definition input_kbd_matrix.h:123
uint8_t * scan_cycle_idx
Definition input_kbd_matrix.h:134
uint32_t settle_time_us
Definition input_kbd_matrix.h:125
bool ghostkey_check
Definition input_kbd_matrix.h:126
uint32_t poll_period_us
Definition input_kbd_matrix.h:120
kbd_row_t * matrix_previous_state
Definition input_kbd_matrix.h:132
kbd_row_t * matrix_unstable_state
Definition input_kbd_matrix.h:131
uint32_t debounce_up_us
Definition input_kbd_matrix.h:124
kbd_row_t * matrix_new_state
Definition input_kbd_matrix.h:133
uint32_t poll_timeout_ms
Definition input_kbd_matrix.h:122
kbd_row_t * matrix_stable_state
Definition input_kbd_matrix.h:130
kbd_row_t * actual_key_mask
Definition input_kbd_matrix.h:127
uint32_t stable_poll_period_us
Definition input_kbd_matrix.h:121
const struct input_kbd_matrix_api * api
Definition input_kbd_matrix.h:117
uint8_t col_size
Definition input_kbd_matrix.h:119
uint8_t row_size
Definition input_kbd_matrix.h:118
Common keyboard matrix data.
Definition input_kbd_matrix.h:255
struct k_thread thread
Definition input_kbd_matrix.h:265
uint8_t scan_cycles_idx
Definition input_kbd_matrix.h:258
uint32_t scan_clk_cycle[30U]
Definition input_kbd_matrix.h:257
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_INPUT_KBD_MATRIX_THREAD_STACK_SIZE)
struct k_sem poll_lock
Definition input_kbd_matrix.h:260
Semaphore structure.
Definition kernel.h:3663
Thread Structure.
Definition thread.h:259
Misc utilities.
Macros to abstract toolchain specific capabilities.