Compare commits
10 Commits
d56989d219
...
e97bd47e36
| Author | SHA1 | Date | |
|---|---|---|---|
| e97bd47e36 | |||
| e226338565 | |||
| b9bb326e8b | |||
| 0da731e59d | |||
| 6610b3471d | |||
| e4c824d657 | |||
| cfcefbf28a | |||
| 42aee4c511 | |||
| 528b486090 | |||
| 2c421b23b6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@
|
||||
|
||||
# build
|
||||
/build*/
|
||||
/external/
|
||||
|
||||
@@ -4,4 +4,22 @@ cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(blinky)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
zephyr_include_directories(
|
||||
inc
|
||||
inc/events
|
||||
)
|
||||
add_subdirectory(drivers)
|
||||
|
||||
target_sources(app PRIVATE
|
||||
src/main.c
|
||||
src/battery_module.c
|
||||
src/encoder_module.c
|
||||
src/keyboard_core_module.c
|
||||
src/usb_hid_module.c
|
||||
src/events/encoder_event.c
|
||||
src/events/hid_led_event.c
|
||||
src/mode_switch_module.c
|
||||
src/events/keyboard_hid_report_event.c
|
||||
src/events/mode_switch_event.c
|
||||
src/events/set_protocol_event.c
|
||||
)
|
||||
|
||||
7
Kconfig
Normal file
7
Kconfig
Normal file
@@ -0,0 +1,7 @@
|
||||
mainmenu "blinky"
|
||||
|
||||
source "Kconfig.zephyr"
|
||||
|
||||
menu "Application Drivers"
|
||||
rsource "drivers/Kconfig"
|
||||
endmenu
|
||||
@@ -1,2 +1,33 @@
|
||||
&pinctrl {
|
||||
i2c0_default: i2c0_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(TWIM_SDA, 1, 0)>,
|
||||
<NRF_PSEL(TWIM_SCL, 0, 24)>;
|
||||
};
|
||||
};
|
||||
|
||||
i2c0_sleep: i2c0_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(TWIM_SDA, 1, 0)>,
|
||||
<NRF_PSEL(TWIM_SCL, 0, 24)>;
|
||||
low-power-enable;
|
||||
};
|
||||
};
|
||||
|
||||
encoder_default: encoder_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(QDEC_A, 0, 10)>,
|
||||
<NRF_PSEL(QDEC_B, 1, 6)>;
|
||||
bias-pull-up;
|
||||
};
|
||||
};
|
||||
|
||||
encoder_sleep: encoder_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(QDEC_A, 0, 10)>,
|
||||
<NRF_PSEL(QDEC_B, 1, 6)>;
|
||||
low-power-enable;
|
||||
bias-pull-up;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/dts-v1/;
|
||||
#include <nordic/nrf52840_qiaa.dtsi>
|
||||
#include "mini_keyboard-pinctrl.dtsi"
|
||||
#include <zephyr/dt-bindings/adc/adc.h>
|
||||
|
||||
/ {
|
||||
model = "Mini keyboard";
|
||||
@@ -14,14 +15,49 @@
|
||||
|
||||
aliases {
|
||||
led0 = &myled0;
|
||||
qdec0 = &qdec;
|
||||
};
|
||||
|
||||
hid_kbd: hid_kbd {
|
||||
compatible = "zephyr,hid-device";
|
||||
label = "HID_KBD";
|
||||
protocol-code = "keyboard";
|
||||
in-report-size = <29>;
|
||||
out-report-size = <1>;
|
||||
in-polling-period-us = <1000>;
|
||||
out-polling-period-us = <1000>;
|
||||
};
|
||||
|
||||
hid_consumer: hid_consumer {
|
||||
compatible = "zephyr,hid-device";
|
||||
label = "HID_CONSUMER";
|
||||
protocol-code = "none";
|
||||
in-report-size = <2>;
|
||||
in-polling-period-us = <1000>;
|
||||
};
|
||||
|
||||
leds {
|
||||
compatible = "gpio-leds";
|
||||
myled0: led_0 {
|
||||
gpios = <&gpio1 2 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
};
|
||||
|
||||
vbatt: vbatt {
|
||||
compatible = "voltage-divider";
|
||||
io-channels = <&adc 7>;
|
||||
output-ohms = <100000>;
|
||||
full-ohms = <200000>;
|
||||
power-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
|
||||
power-on-sample-delay-us = <200>;
|
||||
};
|
||||
|
||||
mode_switch_adc: mode-switch-adc {
|
||||
compatible = "voltage-divider";
|
||||
io-channels = <&adc 5>;
|
||||
output-ohms = <1>;
|
||||
full-ohms = <1>;
|
||||
};
|
||||
};
|
||||
|
||||
&flash0 {
|
||||
@@ -52,6 +88,58 @@
|
||||
};
|
||||
};
|
||||
|
||||
&uicr {
|
||||
nfct-pins-as-gpios;
|
||||
};
|
||||
|
||||
&adc {
|
||||
status = "okay";
|
||||
#address-cells = <1>;
|
||||
#size-cells = <0>;
|
||||
|
||||
channel@5 {
|
||||
reg = <5>;
|
||||
zephyr,gain = "ADC_GAIN_1_6";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40)>;
|
||||
zephyr,input-positive = <NRF_SAADC_AIN5>;
|
||||
zephyr,resolution = <12>;
|
||||
zephyr,oversampling = <4>;
|
||||
};
|
||||
|
||||
channel@7 {
|
||||
reg = <7>;
|
||||
zephyr,gain = "ADC_GAIN_1_4";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40)>;
|
||||
zephyr,input-positive = <NRF_SAADC_AIN7>;
|
||||
zephyr,resolution = <14>;
|
||||
zephyr,oversampling = <4>;
|
||||
};
|
||||
};
|
||||
|
||||
&i2c0 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&i2c0_default>;
|
||||
pinctrl-1 = <&i2c0_sleep>;
|
||||
pinctrl-names = "default", "sleep";
|
||||
clock-frequency = <400000>;
|
||||
|
||||
ip5306: pmic@75 {
|
||||
compatible = "injoinic,ip5306";
|
||||
reg = <0x75>;
|
||||
wakeup-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
|
||||
keepalive-interval-ms = <8000>;
|
||||
keepalive-pulse-width-ms = <500>;
|
||||
keepalive-hardware;
|
||||
status = "okay";
|
||||
};
|
||||
};
|
||||
|
||||
&gpio0 {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&gpio1 {
|
||||
status = "okay";
|
||||
};
|
||||
@@ -59,3 +147,22 @@
|
||||
&gpiote {
|
||||
status = "okay";
|
||||
};
|
||||
|
||||
&qdec {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&encoder_default>;
|
||||
pinctrl-1 = <&encoder_sleep>;
|
||||
pinctrl-names = "default", "sleep";
|
||||
led-pre = <0>;
|
||||
steps = <80>;
|
||||
nordic,period = "SAMPLEPER_1024US";
|
||||
};
|
||||
|
||||
&usbd {
|
||||
status = "okay";
|
||||
num-bidir-endpoints = <0>;
|
||||
num-in-endpoints = <2>;
|
||||
num-out-endpoints = <1>;
|
||||
num-isoin-endpoints = <0>;
|
||||
num-isoout-endpoints = <0>;
|
||||
};
|
||||
|
||||
782
docs/caf_stock_modules_guide.md
Normal file
782
docs/caf_stock_modules_guide.md
Normal file
@@ -0,0 +1,782 @@
|
||||
# CAF 官方现成模块清单与使用方法
|
||||
|
||||
本文基于本地 `NCS v3.2.3` 的官方源码与文档整理,范围以以下目录为准:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf`
|
||||
- `C:\ncs\v3.2.3\nrf\subsys\caf\modules`
|
||||
|
||||
目标是回答两个问题:
|
||||
|
||||
1. CAF 官方现成提供了哪些模块可以直接用。
|
||||
2. 这些模块最小要怎么启用、怎么接入。
|
||||
|
||||
## 1. CAF 是什么
|
||||
|
||||
CAF, Common Application Framework,是 Nordic 基于 `app_event_manager` 封装的一组现成模块和事件。
|
||||
|
||||
它的基本模式是:
|
||||
|
||||
- 你启用某个 CAF 模块
|
||||
- 模块监听 CAF 事件或应用自定义事件
|
||||
- 模块自己和别的模块通过事件解耦通信
|
||||
|
||||
因此 CAF 更像一套“事件驱动应用积木”,而不是单个库函数。
|
||||
|
||||
## 2. 使用 CAF 的最小前提
|
||||
|
||||
在使用任何 CAF 模块前,建议先完成这 3 件事。
|
||||
|
||||
### 2.1 打开 CAF
|
||||
|
||||
在 `prj.conf` 中至少启用:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF=y
|
||||
```
|
||||
|
||||
### 2.2 启用并初始化 Application Event Manager
|
||||
|
||||
你的应用需要正常使用 `app_event_manager`。
|
||||
|
||||
### 2.3 在 `main()` 中发出第一条 `module_state_event`
|
||||
|
||||
CAF 模块在收到 `main` 模块的 `MODULE_STATE_READY` 后才会继续初始化。
|
||||
|
||||
典型写法:
|
||||
|
||||
```c
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE main
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (app_event_manager_init()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
可以参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\caf_overview.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\main.c`
|
||||
|
||||
## 3. CAF 官方现成模块列表
|
||||
|
||||
按 `C:\ncs\v3.2.3\nrf\subsys\caf\modules\Kconfig`,NCS 3.2.3 中 CAF 官方模块包括:
|
||||
|
||||
1. `CAF_BLE_ADV`
|
||||
2. `CAF_BLE_BOND`
|
||||
3. `CAF_BLE_SMP`
|
||||
4. `CAF_BLE_STATE`
|
||||
5. `CAF_BLE_STATE_PM`
|
||||
6. `CAF_BUTTONS`
|
||||
7. `CAF_BUTTONS_PM_KEEP_ALIVE`
|
||||
8. `CAF_CLICK_DETECTOR`
|
||||
9. `CAF_FACTORY_RESET_REQUEST`
|
||||
10. `CAF_LEDS`
|
||||
11. `CAF_NET_STATE`
|
||||
12. `CAF_POWER_MANAGER`
|
||||
13. `CAF_SENSOR_DATA_AGGREGATOR`
|
||||
14. `CAF_SENSOR_MANAGER`
|
||||
15. `CAF_SETTINGS_LOADER`
|
||||
16. `CAF_SHELL`
|
||||
|
||||
## 4. 模块总览表
|
||||
|
||||
| 模块 | 主要作用 | 典型输入 | 典型输出 | 最小启用点 |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| `CAF_BUTTONS` | 扫描按键/矩阵键盘 GPIO | GPIO 变化 | `button_event` | `CONFIG_CAF_BUTTONS=y` |
|
||||
| `CAF_BUTTONS_PM_KEEP_ALIVE` | 按键活动保持系统唤醒 | `button_event` | `keep_alive_event` | `CONFIG_CAF_BUTTONS_PM_KEEP_ALIVE=y` |
|
||||
| `CAF_CLICK_DETECTOR` | 将按键动作识别为短按/长按/双击 | `button_event` | `click_event` | `CONFIG_CAF_CLICK_DETECTOR=y` |
|
||||
| `CAF_LEDS` | 根据 LED effect 控制 LED | `led_event` | LED 状态变化 | `CONFIG_CAF_LEDS=y` |
|
||||
| `CAF_POWER_MANAGER` | 管理挂起/唤醒/关机 | keep-alive、restriction、error | `power_down_event`、`wake_up_event`、`power_off_event` | `CONFIG_CAF_POWER_MANAGER=y` |
|
||||
| `CAF_BLE_STATE` | 打开 BLE、管理连接回调 | 蓝牙栈回调 | `ble_peer_event`、`ble_peer_conn_params_event` | `CONFIG_CAF_BLE_STATE=y` |
|
||||
| `CAF_BLE_STATE_PM` | BLE 连接存在时限制省电级别 | `ble_peer_event` | power restriction | `CONFIG_CAF_BLE_STATE_PM=y` |
|
||||
| `CAF_BLE_ADV` | Peripheral 侧广播控制 | BLE 状态、advertising provider 数据 | 广播行为、`force_power_down_event` | `CONFIG_CAF_BLE_ADV=y` |
|
||||
| `CAF_BLE_BOND` | 默认 BLE bond 管理 | `click_event`、settings | bond erase 行为 | `CONFIG_CAF_BLE_BOND=y` |
|
||||
| `CAF_BLE_SMP` | BLE 上 MCUmgr DFU | MCUmgr 传输 | `ble_smp_transfer_event` | `CONFIG_CAF_BLE_SMP=y` |
|
||||
| `CAF_SETTINGS_LOADER` | 在合适时机调用 `settings_load()` | `module_state_event` | settings 已装载 | `CONFIG_CAF_SETTINGS_LOADER=y` |
|
||||
| `CAF_NET_STATE` | 上报网络连接状态 | LTE / OpenThread backend | `net_state_event` | `CONFIG_CAF_NET_STATE=y` |
|
||||
| `CAF_SENSOR_MANAGER` | 周期采样传感器 | sensor driver | `sensor_event`、`sensor_state_event` | `CONFIG_CAF_SENSOR_MANAGER=y` |
|
||||
| `CAF_SENSOR_DATA_AGGREGATOR` | 聚合 sensor_event 数据块 | `sensor_event` | `sensor_data_aggregator_event` | `CONFIG_CAF_SENSOR_DATA_AGGREGATOR=y` |
|
||||
| `CAF_FACTORY_RESET_REQUEST` | 上电窗口内按键触发恢复出厂请求 | `button_event` | `factory_reset_event` | `CONFIG_CAF_FACTORY_RESET_REQUEST=y` |
|
||||
| `CAF_SHELL` | 通过 shell 手动发 CAF 事件 | shell 命令 | `button_event` 等 | `CONFIG_CAF_SHELL=y` |
|
||||
|
||||
## 5. 各模块使用方法
|
||||
|
||||
下面按“用途、最小接入方法、何时使用”来总结。
|
||||
|
||||
### 5.1 `CAF_BUTTONS`
|
||||
|
||||
**用途**
|
||||
|
||||
- 读取独立按键或矩阵键盘
|
||||
- 统一生成 `button_event`
|
||||
|
||||
**最小接入**
|
||||
|
||||
1. 在 `prj.conf` 打开:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BUTTONS=y
|
||||
CONFIG_GPIO=y
|
||||
```
|
||||
|
||||
2. 新建按钮定义头文件,例如 `buttons_def.h`,定义:
|
||||
|
||||
- `row[]`
|
||||
- `col[]`
|
||||
|
||||
3. 用 `CONFIG_CAF_BUTTONS_DEF_PATH` 指向这个配置文件。
|
||||
|
||||
例如:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BUTTONS_DEF_PATH="buttons_def.h"
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 你要做按键输入,基本都会先用它
|
||||
- 对键盘项目最常用
|
||||
|
||||
**补充**
|
||||
|
||||
- 支持矩阵键盘和直接 GPIO 按键
|
||||
- 支持去抖、扫描周期、按键极性配置
|
||||
- 可选支持 PM 事件和唤醒
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\buttons.rst`
|
||||
|
||||
### 5.2 `CAF_BUTTONS_PM_KEEP_ALIVE`
|
||||
|
||||
**用途**
|
||||
|
||||
- 按键按下时自动发 `keep_alive_event`
|
||||
- 常配合 `CAF_POWER_MANAGER`
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BUTTONS=y
|
||||
CONFIG_CAF_POWER_MANAGER=y
|
||||
CONFIG_CAF_BUTTONS_PM_KEEP_ALIVE=y
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 设备需要超时休眠
|
||||
- 但用户按键活动应重置休眠计时
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\buttons_pm_keep_alive.rst`
|
||||
|
||||
### 5.3 `CAF_CLICK_DETECTOR`
|
||||
|
||||
**用途**
|
||||
|
||||
- 从 `button_event` 识别短按、长按、双击
|
||||
- 生成 `click_event`
|
||||
|
||||
**最小接入**
|
||||
|
||||
1. 打开:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_CLICK_DETECTOR=y
|
||||
```
|
||||
|
||||
2. 新建 click 配置头文件,定义 `click_detector_config[]`。
|
||||
|
||||
至少要给出:
|
||||
|
||||
- `key_id`
|
||||
- `consume_button_event`
|
||||
|
||||
3. 用 `CONFIG_CAF_CLICK_DETECTOR_DEF_PATH` 指向该文件。
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 一个键要复用多个动作
|
||||
- 如长按进入配对、双击切层、长按恢复出厂
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\click_detector.rst`
|
||||
|
||||
### 5.4 `CAF_LEDS`
|
||||
|
||||
**用途**
|
||||
|
||||
- 接收 `led_event`
|
||||
- 用 PWM 或 GPIO 驱动 LED
|
||||
- 支持 LED effect
|
||||
|
||||
**最小接入**
|
||||
|
||||
PWM 方案示例:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_LEDS=y
|
||||
CONFIG_CAF_LEDS_PWM=y
|
||||
CONFIG_LED=y
|
||||
CONFIG_LED_PWM=y
|
||||
CONFIG_PWM=y
|
||||
```
|
||||
|
||||
GPIO 方案示例:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_LEDS=y
|
||||
CONFIG_CAF_LEDS_GPIO=y
|
||||
CONFIG_LED=y
|
||||
CONFIG_LED_GPIO=y
|
||||
CONFIG_GPIO=y
|
||||
```
|
||||
|
||||
同时还需要:
|
||||
|
||||
- 在 DTS 或 overlay 中定义 LED 节点
|
||||
- 在应用里由别的模块发 `led_event`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 做状态灯、层指示、配对指示、电量指示
|
||||
|
||||
**补充**
|
||||
|
||||
- 想做平滑呼吸灯,优先用 PWM
|
||||
- GPIO 版只适合开关式 LED
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\leds.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\samples\caf`
|
||||
|
||||
### 5.5 `CAF_POWER_MANAGER`
|
||||
|
||||
**用途**
|
||||
|
||||
- 管理系统从 idle 到 suspended/off 的切换
|
||||
- 对外广播 `power_down_event`、`wake_up_event`、`power_off_event`
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_POWER_MANAGER=y
|
||||
```
|
||||
|
||||
常用附加项:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
||||
CONFIG_CAF_POWER_MANAGER_ERROR_TIMEOUT=30
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 电池设备
|
||||
- 需要空闲休眠/关机
|
||||
- 多模块都需要统一响应省电状态
|
||||
|
||||
**补充**
|
||||
|
||||
- 常和 `CAF_BUTTONS_PM_KEEP_ALIVE` 配合
|
||||
- 其他模块可用 `power_manager_restrict_event` 限制最大省电级别
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\power_manager.rst`
|
||||
|
||||
### 5.6 `CAF_BLE_STATE`
|
||||
|
||||
**用途**
|
||||
|
||||
- 启动 BLE
|
||||
- 处理连接和参数回调
|
||||
- 向应用广播 BLE 连接状态事件
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_CAF_BLE_STATE=y
|
||||
```
|
||||
|
||||
常见附加项:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_STATE_SECURITY_REQ=y
|
||||
CONFIG_CAF_BLE_USE_LLPM=y
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 只要 CAF 体系下要做 BLE,几乎都先启用它
|
||||
- 它本身不负责广播或扫描
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_state.rst`
|
||||
|
||||
### 5.7 `CAF_BLE_STATE_PM`
|
||||
|
||||
**用途**
|
||||
|
||||
- 当 BLE 连接存在时,阻止系统进入过深省电级别
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_STATE=y
|
||||
CONFIG_CAF_POWER_MANAGER=y
|
||||
CONFIG_CAF_BLE_STATE_PM=y
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- BLE 外设连接后不能立即休眠
|
||||
- 要让 BLE 连接期间系统维持可通信
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_state_pm.rst`
|
||||
|
||||
### 5.8 `CAF_BLE_ADV`
|
||||
|
||||
**用途**
|
||||
|
||||
- 作为 BLE Peripheral 控制广播
|
||||
|
||||
**最小接入**
|
||||
|
||||
1. 启用 `CAF_BLE_STATE`
|
||||
2. 启用:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_ADV=y
|
||||
```
|
||||
|
||||
3. 配置 advertising data provider 和 scan response provider
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 你的设备要作为 BLE Peripheral 被手机/PC/主机发现
|
||||
|
||||
**补充**
|
||||
|
||||
- 支持快慢广播
|
||||
- 支持 directed advertising
|
||||
- 支持 suspend/resume
|
||||
- 支持 grace period
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_adv.rst`
|
||||
|
||||
### 5.9 `CAF_BLE_BOND`
|
||||
|
||||
**用途**
|
||||
|
||||
- 提供默认 BLE bond 管理
|
||||
- 可通过特定点击动作执行 bond erase
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_BOND=y
|
||||
CONFIG_BT_BONDABLE=y
|
||||
CONFIG_BT_SETTINGS=y
|
||||
CONFIG_CAF_SETTINGS_LOADER=y
|
||||
CONFIG_CAF_BLE_COMMON_EVENTS=y
|
||||
```
|
||||
|
||||
如果要支持按键清除 bond,还要配置:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK=y
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_KEY_ID=0x0000
|
||||
```
|
||||
|
||||
并选择触发类型之一:
|
||||
|
||||
- `CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_SHORT`
|
||||
- `CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_LONG`
|
||||
- `CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_DOUBLE`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 简单 BLE 应用需要默认配对/绑定位管理
|
||||
|
||||
**补充**
|
||||
|
||||
- 只适合简单应用
|
||||
- 更复杂的多 identity / 多 peer 管理,通常要自己实现
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_bond.rst`
|
||||
|
||||
### 5.10 `CAF_BLE_SMP`
|
||||
|
||||
**用途**
|
||||
|
||||
- 通过 BLE 做 MCUmgr DFU
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_BLE_STATE=y
|
||||
CONFIG_CAF_BLE_SMP=y
|
||||
CONFIG_MCUMGR_GRP_IMG=y
|
||||
CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS=y
|
||||
CONFIG_MCUMGR_GRP_IMG_UPLOAD_CHECK_HOOK=y
|
||||
CONFIG_MCUMGR_TRANSPORT_BT=y
|
||||
CONFIG_BOOTLOADER_MCUBOOT=y
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 需要 BLE OTA/DFU
|
||||
|
||||
**补充**
|
||||
|
||||
- 依赖 MCUboot
|
||||
- 构建后会在 build 目录生成 `dfu_application.zip`
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_smp.rst`
|
||||
|
||||
### 5.11 `CAF_SETTINGS_LOADER`
|
||||
|
||||
**用途**
|
||||
|
||||
- 在合适的初始化时机调用 `settings_load()`
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_SETTINGS_LOADER=y
|
||||
CONFIG_SETTINGS=y
|
||||
```
|
||||
|
||||
还需要:
|
||||
|
||||
- 新建配置头文件
|
||||
- 实现 `get_req_modules(struct module_flags *mf)`
|
||||
|
||||
这个函数用于告诉 settings loader:
|
||||
|
||||
- 哪些模块 ready 以后再加载 settings
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 你的应用用了 settings/NVS/BT settings
|
||||
- 比如 BLE bond、用户配置、持久化参数
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\settings_loader.rst`
|
||||
|
||||
### 5.12 `CAF_NET_STATE`
|
||||
|
||||
**用途**
|
||||
|
||||
- 上报网络连接状态
|
||||
- 提供 LTE / OpenThread backend
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_NET_STATE=y
|
||||
```
|
||||
|
||||
具体 backend 由链路层决定,例如:
|
||||
|
||||
- `CONFIG_CAF_NET_STATE_LTE`
|
||||
- `CONFIG_CAF_NET_STATE_OT`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 不是键盘/鼠标常用模块
|
||||
- 更适合蜂窝或 Thread 设备
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\net_state.rst`
|
||||
|
||||
### 5.13 `CAF_SENSOR_MANAGER`
|
||||
|
||||
**用途**
|
||||
|
||||
- 周期采样传感器
|
||||
- 统一生成 `sensor_event` 和 `sensor_state_event`
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_SENSOR_MANAGER=y
|
||||
CONFIG_SENSOR=y
|
||||
```
|
||||
|
||||
还需要:
|
||||
|
||||
1. 在 DTS/overlay 中启用传感器
|
||||
2. 打开对应传感器驱动 Kconfig
|
||||
3. 新建 `sm_sensor_config[]` 配置文件
|
||||
4. 用 `CONFIG_CAF_SENSOR_MANAGER_DEF_PATH` 指向该文件
|
||||
|
||||
配置项通常至少包括:
|
||||
|
||||
- `dev_name`
|
||||
- `event_descr`
|
||||
- `chans`
|
||||
- `chan_cnt`
|
||||
- `sampling_period_ms`
|
||||
- `active_events_limit`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 有 IMU、加速度计、光传感器、环境传感器等
|
||||
|
||||
**补充**
|
||||
|
||||
- 有独立采样线程
|
||||
- 支持 trigger
|
||||
- 支持 PM
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\sensor_manager.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\samples\caf_sensor_manager`
|
||||
|
||||
### 5.14 `CAF_SENSOR_DATA_AGGREGATOR`
|
||||
|
||||
**用途**
|
||||
|
||||
- 将多个 `sensor_event` 聚合成更大的数据包
|
||||
- 多核 SoC 下可降低核间唤醒频率
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_SENSOR_DATA_AGGREGATOR=y
|
||||
```
|
||||
|
||||
如果另一核心要接收聚合事件,还可启用:
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_SENSOR_DATA_AGGREGATOR_EVENTS=y
|
||||
```
|
||||
|
||||
还需要:
|
||||
|
||||
- 在 DTS/overlay 里添加 `compatible = "caf,aggregator"` 的节点
|
||||
|
||||
关键属性:
|
||||
|
||||
- `sensor_descr`
|
||||
- `buf_data_length`
|
||||
- `sample_size`
|
||||
- `buf_count`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 传感器数据量较大
|
||||
- 需要批处理
|
||||
- 或者多核 SoC 上做功耗优化
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\sensor_data_aggregator.rst`
|
||||
|
||||
### 5.15 `CAF_FACTORY_RESET_REQUEST`
|
||||
|
||||
**用途**
|
||||
|
||||
- 在上电初始化窗口中检测某个按键是否被按住
|
||||
- 若满足条件,发出 `factory_reset_event`
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_FACTORY_RESET_REQUEST=y
|
||||
CONFIG_CAF_FACTORY_RESET_REQUEST_BUTTON=0x0000
|
||||
CONFIG_CAF_FACTORY_RESET_REQUEST_DELAY=50
|
||||
```
|
||||
|
||||
它依赖:
|
||||
|
||||
- `CAF_BUTTONS`
|
||||
- `button_event`
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 开机长按某键请求恢复出厂
|
||||
- 需要给上层模块一个统一 factory-reset 事件入口
|
||||
|
||||
**补充**
|
||||
|
||||
- 这个模块在 `NCS v3.2.3` 源码中存在
|
||||
- 但没有找到与其他模块同等级的独立官方 `.rst` 页面
|
||||
- 当前说明主要依据:
|
||||
- `C:\ncs\v3.2.3\nrf\subsys\caf\modules\Kconfig.factory_reset_request`
|
||||
- `C:\ncs\v3.2.3\nrf\subsys\caf\modules\factory_reset_request.c`
|
||||
|
||||
### 5.16 `CAF_SHELL`
|
||||
|
||||
**用途**
|
||||
|
||||
- 通过 Zephyr Shell 手工触发 CAF 事件
|
||||
- 便于调试
|
||||
|
||||
**最小接入**
|
||||
|
||||
```conf
|
||||
CONFIG_CAF_SHELL=y
|
||||
CONFIG_SHELL=y
|
||||
CONFIG_CAF=y
|
||||
```
|
||||
|
||||
常见命令:
|
||||
|
||||
```text
|
||||
caf_events button_event [button_id] [pressed]
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```text
|
||||
caf_events button_event 1 y
|
||||
caf_events button_event 1 n
|
||||
```
|
||||
|
||||
**何时使用**
|
||||
|
||||
- 没有真实按键硬件时做联调
|
||||
- 验证 button/click/LED/状态机逻辑
|
||||
|
||||
参考:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\caf_shell.rst`
|
||||
|
||||
## 6. 对键盘项目最常用的 CAF 组合
|
||||
|
||||
如果你的项目是 `C:\projects\blinky` 这种自定义键盘方向,最常见的组合通常是:
|
||||
|
||||
### 6.1 纯输入层
|
||||
|
||||
- `CAF_BUTTONS`
|
||||
- `CAF_CLICK_DETECTOR`
|
||||
|
||||
适合:
|
||||
|
||||
- 扫描键盘按键
|
||||
- 做短按/长按/双击功能键
|
||||
|
||||
### 6.2 带状态灯
|
||||
|
||||
- `CAF_BUTTONS`
|
||||
- `CAF_CLICK_DETECTOR`
|
||||
- `CAF_LEDS`
|
||||
|
||||
适合:
|
||||
|
||||
- Caps Lock 指示
|
||||
- 蓝牙通道指示
|
||||
- 层状态指示
|
||||
|
||||
### 6.3 电池设备
|
||||
|
||||
- `CAF_BUTTONS`
|
||||
- `CAF_POWER_MANAGER`
|
||||
- `CAF_BUTTONS_PM_KEEP_ALIVE`
|
||||
- `CAF_LEDS`
|
||||
|
||||
适合:
|
||||
|
||||
- 一段时间无操作后休眠
|
||||
- 按键恢复活跃状态
|
||||
|
||||
### 6.4 BLE 键盘
|
||||
|
||||
- `CAF_BLE_STATE`
|
||||
- `CAF_BLE_ADV`
|
||||
- `CAF_BLE_BOND`
|
||||
- `CAF_SETTINGS_LOADER`
|
||||
|
||||
按需叠加:
|
||||
|
||||
- `CAF_BLE_STATE_PM`
|
||||
- `CAF_BLE_SMP`
|
||||
|
||||
适合:
|
||||
|
||||
- 支持广播、连接、加密、绑定
|
||||
- 支持设置装载
|
||||
- 需要时支持 OTA
|
||||
|
||||
## 7. 推荐的接入顺序
|
||||
|
||||
如果你要在 `blinky` 里逐步引入 CAF,建议顺序如下:
|
||||
|
||||
1. 先接 `CAF_BUTTONS`
|
||||
2. 再接 `CAF_LEDS`
|
||||
3. 然后根据需要加 `CAF_CLICK_DETECTOR`
|
||||
4. 如果是电池设备,再接 `CAF_POWER_MANAGER`
|
||||
5. 如果要做 BLE,再接 `CAF_BLE_STATE`、`CAF_BLE_ADV`、`CAF_BLE_BOND`
|
||||
6. 如果要做 OTA,再接 `CAF_BLE_SMP`
|
||||
|
||||
这样改动面最小,也最容易定位问题。
|
||||
|
||||
## 8. 官方参考路径
|
||||
|
||||
建议优先阅读这些本地官方文件:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\caf_overview.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\buttons.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\click_detector.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\leds.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\power_manager.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_state.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_state_pm.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_adv.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_bond.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_smp.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\settings_loader.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\net_state.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\sensor_manager.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\sensor_data_aggregator.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\caf_shell.rst`
|
||||
|
||||
同时可参考源码:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\subsys\caf\modules`
|
||||
- `C:\ncs\v3.2.3\nrf\samples\caf`
|
||||
- `C:\ncs\v3.2.3\nrf\samples\caf_sensor_manager`
|
||||
|
||||
## 9. 一句话结论
|
||||
|
||||
如果只看键盘/鼠标类项目,CAF 里最值得优先使用的官方模块通常是:
|
||||
|
||||
- `CAF_BUTTONS`
|
||||
- `CAF_CLICK_DETECTOR`
|
||||
- `CAF_LEDS`
|
||||
- `CAF_POWER_MANAGER`
|
||||
- `CAF_BLE_STATE`
|
||||
- `CAF_BLE_ADV`
|
||||
- `CAF_BLE_BOND`
|
||||
- `CAF_SETTINGS_LOADER`
|
||||
|
||||
它们已经覆盖了一个 BLE/USB 输入设备项目里最常见的基础设施。
|
||||
500
docs/nrf_desktop_architecture.md
Normal file
500
docs/nrf_desktop_architecture.md
Normal file
@@ -0,0 +1,500 @@
|
||||
# nRF Desktop 官方程序架构说明
|
||||
|
||||
本文基于 `C:\ncs\v3.2.3\nrf\applications\nrf_desktop` 中的官方源码与文档整理,目的是帮助在 `C:\projects\blinky` 中开发自定义键盘或 HID 设备时,理解 `nrf_desktop` 的整体设计思路。
|
||||
|
||||
## 1. nRF Desktop 是什么
|
||||
|
||||
`nrf_desktop` 不是一个单体应用,而是 Nordic 在 NCS 中提供的一个参考级 HID 框架。它可以通过不同配置,工作成以下几类设备:
|
||||
|
||||
- 鼠标
|
||||
- 键盘
|
||||
- Dongle
|
||||
|
||||
它同时支持以下传输方式:
|
||||
|
||||
- Bluetooth Low Energy
|
||||
- USB
|
||||
- BLE + USB 并存
|
||||
|
||||
官方文档的核心描述是:这个应用是一个基于 CAF 和 Application Event Manager 的模块化、事件驱动架构。
|
||||
|
||||
对应源码和文档位置:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\main.c`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\description.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\modules.rst`
|
||||
|
||||
## 2. 整体设计思想
|
||||
|
||||
它的设计目标主要有三个:
|
||||
|
||||
- 高性能,尤其是 HID report rate 和输入延迟
|
||||
- 可配置,不同板子和不同产品形态共用同一套代码骨架
|
||||
- 可扩展,通过增加模块或替换模块实现新功能
|
||||
|
||||
`nrf_desktop` 的关键点在于:
|
||||
|
||||
- `main()` 几乎不做业务逻辑
|
||||
- 功能被拆成很多独立模块
|
||||
- 模块之间主要通过事件通信,而不是直接互相调用
|
||||
- 不同产品形态通过 Kconfig、DTS overlay 和配置头文件组合出来
|
||||
|
||||
因此它更像一个“产品框架”,而不是一个简单示例。
|
||||
|
||||
## 3. 启动流程
|
||||
|
||||
`src/main.c` 非常简单,核心逻辑只有两步:
|
||||
|
||||
1. 初始化 `app_event_manager`
|
||||
2. 发送 `module_state_event(MODULE_STATE_READY)`
|
||||
|
||||
也就是说,`main()` 只是启动系统并广播“主模块已经准备好”。其他模块监听这个事件后,再分别完成自己的初始化。
|
||||
|
||||
这和传统的串行初始化方式不同:
|
||||
|
||||
- 传统方式:`main -> init_ble -> init_usb -> init_keys -> init_hid`
|
||||
- `nrf_desktop` 方式:`main` 只发启动事件,各模块自己响应并进入就绪状态
|
||||
|
||||
这种模式的优点是模块之间耦合更低,方便裁剪和重用。
|
||||
|
||||
## 4. 源码目录分层
|
||||
|
||||
`nrf_desktop` 的目录结构本身就体现了它的架构分层:
|
||||
|
||||
### 4.1 `src/events`
|
||||
|
||||
这里定义事件类型,相当于模块间通信协议。
|
||||
|
||||
常见事件包括:
|
||||
|
||||
- `motion_event`
|
||||
- `hid_report_event`
|
||||
- `hid_report_sent_event`
|
||||
- `ble_event`
|
||||
- `usb_event`
|
||||
- `battery_event`
|
||||
- `config_event`
|
||||
|
||||
这些事件不是“业务实现”,而是模块之间交换信息的数据载体。
|
||||
|
||||
### 4.2 `src/hw_interface`
|
||||
|
||||
这一层负责直接接触硬件,把硬件输入转换为内部事件。
|
||||
|
||||
例如:
|
||||
|
||||
- `board.c`
|
||||
- `motion_sensor.c`
|
||||
- `motion_buttons.c`
|
||||
- `wheel.c`
|
||||
- `battery_meas.c`
|
||||
- `passkey_buttons.c`
|
||||
|
||||
这一层可以理解成“硬件抽象输入层”。
|
||||
|
||||
### 4.3 `src/modules`
|
||||
|
||||
这是最核心的一层,负责系统行为和业务逻辑。
|
||||
|
||||
例如:
|
||||
|
||||
- `hid_state.c`
|
||||
- `hids.c`
|
||||
- `usb_state.c`
|
||||
- `hid_forward.c`
|
||||
- `ble_scan.c`
|
||||
- `ble_discovery.c`
|
||||
- `ble_bond.c`
|
||||
- `led_state.c`
|
||||
- `dfu.c`
|
||||
- `qos.c`
|
||||
|
||||
如果说 `hw_interface` 负责“采集输入”,那么 `modules` 负责“处理输入并把它交付给主机或其他设备”。
|
||||
|
||||
### 4.4 `src/util`
|
||||
|
||||
这一层放通用工具和公共基础能力。
|
||||
|
||||
例如:
|
||||
|
||||
- `hid_reportq.c`
|
||||
- `hid_eventq.c`
|
||||
- `hid_keymap.c`
|
||||
- `hwid.c`
|
||||
- `config_channel_transport.c`
|
||||
|
||||
这一层是支撑模块工作的辅助库。
|
||||
|
||||
## 5. 事件驱动架构
|
||||
|
||||
`nrf_desktop` 的核心是事件驱动。
|
||||
|
||||
典型模式是:
|
||||
|
||||
1. 某个模块监听一个或多个事件
|
||||
2. 收到事件后更新内部状态
|
||||
3. 如有需要,再提交新的事件
|
||||
4. 其他模块继续响应
|
||||
|
||||
例如:
|
||||
|
||||
- 按键模块产生 `button_event`
|
||||
- HID provider 收到后更新 report 数据
|
||||
- `hid_state` 请求生成 HID report
|
||||
- `usb_state` 或 `hids` 把 report 发送出去
|
||||
- 发送完成后提交 `hid_report_sent_event`
|
||||
- 上游模块再决定是否采下一帧输入
|
||||
|
||||
所以它的整体运行更像“事件链路”,而不是“函数调用链”。
|
||||
|
||||
官方对这种模式的说明可以参考:
|
||||
|
||||
- `description.rst`
|
||||
- `doc/event_propagation.rst`
|
||||
|
||||
## 6. 两种核心设备角色
|
||||
|
||||
`nrf_desktop` 架构里最重要的划分不是“鼠标还是键盘”,而是以下两个角色:
|
||||
|
||||
- HID Peripheral
|
||||
- HID Dongle
|
||||
|
||||
这两个角色决定了系统的核心数据流完全不同。
|
||||
|
||||
### 6.1 HID Peripheral
|
||||
|
||||
这种角色下,设备本身就是输入设备,比如键盘或鼠标。
|
||||
|
||||
它的职责是:
|
||||
|
||||
- 采集本地硬件输入
|
||||
- 生成 HID report
|
||||
- 通过 BLE 或 USB 发给主机
|
||||
|
||||
典型模块包括:
|
||||
|
||||
- `buttons`
|
||||
- `motion`
|
||||
- `wheel`
|
||||
- `hid_provider_*`
|
||||
- `hid_state`
|
||||
- `hids`
|
||||
- `usb_state`
|
||||
|
||||
### 6.2 HID Dongle
|
||||
|
||||
这种角色下,设备本身不直接生成输入,而是做桥接转发。
|
||||
|
||||
它的职责是:
|
||||
|
||||
- 作为 BLE Central 连接外部 HID 外设
|
||||
- 接收这些外设通过 HOGP 发来的 HID report
|
||||
- 再通过 USB 转发给 PC 主机
|
||||
|
||||
典型模块包括:
|
||||
|
||||
- `ble_scan`
|
||||
- `ble_discovery`
|
||||
- `ble_conn_params`
|
||||
- `hid_forward`
|
||||
- `usb_state`
|
||||
|
||||
所以:
|
||||
|
||||
- Peripheral 模式更像“输入源”
|
||||
- Dongle 模式更像“协议桥”
|
||||
|
||||
## 7. Peripheral 模式的核心链路
|
||||
|
||||
在 Peripheral 模式下,系统最核心的中心模块是 `hid_state`。
|
||||
|
||||
它负责:
|
||||
|
||||
- 管理哪些 HID subscriber 当前有效
|
||||
- 决定 report 应该发给 USB 还是 BLE
|
||||
- 和各类 `hid_provider` 交互
|
||||
- 处理 HID output report,例如键盘 LED 状态
|
||||
|
||||
你可以把它理解成“本地 HID 数据总调度器”。
|
||||
|
||||
### 7.1 典型数据流
|
||||
|
||||
以键盘或鼠标为例,数据流大致是:
|
||||
|
||||
`buttons / motion / wheel`
|
||||
-> 产生原始输入事件
|
||||
-> `hid_provider_*`
|
||||
-> `hid_state`
|
||||
-> `usb_state` 或 `hids`
|
||||
-> 主机
|
||||
|
||||
其中:
|
||||
|
||||
- `hid_provider_mouse` 负责组装鼠标输入 report
|
||||
- `hid_provider_keyboard` 负责组装键盘 report
|
||||
- `hid_provider_consumer_ctrl` 负责多媒体键
|
||||
- `hid_provider_system_ctrl` 负责系统控制键
|
||||
|
||||
它们都不是最终传输模块,而是“report 生成器”。
|
||||
|
||||
### 7.2 `hid_state` 为什么重要
|
||||
|
||||
`hid_state` 的价值在于把“输入生成”和“传输介质”解耦:
|
||||
|
||||
- 上游模块只关心输入语义
|
||||
- 下游模块只关心通过 USB 或 BLE 发送
|
||||
- `hid_state` 负责把两边连起来
|
||||
|
||||
这样键盘逻辑不需要知道当前是 USB 主机在收,还是 BLE 主机在收。
|
||||
|
||||
## 8. 鼠标高性能链路
|
||||
|
||||
`nrf_desktop` 对鼠标场景做了专门优化,尤其是高 report rate。
|
||||
|
||||
官方文档里说明得很清楚:
|
||||
|
||||
- Motion sensor 的采样和 HID report 的发送是同步的
|
||||
- `hid_report_sent_event` 会触发下一次采样
|
||||
- 在 BLE 或某些 USB 配置下,会建立两个 report 的 pipeline
|
||||
|
||||
原因是:
|
||||
|
||||
- BLE 通知完成的确认存在一个连接间隔延迟
|
||||
- USB poll 也可能有时间抖动
|
||||
|
||||
因此系统不是“采样器一直跑,report 有空再发”,而是“根据发送节奏反推采样节奏”。
|
||||
|
||||
这是一种很典型的低延迟输入设备设计。
|
||||
|
||||
对键盘来说,这种 pipeline 不一定像鼠标那样关键,但它说明官方非常重视链路级时序设计。
|
||||
|
||||
## 9. BLE 传输层模块
|
||||
|
||||
在 Peripheral 角色下,BLE 相关模块大致分为几类:
|
||||
|
||||
- `ble_state`
|
||||
- `ble_adv`
|
||||
- `ble_bond`
|
||||
- `ble_latency`
|
||||
- `hids`
|
||||
- `bas`
|
||||
- `dev_descr`
|
||||
|
||||
职责可以简单理解为:
|
||||
|
||||
- `ble_state`:打开蓝牙、处理连接状态和参数回调
|
||||
- `ble_adv`:负责广播
|
||||
- `ble_bond`:负责绑定和身份管理
|
||||
- `ble_latency`:在配置或升级场景下降低连接延迟
|
||||
- `hids`:真正承载 HID over GATT
|
||||
- `bas`:电池服务
|
||||
- `dev_descr`:设备描述和硬件 ID
|
||||
|
||||
其中真正“把 HID report 发到 BLE 主机”的是 `hids`。
|
||||
|
||||
## 10. USB 传输层模块
|
||||
|
||||
USB 侧的核心模块是 `usb_state`。
|
||||
|
||||
它负责:
|
||||
|
||||
- 跟踪 USB 连接状态
|
||||
- 注册 HID class 实例
|
||||
- 接收内部 `hid_report_event`
|
||||
- 把 report 送入 USB 栈
|
||||
- 发送完成后产生 `hid_report_sent_event`
|
||||
- 在需要时处理 output report
|
||||
|
||||
它既是传输模块,也是系统状态模块。
|
||||
|
||||
官方还支持:
|
||||
|
||||
- legacy USB stack
|
||||
- USB next stack
|
||||
|
||||
而且不同设备还能配置:
|
||||
|
||||
- 单个 HID USB 实例
|
||||
- 多个 HID USB 实例
|
||||
- boot protocol
|
||||
- report protocol
|
||||
|
||||
这说明 `usb_state` 设计得非常通用,并不是只为单一 demo 服务。
|
||||
|
||||
## 11. Dongle 模式的核心链路
|
||||
|
||||
如果系统工作在 Dongle 模式,最核心的模块不再是 `hid_state`,而是 `hid_forward`。
|
||||
|
||||
它负责:
|
||||
|
||||
- 从 BLE 外设接收 HID report
|
||||
- 必要时用队列缓存 report
|
||||
- 转成内部事件
|
||||
- 再发给 `usb_state`
|
||||
- 把主机下发的 output report 再转发回 BLE 外设
|
||||
|
||||
典型数据流是:
|
||||
|
||||
BLE HID Peripheral
|
||||
-> `ble_discovery`
|
||||
-> `hid_forward`
|
||||
-> `usb_state`
|
||||
-> PC 主机
|
||||
|
||||
在这个角色里,设备自己不再生产键值或鼠标移动,而是充当“BLE 到 USB 的协议转换桥”。
|
||||
|
||||
## 12. 配置通道和 DFU
|
||||
|
||||
`nrf_desktop` 很强的一点是,它不仅有“输入数据面”,还有“控制面”。
|
||||
|
||||
控制面主要由 `config_channel` 提供,基于 HID feature report 实现。
|
||||
|
||||
它可以做:
|
||||
|
||||
- 读取设备信息
|
||||
- 修改模块参数
|
||||
- 调节传感器 CPI
|
||||
- 下发 LED 数据
|
||||
- 执行 DFU
|
||||
|
||||
Dongle 还可以把这些请求继续转发给 BLE Peripheral。
|
||||
|
||||
这意味着官方架构已经把“量产设备常见需求”纳入了统一框架,而不是把配置、升级、输入完全割裂开。
|
||||
|
||||
## 13. 线程模型
|
||||
|
||||
`nrf_desktop` 总体上是少线程设计。
|
||||
|
||||
官方文档说明,大多数逻辑都运行在:
|
||||
|
||||
- system workqueue
|
||||
- App Event Manager 回调上下文
|
||||
|
||||
只有少量功能单独开线程,例如:
|
||||
|
||||
- motion sensor sampling thread
|
||||
- settings loader thread
|
||||
- QoS sampling thread
|
||||
|
||||
这种设计的直接好处是:
|
||||
|
||||
- 降低并发复杂度
|
||||
- 大部分路径不需要显式资源保护
|
||||
- 更有利于保持时序可控
|
||||
|
||||
对于嵌入式 HID 设备,这是非常务实的选择。
|
||||
|
||||
## 14. 配置目录如何决定产品形态
|
||||
|
||||
`nrf_desktop` 的另一个重要架构点是“同一套源码,多套产品配置”。
|
||||
|
||||
配置目录位于:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\configuration`
|
||||
|
||||
每个板子一个目录,目录内通过这些文件控制构建结果:
|
||||
|
||||
- `prj.conf`
|
||||
- `prj_keyboard.conf`
|
||||
- `prj_dongle.conf`
|
||||
- `app.overlay`
|
||||
- 各种 `*_def.h`
|
||||
- `sysbuild.conf`
|
||||
- `pm_static.yml`
|
||||
|
||||
例如在 `nrf52840dk_nrf52840` 中:
|
||||
|
||||
- `prj_keyboard.conf` 把它构造成键盘
|
||||
- `prj_dongle.conf` 把它构造成 dongle
|
||||
|
||||
也就是说,架构的复用不是靠复制工程,而是靠配置裁剪模块集合。
|
||||
|
||||
## 15. 对你的 `blinky` 项目的启发
|
||||
|
||||
你的项目路径是:
|
||||
|
||||
- `C:\projects\blinky`
|
||||
|
||||
如果你后续想把它做成一个更完整的自定义键盘,而不是只停留在简单 GPIO 读取和发送,那么 `nrf_desktop` 最值得借鉴的不是某一个文件,而是下面这些架构思想。
|
||||
|
||||
### 15.1 把硬件输入和 HID 输出分层
|
||||
|
||||
建议至少拆成两层:
|
||||
|
||||
- 输入层:按键扫描、编码器、LED、传感器
|
||||
- HID 层:按键状态汇总、键值映射、report 生成、USB/BLE 发送
|
||||
|
||||
不要让扫描函数直接拼 USB report。
|
||||
|
||||
### 15.2 用事件或消息队列解耦模块
|
||||
|
||||
即使不完全照搬 CAF,也可以参考它的思路:
|
||||
|
||||
- 输入模块只上报事件
|
||||
- HID 模块只处理事件
|
||||
- 传输模块只发送 report
|
||||
|
||||
这样后面加 BLE、加层切换、加宏录制时,不容易改崩整套逻辑。
|
||||
|
||||
### 15.3 先定义“角色”和“能力”
|
||||
|
||||
在正式扩展前,先明确你的设备属于哪一类:
|
||||
|
||||
- 纯 USB 键盘
|
||||
- BLE 键盘
|
||||
- 双模键盘
|
||||
- 带配置通道的键盘
|
||||
|
||||
不同目标会直接影响:
|
||||
|
||||
- 模块边界
|
||||
- report 设计
|
||||
- 状态管理
|
||||
- 功耗策略
|
||||
|
||||
### 15.4 把板级配置独立出来
|
||||
|
||||
你现在已经有:
|
||||
|
||||
- `boards/atguigu/mini_keyboard/mini_keyboard.dts`
|
||||
- `boards/atguigu/mini_keyboard/Kconfig.mini_keyboard`
|
||||
|
||||
这条路是对的。后面建议继续把下列内容尽量配置化:
|
||||
|
||||
- matrix 行列定义
|
||||
- LED 引脚
|
||||
- 特殊按键定义
|
||||
- 默认 keymap
|
||||
|
||||
这样应用逻辑就不会和某一块板子绑定太死。
|
||||
|
||||
## 16. 一句话总结
|
||||
|
||||
`nrf_desktop` 的官方架构可以概括为:
|
||||
|
||||
一个基于 CAF 和事件总线的模块化 HID 产品框架,通过配置来组合出键盘、鼠标、dongle 等不同设备形态,并在输入采集、HID report 生成、BLE/USB 传输、配置通道、DFU 和状态管理之间建立清晰分层。
|
||||
|
||||
如果你后续想把 `C:\projects\blinky` 往自定义键盘方向继续演进,那么最值得学习的是它的:
|
||||
|
||||
- 模块分层
|
||||
- 事件驱动
|
||||
- 角色化配置
|
||||
- 传输层与输入层解耦
|
||||
- 控制面与数据面分离
|
||||
|
||||
## 17. 参考源码位置
|
||||
|
||||
建议重点阅读以下路径:
|
||||
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\main.c`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\description.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\modules.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\bluetooth.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\board_configuration.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\event_propagation.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\hid_state.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\hids.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\hid_forward.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\motion.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\usb_state.rst`
|
||||
- `C:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\config_channel.rst`
|
||||
1
drivers/CMakeLists.txt
Normal file
1
drivers/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
add_subdirectory(pmic)
|
||||
1
drivers/Kconfig
Normal file
1
drivers/Kconfig
Normal file
@@ -0,0 +1 @@
|
||||
rsource "pmic/Kconfig"
|
||||
1
drivers/pmic/CMakeLists.txt
Normal file
1
drivers/pmic/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
add_subdirectory(ip5306)
|
||||
1
drivers/pmic/Kconfig
Normal file
1
drivers/pmic/Kconfig
Normal file
@@ -0,0 +1 @@
|
||||
rsource "ip5306/Kconfig"
|
||||
1
drivers/pmic/ip5306/CMakeLists.txt
Normal file
1
drivers/pmic/ip5306/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources_ifdef(CONFIG_IP5306 app PRIVATE ip5306.c)
|
||||
11
drivers/pmic/ip5306/Kconfig
Normal file
11
drivers/pmic/ip5306/Kconfig
Normal file
@@ -0,0 +1,11 @@
|
||||
menu "PMIC Drivers"
|
||||
|
||||
config IP5306
|
||||
bool "IP5306 PMIC driver"
|
||||
default y
|
||||
depends on I2C
|
||||
depends on DT_HAS_INJOINIC_IP5306_ENABLED
|
||||
help
|
||||
Enable the out-of-tree IP5306 PMIC driver.
|
||||
|
||||
endmenu
|
||||
357
drivers/pmic/ip5306/ip5306.c
Normal file
357
drivers/pmic/ip5306/ip5306.c
Normal file
@@ -0,0 +1,357 @@
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <soc_nrf_common.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include <helpers/nrfx_gppi.h>
|
||||
#include <drivers/pmic/ip5306.h>
|
||||
#include <gpiote_nrfx.h>
|
||||
#include <nrfx_gpiote.h>
|
||||
#include <nrfx_rtc.h>
|
||||
|
||||
#define DT_DRV_COMPAT injoinic_ip5306
|
||||
|
||||
LOG_MODULE_REGISTER(ip5306, LOG_LEVEL_INF);
|
||||
|
||||
#define IP5306_REG_READ0 0x70
|
||||
#define IP5306_REG_READ1 0x71
|
||||
#define IP5306_CHARGING_BIT BIT(3)
|
||||
#define IP5306_FULL_BIT BIT(3)
|
||||
#define IP5306_KEEPALIVE_RTC_FREQ_HZ 32768U
|
||||
#define IP5306_KEEPALIVE_RTC_CHANNEL_END 0U
|
||||
#define IP5306_KEEPALIVE_RTC_CHANNEL_START 1U
|
||||
|
||||
struct ip5306_config {
|
||||
struct i2c_dt_spec bus;
|
||||
nrfx_gpiote_t *gpiote;
|
||||
struct gpio_dt_spec wakeup_gpio;
|
||||
uint8_t wakeup_pin;
|
||||
uint32_t keepalive_interval_ms;
|
||||
uint32_t keepalive_pulse_width_ms;
|
||||
bool keepalive_hardware;
|
||||
};
|
||||
|
||||
struct ip5306_data {
|
||||
const struct device *dev;
|
||||
struct k_work_delayable keepalive_work;
|
||||
uint8_t gpiote_channel;
|
||||
nrfx_gppi_handle_t ppi_start;
|
||||
nrfx_gppi_handle_t ppi_end;
|
||||
nrfx_gppi_handle_t ppi_clear;
|
||||
bool started;
|
||||
bool pulse_active;
|
||||
bool hardware_ready;
|
||||
};
|
||||
|
||||
static const nrfx_rtc_t keepalive_rtc = NRFX_RTC_INSTANCE(2);
|
||||
|
||||
static uint32_t keepalive_low_delay_ms(const struct ip5306_config *config)
|
||||
{
|
||||
if (config->keepalive_interval_ms > config->keepalive_pulse_width_ms) {
|
||||
return config->keepalive_interval_ms -
|
||||
config->keepalive_pulse_width_ms;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t keepalive_ms_to_rtc_ticks(uint32_t time_ms)
|
||||
{
|
||||
return (uint32_t)(((uint64_t)time_ms * IP5306_KEEPALIVE_RTC_FREQ_HZ) / 1000U);
|
||||
}
|
||||
|
||||
static void keepalive_work_handler(struct k_work *work)
|
||||
{
|
||||
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
||||
struct ip5306_data *data =
|
||||
CONTAINER_OF(dwork, struct ip5306_data, keepalive_work);
|
||||
const struct device *dev = data->dev;
|
||||
const struct ip5306_config *config = dev->config;
|
||||
int err;
|
||||
|
||||
if (!data->started) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data->pulse_active) {
|
||||
err = gpio_pin_set_dt(&config->wakeup_gpio, 1);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to assert wakeup pulse (%d)", err);
|
||||
}
|
||||
|
||||
data->pulse_active = true;
|
||||
k_work_reschedule(&data->keepalive_work,
|
||||
K_MSEC(config->keepalive_pulse_width_ms));
|
||||
return;
|
||||
}
|
||||
|
||||
err = gpio_pin_set_dt(&config->wakeup_gpio, 0);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to deassert wakeup pulse (%d)", err);
|
||||
}
|
||||
|
||||
data->pulse_active = false;
|
||||
k_work_reschedule(&data->keepalive_work,
|
||||
K_MSEC(keepalive_low_delay_ms(config)));
|
||||
}
|
||||
|
||||
static void ip5306_keepalive_rtc_handler(nrfx_rtc_int_type_t int_type)
|
||||
{
|
||||
ARG_UNUSED(int_type);
|
||||
}
|
||||
|
||||
static int hardware_keepalive_init(const struct device *dev)
|
||||
{
|
||||
const struct ip5306_config *config = dev->config;
|
||||
struct ip5306_data *data = dev->data;
|
||||
nrfx_gpiote_output_config_t output_config = NRFX_GPIOTE_DEFAULT_OUTPUT_CONFIG;
|
||||
nrfx_gpiote_task_config_t task_config = {
|
||||
.task_ch = 0,
|
||||
.polarity = NRF_GPIOTE_POLARITY_TOGGLE,
|
||||
.init_val = (config->wakeup_gpio.dt_flags & GPIO_ACTIVE_LOW) ?
|
||||
NRF_GPIOTE_INITIAL_VALUE_HIGH :
|
||||
NRF_GPIOTE_INITIAL_VALUE_LOW,
|
||||
};
|
||||
nrfx_rtc_config_t rtc_cfg = NRFX_RTC_DEFAULT_CONFIG;
|
||||
uint32_t period_ticks = keepalive_ms_to_rtc_ticks(config->keepalive_interval_ms);
|
||||
uint32_t pulse_ticks = keepalive_ms_to_rtc_ticks(config->keepalive_pulse_width_ms);
|
||||
uint32_t start_ticks = period_ticks - pulse_ticks;
|
||||
uint32_t eep_start;
|
||||
uint32_t eep_end;
|
||||
uint32_t tep_toggle;
|
||||
uint32_t tep_clear;
|
||||
int err;
|
||||
|
||||
if (data->hardware_ready) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!nrfx_gpiote_init_check(config->gpiote)) {
|
||||
LOG_ERR("GPIOTE shared instance is not initialized");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = nrfx_gpiote_channel_alloc(config->gpiote, &data->gpiote_channel);
|
||||
if (err) {
|
||||
LOG_ERR("GPIOTE channel alloc failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
task_config.task_ch = data->gpiote_channel;
|
||||
|
||||
err = nrfx_gpiote_output_configure(config->gpiote,
|
||||
config->wakeup_pin,
|
||||
&output_config,
|
||||
&task_config);
|
||||
if (err) {
|
||||
LOG_ERR("GPIOTE output configure failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
nrfx_gpiote_out_task_enable(config->gpiote, config->wakeup_pin);
|
||||
|
||||
err = nrfx_rtc_init(&keepalive_rtc, &rtc_cfg, ip5306_keepalive_rtc_handler);
|
||||
if ((err != 0) && (err != -EALREADY)) {
|
||||
LOG_ERR("RTC2 init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = nrfx_rtc_cc_set(&keepalive_rtc,
|
||||
IP5306_KEEPALIVE_RTC_CHANNEL_END,
|
||||
period_ticks,
|
||||
false);
|
||||
if (err) {
|
||||
LOG_ERR("RTC2 CC end set failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = nrfx_rtc_cc_set(&keepalive_rtc,
|
||||
IP5306_KEEPALIVE_RTC_CHANNEL_START,
|
||||
start_ticks,
|
||||
false);
|
||||
if (err) {
|
||||
LOG_ERR("RTC2 CC start set failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
eep_start = nrfx_rtc_event_address_get(&keepalive_rtc,
|
||||
NRF_RTC_EVENT_COMPARE_1);
|
||||
eep_end = nrfx_rtc_event_address_get(&keepalive_rtc,
|
||||
NRF_RTC_EVENT_COMPARE_0);
|
||||
tep_toggle = nrfx_gpiote_out_task_address_get(config->gpiote,
|
||||
config->wakeup_pin);
|
||||
tep_clear = nrfx_rtc_task_address_get(&keepalive_rtc, NRF_RTC_TASK_CLEAR);
|
||||
|
||||
err = nrfx_gppi_conn_alloc(eep_start, tep_toggle, &data->ppi_start);
|
||||
if (err) {
|
||||
LOG_ERR("GPPI start alloc failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = nrfx_gppi_conn_alloc(eep_end, tep_toggle, &data->ppi_end);
|
||||
if (err) {
|
||||
LOG_ERR("GPPI end alloc failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = nrfx_gppi_conn_alloc(eep_end, tep_clear, &data->ppi_clear);
|
||||
if (err) {
|
||||
LOG_ERR("GPPI clear alloc failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
nrfx_gppi_conn_enable(data->ppi_start);
|
||||
nrfx_gppi_conn_enable(data->ppi_end);
|
||||
nrfx_gppi_conn_enable(data->ppi_clear);
|
||||
|
||||
nrfx_rtc_counter_clear(&keepalive_rtc);
|
||||
nrfx_rtc_enable(&keepalive_rtc);
|
||||
|
||||
data->hardware_ready = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int software_keepalive_init(const struct device *dev)
|
||||
{
|
||||
struct ip5306_data *data = dev->data;
|
||||
|
||||
k_work_init_delayable(&data->keepalive_work, keepalive_work_handler);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ip5306_init_api(const struct device *dev)
|
||||
{
|
||||
const struct ip5306_config *config = dev->config;
|
||||
struct ip5306_data *data = dev->data;
|
||||
uint8_t reg_val;
|
||||
int err;
|
||||
|
||||
if (data->started) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ0, ®_val);
|
||||
if (err) {
|
||||
LOG_ERR("IP5306 probe failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
ARG_UNUSED(reg_val);
|
||||
|
||||
data->started = true;
|
||||
data->pulse_active = false;
|
||||
|
||||
if (config->keepalive_hardware) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
k_work_reschedule(&data->keepalive_work,
|
||||
K_MSEC(keepalive_low_delay_ms(config)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ip5306_get_status_api(const struct device *dev,
|
||||
struct ip5306_status *status)
|
||||
{
|
||||
const struct ip5306_config *config = dev->config;
|
||||
uint8_t read0;
|
||||
uint8_t read1;
|
||||
int err;
|
||||
|
||||
if (status == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ0, &read0);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ1, &read1);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
status->charging = (read0 & IP5306_CHARGING_BIT) != 0U;
|
||||
status->full = (read1 & IP5306_FULL_BIT) != 0U;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ip5306_dev_init(const struct device *dev)
|
||||
{
|
||||
const struct ip5306_config *config = dev->config;
|
||||
struct ip5306_data *data = dev->data;
|
||||
|
||||
if (!i2c_is_ready_dt(&config->bus)) {
|
||||
LOG_ERR("I2C bus not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!gpio_is_ready_dt(&config->wakeup_gpio)) {
|
||||
LOG_ERR("Wakeup GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (config->keepalive_pulse_width_ms == 0U) {
|
||||
LOG_ERR("Invalid keepalive pulse width");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (config->keepalive_interval_ms == 0U) {
|
||||
LOG_ERR("Invalid keepalive interval");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (config->keepalive_pulse_width_ms > config->keepalive_interval_ms) {
|
||||
LOG_ERR("Pulse width cannot exceed interval");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->dev = dev;
|
||||
data->started = false;
|
||||
data->pulse_active = false;
|
||||
data->hardware_ready = false;
|
||||
|
||||
if (config->keepalive_hardware) {
|
||||
return hardware_keepalive_init(dev);
|
||||
}
|
||||
|
||||
return software_keepalive_init(dev);
|
||||
}
|
||||
|
||||
static const struct ip5306_driver_api ip5306_driver_api = {
|
||||
.init = ip5306_init_api,
|
||||
.get_status = ip5306_get_status_api,
|
||||
};
|
||||
|
||||
#define IP5306_DEFINE(inst) \
|
||||
static struct ip5306_data ip5306_data_##inst; \
|
||||
\
|
||||
static const struct ip5306_config ip5306_config_##inst = { \
|
||||
.bus = I2C_DT_SPEC_INST_GET(inst), \
|
||||
.gpiote = &GPIOTE_NRFX_INST_BY_NODE( \
|
||||
NRF_DT_GPIOTE_NODE(DT_DRV_INST(inst), wakeup_gpios)), \
|
||||
.wakeup_gpio = GPIO_DT_SPEC_INST_GET(inst, wakeup_gpios), \
|
||||
.wakeup_pin = NRF_DT_GPIOS_TO_PSEL(DT_DRV_INST(inst), wakeup_gpios), \
|
||||
.keepalive_interval_ms = \
|
||||
DT_INST_PROP(inst, keepalive_interval_ms), \
|
||||
.keepalive_pulse_width_ms = \
|
||||
DT_INST_PROP(inst, keepalive_pulse_width_ms), \
|
||||
.keepalive_hardware = \
|
||||
DT_INST_NODE_HAS_PROP(inst, keepalive_hardware), \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(inst, ip5306_dev_init, NULL, \
|
||||
&ip5306_data_##inst, &ip5306_config_##inst, \
|
||||
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
|
||||
&ip5306_driver_api)
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(IP5306_DEFINE)
|
||||
25
dts/bindings/pmic/injoinic,ip5306.yaml
Normal file
25
dts/bindings/pmic/injoinic,ip5306.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
description: Injoinic IP5306 PMIC with selectable software or hardware wakeup keepalive support
|
||||
|
||||
compatible: "injoinic,ip5306"
|
||||
|
||||
include: i2c-device.yaml
|
||||
|
||||
properties:
|
||||
wakeup-gpios:
|
||||
type: phandle-array
|
||||
required: true
|
||||
description: GPIO used to generate the wakeup keepalive pulse.
|
||||
|
||||
keepalive-interval-ms:
|
||||
type: int
|
||||
required: true
|
||||
description: Period between two keepalive pulses, measured from pulse start.
|
||||
|
||||
keepalive-pulse-width-ms:
|
||||
type: int
|
||||
required: true
|
||||
description: Active-high pulse width for the keepalive wakeup GPIO.
|
||||
|
||||
keepalive-hardware:
|
||||
type: boolean
|
||||
description: Enable RTC2 plus GPPI plus GPIOTE based hardware keepalive pulses.
|
||||
2
dts/bindings/vendor-prefixes.txt
Normal file
2
dts/bindings/vendor-prefixes.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
atguigu Atguigu
|
||||
injoinic Injoinic
|
||||
34
inc/buttons_def.h
Normal file
34
inc/buttons_def.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* CAF buttons 矩阵引脚定义
|
||||
*
|
||||
* 设计说明:
|
||||
* - 本文件被 CAF buttons 模块通过 CONFIG_CAF_BUTTONS_DEF_PATH 直接包含;
|
||||
* - 行列引脚顺序必须与板级 DTS 中 my_keyboard 的 row-gpios/col-gpios 保持一致;
|
||||
* - key_id 的行列编号完全基于这里的数组下标,不依赖 input-keymap 节点。
|
||||
*/
|
||||
|
||||
#include <caf/gpio_pins.h>
|
||||
|
||||
/*
|
||||
* 该符号用于保证配置文件只被链接一次:
|
||||
* 若被重复包含到多个编译单元,会在链接阶段报重复定义,避免静默错配。
|
||||
*/
|
||||
const struct {} buttons_def_include_once;
|
||||
|
||||
/* 列引脚:对应 atguigu_mini_keyboard.dts 中 my_keyboard/col-gpios 顺序。 */
|
||||
static const struct gpio_pin col[] = {
|
||||
{ .port = 0, .pin = 5 },
|
||||
{ .port = 0, .pin = 6 },
|
||||
{ .port = 0, .pin = 26 },
|
||||
{ .port = 0, .pin = 30 },
|
||||
};
|
||||
|
||||
/* 行引脚:对应 atguigu_mini_keyboard.dts 中 my_keyboard/row-gpios 顺序。 */
|
||||
static const struct gpio_pin row[] = {
|
||||
{ .port = 0, .pin = 15 },
|
||||
{ .port = 0, .pin = 7 },
|
||||
{ .port = 0, .pin = 12 },
|
||||
{ .port = 0, .pin = 4 },
|
||||
{ .port = 1, .pin = 9 },
|
||||
{ .port = 0, .pin = 8 },
|
||||
};
|
||||
44
inc/drivers/pmic/ip5306.h
Normal file
44
inc/drivers/pmic/ip5306.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef BLINKY_DRIVERS_PMIC_IP5306_H_
|
||||
#define BLINKY_DRIVERS_PMIC_IP5306_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
struct ip5306_status {
|
||||
bool charging;
|
||||
bool full;
|
||||
};
|
||||
|
||||
struct ip5306_driver_api {
|
||||
int (*init)(const struct device *dev);
|
||||
int (*get_status)(const struct device *dev, struct ip5306_status *status);
|
||||
};
|
||||
|
||||
static inline int ip5306_init(const struct device *dev)
|
||||
{
|
||||
const struct ip5306_driver_api *api =
|
||||
(const struct ip5306_driver_api *)dev->api;
|
||||
|
||||
if ((api == NULL) || (api->init == NULL)) {
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
return api->init(dev);
|
||||
}
|
||||
|
||||
static inline int ip5306_get_status(const struct device *dev,
|
||||
struct ip5306_status *status)
|
||||
{
|
||||
const struct ip5306_driver_api *api =
|
||||
(const struct ip5306_driver_api *)dev->api;
|
||||
|
||||
if ((api == NULL) || (api->get_status == NULL)) {
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
return api->get_status(dev, status);
|
||||
}
|
||||
|
||||
#endif /* BLINKY_DRIVERS_PMIC_IP5306_H_ */
|
||||
22
inc/events/encoder_event.h
Normal file
22
inc/events/encoder_event.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef BLINKY_ENCODER_EVENT_H_
|
||||
#define BLINKY_ENCODER_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct encoder_event {
|
||||
struct app_event_header header;
|
||||
int8_t detents;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(encoder_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_ENCODER_EVENT_H_ */
|
||||
22
inc/events/hid_led_event.h
Normal file
22
inc/events/hid_led_event.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef BLINKY_HID_LED_EVENT_H_
|
||||
#define BLINKY_HID_LED_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hid_led_event {
|
||||
struct app_event_header header;
|
||||
uint8_t led_bm;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_HID_LED_EVENT_H_ */
|
||||
28
inc/events/keyboard_hid_report_event.h
Normal file
28
inc/events/keyboard_hid_report_event.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct keyboard_hid_report_event {
|
||||
struct app_event_header header;
|
||||
enum mode_switch_mode mode;
|
||||
enum keyboard_report_type report_type;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_KEYBOARD_HID_REPORT_EVENT_H_ */
|
||||
29
inc/events/mode_switch_event.h
Normal file
29
inc/events/mode_switch_event.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef BLINKY_MODE_SWITCH_EVENT_H_
|
||||
#define BLINKY_MODE_SWITCH_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum mode_switch_mode {
|
||||
MODE_SWITCH_USB,
|
||||
MODE_SWITCH_24G,
|
||||
MODE_SWITCH_BLE,
|
||||
};
|
||||
|
||||
struct mode_switch_event {
|
||||
struct app_event_header header;
|
||||
enum mode_switch_mode mode;
|
||||
uint16_t voltage_mv;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(mode_switch_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_MODE_SWITCH_EVENT_H_ */
|
||||
24
inc/events/set_protocol_event.h
Normal file
24
inc/events/set_protocol_event.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLINKY_SET_PROTOCOL_EVENT_H_
|
||||
#define BLINKY_SET_PROTOCOL_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct set_protocol_event {
|
||||
struct app_event_header header;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(set_protocol_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_SET_PROTOCOL_EVENT_H_ */
|
||||
40
inc/keyboard_core.h
Normal file
40
inc/keyboard_core.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BLINKY_KEYBOARD_CORE_H_
|
||||
#define BLINKY_KEYBOARD_CORE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define KEYBOARD_BOOT_REPORT_SIZE 8U
|
||||
#define KEYBOARD_NKRO_USAGE_MAX 0xDFU
|
||||
#define KEYBOARD_NKRO_BITMAP_BYTES ((KEYBOARD_NKRO_USAGE_MAX + 8U) / 8U)
|
||||
#define KEYBOARD_NKRO_REPORT_SIZE (1U + KEYBOARD_NKRO_BITMAP_BYTES)
|
||||
#define KEYBOARD_CONSUMER_REPORT_SIZE 2U
|
||||
|
||||
enum keyboard_protocol_mode {
|
||||
KEYBOARD_PROTOCOL_MODE_BOOT,
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
};
|
||||
|
||||
enum keyboard_report_type {
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
};
|
||||
|
||||
enum keyboard_consumer_control {
|
||||
KEYBOARD_CONSUMER_CTRL_MUTE,
|
||||
KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
||||
KEYBOARD_CONSUMER_CTRL_VOLUME_DOWN,
|
||||
KEYBOARD_CONSUMER_CTRL_PLAY_PAUSE,
|
||||
KEYBOARD_CONSUMER_CTRL_NEXT_TRACK,
|
||||
KEYBOARD_CONSUMER_CTRL_PREV_TRACK,
|
||||
KEYBOARD_CONSUMER_CTRL_COUNT,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_KEYBOARD_CORE_H_ */
|
||||
20
prj.conf
20
prj.conf
@@ -1,4 +1,24 @@
|
||||
CONFIG_CAF=y
|
||||
CONFIG_CAF_BUTTONS=y
|
||||
CONFIG_CAF_BUTTONS_DEF_PATH="buttons_def.h"
|
||||
CONFIG_GPIO=y
|
||||
CONFIG_I2C=y
|
||||
CONFIG_NRFX_RTC2=y
|
||||
CONFIG_NRFX_GPPI=y
|
||||
CONFIG_NRFX_QDEC=y
|
||||
CONFIG_PINCTRL_DYNAMIC=y
|
||||
CONFIG_REBOOT=y
|
||||
CONFIG_SENSOR=y
|
||||
CONFIG_ADC=y
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
||||
CONFIG_LOG=y
|
||||
CONFIG_ASSERT=y
|
||||
|
||||
# USB HID next stack
|
||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||
CONFIG_USBD_HID_SUPPORT=y
|
||||
|
||||
# Power manager
|
||||
CONFIG_CAF_POWER_MANAGER=y
|
||||
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
||||
# CONFIG_CAF_POWER_MANAGER_STAY_ON=y
|
||||
|
||||
209
src/battery_module.c
Normal file
209
src/battery_module.c
Normal file
@@ -0,0 +1,209 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE battery_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_manager_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <drivers/pmic/ip5306.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define VBATT_NODE DT_PATH(vbatt)
|
||||
#define IP5306_NODE DT_NODELABEL(ip5306)
|
||||
#define BATTERY_SAMPLE_INTERVAL K_SECONDS(1)
|
||||
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
||||
"Missing /vbatt voltage-divider node in devicetree");
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(IP5306_NODE, okay),
|
||||
"Missing ip5306 node in devicetree");
|
||||
|
||||
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
||||
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
||||
static struct k_work_delayable battery_sample_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
|
||||
static int sensor_value_to_mv(const struct sensor_value *value)
|
||||
{
|
||||
return (value->val1 * 1000) + (value->val2 / 1000);
|
||||
}
|
||||
|
||||
static int measurement_enable(bool enable)
|
||||
{
|
||||
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
|
||||
: PM_DEVICE_ACTION_SUSPEND;
|
||||
int err = pm_device_action_run(vbatt_dev, action);
|
||||
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot %s vbatt sensor (%d)", enable ? "resume" : "suspend", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void battery_sample_fn(struct k_work *work)
|
||||
{
|
||||
struct ip5306_status pmic_status;
|
||||
struct sensor_value voltage;
|
||||
int voltage_mv;
|
||||
int err;
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(vbatt_dev);
|
||||
if (err) {
|
||||
LOG_WRN("Battery sample fetch failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
if (err) {
|
||||
LOG_WRN("Battery channel get failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = ip5306_get_status(ip5306_dev, &pmic_status);
|
||||
if (err) {
|
||||
LOG_WRN("IP5306 status read failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
voltage_mv = sensor_value_to_mv(&voltage);
|
||||
// LOG_INF("Battery: %d mV, charging=%d, full=%d",
|
||||
// voltage_mv, pmic_status.charging, pmic_status.full);
|
||||
|
||||
reschedule:
|
||||
if (running) {
|
||||
k_work_reschedule(&battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
if (!device_is_ready(vbatt_dev)) {
|
||||
LOG_ERR("vbatt device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_ready(ip5306_dev)) {
|
||||
LOG_ERR("ip5306 device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int err = ip5306_init(ip5306_dev);
|
||||
if (err) {
|
||||
LOG_ERR("ip5306 init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
||||
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = measurement_enable(true);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
running = true;
|
||||
k_work_reschedule(&battery_sample_work, K_NO_WAIT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)k_work_cancel_delayable(&battery_sample_work);
|
||||
(void)measurement_enable(false);
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
226
src/encoder_module.c
Normal file
226
src/encoder_module.c
Normal file
@@ -0,0 +1,226 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE encoder_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define ENCODER_QDEC_NODE DT_ALIAS(qdec0)
|
||||
#define ENCODER_PULSES_PER_DETENT 4LL
|
||||
#define ENCODER_DETENT_UDEG (360000000LL / (80LL / ENCODER_PULSES_PER_DETENT))
|
||||
|
||||
BUILD_ASSERT(DT_NODE_EXISTS(ENCODER_QDEC_NODE), "Missing qdec0 alias");
|
||||
|
||||
static const struct device *const qdec_dev = DEVICE_DT_GET(ENCODER_QDEC_NODE);
|
||||
|
||||
static struct k_work encoder_report_work;
|
||||
static struct sensor_trigger encoder_trigger = {
|
||||
.type = SENSOR_TRIG_DATA_READY,
|
||||
.chan = SENSOR_CHAN_ROTATION,
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static int64_t angle_remainder_udeg;
|
||||
|
||||
static int64_t sensor_value_to_udeg(const struct sensor_value *value)
|
||||
{
|
||||
return ((int64_t)value->val1 * 1000000LL) + value->val2;
|
||||
}
|
||||
|
||||
static void submit_detents(int32_t detents)
|
||||
{
|
||||
while (detents != 0) {
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
|
||||
if (detents > INT8_MAX) {
|
||||
event->detents = INT8_MAX;
|
||||
detents -= INT8_MAX;
|
||||
} else if (detents < INT8_MIN) {
|
||||
event->detents = INT8_MIN;
|
||||
detents -= INT8_MIN;
|
||||
} else {
|
||||
event->detents = (int8_t)detents;
|
||||
detents = 0;
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
|
||||
static void encoder_report_work_handler(struct k_work *work)
|
||||
{
|
||||
struct sensor_value rotation;
|
||||
int64_t total_udeg;
|
||||
int32_t detents;
|
||||
int err;
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(qdec_dev);
|
||||
if (err) {
|
||||
LOG_WRN("QDEC sample fetch failed (%d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(qdec_dev, SENSOR_CHAN_ROTATION, &rotation);
|
||||
if (err) {
|
||||
LOG_WRN("QDEC channel get failed (%d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
total_udeg = angle_remainder_udeg + sensor_value_to_udeg(&rotation);
|
||||
detents = (int32_t)(total_udeg / ENCODER_DETENT_UDEG);
|
||||
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
||||
|
||||
if (detents != 0) {
|
||||
submit_detents(detents);
|
||||
}
|
||||
}
|
||||
|
||||
static void encoder_trigger_handler(const struct device *dev,
|
||||
const struct sensor_trigger *trigger)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(trigger);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
k_work_submit(&encoder_report_work);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(qdec_dev)) {
|
||||
LOG_ERR("QDEC device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
k_work_init(&encoder_report_work, encoder_report_work_handler);
|
||||
angle_remainder_udeg = 0;
|
||||
|
||||
err = sensor_trigger_set(qdec_dev, &encoder_trigger, encoder_trigger_handler);
|
||||
if (err) {
|
||||
LOG_ERR("Cannot set QDEC trigger (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = pm_device_action_run(qdec_dev, PM_DEVICE_ACTION_RESUME);
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot resume QDEC device (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
angle_remainder_udeg = 0;
|
||||
running = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = pm_device_action_run(qdec_dev, PM_DEVICE_ACTION_SUSPEND);
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_WRN("Cannot suspend QDEC device (%d)", err);
|
||||
}
|
||||
|
||||
angle_remainder_udeg = 0;
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
27
src/events/encoder_event.c
Normal file
27
src/events/encoder_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "encoder_event.h"
|
||||
|
||||
static void log_encoder_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct encoder_event *event = cast_encoder_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "detents:%d", event->detents);
|
||||
}
|
||||
|
||||
static void profile_encoder_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct encoder_event *event = cast_encoder_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_int8(buf, event->detents);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(encoder_event,
|
||||
ENCODE(NRF_PROFILER_ARG_S8),
|
||||
ENCODE("detents"),
|
||||
profile_encoder_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(encoder_event,
|
||||
log_encoder_event,
|
||||
&encoder_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
27
src/events/hid_led_event.c
Normal file
27
src/events/hid_led_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "hid_led_event.h"
|
||||
|
||||
static void log_hid_led_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "led_bm:0x%02x", event->led_bm);
|
||||
}
|
||||
|
||||
static void profile_hid_led_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->led_bm);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(hid_led_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("led_bm"),
|
||||
profile_hid_led_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_led_event,
|
||||
log_hid_led_event,
|
||||
&hid_led_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
107
src/events/keyboard_hid_report_event.c
Normal file
107
src/events/keyboard_hid_report_event.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "keyboard_hid_report_event.h"
|
||||
|
||||
#define KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN 192
|
||||
|
||||
static const char *mode_name(enum mode_switch_mode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
return "USB";
|
||||
case MODE_SWITCH_24G:
|
||||
return "2.4G";
|
||||
case MODE_SWITCH_BLE:
|
||||
return "BLE";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *report_type_name(enum keyboard_report_type report_type)
|
||||
{
|
||||
switch (report_type) {
|
||||
case KEYBOARD_REPORT_TYPE_KEYS:
|
||||
return "keys";
|
||||
case KEYBOARD_REPORT_TYPE_CONSUMER:
|
||||
return "consumer";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
switch (protocol_mode) {
|
||||
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||
return "boot";
|
||||
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||
return "report";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_keyboard_hid_report_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct keyboard_hid_report_event *event =
|
||||
cast_keyboard_hid_report_event(aeh);
|
||||
char log_buf[KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "mode:%s type:%s protocol:%s len:%zu",
|
||||
mode_name(event->mode),
|
||||
report_type_name(event->report_type),
|
||||
protocol_mode_name(event->protocol_mode),
|
||||
event->dyndata.size);
|
||||
if ((pos > 0) && (pos < sizeof(log_buf))) {
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos,
|
||||
" %02x", event->dyndata.data[i]);
|
||||
|
||||
if (tmp < 0) {
|
||||
log_buf[sizeof(log_buf) - 2] = '~';
|
||||
pos = tmp;
|
||||
break;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
if (pos >= sizeof(log_buf)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pos < 0) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_keyboard_hid_report_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct keyboard_hid_report_event *event =
|
||||
cast_keyboard_hid_report_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->mode);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(keyboard_hid_report_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("mode", "report_type", "protocol_mode", "len"),
|
||||
profile_keyboard_hid_report_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(keyboard_hid_report_event,
|
||||
log_keyboard_hid_report_event,
|
||||
&keyboard_hid_report_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
45
src/events/mode_switch_event.c
Normal file
45
src/events/mode_switch_event.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
static const char *mode_name(enum mode_switch_mode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
return "USB";
|
||||
case MODE_SWITCH_24G:
|
||||
return "2.4G";
|
||||
case MODE_SWITCH_BLE:
|
||||
return "BLE";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_mode_switch_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "mode:%s voltage:%" PRIu16 "mV",
|
||||
mode_name(event->mode), event->voltage_mv);
|
||||
}
|
||||
|
||||
static void profile_mode_switch_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->mode);
|
||||
nrf_profiler_log_encode_uint16(buf, event->voltage_mv);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(mode_switch_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("mode", "voltage_mv"),
|
||||
profile_mode_switch_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(mode_switch_event,
|
||||
log_mode_switch_event,
|
||||
&mode_switch_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
40
src/events/set_protocol_event.c
Normal file
40
src/events/set_protocol_event.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
switch (protocol_mode) {
|
||||
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||
return "boot";
|
||||
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||
return "report";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_set_protocol_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "protocol:%s",
|
||||
protocol_mode_name(event->protocol_mode));
|
||||
}
|
||||
|
||||
static void profile_set_protocol_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(set_protocol_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("protocol_mode"),
|
||||
profile_set_protocol_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(set_protocol_event,
|
||||
log_set_protocol_event,
|
||||
&set_protocol_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
503
src/keyboard_core_module.c
Normal file
503
src/keyboard_core_module.c
Normal file
@@ -0,0 +1,503 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE keyboard_core_module
|
||||
#include <caf/events/button_event.h>
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/key_id.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define KEYBOARD_USAGE_FIRST_MODIFIER 0xE0U
|
||||
#define KEYBOARD_USAGE_LAST_MODIFIER 0xE7U
|
||||
#define KEYBOARD_USAGE_ERROR_ROLLOVER 0x01U
|
||||
#define KEYBOARD_BOOT_RESERVED_BYTE 0x00U
|
||||
|
||||
enum key_usage_type {
|
||||
KEY_USAGE_TYPE_KEYBOARD,
|
||||
KEY_USAGE_TYPE_CONSUMER,
|
||||
};
|
||||
|
||||
struct keymap_entry {
|
||||
uint16_t key_id;
|
||||
uint8_t usage_type;
|
||||
uint16_t usage_id;
|
||||
};
|
||||
|
||||
struct keyboard_state {
|
||||
uint8_t modifiers;
|
||||
uint8_t keys_bitmap[KEYBOARD_NKRO_BITMAP_BYTES];
|
||||
uint32_t consumer_bits;
|
||||
};
|
||||
|
||||
struct keyboard_reports_cache {
|
||||
uint8_t boot_report[KEYBOARD_BOOT_REPORT_SIZE];
|
||||
uint8_t nkro_report[KEYBOARD_NKRO_REPORT_SIZE];
|
||||
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
bool boot_valid;
|
||||
bool nkro_valid;
|
||||
bool consumer_valid;
|
||||
};
|
||||
|
||||
static const struct keymap_entry keymap[] = {
|
||||
{ KEY_ID(0, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0053 }, /* num lock */
|
||||
{ KEY_ID(0, 2), KEY_USAGE_TYPE_KEYBOARD, 0x005F }, /* keypad 7 */
|
||||
{ KEY_ID(0, 3), KEY_USAGE_TYPE_KEYBOARD, 0x005C }, /* keypad 4 */
|
||||
{ KEY_ID(0, 4), KEY_USAGE_TYPE_KEYBOARD, 0x0059 }, /* keypad 1 */
|
||||
{ KEY_ID(0, 5), KEY_USAGE_TYPE_KEYBOARD, 0x0062 }, /* keypad 0 */
|
||||
{ KEY_ID(1, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0054 }, /* keypad / */
|
||||
{ KEY_ID(1, 2), KEY_USAGE_TYPE_KEYBOARD, 0x0060 }, /* keypad 8 */
|
||||
{ KEY_ID(1, 3), KEY_USAGE_TYPE_KEYBOARD, 0x005D }, /* keypad 5 */
|
||||
{ KEY_ID(1, 4), KEY_USAGE_TYPE_KEYBOARD, 0x005A }, /* keypad 2 */
|
||||
{ KEY_ID(1, 5), KEY_USAGE_TYPE_KEYBOARD, 0x0063 }, /* keypad . */
|
||||
{ KEY_ID(2, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0055 }, /* keypad * */
|
||||
{ KEY_ID(2, 2), KEY_USAGE_TYPE_KEYBOARD, 0x0061 }, /* keypad 9 */
|
||||
{ KEY_ID(2, 3), KEY_USAGE_TYPE_KEYBOARD, 0x005E }, /* keypad 6 */
|
||||
{ KEY_ID(2, 4), KEY_USAGE_TYPE_KEYBOARD, 0x005B }, /* keypad 3 */
|
||||
{ KEY_ID(3, 0), KEY_USAGE_TYPE_CONSUMER, KEYBOARD_CONSUMER_CTRL_MUTE },
|
||||
{ KEY_ID(3, 1), KEY_USAGE_TYPE_KEYBOARD, 0x0056 }, /* keypad - */
|
||||
{ KEY_ID(3, 3), KEY_USAGE_TYPE_KEYBOARD, 0x0057 }, /* keypad + */
|
||||
{ KEY_ID(3, 5), KEY_USAGE_TYPE_KEYBOARD, 0x0058 }, /* keypad enter */
|
||||
};
|
||||
|
||||
static const uint16_t consumer_usage_map[KEYBOARD_CONSUMER_CTRL_COUNT] = {
|
||||
[KEYBOARD_CONSUMER_CTRL_MUTE] = 0x00E2,
|
||||
[KEYBOARD_CONSUMER_CTRL_VOLUME_UP] = 0x00E9,
|
||||
[KEYBOARD_CONSUMER_CTRL_VOLUME_DOWN] = 0x00EA,
|
||||
[KEYBOARD_CONSUMER_CTRL_PLAY_PAUSE] = 0x00CD,
|
||||
[KEYBOARD_CONSUMER_CTRL_NEXT_TRACK] = 0x00B5,
|
||||
[KEYBOARD_CONSUMER_CTRL_PREV_TRACK] = 0x00B6,
|
||||
};
|
||||
|
||||
static struct keyboard_state keyboard_state;
|
||||
static struct keyboard_reports_cache reports_cache;
|
||||
static enum keyboard_protocol_mode protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
static enum mode_switch_mode current_mode;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool mode_valid;
|
||||
|
||||
static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
{
|
||||
size_t left = 0;
|
||||
size_t right = ARRAY_SIZE(keymap);
|
||||
|
||||
while (left < right) {
|
||||
size_t mid = left + ((right - left) / 2U);
|
||||
|
||||
if (keymap[mid].key_id == key_id) {
|
||||
return &keymap[mid];
|
||||
}
|
||||
|
||||
if (keymap[mid].key_id < key_id) {
|
||||
left = mid + 1U;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool usage_is_modifier(uint16_t usage_id)
|
||||
{
|
||||
return IN_RANGE(usage_id, KEYBOARD_USAGE_FIRST_MODIFIER,
|
||||
KEYBOARD_USAGE_LAST_MODIFIER);
|
||||
}
|
||||
|
||||
static bool keyboard_key_update(uint16_t usage_id, bool pressed)
|
||||
{
|
||||
if (usage_is_modifier(usage_id)) {
|
||||
uint8_t new_modifiers = keyboard_state.modifiers;
|
||||
|
||||
WRITE_BIT(new_modifiers, usage_id - KEYBOARD_USAGE_FIRST_MODIFIER, pressed);
|
||||
if (new_modifiers == keyboard_state.modifiers) {
|
||||
return false;
|
||||
}
|
||||
|
||||
keyboard_state.modifiers = new_modifiers;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (usage_id > KEYBOARD_NKRO_USAGE_MAX) {
|
||||
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
bool was_pressed = (keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
WRITE_BIT(keyboard_state.keys_bitmap[byte_idx], bit_idx, pressed);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool consumer_key_update(uint16_t consumer_id, bool pressed)
|
||||
{
|
||||
if (consumer_id >= KEYBOARD_CONSUMER_CTRL_COUNT) {
|
||||
LOG_WRN("Unsupported consumer id %u", consumer_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool was_pressed = (keyboard_state.consumer_bits & BIT(consumer_id)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
WRITE_BIT(keyboard_state.consumer_bits, consumer_id, pressed);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void keyboard_state_clear(void)
|
||||
{
|
||||
memset(&keyboard_state, 0, sizeof(keyboard_state));
|
||||
}
|
||||
|
||||
static void reports_cache_invalidate(void)
|
||||
{
|
||||
reports_cache.boot_valid = false;
|
||||
reports_cache.nkro_valid = false;
|
||||
reports_cache.consumer_valid = false;
|
||||
}
|
||||
|
||||
static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
{
|
||||
size_t key_count = 0;
|
||||
|
||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||
report[0] = keyboard_state.modifiers;
|
||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
|
||||
if ((keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key_count == (KEYBOARD_BOOT_REPORT_SIZE - 2U)) {
|
||||
memset(&report[2], KEYBOARD_USAGE_ERROR_ROLLOVER,
|
||||
KEYBOARD_BOOT_REPORT_SIZE - 2U);
|
||||
return;
|
||||
}
|
||||
|
||||
report[2U + key_count] = (uint8_t)usage_id;
|
||||
key_count++;
|
||||
}
|
||||
}
|
||||
|
||||
static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||
{
|
||||
report[0] = keyboard_state.modifiers;
|
||||
memcpy(&report[1], keyboard_state.keys_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
}
|
||||
|
||||
static uint16_t active_consumer_usage_get(void)
|
||||
{
|
||||
for (uint8_t consumer_id = 0; consumer_id < KEYBOARD_CONSUMER_CTRL_COUNT; consumer_id++) {
|
||||
if ((keyboard_state.consumer_bits & BIT(consumer_id)) != 0U) {
|
||||
return consumer_usage_map[consumer_id];
|
||||
}
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
|
||||
{
|
||||
sys_put_le16(active_consumer_usage_get(), report);
|
||||
}
|
||||
|
||||
static void submit_keyboard_report_event(enum keyboard_report_type report_type,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
struct keyboard_hid_report_event *event =
|
||||
new_keyboard_hid_report_event(size);
|
||||
|
||||
event->mode = current_mode;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void emit_keys_report(bool force)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_NKRO_REPORT_SIZE];
|
||||
uint8_t report_size;
|
||||
uint8_t *cache_buf;
|
||||
bool *cache_valid;
|
||||
|
||||
if (!mode_valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
build_boot_report(report_buf);
|
||||
report_size = KEYBOARD_BOOT_REPORT_SIZE;
|
||||
cache_buf = reports_cache.boot_report;
|
||||
cache_valid = &reports_cache.boot_valid;
|
||||
} else {
|
||||
build_nkro_report(report_buf);
|
||||
report_size = KEYBOARD_NKRO_REPORT_SIZE;
|
||||
cache_buf = reports_cache.nkro_report;
|
||||
cache_valid = &reports_cache.nkro_valid;
|
||||
}
|
||||
|
||||
if (!force && *cache_valid && (memcmp(cache_buf, report_buf, report_size) == 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(cache_buf, report_buf, report_size);
|
||||
*cache_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS, report_buf, report_size);
|
||||
}
|
||||
|
||||
static void emit_consumer_report(bool force)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
|
||||
if (!mode_valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
build_consumer_report(report_buf);
|
||||
if (!force && reports_cache.consumer_valid &&
|
||||
(memcmp(reports_cache.consumer_report, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE) == 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
reports_cache.consumer_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
static void emit_all_reports(bool force)
|
||||
{
|
||||
emit_keys_report(force);
|
||||
emit_consumer_report(force);
|
||||
}
|
||||
|
||||
static void emit_release_reports(enum mode_switch_mode mode)
|
||||
{
|
||||
struct keyboard_hid_report_event *event;
|
||||
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
|
||||
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
||||
size_t keys_report_size =
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
||||
|
||||
event = new_keyboard_hid_report_event(keys_report_size);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_KEYS;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, consumer_report, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
mode_valid = false;
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
}
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
mode_valid = false;
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_button_event(const struct button_event *event)
|
||||
{
|
||||
const struct keymap_entry *entry;
|
||||
bool changed;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entry = keymap_get(event->key_id);
|
||||
if (!entry) {
|
||||
LOG_WRN("Unmapped key id 0x%04x", event->key_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entry->usage_type == KEY_USAGE_TYPE_KEYBOARD) {
|
||||
changed = keyboard_key_update(entry->usage_id, event->pressed);
|
||||
if (changed) {
|
||||
emit_keys_report(false);
|
||||
}
|
||||
} else {
|
||||
changed = consumer_key_update(entry->usage_id, event->pressed);
|
||||
if (changed) {
|
||||
emit_consumer_report(false);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
{
|
||||
bool mode_changed;
|
||||
|
||||
if (!running) {
|
||||
current_mode = event->mode;
|
||||
return false;
|
||||
}
|
||||
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
current_mode = event->mode;
|
||||
mode_valid = true;
|
||||
emit_all_reports(true);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_button_event(aeh)) {
|
||||
return handle_button_event(cast_button_event(aeh));
|
||||
}
|
||||
|
||||
if (is_set_protocol_event(aeh)) {
|
||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||
|
||||
if (protocol_mode != event->protocol_mode) {
|
||||
protocol_mode = event->protocol_mode;
|
||||
|
||||
if (running && mode_valid && (current_mode == MODE_SWITCH_USB)) {
|
||||
emit_keys_report(true);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, set_protocol_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
215
src/mode_switch_module.c
Normal file
215
src/mode_switch_module.c
Normal file
@@ -0,0 +1,215 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE mode_switch_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define MODE_SWITCH_ADC_NODE DT_NODELABEL(mode_switch_adc)
|
||||
#define MODE_SWITCH_SAMPLE_INTERVAL K_MSEC(500)
|
||||
#define MODE_SWITCH_USB_MAX_MV 825
|
||||
#define MODE_SWITCH_24G_MAX_MV 2475
|
||||
|
||||
BUILD_ASSERT(DT_NODE_EXISTS(MODE_SWITCH_ADC_NODE),
|
||||
"Missing mode_switch_adc node in devicetree");
|
||||
|
||||
static const struct device *const mode_switch_adc_dev =
|
||||
DEVICE_DT_GET(MODE_SWITCH_ADC_NODE);
|
||||
|
||||
static struct k_work_delayable mode_switch_sample_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool force_report;
|
||||
static bool mode_valid;
|
||||
static enum mode_switch_mode last_mode;
|
||||
|
||||
static enum mode_switch_mode detect_mode(uint16_t voltage_mv)
|
||||
{
|
||||
if (voltage_mv < MODE_SWITCH_USB_MAX_MV) {
|
||||
return MODE_SWITCH_USB;
|
||||
}
|
||||
|
||||
if (voltage_mv < MODE_SWITCH_24G_MAX_MV) {
|
||||
return MODE_SWITCH_24G;
|
||||
}
|
||||
|
||||
return MODE_SWITCH_BLE;
|
||||
}
|
||||
|
||||
static int mode_switch_enable(bool enable)
|
||||
{
|
||||
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
|
||||
: PM_DEVICE_ACTION_SUSPEND;
|
||||
int err = pm_device_action_run(mode_switch_adc_dev, action);
|
||||
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot %s mode switch ADC (%d)",
|
||||
enable ? "resume" : "suspend", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mode_switch_sample_fn(struct k_work *work)
|
||||
{
|
||||
struct sensor_value voltage;
|
||||
int err;
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(mode_switch_adc_dev);
|
||||
if (err) {
|
||||
LOG_WRN("Mode switch ADC sample fetch failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(mode_switch_adc_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
if (err) {
|
||||
LOG_WRN("Mode switch ADC channel get failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
uint16_t sample_mv = (uint16_t)((voltage.val1 * 1000) + (voltage.val2 / 1000));
|
||||
enum mode_switch_mode mode = detect_mode(sample_mv);
|
||||
|
||||
if (force_report || !mode_valid || (mode != last_mode)) {
|
||||
struct mode_switch_event *event = new_mode_switch_event();
|
||||
|
||||
event->mode = mode;
|
||||
event->voltage_mv = sample_mv;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
last_mode = mode;
|
||||
mode_valid = true;
|
||||
force_report = false;
|
||||
}
|
||||
|
||||
reschedule:
|
||||
if (running) {
|
||||
k_work_reschedule(&mode_switch_sample_work, MODE_SWITCH_SAMPLE_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
if (!device_is_ready(mode_switch_adc_dev)) {
|
||||
LOG_ERR("Mode switch ADC device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&mode_switch_sample_work, mode_switch_sample_fn);
|
||||
mode_valid = false;
|
||||
force_report = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = mode_switch_enable(true);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
running = true;
|
||||
force_report = true;
|
||||
k_work_reschedule(&mode_switch_sample_work, K_NO_WAIT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)k_work_cancel_delayable(&mode_switch_sample_work);
|
||||
(void)mode_switch_enable(false);
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
652
src/usb_hid_module.c
Normal file
652
src/usb_hid_module.c
Normal file
@@ -0,0 +1,652 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_hid_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/usb/udc_buf.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/usb/class/usbd_hid.h>
|
||||
#include <zephyr/usb/usbd.h>
|
||||
|
||||
#include "hid_led_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define USB_HID_VID 0x1915
|
||||
#define USB_HID_PID 0x52F0
|
||||
#define USB_HID_MANUFACTURER "blinky"
|
||||
#define USB_HID_PRODUCT "Mini Keyboard"
|
||||
#define USB_HID_POLLING_US 1000U
|
||||
|
||||
#define KBD_LED_REPORT_SIZE 1U
|
||||
|
||||
enum usb_hid_interface {
|
||||
USB_HID_INTERFACE_KEYBOARD,
|
||||
USB_HID_INTERFACE_CONSUMER,
|
||||
USB_HID_INTERFACE_COUNT,
|
||||
};
|
||||
|
||||
struct usb_hid_interface_state {
|
||||
const struct device *dev;
|
||||
bool ready;
|
||||
};
|
||||
|
||||
static const uint8_t keyboard_report_desc[] = {
|
||||
0x05, 0x01, /* Usage Page (Generic Desktop) */
|
||||
0x09, 0x06, /* Usage (Keyboard) */
|
||||
0xA1, 0x01, /* Collection (Application) */
|
||||
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||
0x19, 0xE0, /* Usage Minimum (0xE0) */
|
||||
0x29, 0xE7, /* Usage Maximum (0xE7) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x95, 0x08, /* Report Count (8) */
|
||||
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||
0x19, 0x00, /* Usage Minimum (0x00) */
|
||||
0x2A, 0xDF, 0x00, /* Usage Maximum (0x00DF) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x96, 0xE0, 0x00, /* Report Count (224) */
|
||||
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||
0x05, 0x08, /* Usage Page (LEDs) */
|
||||
0x19, 0x01, /* Usage Minimum (1) */
|
||||
0x29, 0x05, /* Usage Maximum (5) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x95, 0x05, /* Report Count (5) */
|
||||
0x91, 0x02, /* Output (Data,Var,Abs) */
|
||||
0x75, 0x03, /* Report Size (3) */
|
||||
0x95, 0x01, /* Report Count (1) */
|
||||
0x91, 0x01, /* Output (Const,Array,Abs) */
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static const uint8_t consumer_report_desc[] = {
|
||||
0x05, 0x0C, /* Usage Page (Consumer) */
|
||||
0x09, 0x01, /* Usage (Consumer Control) */
|
||||
0xA1, 0x01, /* Collection (Application) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x26, 0xFF, 0x03, /* Logical Maximum (1023) */
|
||||
0x19, 0x00, /* Usage Minimum (0) */
|
||||
0x2A, 0xFF, 0x03, /* Usage Maximum (1023) */
|
||||
0x75, 0x10, /* Report Size (16) */
|
||||
0x95, 0x01, /* Report Count (1) */
|
||||
0x81, 0x00, /* Input (Data,Array,Abs) */
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static struct usb_hid_interface_state hid_ifaces[USB_HID_INTERFACE_COUNT] = {
|
||||
[USB_HID_INTERFACE_KEYBOARD] = {
|
||||
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_kbd)),
|
||||
},
|
||||
[USB_HID_INTERFACE_CONSUMER] = {
|
||||
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_consumer)),
|
||||
},
|
||||
};
|
||||
|
||||
static enum keyboard_protocol_mode keyboard_protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_enabled;
|
||||
static bool keyboard_report_in_flight;
|
||||
static bool consumer_report_in_flight;
|
||||
|
||||
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
||||
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
|
||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||
USB_HID_VID, USB_HID_PID);
|
||||
|
||||
USBD_DESC_LANG_DEFINE(blinky_lang);
|
||||
USBD_DESC_MANUFACTURER_DEFINE(blinky_mfr, USB_HID_MANUFACTURER);
|
||||
USBD_DESC_PRODUCT_DEFINE(blinky_product, USB_HID_PRODUCT);
|
||||
|
||||
USBD_DESC_CONFIG_DEFINE(blinky_fs_cfg_desc, "FS Configuration");
|
||||
USBD_CONFIGURATION_DEFINE(blinky_fs_config, 0, 250, &blinky_fs_cfg_desc);
|
||||
|
||||
static const char *const class_blocklist[] = {
|
||||
NULL,
|
||||
};
|
||||
|
||||
static struct usb_hid_interface_state *iface_from_dev(const struct device *dev)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
if (hid_ifaces[i].dev == dev) {
|
||||
return &hid_ifaces[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static enum keyboard_protocol_mode usb_proto_to_keyboard_proto(uint8_t proto)
|
||||
{
|
||||
return (proto == HID_PROTOCOL_BOOT) ? KEYBOARD_PROTOCOL_MODE_BOOT
|
||||
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
}
|
||||
|
||||
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_hid_led_event(uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||
|
||||
if (!iface) {
|
||||
return;
|
||||
}
|
||||
|
||||
iface->ready = ready;
|
||||
if (!ready) {
|
||||
keyboard_report_in_flight = false;
|
||||
}
|
||||
LOG_INF("%s interface %s",
|
||||
dev->name, ready ? "ready" : "not ready");
|
||||
}
|
||||
|
||||
static int keyboard_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int keyboard_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static void keyboard_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t keyboard_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
enum keyboard_protocol_mode new_mode = usb_proto_to_keyboard_proto(proto);
|
||||
|
||||
if (keyboard_protocol_mode == new_mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
keyboard_protocol_mode = new_mode;
|
||||
LOG_INF("USB keyboard protocol -> %s",
|
||||
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||
submit_set_protocol_event(new_mode);
|
||||
}
|
||||
|
||||
static void keyboard_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(report);
|
||||
|
||||
keyboard_report_in_flight = false;
|
||||
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
||||
}
|
||||
|
||||
static void keyboard_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
if ((len < KBD_LED_REPORT_SIZE) || (buf == NULL)) {
|
||||
LOG_WRN("Invalid keyboard output report");
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_led_event(buf[0]);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops keyboard_ops = {
|
||||
.iface_ready = keyboard_iface_ready,
|
||||
.get_report = keyboard_get_report,
|
||||
.set_report = keyboard_set_report,
|
||||
.set_idle = keyboard_set_idle,
|
||||
.get_idle = keyboard_get_idle,
|
||||
.set_protocol = keyboard_set_protocol,
|
||||
.input_report_done = keyboard_input_report_done,
|
||||
.output_report = keyboard_output_report,
|
||||
};
|
||||
|
||||
static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||
|
||||
if (!iface) {
|
||||
return;
|
||||
}
|
||||
|
||||
iface->ready = ready;
|
||||
if (!ready) {
|
||||
consumer_report_in_flight = false;
|
||||
}
|
||||
LOG_INF("%s interface %s",
|
||||
dev->name, ready ? "ready" : "not ready");
|
||||
}
|
||||
|
||||
static int consumer_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int consumer_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static void consumer_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t consumer_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void consumer_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(proto);
|
||||
}
|
||||
|
||||
static void consumer_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(report);
|
||||
|
||||
consumer_report_in_flight = false;
|
||||
LOG_DBG("USB consumer report sent by %s", dev->name);
|
||||
}
|
||||
|
||||
static void consumer_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops consumer_ops = {
|
||||
.iface_ready = consumer_iface_ready,
|
||||
.get_report = consumer_get_report,
|
||||
.set_report = consumer_set_report,
|
||||
.set_idle = consumer_set_idle,
|
||||
.get_idle = consumer_get_idle,
|
||||
.set_protocol = consumer_set_protocol,
|
||||
.input_report_done = consumer_input_report_done,
|
||||
.output_report = consumer_output_report,
|
||||
};
|
||||
|
||||
static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
const struct usbd_msg *const msg)
|
||||
{
|
||||
ARG_UNUSED(usbd_ctx);
|
||||
|
||||
if (msg->type == USBD_MSG_VBUS_READY) {
|
||||
if (!usb_enabled) {
|
||||
int err = usbd_enable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg->type == USBD_MSG_VBUS_REMOVED) {
|
||||
if (usb_enabled) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_disable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = false;
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
hid_ifaces[i].ready = false;
|
||||
}
|
||||
keyboard_report_in_flight = false;
|
||||
consumer_report_in_flight = false;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static int usb_descriptors_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_lang);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_mfr);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_product);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_add_configuration(&blinky_usbd, USBD_SPEED_FS, &blinky_fs_config);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_register_all_classes(&blinky_usbd, USBD_SPEED_FS, 1, class_blocklist);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
usbd_device_set_code_triple(&blinky_usbd, USBD_SPEED_FS, 0, 0, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_hid_register_devices(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
if (!device_is_ready(hid_ifaces[i].dev)) {
|
||||
LOG_ERR("HID device %s not ready", hid_ifaces[i].dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
}
|
||||
|
||||
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||
keyboard_report_desc,
|
||||
sizeof(keyboard_report_desc),
|
||||
&keyboard_ops);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||
consumer_report_desc,
|
||||
sizeof(consumer_report_desc),
|
||||
&consumer_ops);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = usb_hid_register_devices();
|
||||
if (err) {
|
||||
LOG_ERR("hid_device_register failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usb_descriptors_init();
|
||||
if (err) {
|
||||
LOG_ERR("usb descriptor init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = usbd_init(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_enabled = true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || (event->mode != MODE_SWITCH_USB)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
if (event->protocol_mode != keyboard_protocol_mode) {
|
||||
LOG_WRN("Drop USB keys report due to protocol mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready) {
|
||||
LOG_DBG("USB keyboard interface not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (keyboard_report_in_flight) {
|
||||
LOG_WRN("Drop USB keyboard report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(keyboard_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
|
||||
err = hid_device_submit_report(
|
||||
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||
(uint16_t)event->dyndata.size,
|
||||
keyboard_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||
} else {
|
||||
keyboard_report_in_flight = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||
if (!hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready) {
|
||||
LOG_DBG("USB consumer interface not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (consumer_report_in_flight) {
|
||||
LOG_WRN("Drop USB consumer report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(consumer_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
|
||||
err = hid_device_submit_report(
|
||||
hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||
(uint16_t)event->dyndata.size,
|
||||
consumer_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||
} else {
|
||||
consumer_report_in_flight = true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_keyboard_hid_report_event(aeh)) {
|
||||
return handle_keyboard_hid_report_event(
|
||||
cast_keyboard_hid_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
Reference in New Issue
Block a user