bsp: k230: add watchdog support
pkgs_test / change (push) Has been skipped
AutoTestCI / components/cpp11 (push) Has been cancelled
AutoTestCI / kernel/atomic (push) Has been cancelled
AutoTestCI / kernel/atomic/riscv64 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11/riscv64 (push) Has been cancelled
AutoTestCI / kernel/device (push) Has been cancelled
AutoTestCI / kernel/ipc (push) Has been cancelled
AutoTestCI / kernel/irq (push) Has been cancelled
AutoTestCI / kernel/mem (push) Has been cancelled
AutoTestCI / kernel/mem/riscv64 (push) Has been cancelled
AutoTestCI / kernel/thread (push) Has been cancelled
AutoTestCI / kernel/timer (push) Has been cancelled
AutoTestCI / rtsmart/aarch64 (push) Has been cancelled
AutoTestCI / rtsmart/arm (push) Has been cancelled
AutoTestCI / rtsmart/riscv64 (push) Has been cancelled
AutoTestCI / components/utest (push) Has been cancelled
RT-Thread BSP Static Build Check / 🔍 Summary of Git Diff Changes (push) Has been cancelled
RT-Thread BSP Static Build Check / ${{ matrix.legs.RTT_BSP }} (push) Has been cancelled
RT-Thread BSP Static Build Check / collect-artifacts (push) Has been cancelled
utest_auto_run / AARCH64-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / A9-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64 :default.cfg (push) Has been cancelled
utest_auto_run / A9 :default.cfg (push) Has been cancelled
utest_auto_run / A9-smp :default.cfg (push) Has been cancelled
utest_auto_run / RISCV :default.cfg (push) Has been cancelled
ToolsCI / Tools (push) Has been cancelled

K230 has two watchdog timers.

Add unit tests based on utest framework.

Watchdog and test are both configuable, disabled by default.

Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
This commit is contained in:
Chen Wang
2025-05-28 13:49:49 +08:00
committed by Rbb666
parent 8f8354347e
commit 951bc922bc
7 changed files with 571 additions and 0 deletions
+2
View File
@@ -1497,6 +1497,8 @@ CONFIG_BSP_USING_SDIO0=y
# CONFIG_BSP_SDIO0_1V8 is not set
# CONFIG_BSP_USING_SDIO1 is not set
CONFIG_BSP_SD_MNT_DEVNAME="sd0p1"
# CONFIG_BSP_USING_WDT is not set
# CONFIG_BSP_UTEST_DRIVERS is not set
# end of Drivers Configuration
CONFIG_BOARD_fpgac908=y
+21
View File
@@ -39,4 +39,25 @@ menu "Drivers Configuration"
default "sd0p1"
endif
menuconfig BSP_USING_WDT
bool "Enable Watchdog Timer"
select RT_USING_WDT
default n
if BSP_USING_WDT
config BSP_USING_WDT0
bool "Enable WDT0"
default n
config BSP_USING_WDT1
bool "Enable WDT1"
default n
endif
config BSP_UTEST_DRIVERS
bool "Enable drivers utest"
select RT_USING_UTEST
default n
endmenu
+19
View File
@@ -0,0 +1,19 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('WDT', src, depend = ['BSP_USING_WDT'], CPPPATH = CPPPATH)
objs = [group]
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
objs = objs + SConscript(os.path.join(item, 'SConscript'))
Return('objs')
File diff suppressed because it is too large Load Diff
+73
View File
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef DRV_WDT_H_
#define DRV_WDT_H_
#include <drivers/dev_watchdog.h>
#include <sys/ioctl.h>
#define DEFUALT_TIMEOUT 30*1000 /* ms */
#define KD_WDT_NUM_TOPS 16
#define KD_WDT_FIX_TOP(_idx) (1U << (16 + _idx))
#define KD_WDT_RMOD_RESET 1
#define KD_WDT_RMOD_IRQ 2
#define KD_DEVICE_CTRL_WDT_GET_TIMEOUT _IOW('W', 1, int) /* get timeout(in seconds) */
#define KD_DEVICE_CTRL_WDT_SET_TIMEOUT _IOW('W', 2, int) /* set timeout(in seconds) */
#define KD_DEVICE_CTRL_WDT_GET_TIMELEFT _IOW('W', 3, int) /* get the left time before reboot(in seconds) */
#define KD_DEVICE_CTRL_WDT_KEEPALIVE _IOW('W', 4, int) /* refresh watchdog */
#define KD_DEVICE_CTRL_WDT_START _IOW('W', 5, int) /* start watchdog */
#define KD_DEVICE_CTRL_WDT_STOP _IOW('W', 6, int) /* stop watchdog */
struct k230_wdt_timeout {
rt_uint32_t top_val;
rt_uint32_t sec;
rt_uint32_t msec;
};
#define swap(a, b) \
do { struct k230_wdt_timeout __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
/* wdt register map */
typedef struct _wdt
{
/* WDT Control Register (0x00) */
volatile uint32_t cr;
/* WDT Timeout Range Register (0x04) */
volatile uint32_t torr;
/* WDT Current Counter Value Register (0x08) */
volatile uint32_t ccvr;
/* WDT Counter Restart Register (0x0c) */
volatile uint32_t crr;
/* WDT Interrupt Status Register (0x10) */
volatile uint32_t stat;
/* WDT Interrupt Clear Register (0x14) */
volatile uint32_t eoi;
/* reserverd (0x18) */
volatile uint32_t resv1;
/* WDT Protection level Register (0x1c) */
volatile uint32_t prot_level;
/* reserved (0x20-0xe0) */
volatile uint32_t resv4[49];
/* WDT Component Parameters Register 5 (0xe4) */
volatile uint32_t comp_param_5;
/* WDT Component Parameters Register 4 (0xe8) */
volatile uint32_t comp_param_4;
/* WDT Component Parameters Register 3 (0xec) */
volatile uint32_t comp_param_3;
/* WDT Component Parameters Register 2 (0xf0) */
volatile uint32_t comp_param_2;
/* WDT Component Parameters Register 1 (0xf4) */
volatile uint32_t comp_param_1;
/* WDT Component Version Register (0xf8) */
volatile uint32_t comp_version;
/* WDT Component Type Register (0xfc) */
volatile uint32_t comp_type;
} __attribute__((packed, aligned(4))) k230_wdt_t;
#endif /* DRV_WDT_H_ */
+12
View File
@@ -0,0 +1,12 @@
from building import *
src = []
if GetDepend('RT_UTEST_USING_ALL_CASES') or GetDepend('BSP_UTEST_DRIVERS'):
if GetDepend('BSP_USING_WDT'):
src += ['test_wdt.c']
group = DefineGroup('utestcases', src, depend = [''])
Return('group')
+170
View File
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Refer to https://www.rt-thread.org/document/api/iwdg_sample_8c-example.html
* This is a watchdog device test routine, the program searches for the watchdog
* device by device name, then initializes the device and sets the watchdog
* device overflow time.
*
* "test_wdt_feed" tests that the watchdog timer will not time out when the
* watchdog is fed and will not cause the system to restart.
*
* "test_wdt_nofeed" tests that the watchdog timer will time out when the
* watchdog is not fed and cause the system to restart.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "../interdrv/wdt/drv_wdt.h"
#include "utest.h"
/* Default watchdog device name */
/* If WDT0 and WDT1 both enabled, we use WDT0 */
#ifdef BSP_USING_WDT0
#define IWDG_DEVICE_NAME "wdt0"
#elif defined(BSP_USING_WDT1)
#define IWDG_DEVICE_NAME "wdt1"
#else
#error "No watchdog device defined!"
#endif
/* Watchdog device handle */
static rt_device_t wdg_dev;
/*
* Dog feeding flag, 1 means feeding the dog, 0 means not feeding the dog.
* This flag is used in the idle thread callback function.
* If the dog is not fed, the system will restart due to watchdog timeout.
*/
static int flag_feed;
/* Overflow time, in seconds. */
static rt_uint32_t timeout = 3;
static void idle_hook(void)
{
/* Feed the dog in the callback function of the idle thread */
if (flag_feed)
rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
/* Do not print, otherwise the screen will display too much. */
}
static void waiting_for_timeout(int seconds)
{
LOG_I("Waiting for watchdog timer time-out...\n");
for (int i = 0; i < seconds; i++)
{
rt_thread_mdelay(1000);
LOG_I(".");
}
}
static void test_wdt_feed(void)
{
rt_err_t ret = RT_EOK;
/* Find the watchdog device according to the name and obtain the handle */
wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
if (!wdg_dev)
{
LOG_E("find %s failed!\n", IWDG_DEVICE_NAME);
return;
}
/* Initialize the device */
ret = rt_device_init(wdg_dev);
if (ret != RT_EOK)
{
LOG_E("initialize %s failed!\n", IWDG_DEVICE_NAME);
return;
}
/* Set the watchdog timeout time */
ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
if (ret != RT_EOK)
{
LOG_E("set %s timeout failed!\n", IWDG_DEVICE_NAME);
return;
}
flag_feed = 1; /* Set feed the dog sign */
/* start watchdog timer */
rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
/* Delay enough time to see if the system will be restarted by the watchdog */
waiting_for_timeout(10);
LOG_I("Thanks for feeding me, I'm still alive!\n");
/* If you feed the dog successfully, you will have a chance to come here and close the gate dog. */
rt_device_close(wdg_dev);
LOG_I("Watchdog is closed!\n");
LOG_I("Test Done with Feed!\n");
return;
}
static void test_wdt_nofeed(void)
{
rt_err_t ret = RT_EOK;
/* Find the watchdog device according to the name and obtain the handle */
wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
if (!wdg_dev)
{
LOG_E("find %s failed!\n", IWDG_DEVICE_NAME);
return;
}
/* Initialize the device */
ret = rt_device_init(wdg_dev);
if (ret != RT_EOK)
{
LOG_E("initialize %s failed!\n", IWDG_DEVICE_NAME);
return;
}
/* Set the watchdog timeout time */
ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
if (ret != RT_EOK)
{
LOG_E("set %s timeout failed!\n", IWDG_DEVICE_NAME);
return;
}
flag_feed = 0; /* Do not feed the dog */
rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
/* Delay long enough and the system should reboot due to watchdog timeout. */
LOG_I("Oops, I am so hungary and will be killed in seconds!\n");
waiting_for_timeout(10);
LOG_I("SHOULD NOT SEE THIS PRINT!\n");
return;
}
static void test_wdt (void)
{
UTEST_UNIT_RUN(test_wdt_feed);
UTEST_UNIT_RUN(test_wdt_nofeed);
}
static rt_err_t utest_init(void)
{
flag_feed = 0;
rt_thread_idle_sethook(idle_hook);
return RT_EOK;
}
UTEST_TC_EXPORT(test_wdt, "wdt", utest_init, NULL, 10);