refactor(vd960DBN): 看门狗精简 — 去掉平台响应超时, 只保留硬件 IWDG
heartbeat 是单向设备上报, 平台不回复; 用 '3×心跳无平台回复→重启' 逻辑不成立。保活依赖三层已有机制: MQTT层: PINGREQ/PINGRESP (60s) → broker不回 → TCP超时 TCP层: KeepAlive → WCHNET SINT_STAT_DISCONNECT/TIMEOUT 硬件层: IWDG (4s) → 主循环卡死 → 硬件复位
This commit is contained in:
@@ -42,7 +42,6 @@ static uint32_t _iot_last_heartbeat = 0; // 上次心跳时刻 (ms)
|
||||
static uint32_t _iot_reconnect_deadline = 0;
|
||||
static uint32_t _iot_reconnect_backoff = 0;
|
||||
static uint32_t _iot_connect_start = 0; // TCP connect 开始时刻
|
||||
static uint32_t _iot_last_recv_ms = 0; // 上次收到平台回复时刻 (看门狗追踪)
|
||||
|
||||
/*===========================================================================
|
||||
* 传感器数据缓存 & 上报间隔控制
|
||||
@@ -209,7 +208,6 @@ void iot_evt_handle_ack(uint32_t msg_id, int code) {
|
||||
return;
|
||||
}
|
||||
/* 确认成功 → 弹出本包事件 */
|
||||
_iot_last_recv_ms = mstick(); // 看门狗: 平台对 event_report 的 ACK 也算响应
|
||||
_evt_head = (_evt_head + _evt_pend_n) % IOT_EVT_QUEUE_DEPTH;
|
||||
_evt_count -= _evt_pend_n;
|
||||
PRINT("EVT: ack ok, %d events dequeued (left=%d)\n", _evt_pend_n, _evt_count);
|
||||
@@ -398,8 +396,6 @@ static void iot_handle_publish(const char *topic, uint8_t *payload, int payload_
|
||||
|
||||
PRINT("IOT: PUBLISH topic=%s payload=%s\n", topic, json);
|
||||
|
||||
_iot_last_recv_ms = mstick(); // 看门狗: 收到平台任意消息即刷新
|
||||
|
||||
/* 提取 msg_id 和 cmd */
|
||||
char tmp[256];
|
||||
uint32_t msg_id = 0;
|
||||
@@ -555,7 +551,6 @@ static void iot_handle_suback(void) {
|
||||
PRINT("IOT: ← SUBACK, topics subscribed\n");
|
||||
g_iot_state = IOT_STATE_READY;
|
||||
_iot_reconnect_backoff = 0; // 连接成功, 重置退避
|
||||
_iot_last_recv_ms = mstick(); // 看门狗: 刚连上, 重置平台响应计时
|
||||
|
||||
// V1.03: 订阅成功后发布 initialize 告知服务器上线 (IoT 路径)
|
||||
iot_send_initialize();
|
||||
@@ -905,12 +900,13 @@ void iot_mqtt_handle_sock_int(uint8_t socketid, uint8_t intstat) {
|
||||
}
|
||||
|
||||
/*===========================================================================
|
||||
* 看门狗 — 硬件 IWDG + 平台响应超时
|
||||
* IWDG: ~4s LSI超时, 主循环喂狗, 防死循环/硬故障
|
||||
* 平台狗: 3×心跳周期(180s)无平台回复 → NVIC_SystemReset
|
||||
* 硬件看门狗 IWDG — 主循环卡死保护
|
||||
* LSI≈40kHz, Prescaler=256, Reload=625 → ~4s 超时
|
||||
* 保活链路: MQTT PINGREQ/PINGRESP → TCP KeepAlive → IWDG
|
||||
* (heartbeat 是单向设备上报, 平台不回复, 不能用作存活检测)
|
||||
*===========================================================================*/
|
||||
|
||||
/* IWDG 初始化: LSI≈40kHz, Prescaler=256 → 156Hz tick, Reload=625 → 4s 超时 */
|
||||
/* IWDG 初始化 */
|
||||
void iot_watchdog_init(void) {
|
||||
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
|
||||
IWDG_SetPrescaler(IWDG_Prescaler_256);
|
||||
@@ -925,27 +921,12 @@ void iot_watchdog_kick(void) {
|
||||
IWDG_ReloadCounter();
|
||||
}
|
||||
|
||||
/* 平台响应超时检查 — iot_mqtt_poll 中调用 */
|
||||
void iot_watchdog_check(void) {
|
||||
if (g_iot_state != IOT_STATE_READY) return;
|
||||
uint32_t now = mstick();
|
||||
if (now - _iot_last_recv_ms > IOT_MQTT_WDT_TIMEOUT_MS) {
|
||||
PRINT("IOT: WATCHDOG no platform reply for %lu ms (>%u ms), resetting...\n",
|
||||
(unsigned long)(now - _iot_last_recv_ms), IOT_MQTT_WDT_TIMEOUT_MS);
|
||||
/* 不再喂狗, IWDG 4s 后自动复位; 或直接 NVIC_SystemReset */
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
}
|
||||
|
||||
/*===========================================================================
|
||||
* 主循环轮询
|
||||
*===========================================================================*/
|
||||
void iot_mqtt_poll(void) {
|
||||
iot_watchdog_kick(); // 喂硬件 IWDG
|
||||
|
||||
/* 平台响应超时检查 */
|
||||
iot_watchdog_check();
|
||||
|
||||
/* 断线重连 */
|
||||
if (g_iot_state == IOT_STATE_DISCONNECTED && g_iot_socket == 0xFF) {
|
||||
if (_iot_reconnect_deadline == 0 || mstick() > _iot_reconnect_deadline) {
|
||||
|
||||
Reference in New Issue
Block a user