From c138885fdc242bdf98fb44e592559557b8edb7f2 Mon Sep 17 00:00:00 2001 From: stark1898y <1658608470@qq.com> Date: Sun, 13 Apr 2025 23:18:47 +0800 Subject: [PATCH] zancun --- APP/peripheral_main.c | 33 ++++++++++++++++++++------------- BSP/inc/bsp_iwdg.h | 8 ++++++-- BSP/src/bsp_adc.c | 10 ++++++++-- BSP/src/bsp_beep_led_emv.c | 3 ++- BSP/src/bsp_iwdg.c | 27 +++++++++++++++++++++++++-- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c index 95ab06c..cfae4fd 100644 --- a/APP/peripheral_main.c +++ b/APP/peripheral_main.c @@ -131,7 +131,7 @@ void app_task_handler(TeAppEvtType app_evt_type) { BSP_BlockSleep(); // BSP_BlockSleep(); - BSP_RequestBoost(); + // BSP_RequestBoost(); switch(app_evt_type) { @@ -144,8 +144,8 @@ void app_task_handler(TeAppEvtType app_evt_type) logDebug("LED显示状态"); // tmos_set_event(led_task_id, LED_SHOW_START_EVT); BSP_BlockSleep(); - BSP_RequestBoost(); - DelayMs(1); + // BSP_RequestBoost(); + DelayMs(3); ShowLed(); // tmos_stop_task(led_task_id, LED_SHOW_ONCE_EVT); // tmos_start_task(led_task_id, LED_SHOW_ONCE_EVT, 1000); @@ -158,8 +158,8 @@ void app_task_handler(TeAppEvtType app_evt_type) // tmos_set_event(led_task_id, LED_SHOW_START_EVT); gValveData.switch_status = kOpened; BSP_BlockSleep(); - BSP_RequestBoost(); - DelayMs(1); + // BSP_RequestBoost(); + DelayMs(3); ShowLed(); // tmos_stop_task(led_task_id, LED_SHOW_ONCE_EVT); // tmos_start_task(led_task_id, LED_SHOW_ONCE_EVT, 1000); @@ -172,8 +172,8 @@ void app_task_handler(TeAppEvtType app_evt_type) // tmos_set_event(led_task_id, LED_SHOW_START_EVT); BSP_BlockSleep(); - BSP_RequestBoost(); - DelayMs(1); + // BSP_RequestBoost(); + DelayMs(3); ShowLed(); // tmos_stop_task(led_task_id, LED_SHOW_ONCE_EVT); // tmos_start_task(led_task_id, LED_SHOW_ONCE_EVT, 1000); @@ -221,6 +221,7 @@ __attribute__((noinline)) void Main_Circulation() TMOS_SystemProcess(); KEY_ProcessLoop(); BSP_UART1_TxLoop(); + FEED_IWDG(); } } @@ -261,9 +262,16 @@ int main(void) // 改成 1500000 波特率了 BSP_UART1_Init(2500000); #endif - DelayMs(1000 * 3); + ShowRestartReason(); CH58x_BLEInit(); + + HAL_Init(); + GAPRole_PeripheralInit(); + Peripheral_Init(); + + DelayMs(1000 * 3); + // HAL_Init(); // GAPRole_PeripheralInit(); // Peripheral_Init(); @@ -303,12 +311,9 @@ int main(void) BSP_VAVLE_Init(); - HAL_Init(); - GAPRole_PeripheralInit(); - Peripheral_Init(); - BSP_BlockSleep(); BSP_RequestBoost(); + DelayMs(10); #if 0 DelayMs(1); EMV_CHARGE_EN; @@ -368,7 +373,7 @@ int main(void) ShowLed(); BEEP_ON; - DelayMs(1000 * 1); + DelayMs(500 * 1); // tmos_set_event(Peripheral_TaskID, SBP_REPLY_CMD_EVT); #if 0 @@ -414,6 +419,8 @@ int main(void) BSP_NoNeedBoost(); BSP_RequestSleep(); + // IWDG_Init(IWDG_MS); + // 在连接间隔和广播间隔到来时,会进入回调中喂狗 // LL_AdvertiseEventRegister(BLE_AdvertiseEventCB); // LL_ConnectEventRegister(BLE_ConnectEventCB); diff --git a/BSP/inc/bsp_iwdg.h b/BSP/inc/bsp_iwdg.h index 13a111c..89fca96 100644 --- a/BSP/inc/bsp_iwdg.h +++ b/BSP/inc/bsp_iwdg.h @@ -2,8 +2,8 @@ * @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_CH584M - 副本\BSP\inc\bsp_iwdg.h + * @LastEditTime : 2025-04-13 22:48:38 + * @FilePath : \BLE_TYQ_CH584M\BSP\inc\bsp_iwdg.h * @Description : * * Copyright (c) 2024 by yzy, All Rights Reserved. @@ -31,6 +31,10 @@ #define RB_WR_PROTECT 0x40000000 // RO, write protect #define RB_IWDG_EN 0x80000000 // RO, watch-dog enable +#define IWDG_MS (6000) + +#define IWIG_FEED_EVENT (0x01 << 0) + void IWDG_Init(uint16_t ms); // 需要定时调用喂狗 diff --git a/BSP/src/bsp_adc.c b/BSP/src/bsp_adc.c index 42ba376..3283f20 100644 --- a/BSP/src/bsp_adc.c +++ b/BSP/src/bsp_adc.c @@ -31,6 +31,8 @@ uint8_t BSP_ReadVbat(void) uint8_t i; // VBAT uint32_t CountBat = 0; + + DelayUs(50); ADC_InterBATSampInit(); DelayUs(50); ADC_ExcutSingleConver(); @@ -38,10 +40,12 @@ uint8_t BSP_ReadVbat(void) for (i = 0; i < 20; i++) { adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次 + // logDebug("%d", adcBuff[i]); } for (i = 0; i < 20; i++) { CountBat += adcBuff[i]; + // logDebug("%d, %d", adcBuff[i], CountBat); if (i == 0) { min_number = adcBuff[i]; @@ -52,9 +56,11 @@ uint8_t BSP_ReadVbat(void) } logDebug("min_number = %d, max_number = %d", min_number, max_number); CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值 + // logDebug("CountBat = %d", CountBat); // PGA=-12db - // CountBat = CountBat * 1050 / 512 - (3 * 1050); - CountBat = CountBat * 2.05078125 - 3150; + CountBat = CountBat * 1050 / 512 - (3 * 1050); + // CountBat = CountBat * 2.05078125 - 3150; // 有问题 + // logDebug("CountBat = %d", CountBat); uint8_t vbat = (uint8_t)((CountBat + 50) / 100); logDebug("AverageCountBat = %dmV, vbat = %d(100mV)", CountBat, vbat); diff --git a/BSP/src/bsp_beep_led_emv.c b/BSP/src/bsp_beep_led_emv.c index 27ac425..65c8b9a 100644 --- a/BSP/src/bsp_beep_led_emv.c +++ b/BSP/src/bsp_beep_led_emv.c @@ -24,6 +24,7 @@ tmosTaskID led_task_id = INVALID_TASK_ID; void ShowLed() { gValveData.bat = BSP_ReadVbat(); + BSP_RequestBoost(); // 电压过低就关阀 if (gValveData.bat <= LOW_VABAT_ALARM_VALUE) @@ -59,7 +60,7 @@ static tmosEvents LED_Task_ProcessEvent(tmosTaskID taskID, tmosEvents event) logDebug("LED_SHOW_START_EVT"); BSP_BlockSleep(); BSP_RequestBoost(); - DelayMs(1); + DelayMs(3); ShowLed(); tmos_stop_task(led_task_id, LED_SHOW_ONCE_EVT); tmos_start_task(led_task_id, LED_SHOW_ONCE_EVT, 1000); diff --git a/BSP/src/bsp_iwdg.c b/BSP/src/bsp_iwdg.c index aaae803..b3c6011 100644 --- a/BSP/src/bsp_iwdg.c +++ b/BSP/src/bsp_iwdg.c @@ -2,7 +2,7 @@ * @Author : stark1898y 1658608470@qq.com * @Date : 2024-12-15 16:13:41 * @LastEditors : stark1898y 1658608470@qq.com - * @LastEditTime : 2025-02-22 17:28:20 + * @LastEditTime : 2025-04-13 22:57:23 * @FilePath : \BLE_TYQ_CH584M\BSP\src\bsp_iwdg.c * @Description : * @@ -22,12 +22,27 @@ #undef LOG_TAG #define LOG_TAG "idwg" +uint8_t IWDGTaskId; + +uint16_t IWDG_ProcessEvent(uint8_t task_id, uint16_t events) +{ + if (events & IWIG_FEED_EVENT) + { + FEED_IWDG(); + tmos_start_task(task_id, IWIG_FEED_EVENT, 5000); + return (events ^ IWIG_FEED_EVENT); + } + return 0; +} + + // 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)驱动,能够在低功耗模式下正常工作。 +// 根据寄存器描述,IWDG最大时间可以是(0xFFF/(32K/512))=65.52s void IWDG_Init(uint16_t ms) { uint16_t div = 512; @@ -38,11 +53,18 @@ void IWDG_Init(uint16_t ms) // 解除IWDG保护 R32_IWDG_KR = 0x5555; - R32_IWDG_CFG = (RB_PR & (div << 12)) | (RB_STOP_EN) | (reload & RB_RLR); + // R32_IWDG_CFG = (RB_PR & (div << 12)) | (RB_STOP_EN) | (reload & RB_RLR); + R32_IWDG_CFG |= (7 << 12); // 32K分频512,时钟62.5Hz + R32_IWDG_CFG &= 0xFFFFF000; + R32_IWDG_CFG |= 0x7D * 3; // 看门狗时间约256ms(0x10/(32K/512)) + // 开启IWDG保护 R32_IWDG_KR = 0xCCCC; logDebug("R32_IWDG_CFG = %X", R32_IWDG_CFG); + + IWDGTaskId = TMOS_ProcessEventRegister(IWDG_ProcessEvent); + tmos_set_event(IWDGTaskId, IWIG_FEED_EVENT); } // #define RB_RESET_FLAG 0x07 // RO: recent reset flag @@ -107,3 +129,4 @@ void ShowRestartReason(void) +