diff --git a/APP/peripheral.c b/APP/peripheral.c index 90e6daf..2007d94 100644 --- a/APP/peripheral.c +++ b/APP/peripheral.c @@ -18,7 +18,7 @@ #include "devinfoservice.h" #include "gattprofile.h" #include "peripheral.h" - +#include "bsp_iwdg.h" /********************************************************************* * MACROS */ @@ -156,6 +156,9 @@ static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList); static void peripheralRssiCB(uint16_t connHandle, int8_t rssi); static void peripheralChar4Notify(uint8_t *pValue, uint16_t len); +// 广播事件和连接事件回调函数声明 +extern void BLE_AdvertiseEventCB(uint32_t timeUs); +extern void BLE_ConnectEventCB(uint32_t timeUs); /********************************************************************* * PROFILE CALLBACKS */ @@ -280,6 +283,10 @@ void Peripheral_Init() // Setup a delayed profile startup tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT); + + // 注册广播事件和连接事件回调函数 + LL_AdvertiseEventRegister(BLE_AdvertiseEventCB); + LL_ConnectEventRegister(BLE_ConnectEventCB); } /********************************************************************* @@ -736,5 +743,36 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len } } + +/********************************************************************* + * @fn BLE_AdvertiseEventCB + * + * @brief 广播事件回调函数,在每次广播间隔到来时调用 + * + * @param timeUs - 当前事件的时间戳(微秒) + * + * @return none + */ +void BLE_AdvertiseEventCB(uint32_t timeUs) +{ + FEED_IWDG(); + // PRINT("BLE_AdvertiseEventCB\n"); +} + +/********************************************************************* + * @fn BLE_ConnectEventCB + * + * @brief 连接事件回调函数,在每次连接间隔到来时调用 + * + * @param timeUs - 当前事件的时间戳(微秒) + * + * @return none + */ +void BLE_ConnectEventCB(uint32_t timeUs) +{ + FEED_IWDG(); + // PRINT("BLE_ConnectEventCB\n"); +} + /********************************************************************* *********************************************************************/ diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c index 7b9ad81..92a0bdd 100644 --- a/APP/peripheral_main.c +++ b/APP/peripheral_main.c @@ -23,6 +23,7 @@ #include "bsp_motor.h" #include "bsp_led.h" #include "bsp_key.h" +#include "bsp_iwdg.h" #include "log.h" #undef LOG_ENABLE @@ -132,6 +133,10 @@ int main(void) logDebug("BSP_M1307r ok\n"); BSP_KEY_Init(app_task_handler); + + ShowRestartReason(); + IWDG_Init(3000); + logDebug("IWDG init ok\n"); BSP_PRESS_Init(); logDebug("BSP init ok\n"); diff --git a/bsp/inc/bsp_adc.h b/bsp/inc/bsp_adc.h index 0e1c01e..127f0b9 100644 --- a/bsp/inc/bsp_adc.h +++ b/bsp/inc/bsp_adc.h @@ -11,7 +11,7 @@ #include "CH58x_common.h" #define VBAT_EVT_START (0x0001 << 2) -#define VBAT_LOW_THRESHOLD 3.0f // 电池电压低阈值,单位V +#define VBAT_LOW_THRESHOLD 2.0f // 电池电压低阈值,单位V void VBAT_ADC_Init(void); void BSP_VBAT_Init(void); diff --git a/bsp/inc/bsp_iwdg.h b/bsp/inc/bsp_iwdg.h new file mode 100644 index 0000000..8cb341a --- /dev/null +++ b/bsp/inc/bsp_iwdg.h @@ -0,0 +1,35 @@ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-15 16:13:37 + * @LastEditors : czq 860517298@qq.com + * @LastEditTime : 2025-04-01 14:00:00 + * @FilePath : \IOT_SCV_CH584M - \BSP\inc\bsp_iwdg.h + * @Description : + * + * Copyright (c) 2024 by yzy, All Rights Reserved. + */ + +#ifndef __BSP_IWDG_H__ +#define __BSP_IWDG_H__ + +#include "CONFIG.h" + +#define RB_RLR 0x0FFF // RW, watch-dog counter reload (write protect) +#define RB_PR 0x7000 // PR, prescale (write protect) +#define RB_PVU 0x8000 // RO, register update flag (write protect) +#define RB_COUNT 0xFF0000 // RO, watch-dog down counter +#define RB_STOP_EN 0x20000000 // RW, watch-dog stop enable (write protect) +#define RB_WR_PROTECT 0x40000000 // RO, write protect +#define RB_IWDG_EN 0x80000000 // RO, watch-dog enable + +void IWDG_Init(uint16_t ms); + +#define FEED_IWDG() {R32_IWDG_KR=0xAAAA;} + +void ShowRestartReason(void); + +#endif // !__BSP_IWDG_H__ + + + + diff --git a/bsp/src/bsp_iwdg.c b/bsp/src/bsp_iwdg.c new file mode 100644 index 0000000..49b24fe --- /dev/null +++ b/bsp/src/bsp_iwdg.c @@ -0,0 +1,95 @@ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-15 16:13:37 + * @LastEditors : czq 860517298@qq.com + * @LastEditTime : 2025-04-01 14:00:00 + * @FilePath : \IOT_SCV_CH584M - \BSP\inc\bsp_iwdg.c + * @Description : + * + * Copyright (c) 2024 by yzy, All Rights Reserved. + */ +// https://www.cnblogs.com/debugdabiaoge/p/17580033.html +// https://www.cnblogs.com/risc5-ble/p/17853714.html +#include "bsp_iwdg.h" +#include "bsp_uart.h" +#include "log.h" + +#undef LOG_ENABLE +#define LOG_ENABLE 1 + +#undef LOG_TAG +#define LOG_TAG "idwg" + +/********************************************************************* + * @fn IWDG_Init + * + * @brief 鐙珛鐪嬮棬鐙楋紙IWDG锛夌敱涓撶敤鐨勫唴閮ㄤ綆閫熸椂閽燂紙LSI锛夐┍鍔紝鑳藉鍦ㄤ綆鍔熻楁ā寮忎笅姝e父宸ヤ綔銆 + * + * @param ms + 32k rc + div 0:4 | 1:8 | 2:16 | 3:32 | + 4:64 | 5:128 | 6:256 | 7:512(32K鍒嗛512锛屾椂閽62.5Hz) | + reload max 4095 + * @return none + */ +void IWDG_Init(uint16_t ms) +{ + uint16_t div = 7; + float tick_ms = 1000 / (32000 / 512); + uint16_t reload = (ms / tick_ms); + R32_IWDG_KR = 0x5555; + R32_IWDG_CFG = ((div & 0x7) << 12) | (RB_STOP_EN) | (reload & RB_RLR); + R32_IWDG_KR = 0xCCCC; + // logDebug("IWDG set: div=%d, reload=%d, R32_IWDG_CFG=0x%08X", div, reload, R32_IWDG_CFG); +} + + +/********************************************************************* + * @fn ShowRestartReason + * + * @brief 鎵撳嵃澶嶄綅鐘舵佸瘎瀛樺櫒锛屾樉绀哄浣嶅師鍥 + * + * 000锛氳蒋浠跺浣 SR锛圧B_WDOG_RST_EN=0鏃惰蒋浠跺浣嶅彲浜х敓姝ょ姸鎬侊紝鍚﹀垯鍙浣嶄絾涓嶄骇鐢熸鐘舵侊級锛 + 001锛氫笂鐢靛浣 RPOR锛 + 010锛氱湅闂ㄧ嫍瓒呮椂澶嶄綅 WTR锛 + 011锛氬閮ㄦ墜鍔ㄥ浣 MR锛 + 101锛氫粠涓嬬數妯″紡鍞ら啋鏃剁殑澶嶄綅 GRWSM锛 + 100/110/111锛氬敜閱掑浣 LRW锛屼笖姝ゅ墠鐨勪笂涓娆″浣嶅垎鍒槸 SR/WTR/MR銆 + * @return none + */ +void ShowRestartReason(void) +{ + uint8_t reason = R8_RESET_STATUS&0x07; + logDebug("R8_RESET_STATUS = %02x", reason); + switch (reason) + { + case RST_FLAG_SW: + logDebug("RST_FLAG_SW"); + break; + case RST_FLAG_RPOR: + logDebug("RST_FLAG_RPOR"); + break; + case RST_FLAG_WTR: + logDebug("RST_FLAG_WTR"); + break; + case RST_FLAG_MR: + logDebug("RST_FLAG_MR"); + break; + + case RST_FLAG_GPWSM: + logDebug("RST_FLAG_GPWSM"); + break; + + case 0x04: + logDebug("LRW, last SR"); + break; + case 0x06: + logDebug("LRW, last WTR"); + break; + case 0x07: + logDebug("LRW, last MR"); + + default: + break; + } +} \ No newline at end of file