diff --git a/BSP/inc/bsp_iwdg.h b/BSP/inc/bsp_iwdg.h new file mode 100644 index 0000000..70b167c --- /dev/null +++ b/BSP/inc/bsp_iwdg.h @@ -0,0 +1,47 @@ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-15 16:13:37 + * @LastEditors : stark1898y 1658608470@qq.com + * @LastEditTime : 2024-12-15 16:13:37 + * @FilePath : \BLE_TYQ_CH592F - 副本\BSP\inc\bsp_iwdg.h + * @Description : + * + * Copyright (c) 2024 by yzy, All Rights Reserved. + */ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-14 10:51:01 + * @LastEditors : stark1898y 1658608470@qq.com + * @LastEditTime : 2024-12-15 11:16:24 + * @FilePath : \BLE_TYQ_CH592F\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); + +// 需要定时调用喂狗 +// 在看门狗键寄存器 (R32_IWDG_KR) 中,软件要以一定的间隔写入 0xAAAA,重装载计数值,这就是喂狗的操作。 +// 否则,当计数器为 0 时,看门狗会产生复位。 +#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..a4c5f80 --- /dev/null +++ b/BSP/src/bsp_iwdg.c @@ -0,0 +1,109 @@ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-15 16:13:41 + * @LastEditors : stark1898y 1658608470@qq.com + * @LastEditTime : 2024-12-15 16:13:42 + * @FilePath : \BLE_TYQ_CH592F - 副本\BSP\src\bsp_iwdg.c + * @Description : + * + * Copyright (c) 2024 by yzy, All Rights Reserved. + */ +#include "bsp_iwdg.h" + +#include "bsp_uart.h" +#include "log.h" + +// https://www.cnblogs.com/debugdabiaoge/p/17580033.html +// https://www.cnblogs.com/risc5-ble/p/17853714.html + +#undef LOG_ENABLE +#define LOG_ENABLE 1 + +#undef LOG_TAG +#define LOG_TAG "idwg" + +// 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 + +// 独立看门狗(IWDG)由专用的内部低速时钟(LSI)驱动,能够在低功耗模式下正常工作。 +void IWDG_Init(uint16_t ms) +{ + uint16_t div = 512; + float tick_ms = 1000 / (32000 / 512); + + uint16_t reload = (ms / tick_ms) + 1; + + // 解除IWDG保护 + R32_IWDG_KR = 0x5555; + + R32_IWDG_CFG = (RB_PR & (div << 12)) | (RB_STOP_EN) | (reload & RB_RLR); + // 开启IWDG保护 + R32_IWDG_KR = 0xCCCC; + + logDebug("R32_IWDG_CFG = %X", R32_IWDG_CFG); +} + +// #define RB_RESET_FLAG 0x07 // RO: recent reset flag +// #define RST_FLAG_SW 0x00 +// #define RST_FLAG_RPOR 0x01 +// #define RST_FLAG_WTR 0x02 +// #define RST_FLAG_MR 0x03 +// //#define RST_FLAG_GPWSM 0x04 // RO, power on reset flag during sleep/shutdown: 0=no power on reset during sleep/shutdown, 1=power on reset occurred during sleep/shutdown +// #define RST_FLAG_GPWSM 0x05 +// // RB_RESET_FLAG: recent reset flag +// // 000 - SR, software reset, by RB_SOFTWARE_RESET=1 @RB_WDOG_RST_EN=0 +// // 001 - RPOR, real power on reset +// // 010 - WTR, watch-dog timer-out reset +// // 011 - MR, external manual reset by RST pin input low +// // 101 - GRWSM, global reset by waking under shutdown mode +// 1?? - LRW, power on reset occurred during sleep + +// 打印复位状态寄存器,显示复位原因 +// 最近一次复位状态: +// 000:软件复位 SR(RB_WDOG_RST_EN=0时软件复位可产生此状态,否则可复位但不产生此状态); +// 001:上电复位 RPOR; +// 010:看门狗超时复位 WTR; +// 011:外部手动复位 MR; +// 101:从下电模式唤醒时的复位 GRWSM; +// 100/110/111:唤醒复位 LRW,且此前的上一次复位分别是 SR/WTR/MR。 +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; + } +} + + +