7
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i;i++)>
15.R_WDT_Open()函数原型
故可以用R_WDT_Open()函数进行初始化和开启WDT。
/* Open the module. */
err = R_WDT_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
16.R_WDT_Refresh()函数原型
故可以用R_WDT_Refresh()函数进行喂狗操作。
/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
17.R_WDT_CounterGet()函数原型
故可以用R_WDT_CounterGet()函数获取当前的计数值。
/* Read the current WDT counter value. */
err = R_WDT_CounterGet(&g_wdt0_ctrl, &wdt_counter);
assert(FSP_SUCCESS == err);
18.WDT周期设定
通过查阅数据手册,可以得知WDT使用的时钟为PCLKB。
在本案例中,使用的PCLKB时钟为24MHz。
WDT从PCLKB运行,依据上文的设定,PCLKB周期如下所示。
Paramete | Equal to |
PLCKB/2 | 24MHz |
Clock division ratio | PLCK/8192 |
Timeout period | 16384 cycles |
WDT clock frequency | 24MHz / 8192 = 2929.6875 Hz |
Cycle time | 1 / 2929.6875 Hz = 341.33 us |
Timeout | 341.33 us * 16384 cycles = 5.59 seconds |
上述可以看到在该设置下的溢出时间为5.59s,那么1s的计数为1s/341.33 us=2930。
19.WDT计数周期
WDT计数是从最高一直减到0,当到0时候触发复位。
20.演示效果
设置每过1s打印一次当前时间,分别设置喂狗和不喂狗,结果如下。
延迟1s的计数为1s/341.33us=2930,打印为13460,由于是向下计数,16384-2930=13554,符合计算值。
当不执行喂狗时候,计数值到0时会进行复位,2个复位之间为5.595s,符合计算的5.59s。
21.完整代码
#include "hal_data.h"
#include
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;ievent == RTC_EVENT_PERIODIC_IRQ)
rtc_flag=1;
}
void hal_entry(void)
{
/* TODO: add your own code here */
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
/* Initialize the RTC module*/
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Set the periodic interrupt rate to 1 second */
R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);
/* (Optional) Enable the WDT to count and generate NMI or reset when the
* debugger is connected. */
R_DEBUG->DBGSTOPCR_b.DBGSTOP_WDT = 0;
/* (Optional) Check if the WDTRF flag is set to know if the system is
* recovering from a WDT reset. */
if (R_SYSTEM->RSTSR1_b.WDTRF)
{
/* Clear the flag. */
R_SYSTEM->RSTSR1 = 0U;
}
/* Open the module. */
err = R_WDT_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* In register start mode, start the watchdog by calling R_WDT_Refresh. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
printf("starting up !\n");
uint32_t wdt_counter = 0U;
while(1)
{
if(rtc_flag)
{
/* Read the current WDT counter value. */
err = R_WDT_CounterGet(&g_wdt0_ctrl, &wdt_counter);
assert(FSP_SUCCESS == err);
printf("wdt_counter=%d\n",wdt_counter);
rtc_flag=0;
/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
}
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
};i++)>
全部0条评论
快来发表一下你的评论吧 !