From 7bf7339f3f7f9a1c5cc9294a6181485ec421e9d6 Mon Sep 17 00:00:00 2001 From: pNexus Date: Wed, 27 May 2026 09:35:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(button):=20=E6=96=B0=E5=A2=9E=20EMI=20?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=20LED=20=E6=8C=87=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ADS1256 受电机 EMI 干扰自恢复后,点亮 LED1 并 3 秒后自动熄灭, 给出可见的故障告警。复用按键反馈所用的 LED1,但用独立的 emi_off_work 延时句柄,避免与按键闪烁的 blink_remaining 节拍 状态机混用导致错乱;重复触发用 k_work_reschedule 刷新熄灭 时刻,支持连续 EMI 事件累计指示。 sensor 线程在 EMI 自恢复路径调用 button_led_indicate_emi() 触发指示。 --- inc/button.h | 10 ++++++++++ src/button.c | 24 ++++++++++++++++++++++++ src/sensor.c | 3 +++ 3 files changed, 37 insertions(+) diff --git a/inc/button.h b/inc/button.h index f3389f2..87d3655 100644 --- a/inc/button.h +++ b/inc/button.h @@ -1,3 +1,13 @@ #pragma once int button_init(void); + +/** + * @brief EMI 自恢复事件指示:点亮 LED1 并在 3 秒后自动熄灭。 + * + * 复用按键反馈所用的 LED1,外部子系统(如 sensor)在检测到 ADS1256 + * 寄存器被 EMI 干扰并完成恢复后调用,给出可见的故障告警。 + * + * @return 无返回值。 + */ +void button_led_indicate_emi(void); diff --git a/src/button.c b/src/button.c index 7b9f475..299bd60 100644 --- a/src/button.c +++ b/src/button.c @@ -13,9 +13,11 @@ static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); static struct gpio_callback btn_cb_data; static struct k_work_delayable led_off_work; +static struct k_work_delayable emi_off_work; #define LED_FLASH_MS 200 #define LED_BLINK_COUNT 3 +#define EMI_LED_HOLD_MS 3000 static struct k_work tare_work; static volatile int blink_remaining; @@ -38,6 +40,21 @@ static void led_off_handler(struct k_work *work) { } } +/** + * @brief EMI 指示熄灯 work:3 秒延时到期后熄灭 LED1。 + * + * 与 led_off_work 分开是因为按键闪烁序列依赖 blink_remaining 的奇偶节拍, + * EMI 指示是单次长亮,混用会让 blink_remaining 状态机错乱。 + * + * @param work Zephyr delayable work 入口参数,当前实现未直接使用。 + * + * @return 无返回值。 + */ +static void emi_off_handler(struct k_work *work) { + ARG_UNUSED(work); + gpio_pin_set_dt(&led1, 0); +} + /** * @brief 在工作队列上下文执行去皮,并启动 LED 闪烁反馈。 * @@ -102,7 +119,14 @@ int button_init(void) { k_work_init(&tare_work, tare_work_handler); k_work_init_delayable(&led_off_work, led_off_handler); + k_work_init_delayable(&emi_off_work, emi_off_handler); LOG_INF("Button1 → tare + LED1 flash initialized"); return 0; } + +void button_led_indicate_emi(void) { + /* 直接拉高 LED 并延后 3 秒熄灭;重复触发会刷新熄灭时刻,从而支持连续 EMI 事件累计指示 */ + gpio_pin_set_dt(&led1, 1); + k_work_reschedule(&emi_off_work, K_MSEC(EMI_LED_HOLD_MS)); +} diff --git a/src/sensor.c b/src/sensor.c index 9015aac..2717693 100644 --- a/src/sensor.c +++ b/src/sensor.c @@ -1,6 +1,7 @@ #include "sensor.h" #include "ads1256.h" #include "ble_transport.h" +#include "button.h" #include "calibration.h" #include "comm_protocol.h" @@ -237,6 +238,8 @@ static void sensor_thread_fn(void *p1, void *p2, void *p3) { if (acq < 10) { LOG_ERR("EMI detected (acq=%lld ms)", (long long)acq); ads1256_recover(10); + /* 点亮 LED1 作为 EMI 恢复指示,3 秒后由 button 模块自动熄灭 */ + button_led_indicate_emi(); continue; }