PSoc62™开发板之WDT应用

看门狗

看门狗定时器(WDT)是一种硬件定时器,在出现意外固件时自动复位设备执行路径。如果启用了WDT,则必须在固件中定期进行服务,以避免复位。否则,计时器失效并产生一个设备复位。此外,WDT可以用作中断源或在低功耗唤醒源模式。PSoC 6 MCU系列包括一个自由运行WDT和两个多计数器WDT (MCWDT)。WDT是16位的计数器。每个MCWDT有两个16位计数器和一个32位计数器。这样,看门狗系统共有7个计数器——5个16位和2个32位。所有16位计数器都可以产生看门狗设备复位。所有七个计数器都可以在匹配事件上生成中断

PSoc62™开发板之WDT应用_第1张图片

实验目的

WDT看门狗功能演示,模拟程序跑飞的情形(按键中断输入一个crash),看看系统能不能正常复位

组件配置

在RT-Thread Settings里边开启Watchdong Timer选项

PSoc62™开发板之WDT应用_第2张图片

按键中断的控制参考文章,这里需要使用按键来生成一个crash

程序设计

WDT初始化

int init_wdt(void)
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t timeout = 100;

    wdg_dev = rt_device_find(WDT_DEV);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", WDT_DEV);
        return RT_ERROR;
    }

     rt_device_init(wdg_dev);

    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", WDT_DEV);
        return RT_ERROR;
    }

    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
    if (ret != RT_EOK)
    {
        rt_kprintf("start %s failed!\n", WDT_DEV);
        return -RT_ERROR;
    }

    rt_thread_idle_sethook(idle_hook);

    return ret;
}

WDT定期喂狗

static rt_device_t wdg_dev;

static void idle_hook(void)
{
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
}

按键GPIO初始化

int main(void)
{
    rt_pin_mode(USER_KEY, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(USER_KEY, PIN_IRQ_MODE_RISING_FALLING, irq_callback, RT_NULL);
    rt_pin_irq_enable(USER_KEY, PIN_IRQ_ENABLE);
    return 0;
}

编写中断异常,当按键按下时程序会触发空指针写入异常进行崩溃

// write a bug to crash
void irq_callback()
{
    rt_kprintf("To be crashed!\n");

    char *p = NULL;
    *p = 1080;
}

整合程序,这里加入了LED交替亮灭来反馈系统是否处于运行状态

#include 
#include 
#include "drv_gpio.h"

#define WDT_DEV    "wdt"
#define LED_PIN     GET_PIN(0, 1)
#define USER_KEY    GET_PIN(6, 2)

static rt_device_t wdg_dev;

static void idle_hook(void)
{
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
}

int init_wdt(void)
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t timeout = 100;

    wdg_dev = rt_device_find(WDT_DEV);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", WDT_DEV);
        return RT_ERROR;
    }

     rt_device_init(wdg_dev);

    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", WDT_DEV);
        return RT_ERROR;
    }

    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
    if (ret != RT_EOK)
    {
        rt_kprintf("start %s failed!\n", WDT_DEV);
        return -RT_ERROR;
    }

    rt_thread_idle_sethook(idle_hook);

    return ret;
}

// write a bug to crash
void irq_callback()
{
    rt_kprintf("To be crashed!\n");

    char *p = NULL;
    *p = 1080;
}

int main(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(USER_KEY, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(USER_KEY, PIN_IRQ_MODE_RISING_FALLING, irq_callback, RT_NULL);
    rt_pin_irq_enable(USER_KEY, PIN_IRQ_ENABLE);

    init_wdt();

    for (;;)
    {
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }

    return 0;
}

注意事项

  • RT-Thread Studio出现warning: tidle0 stack is close to end of stack address.的解决办法:修改工程文件rtconfig.h中IDLE_THREAD_STACK_SIZE的值为512
  • 不能使用debug模式运行,否则看门狗将失效

实验效果

按键触发程序崩溃,LED灯灭掉,系统停止喂狗,重新复位,复位后的LED灯又在交替亮灭

PSoc62™开发板之WDT应用

总结

看门狗的应用非常多,当程序无缘无故跑飞的时候,它可以抓住时机将系统复位,避免嵌入式系统宕机带来的损失,但不要过于依赖看门狗,该解决的问题总是要解决!

你可能感兴趣的:(PSoc™62,Infineon,RT-Thread,WDT,PSoc™62)