看门狗 ok

This commit is contained in:
stark1898y 2025-05-23 18:50:49 +08:00
parent 17b751f331
commit e1d8ff1373
7 changed files with 111 additions and 61 deletions

View File

@ -208,7 +208,7 @@
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.printmap.751686263" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.printmap" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnano.239404511" name="Use newlib-nano (--specs=nano.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnano" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys.351964161" name="Do not use syscalls (--specs=nosys.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.useprintffloat.695795083" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.useprintffloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.useprintffloat.695795083" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.useprintffloat" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usescanffloat.1839373535" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usescanffloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.verbose.1444336626" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.verbose" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.printfloat.2044235126" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.printfloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
@ -249,7 +249,7 @@
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.printmap.2073713641" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.printmap" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usenewlibnano.1540675679" name="Use newlib-nano (--specs=nano.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usenewlibnano" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usenewlibnosys.561457319" name="Do not use syscalls (--specs=nosys.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usenewlibnosys" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.useprintffloat.1497004994" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.useprintffloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.useprintffloat.1497004994" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.useprintffloat" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usescanffloat.881728961" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.usescanffloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.verbose.922041698" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.verbose" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.printfloat.476377985" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.cpp.linker.printfloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>

View File

@ -5,12 +5,12 @@
"type": "mrs-debugger",
"request": "launch",
"name": "IoT_SCV_CH584M",
"cwd": "c:\\Users\\123\\Desktop\\自闭阀项目\\V1.1\\iot_-scv_-ch584-m",
"cwd": "f:\\BaiduSyncdisk\\SXDT\\IoT_SCV\\0V2-0\\Firmware\\IoT_SCV_CH584M",
"openOCDCfg": {
"useLocalOpenOCD": true,
"executable": "d:/MounRiverV2/MounRiver_Studio2/resources/app/resources/win32/components/WCH/OpenOCD/OpenOCD/bin/openocd.exe",
"executable": "c:/MounRiver/MounRiver_Studio2/resources/app/resources/win32/components/WCH/OpenOCD/OpenOCD/bin/openocd.exe",
"configOptions": [
"-f \"d:/MounRiverV2/MounRiver_Studio2/resources/app/resources/win32/components/WCH/OpenOCD/OpenOCD/bin/wch-riscv.cfg\" -c \"chip_id CH32L10x\""
"-f \"c:/MounRiver/MounRiver_Studio2/resources/app/resources/win32/components/WCH/OpenOCD/OpenOCD/bin/wch-riscv.cfg\" -c \"chip_id CH32L10x\""
],
"gdbport": 3333,
"telnetport": 4444,
@ -22,7 +22,7 @@
"enableNoZeroWaitingAreaFlash": false
},
"gdbCfg": {
"executable": "d:/MounRiverV2/MounRiver_Studio2/resources/app/resources/win32/components/WCH/Toolchain/RISC-V Embedded GCC12/bin/riscv-wch-elf-gdb.exe",
"executable": "c:/MounRiver/MounRiver_Studio2/resources/app/resources/win32/components/WCH/Toolchain/RISC-V Embedded GCC12/bin/riscv-wch-elf-gdb.exe",
"commands": [
"set mem inaccessible-by-default off",
"set architecture riscv:rv32",
@ -39,8 +39,8 @@
"additionalCommands": []
},
"loadedFiles": {
"executableFile": "c:\\Users\\123\\Desktop\\自闭阀项目\\V1.1\\iot_-scv_-ch584-m\\obj\\IoT_SCV_CH584M.elf",
"symbolFile": "c:\\Users\\123\\Desktop\\自闭阀项目\\V1.1\\iot_-scv_-ch584-m\\obj\\IoT_SCV_CH584M.elf",
"executableFile": "f:\\BaiduSyncdisk\\SXDT\\IoT_SCV\\0V2-0\\Firmware\\IoT_SCV_CH584M\\obj\\IoT_SCV_CH584M.elf",
"symbolFile": "f:\\BaiduSyncdisk\\SXDT\\IoT_SCV\\0V2-0\\Firmware\\IoT_SCV_CH584M\\obj\\IoT_SCV_CH584M.elf",
"executableFileOffset": 0,
"symbolFileOffset": 0
},

View File

@ -90,7 +90,7 @@ int main(void)
// BSP_Ml307r_Init();
// logDebug("BSP_M1307r ok\n");
IWDG_Init(5000);
IWDG_Init(IWDG_TIMEOUT_MS, IWDG_FEED_MS);
BSP_PRESS_Init();

View File

@ -266,7 +266,7 @@
"cross_reference": false,
"print_link_map": false,
"use_newlib_nano": false,
"use_float_with_nano_printf": false,
"use_float_with_nano_printf": true,
"use_float_with_nano_scanf": false,
"do_not_use_syscalls": true,
"verbose": false,
@ -309,7 +309,7 @@
"cross_reference": false,
"print_link_map": false,
"use_newlib_nano": false,
"use_float_with_nano_printf": false,
"use_float_with_nano_printf": true,
"use_float_with_nano_scanf": false,
"do_not_use_syscalls": true,
"verbose": false,

View File

@ -1,9 +1,9 @@
/*
* @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
* @LastEditors : stark1898y 1658608470@qq.com
* @LastEditTime : 2025-05-23 18:38:52
* @FilePath : \IoT_SCV_CH584M\bsp\inc\bsp_iwdg.h
* @Description :
*
* Copyright (c) 2024 by yzy, All Rights Reserved.
@ -14,18 +14,18 @@
#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
#define IWDG_TIMEOUT_MS 6000
#define IWDG_FEED_MS 3000
void IWDG_Init(uint16_t ms);
#define IWIG_FEED_EVENT (0x01 << 0)
// 需要定时调用喂狗
// 在看门狗键寄存器 (R32_IWDG_KR) 中,软件要以一定的间隔写入 0xAAAA重装载计数值这就是喂狗的操作。
// 否则,当计数器为 0 时,看门狗会产生复位。
#define FEED_IWDG() {R32_IWDG_KR=0xAAAA;}
void IWDG_Init(uint16_t timeout_ms, uint16_t feed_ms);
void ShowRestartReason(void);
#endif // !__BSP_IWDG_H__

View File

@ -1,9 +1,9 @@
/*
* @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
* @LastEditors : stark1898y 1658608470@qq.com
* @LastEditTime : 2025-05-23 18:50:01
* @FilePath : \IoT_SCV_CH584M\bsp\src\bsp_iwdg.c
* @Description :
*
* Copyright (c) 2024 by yzy, All Rights Reserved.
@ -17,46 +17,96 @@
#undef LOG_ENABLE
#define LOG_ENABLE 1
#define LOG_LVL LOG_DEBUG
#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 _timeout_ms, _feed_ms;
tmosTaskID iwdg_task_id = INVALID_TASK_ID;
uint16_t IWDG_ProcessEvent(uint8_t task_id, uint16_t events)
{
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);
if (events & IWIG_FEED_EVENT)
{
FEED_IWDG();
// logDebug("IWDG feed");
// 获取tmos系统堆的剩余内存大小
// uint8_t* a = tmos_msg_allocate(1);
logDebug("tmos_memory_getlen = %d" , tmos_memory_getlen());
tmos_start_task(task_id, IWIG_FEED_EVENT, MS1_TO_SYSTEM_TIME(_feed_ms));
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
/*********************************************************************
* @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
*/
// 独立看门狗IWDG由专用的内部低速时钟LSI驱动能够在低功耗模式下正常工作。
// 根据寄存器描述IWDG最大时间可以是(0xFFF/(32K/512))=65.52s
void IWDG_Init(uint16_t timeout_ms, uint16_t feed_ms)
{
uint16_t div = 512;
// float tick_ms = 1000 / (32000 / 512);
uint8_t tick_ms = 16;
uint16_t reload = (timeout_ms / tick_ms) + 1;
// 解除IWDG保护
R32_IWDG_KR = 0x5555;
R32_IWDG_CFG |= (7 << 12); // 32K分频512时钟62.5Hz
R32_IWDG_CFG &= 0xFFFFF000;
// R32_IWDG_CFG |= 0x7D * 3; // 看门狗时间约6 s0x10/(32K/512))
R32_IWDG_CFG |= reload;
// 开启IWDG保护
R32_IWDG_KR = 0xCCCC;
// uint32_t d = R32_IWDG_CFG;
// logHexDumpAll(d, 4);
// TODO:这里一算就出问题
float a = (((float)reload * 1000) / 62.5);
_timeout_ms = (uint16_t)(a);
_feed_ms = feed_ms;
logDebug("timeout_ms: %d, feed_ms: %d", _timeout_ms, _feed_ms);
// sys_safe_access_enable();
// R8_SAFE_LRST_CTRL |= RB_IWDG_RST_EN;
// sys_safe_access_disable();
iwdg_task_id = TMOS_ProcessEventRegister(IWDG_ProcessEvent);
if (iwdg_task_id == INVALID_TASK_ID)
{
logError("IWDG task register failed");
return;
}
// tmos_set_event(iwdg_task_id, IWIG_FEED_EVENT);
tmos_start_task(iwdg_task_id, IWIG_FEED_EVENT, MS1_TO_SYSTEM_TIME(_feed_ms));
logInfo("iwdg_task_id = %d", iwdg_task_id);
}
#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
// 打印复位状态寄存器,显示复位原因
// 最近一次复位状态:
// 000软件复位 SRRB_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;