HC32F460独立看门狗的使用

使用背景

为了防止程序跑飞,MCU特意引入了看门狗,看门狗分为窗口看门狗和独立看门狗,窗口看门狗采用中断实现,其时间更加的精确,独立看门狗大约会在附近1S左右没有喂狗就产生看门狗复位。

具体代码实现

#include "bsp_wdt.h"
#include "hc32_ddl.h"

//复位类型
#define RST_POWER_ON    1
#define RST_EXTERNAL    2
#define RST_BROWN_OUT   4
#define RST_WATCHDOG    8
#define RST_JTAG       16
#define RST_SOFTWARE   32
#define RST_BACKUP     64

typedef enum {
    RST_CAU_POWER_ON    = 0x01,
    RST_CAU_EXTERNAL    = 0x02,
    RST_CAU_BROWN_OUT   = 0x04,
    RST_CAU_WATCHDOG    = 0x08,
    RST_CAU_JTAG        = 0x10,
    RST_CAU_SOFTWARE    = 0x20,
    RST_CAU_BACKUP      = 0x40,
} rst_cause_t;


// pclk = system_clock/div4 = 50M
// max cycle = 65536
// max feed interval = 65536 / (50000000/8192) = 10.7s
void bsp_wdt_init(void)
{
    stc_wdt_init_t stcWdtInit;

    /* configure structure initialization */
    MEM_ZERO_STRUCT(stcWdtInit);

    stcWdtInit.enClkDiv = WdtPclk3Div8192;
    stcWdtInit.enCountCycle = WdtCountCycle65536;
    stcWdtInit.enRefreshRange = WdtRefresh100Pct;
    stcWdtInit.enSleepModeCountEn = Disable;
    stcWdtInit.enRequestType = WdtTriggerResetRequest;
    WDT_Init(&stcWdtInit);
}

void bsp_wdt_refresh(void)
{
    en_result_t enRet = Error;
    enRet = WDT_RefreshCounter();

    if(enRet != Ok) {
        printf("Failed at function: %s, line: %d\n", __FUNCTION__, __LINE__);
    }
}

//获取复位类型
uint8_t rmu_get_reset_flag(void)
{
    uint8_t res;

    stc_rmu_rstcause_t stcRmuRstCause;

    MEM_ZERO_STRUCT(stcRmuRstCause);

    RMU_GetResetCause(&stcRmuRstCause);

    if(Set == stcRmuRstCause.enSoftware) {
        res = RST_CAU_SOFTWARE;
    } else if(Set == stcRmuRstCause.enWdt) {
        res = RST_CAU_WATCHDOG;
    } else if(Set == stcRmuRstCause.enRstPin) {
        res = RST_CAU_EXTERNAL;
    }

    return res;
}

//清除复位标志
void rmu_clear_reset_flag(void)
{
    RMU_ClrResetFlag();
}

int main()
{

while(1)
{
  const byte mcuFlag = rmu_get_reset_flag();
  if (mcuFlag & RST_POWER_ON) 
  	printf("RST_POWER_ON");
  if (mcuFlag & RST_EXTERNAL) 
  	printf("RST_POWER_ON");
  if (mcuFlag & RST_BROWN_OUT)
   	printf("RST_POWER_ON");
  if (mcuFlag & RST_WATCHDOG) 
  	printf("RST_POWER_ON");
  if (mcuFlag & RST_SOFTWARE)
   	printf("RST_POWER_ON");
  rmu_clear_reset_flag();
  }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路过的小熊~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值