代码暂存 添加看门狗功能

This commit is contained in:
常正强 2025-04-01 15:09:01 +08:00
parent c5978ab58e
commit d8c6878e8b
5 changed files with 175 additions and 2 deletions

View File

@ -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");
}
/*********************************************************************
*********************************************************************/

View File

@ -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");

View File

@ -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);

35
bsp/inc/bsp_iwdg.h Normal file
View File

@ -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__

95
bsp/src/bsp_iwdg.c Normal file
View File

@ -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 IWDGLSI
*
* @param ms
32k rc
div 0:4 | 1:8 | 2:16 | 3:32 |
4:64 | 5:128 | 6:256 | 7:512(32K分频51262.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 SRRB_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;
}
}