From 6afed2f3493a00c3dcd0cb64e66969596a0356c1 Mon Sep 17 00:00:00 2001 From: zhji Date: Sun, 1 Jun 2025 16:18:31 +0800 Subject: [PATCH] [feat] add watchdog --- driver/CMakeLists.txt | 1 + driver/inc/reg/watchdog_reg.h | 29 +++++++++++++++++++++++++++++ driver/inc/watchdog.h | 19 +++++++++++++++++++ driver/src/watchdog.c | 6 ++++++ example/boot2/main.c | 3 ++- 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 driver/inc/reg/watchdog_reg.h create mode 100644 driver/inc/watchdog.h create mode 100644 driver/src/watchdog.c diff --git a/driver/CMakeLists.txt b/driver/CMakeLists.txt index 5abaed0..6716f89 100644 --- a/driver/CMakeLists.txt +++ b/driver/CMakeLists.txt @@ -12,6 +12,7 @@ src/timer.c src/dma.c src/uart.c src/usb.c +src/watchdog.c ) set(TARGET driver) diff --git a/driver/inc/reg/watchdog_reg.h b/driver/inc/reg/watchdog_reg.h new file mode 100644 index 0000000..a54e97c --- /dev/null +++ b/driver/inc/reg/watchdog_reg.h @@ -0,0 +1,29 @@ +#ifndef __HARDWARE_WATCHDOG_REG_H__ +#define __HARDWARE_WATCHDOG_REG_H__ + +#define WATCHDOG_TICK_CYCLES_POS (0U) +#define WATCHDOG_TICK_CYCLES_MASK (0x1FF << WATCHDOG_TICK_CYCLES_POS) +#define WATCHDOG_TICK_ENABLE (1 << 9U) +#define WATCHDOG_TICK_RUNNING (1 << 10U) +#define WATCHDOG_TICK_COUNT_POS (11U) +#define WATCHDOG_TICK_COUNT_MASK (0x1FF << WATCHDOG_TICK_COUNT_POS) + +typedef struct { + io_rw_32 ctrl; + io_wo_32 load; + io_ro_32 reason; + io_rw_32 scratch[8]; + io_rw_32 tick; +} watchdog_hw_t; + +#define watchdog_hw ((watchdog_hw_t *const)WATCHDOG_BASE) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __HARDWARE_WATCHDOG_REG_H__ */ diff --git a/driver/inc/watchdog.h b/driver/inc/watchdog.h new file mode 100644 index 0000000..da7b2b9 --- /dev/null +++ b/driver/inc/watchdog.h @@ -0,0 +1,19 @@ +#ifndef __HARDWARE_WATCHDOG_H__ +#define __HARDWARE_WATCHDOG_H__ + +#include "reg.h" +#include "watchdog_reg.h" + + + +#ifdef __cplusplus +extern "C" { +#endif + +void watchdog_start_tick(uint16_t cycles); + +#ifdef __cplusplus +} +#endif + +#endif /* __HARDWARE_WATCHDOG_H__ */ diff --git a/driver/src/watchdog.c b/driver/src/watchdog.c new file mode 100644 index 0000000..930b6f6 --- /dev/null +++ b/driver/src/watchdog.c @@ -0,0 +1,6 @@ +#include "watchdog.h" + +void watchdog_start_tick(uint16_t cycles) +{ + watchdog_hw->tick = (cycles << WATCHDOG_TICK_CYCLES_POS) | WATCHDOG_TICK_ENABLE; +} diff --git a/example/boot2/main.c b/example/boot2/main.c index f7603b7..343bdf5 100644 --- a/example/boot2/main.c +++ b/example/boot2/main.c @@ -56,6 +56,7 @@ void __attribute__((section(".text.boot2_pre"))) boot2_copy_self(void) #include "clock.h" #include "uart.h" #include "flash.h" +#include "watchdog.h" #include "timer.h" #include "stdio.h" @@ -224,7 +225,7 @@ int main(void) gpio_init(0, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_DRIVE_4MA); /* UART_TX pin */ gpio_init(1, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_SCHMITT | GPIO_PAD_IE | GPIO_PAD_OD); /* UART_RX pin */ uart_init(UART_ID_0, &uart_cfg); - *(volatile uint32_t *)(WATCHDOG_BASE + 0x2C) = ((1 << 9) | (12 << 0)); + watchdog_start_tick(12); /* 12MHz / 12 = 1MHz, as 1us */ timer_start(); gpio_init(2, GPIO_FUNC_NULL | GPIO_PULL_DOWN | GPIO_SCHMITT | GPIO_PAD_IE | GPIO_PAD_OD); /* boot pin */