RA4M2的WDT是一个14位递减计数器,可用于在计数器下溢时复位MCU,因为系统已失控且无法刷新WDT。此外,WDT可用于产生不可屏蔽中断或下溢中断。
![WDT.png](//file.elecfans.com/web2/M00/84/5B/pYYBAGObLO2AYtkmAAEeZ6kmCFg427.png)
The Watchdog Timer (WDT) is a 14-bit down counter that can be used to reset the MCU when the counter underflows because the system has run out of control and is unable to refresh the WDT. In addition, the WDT can be used to generate a non-maskable interrupt or an underflow interrupt.
首先FSP配置USART和WactchDog:
![WDT0.png](//file.elecfans.com/web2/M00/84/5B/pYYBAGObLDiASrdBAACt_xBcgTY138.png)
配置USART9用于log输出,printf代码如下:
void hal_uart9_init()
{
R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
}
int fputc(int ch, FILE* f)
{
(void) f;
uart_tx_done = false;
R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
while (uart_tx_done == false);
return ch;
}
void user_uart9_callback(uart_callback_args_t* p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
break;
case UART_EVENT_TX_COMPLETE:
uart_tx_done = true;
break;
default:
break;
}
}
![WDT1.png](//file.elecfans.com/web2/M00/84/5B/pYYBAGObLKyAQ-zmAACxOeqmwFI861.png)
DEBUG部分记得设置SWD,不要用JTAG,否则会跟USART9的PIN冲突。
![WDT2.png](//file.elecfans.com/web2/M00/83/D2/poYBAGObLTuAFbEuAADf_mTIl6w949.png)
最后点击生成代码:
![WDT3.png](//file.elecfans.com/web2/M00/84/5B/pYYBAGObLYmAFkEsAABCIws2VUI785.png)
后续就是在hal_entry()中使用r_wdt_nmi_internal_callback() 和R_WDT_CallbackSet() 加入打印log的回调函数:
printf("feed the dog!\n ");
最后编译烧录:
compiling common_data.c...
compiling r_wdt.c...
compiling r_ioport.c...
compiling hal_data.c...
compiling main.c...
compiling pin_data.c...
compiling vector_data.c...
compiling r_sci_uart.c...
linking...
Program Size: Code=8712 RO-data=2392 RW-data=16 ZI-data=4216
FromELF: creating hex file...
".\Objects\wdt_ra4m2.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:03
跑起来……
![AAAA.png](//file.elecfans.com/web2/M00/83/D2/poYBAGObL7uAVWJwAAAXoxvcQ78696.png)
##感谢阅读##
|