Renesas e2studio(17) -- independent watchdog IWDT

summary

This article mainly introduces how to use e2studio Independent watchdog on Renesas IWDT Configure, and configure RTC Clock generation 1 s The cycle of is interrupted. View the independent watchdog through serial port printing IWDT Count value of.

Hardware preparation

First, I need to prepare a development board. Here I prepare the development board of chip model R7FA2L1AB2DFL:

New project

Engineering formwork

Save project path

Chip configuration

In this article, R7FA2L1AB2DFL is used for demonstration.

Project template selection

IWDT configuration

Click stacks - > new stack - > driver - > monitoring - > watchdog driver on R_ iwdt.

IWDT attribute configuration

OFS attribute configuration

RTC configuration

Click stacks - > new stack - > driver - > timers - > RTC driver on R_ rtc.

RTC attribute configuration

Set e2studio stack

Redirect printf settings for e2studio

C + + build - > Settings - > GNU arm cross C linker - > miscellaneous remove "– specs=rdimon.specs" in Other linker flags

uart configuration

Click stacks - > new stack - > driver - > connectivity - > UART driver on R_ sci_ uart.

uart attribute configuration

Configure the serial port for printing data.

The printf output is redirected to the serial port

The most common method of printing is printf, so the problem to be solved is to redirect the output of printf to the serial port, and then send the data through the serial port.
Be sure to add the header file #include < stdio. H >

#ifdef __GNUC__                                 // Serial port redirection
    #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<size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}

R_IWDT_Open() function prototype


Therefore, R can be used_ IWDT_ The open() function initializes and opens iwdt.

 /* Open the module. */
     err = R_IWDT_Open(&g_iwdt0_ctrl, &g_iwdt0_cfg);
     /* Handle any errors. This function should be defined by the user. */
     assert(FSP_SUCCESS == err);

R_IWDT_Refresh() function prototype


Therefore, R can be used_ IWDT_ The refresh() function performs the dog feeding operation.

 /* Refresh before the counter underflows to prevent reset or NMI. */
	(void) R_IWDT_Refresh(&g_iwdt0_ctrl);

R_IWDT_CounterGet() function prototype


Therefore, R can be used_ IWDT_ The counterget() function gets the current count value.

 uint32_t iwdt_counter = 0U;
 err = R_IWDT_CounterGet(&g_iwdt0_ctrl, &iwdt_counter);

IWDT cycle setting


IWDT runs from IWDTCLK. According to the above settings, IWDT cycle is as follows.

ParameterEqual to
IWDTCLK15 kHz
Clock division ratioIWDTCLK/32
Timeout period2048 cycles
IWDT clock frequency15 kHz / 32 = 468.75 Hz
Cycle time1 / 468.75 Hz = 2.13 ms
Timeout2.13 ms x 2048 cycles = 4.362 seconds

As can be seen above, the overflow time under this setting is 4.362s, so the count of 1s is 1s/2.13ms=469.

IWDT count cycle

The IWDT count is reduced from the highest to 0. When it reaches 0, reset is triggered.

Demonstration effect

Set the current time to print every 1s, and set feeding dog and not feeding dog respectively. The results are as follows.
The count of 1s delay is 1s/2.13ms=469, and the print is 1570. Because it is a downward count, 2048-1570 = 478, which is in line with the calculated value.

When dog feeding is not performed, it will be reset when the count value reaches 0.

Complete code

#include "hal_data.h"
#include <stdio.h>
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__                                 // Serial port redirection
    #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<size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}

/* Callback function */
/* Example callback called when a watchdog NMI occurs. */
void iwdt_callback (wdt_callback_args_t * p_args)
{

}

volatile bool rtc_flag = 0;//RTC delay 1s flag bit
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
    /* TODO: add your own code here */
    if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
        rtc_flag=1;

}

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
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);

    printf("starting up !\n");

    /* (Optional) Enable the IWDT to count and generate NMI or reset when the
     * debugger is connected. */
    R_DEBUG->DBGSTOPCR_b.DBGSTOP_IWDT = 0;
    /* (Optional) Check if the IWDTRF flag is set to know if the system is
     * recovering from a IWDT reset. */
    if (R_SYSTEM->RSTSR1_b.IWDTRF)
    {
        /* Clear the flag. */
        R_SYSTEM->RSTSR1 = 0U;
    }
    /* Open the module. */
     err = R_IWDT_Open(&g_iwdt0_ctrl, &g_iwdt0_cfg);
     /* Handle any errors. This function should be defined by the user. */
     assert(FSP_SUCCESS == err);
     /* Initialize other application code. */
     /* Do not call R_IWDT_Refresh() in auto start mode unless the
      * counter is in the acceptable refresh window. */
    (void) R_IWDT_Refresh(&g_iwdt0_ctrl);
    while(1)
    {
            if(rtc_flag)
            {
                uint32_t iwdt_counter = 0U;
                err = R_IWDT_CounterGet(&g_iwdt0_ctrl, &iwdt_counter);
                assert(FSP_SUCCESS == err);
                printf("iwdt_counter=%d\n",iwdt_counter);
                /* Refresh before the counter underflows to prevent reset or NMI. */
                (void) R_IWDT_Refresh(&g_iwdt0_ctrl);
                rtc_flag=0;
            }
    }
#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

last

The above code will be shared in Q group. QQ group: 615061293.
Or focus on WeChat's official account, keep updating articles and learning materials, and add WeChat's learning to the author.

Tags: Single-Chip Microcomputer

Posted on Fri, 19 Nov 2021 04:00:07 -0500 by R0d Longfella