From e1d8ff137327a5fd78935582b3c22254295babf2 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Fri, 23 May 2025 18:50:49 +0800
Subject: [PATCH] =?UTF-8?q?=E7=9C=8B=E9=97=A8=E7=8B=97=20ok?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.cproject | 4 +-
...workspace => IoT_SCV_CH584M.mrs-workspace} | 2 +-
.mrs/launch.json | 14 +-
APP/peripheral_main.c | 2 +-
IoT_SCV_CH584M.wvproj | 4 +-
bsp/inc/bsp_iwdg.h | 22 ++--
bsp/src/bsp_iwdg.c | 124 ++++++++++++------
7 files changed, 111 insertions(+), 61 deletions(-)
rename .mrs/{IoT_SCV_CH584M1.mrs-workspace => IoT_SCV_CH584M.mrs-workspace} (99%)
diff --git a/.cproject b/.cproject
index 6a6a3aa..e8f2486 100644
--- a/.cproject
+++ b/.cproject
@@ -208,7 +208,7 @@
-
+
@@ -249,7 +249,7 @@
-
+
diff --git a/.mrs/IoT_SCV_CH584M1.mrs-workspace b/.mrs/IoT_SCV_CH584M.mrs-workspace
similarity index 99%
rename from .mrs/IoT_SCV_CH584M1.mrs-workspace
rename to .mrs/IoT_SCV_CH584M.mrs-workspace
index b324f10..1837e1d 100644
--- a/.mrs/IoT_SCV_CH584M1.mrs-workspace
+++ b/.mrs/IoT_SCV_CH584M.mrs-workspace
@@ -23,4 +23,4 @@
"*.h++": "cpp"
}
}
-}
+}
\ No newline at end of file
diff --git a/.mrs/launch.json b/.mrs/launch.json
index 44a88e0..4c8a465 100644
--- a/.mrs/launch.json
+++ b/.mrs/launch.json
@@ -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
},
@@ -62,4 +62,4 @@
}
}
]
-}
+}
\ No newline at end of file
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index eccbf9c..0508509 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -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();
diff --git a/IoT_SCV_CH584M.wvproj b/IoT_SCV_CH584M.wvproj
index 44f0892..cea8999 100644
--- a/IoT_SCV_CH584M.wvproj
+++ b/IoT_SCV_CH584M.wvproj
@@ -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,
diff --git a/bsp/inc/bsp_iwdg.h b/bsp/inc/bsp_iwdg.h
index 8cb341a..0fd4405 100644
--- a/bsp/inc/bsp_iwdg.h
+++ b/bsp/inc/bsp_iwdg.h
@@ -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__
diff --git a/bsp/src/bsp_iwdg.c b/bsp/src/bsp_iwdg.c
index 49b24fe..a6e753f 100644
--- a/bsp/src/bsp_iwdg.c
+++ b/bsp/src/bsp_iwdg.c
@@ -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 独立看门狗(IWDG)由专用的内部低速时钟(LSI)驱动,能够在低功耗模式下正常工作。
- *
- * @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 _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:软件复位 SR(RB_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 s(0x10/(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:软件复位 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;
@@ -92,4 +142,4 @@ void ShowRestartReason(void)
default:
break;
}
-}
\ No newline at end of file
+}