Watch Dog Timer

该示例通过 GPIO 中断触发喂狗操作重启看门狗定时器。若在定时时间内未进行喂狗操作,看门狗定时器将重启IC。

环境需求

该示例支持以下开发套件:

开发套件

Hardware Platforms

Board Name

RTL87x2G HDK

RTL87x2G EVB

更多信息请参考 快速入门

配置选项

该示例可配置的宏如下:

  1. USE_WDT_SELECT :选择使用WDT或AON_WDT。

硬件连线

连接P4_0和外部输入信号。

编译和下载

该示例的工程路径如下:

Project file: samples\peripheral\wdt\wdt\proj\rtl87x2g\mdk

Project file: samples\peripheral\wdt\wdt\proj\rtl87x2g\gcc

请按照以下步骤操作构建并运行该示例:

  1. 打开工程文件。

  2. 按照 快速入门编译APP Image 给出的步骤构建目标文件。

  3. 编译成功后,在路径 mdk\bingcc\bin 下会生成 app bin app_MP_xxx.bin 文件。

  4. 按照 快速入门MPTool 给出的步骤将app bin烧录至EVB内。

  5. 按下复位按键,开始运行。

测试验证

  1. 当EVB启动后,在DebugAnalyzer工具内观察如下LOG。

    Start wdt test!
    
  2. 看门狗定时时间为10s。若在10s内控制外部输入信号从高电平变为低电平,则触发GPIO中断并进行喂狗操作,打印LOG。

    [APP]  !**[app] app_handle_io_msg: feeding dog, restart wdg timer.
    
  3. 若在10s内没有进行喂狗操作,则IC会自动重启。

代码介绍

该章节分为以下几个部分:

  1. 源码路径

  2. 初始化函数将在 初始化 章节介绍。

  3. 初始化后的功能实现将在 功能实现 章节介绍。

源码路径

  • 工程路径: sdk\samples\peripheral\wdt\wdt\proj

  • 源码路径: sdk\samples\peripheral\wdt\wdt\src

该工程的工程文件代码结构如下:

└── Project: wdt
    └── secure_only_app
        └── Device                   includes startup code
            ├── startup_rtl.c
            └── system_rtl.c
        ├── CMSIS                    includes CMSIS header files
        ├── CMSE Library             Non-secure callable lib
        ├── Lib                      includes all binary symbol files that user application is built on
            └── rtl87x2g_io.lib
        ├── Peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl_rcc.c
            ├── rtl_pinmux.c
            ├── rtl_nvic.c
            └── rtl_gpio.c
        └── APP                      includes the ble_peripheral user application implementation
            ├── main_ns.c
            ├── app.c
            ├── app_task.c
            ├── io_gpio.c
            └── io_wdt.c

初始化

初始化流程包括了 board_gpio_initdriver_gpio_initdriver_wdt_init

board_gpio_initdriver_gpio_init 具体详见 GPIO Input Interrupt 中的 GPIO初始化


driver_wdt_init 包含了对watch dog外设的初始化。

  1. 执行 WDT_Start() 函数,开启看门狗定时,设置定时时间为10 seconds,超时未喂狗时EVB RESET_ALL。

WDT_Start(10000, RESET_ALL);

功能实现

  1. 主函数中执行 os_sched_start ,开启任务调度。在 app_main_task 中,创建消息队列,执行 driver_init ,对GPIO和WDT外设进行初始化。

...
os_msg_queue_create(&io_queue_handle, "io queue", MAX_NUMBER_OF_IO_MESSAGE,  sizeof(uint32_t));
os_msg_queue_create(&evt_queue_handle, "evt queue", MAX_NUMBER_OF_EVENT_MESSAGE, sizeof(uint32_t));
      driver_init();
...
  1. 当P4_0检测到外部有下降沿信号输入时,进入中断服务处理函数 GPIO_Pin_Handler

    1. 关闭GPIO中断,屏蔽GPIO中断。

    2. 定义消息类型为IO_MSG_TYPE_GPIO,执行 app_send_msg_to_apptask ,发送消息至消息队列。

    3. 退出中断函数时清除中断标志位、取消屏蔽和重新使能中断。

/* Mask and disable interrupt */
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, ENABLE);

uint32_t send_val = IO_MSG_TYPE_GPIO;
if (false == app_send_msg_to_apptask(&send_val))
{
    APP_PRINT_ERROR0("[io_gpio] GPIO_Input_Handler: Send int_gpio_msg failed!");
    //Add user code here!
    GPIO_ClearINTPendingBit(GPIO_PORT, GPIO_PIN);
    return;
}

/* Clear int flag, unmask and enable interrupt */
GPIO_ClearINTPendingBit(GPIO_PORT, GPIO_PIN);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, ENABLE);
  1. app_main_task 中,循环执行 os_msg_recv 接收消息。接收到APP发来的消息后,执行 app_handle_io_msg 对消息进行处理,执行 wdt_feed 进行喂狗操作。