当前文档版本为 v0.9.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

Driver: WatchDog

1 功能概述

该sample演示了watchdog的功能。

2 环境要求

  • PAN1080 EVB 一块

  • USB-TypeC 线一条(用于供电和查看串口打印 Log)

  • 硬件接线:

    • 使用 USB 线,将 PC USB 与 EVB USB-TypeC(USB->UART)相连

    • 根据 EVB 核心板芯片的封装不同,使用杜邦线将 EVB 底板上的:

      • TX0 与 P00 相连, RX0 与 P01 相连(若 EVB 板芯片为 QFN32 或 LQFP64 封装)

      • TX0 与 P30 相连, RX0 与 P31 相连(若 EVB 板芯片为 QFN48 封装)

  • PC 软件: 串口调试助手(UartAssist)或终端工具(SecureCRT),波特率921600

3 编译和烧录

例程位置:zephyr\samples_panchip\drivers\watchdog

使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍

4 演示说明

  • sample在配置并启动watchdog后,每隔50ms进行一次喂狗操作,喂狗5次后,停止喂狗,这时系统会超时复位。

  • 程序正确执行时,日志界面如下(系统因复位会持续打印如下日志):

    sample_out

    Watchdog 例程Log

5 开发说明

5.1 启用watchdog模块

在prj.conf文件中添加“CONFIG_WATCHDOG=y”启用watchdog模块。

CONFIG_WATCHDOG=y

5.2 初始化watchdog

const struct device *wdt = device_get_binding("WDT");
if (!wdt) {
    printk("Cannot get WDT device\n");
    return;
}

5.3 watchdog配置

配置watchdog参数

配置的超时时间必须是宏定义中的值,其他值无效。时间计算公式为:例如宏定义WDT_TIMEOUT_MAX_2POW12,则超时时间为2 ^ 12 * WDT clocks。

struct wdt_timeout_cfg wdt_config = {
	/* Reset SoC when watchdog timer expires. */
	.flags = WDT_FLAG_RESET_SOC,
    /* WDT timeout interrupt callback function. */
	.wdt_config.callback = wdt_callback;
	/* Expire watchdog after max window, min must be set to 0. */
	.window.min = 0U,
	.window.max = WDT_MAX_WINDOW,
};

/**
 * @brief Install new timeout.
 *
 * This function must be used before wdt_setup(). Changes applied here
 * have no effects until wdt_setup() is called.
 *
 * @param dev Pointer to the device structure for the driver instance.
 * @param cfg Pointer to timeout configuration structure.
 *
 * @retval channel_id If successful, a non-negative value indicating the index
 *                    of the channel to which the timeout was assigned. This
 *                    value is supposed to be used as the parameter in calls to
 *                    wdt_feed().
 * @retval -EBUSY If timeout can not be installed while watchdog has already
 *		  been setup.
 * @retval -ENOMEM If no more timeouts can be installed.
 * @retval -ENOTSUP If any of the set flags is not supported.
 * @retval -EINVAL If any of the window timeout value is out of possible range.
 *		   This value is also returned if watchdog supports only one
 *		   timeout value for all timeouts and the supplied timeout
 *		   window differs from windows for alarms installed so far.
 */
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
if (err < 0) {
    printk("Watchdog install error\n");
    return;
}

5.4 创建watchdog

创建watchdog并开始运行

/**
 * @brief Set up watchdog instance.
 *
 * This function is used for configuring global watchdog settings that
 * affect all timeouts. It should be called after installing timeouts.
 * After successful return, all installed timeouts are valid and must be
 * serviced periodically by calling wdt_feed().
 *
 * @param dev Pointer to the device structure for the driver instance.
 * @param options Configuration options as defined by the WDT_OPT_* constants
 *
 * @retval 0 If successful.
 * @retval -ENOTSUP If any of the set options is not supported.
 * @retval -EBUSY If watchdog instance has been already setup.
 */
err = wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG);
if (err < 0) {
    printk("Watchdog setup error\n");
    return;
}

5.5 Feed watchdog

实现喂狗操作

/**
 * @brief Feed specified watchdog timeout.
 *
 * @param dev Pointer to the device structure for the driver instance.
 * @param channel_id Index of the fed channel.
 *
 * @retval 0 If successful.
 * @retval -EAGAIN If completing the feed operation would stall the
 *                 caller, for example due to an in-progress watchdog
 *                 operation such as a previous @c wdt_feed().
 * @retval -EINVAL If there is no installed timeout for supplied channel.
 */
wdt_feed(wdt, wdt_channel_id);

5.6 关闭watchdog

关闭watchdog功能

/**
 * @brief Disable watchdog instance.
 *
 * This function disables the watchdog instance and automatically uninstalls all
 * timeouts. To set up a new watchdog, install timeouts and call wdt_setup()
 * again. Not all watchdogs can be restarted after they are disabled.
 *
 * @param dev Pointer to the device structure for the driver instance.
 *
 * @retval 0 If successful.
 * @retval -EFAULT If watchdog instance is not enabled.
 * @retval -EPERM If watchdog can not be disabled directly by application code.
 */
wdt_disable(wdt);

6 RAM/Flash资源使用情况

Memory region         Used Size  Region Size  %age Used
FLASH:       28144 B       256 KB     10.74%
SRAM:       10712 B        64 KB     16.35%

7 WatchDog/WWDT全功能测试case

WatchDog全功能测试case及测试流程请参考文档:03_MCU/mcu_samples_doc/PAN1080 WDT 例程说明.pdf

WWDT全功能测试case及测试流程请参考文档:03_MCU/mcu_samples_doc/PAN1080 WWDT 例程说明.pdf