feat(button): 新增 EMI 恢复 LED 指示
ADS1256 受电机 EMI 干扰自恢复后,点亮 LED1 并 3 秒后自动熄灭, 给出可见的故障告警。复用按键反馈所用的 LED1,但用独立的 emi_off_work 延时句柄,避免与按键闪烁的 blink_remaining 节拍 状态机混用导致错乱;重复触发用 k_work_reschedule 刷新熄灭 时刻,支持连续 EMI 事件累计指示。 sensor 线程在 EMI 自恢复路径调用 button_led_indicate_emi() 触发指示。
This commit is contained in:
@@ -1,3 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int button_init(void);
|
int button_init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief EMI 自恢复事件指示:点亮 LED1 并在 3 秒后自动熄灭。
|
||||||
|
*
|
||||||
|
* 复用按键反馈所用的 LED1,外部子系统(如 sensor)在检测到 ADS1256
|
||||||
|
* 寄存器被 EMI 干扰并完成恢复后调用,给出可见的故障告警。
|
||||||
|
*
|
||||||
|
* @return 无返回值。
|
||||||
|
*/
|
||||||
|
void button_led_indicate_emi(void);
|
||||||
|
|||||||
@@ -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 gpio_callback btn_cb_data;
|
||||||
static struct k_work_delayable led_off_work;
|
static struct k_work_delayable led_off_work;
|
||||||
|
static struct k_work_delayable emi_off_work;
|
||||||
|
|
||||||
#define LED_FLASH_MS 200
|
#define LED_FLASH_MS 200
|
||||||
#define LED_BLINK_COUNT 3
|
#define LED_BLINK_COUNT 3
|
||||||
|
#define EMI_LED_HOLD_MS 3000
|
||||||
|
|
||||||
static struct k_work tare_work;
|
static struct k_work tare_work;
|
||||||
static volatile int blink_remaining;
|
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 闪烁反馈。
|
* @brief 在工作队列上下文执行去皮,并启动 LED 闪烁反馈。
|
||||||
*
|
*
|
||||||
@@ -102,7 +119,14 @@ int button_init(void) {
|
|||||||
|
|
||||||
k_work_init(&tare_work, tare_work_handler);
|
k_work_init(&tare_work, tare_work_handler);
|
||||||
k_work_init_delayable(&led_off_work, led_off_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");
|
LOG_INF("Button1 → tare + LED1 flash initialized");
|
||||||
return 0;
|
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));
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "sensor.h"
|
#include "sensor.h"
|
||||||
#include "ads1256.h"
|
#include "ads1256.h"
|
||||||
#include "ble_transport.h"
|
#include "ble_transport.h"
|
||||||
|
#include "button.h"
|
||||||
#include "calibration.h"
|
#include "calibration.h"
|
||||||
#include "comm_protocol.h"
|
#include "comm_protocol.h"
|
||||||
|
|
||||||
@@ -237,6 +238,8 @@ static void sensor_thread_fn(void *p1, void *p2, void *p3) {
|
|||||||
if (acq < 10) {
|
if (acq < 10) {
|
||||||
LOG_ERR("EMI detected (acq=%lld ms)", (long long)acq);
|
LOG_ERR("EMI detected (acq=%lld ms)", (long long)acq);
|
||||||
ads1256_recover(10);
|
ads1256_recover(10);
|
||||||
|
/* 点亮 LED1 作为 EMI 恢复指示,3 秒后由 button 模块自动熄灭 */
|
||||||
|
button_led_indicate_emi();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user