RA2 UART query

HI,

I am using RA2 EVK and started working on a basic UART printing HELLO! the project prints only "H" in the serial terminal continuously. if i use a delay it prints HELLO! once and ends up strucking in the  "Default exception handler".  even a for loop with delay also results same. I need to print Hello! continuously, Please help to advise here.

I am using delay

Case1 R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS); // Continuous "H" prints in terminal.

Case 2 R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS); // Continuous "HeHHeHHeHHeHHeH" prints in terminal

Case 3 R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); // Hello! prints once and enters into below loop.

/*******************************************************************************************************************//**
* Default exception handler.
**********************************************************************************************************************/
void Default_Handler (void)
{
/** A error has occurred. The user will need to investigate the cause. Common problems are stack corruption
* or use of an invalid pointer. Use the Fault Status window in e2 studio or manually check the fault status
* registers for more information.
*/
BSP_CFG_HANDLE_UNRECOVERABLE_ERROR(0);
}

 

My UART configuration is 

Baud: 9600

Callback is set to NULL

My code looks like below

#include "hal_data.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
//volatile uint32_t delay_counter = ;
/*******************************************************************************************************************//**
* 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) {
uint8_t cstr[] = "Hello!\0 ";
R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
while(1)
{
R_SCI_UART_Write(&g_uart0_ctrl, cstr, 9);
/* Delay */
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);

/* for (delay_counter = 0; delay_counter < 1; delay_counter++)
{
Do nothing.
}
R_SCI_UART_Close(&g_uart0_ctrl);*/
}
}

/*******************************************************************************************************************//**
* This function is called at various points during the startup process. This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event) {
if (BSP_WARM_START_RESET == event) {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;

/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}

if (BSP_WARM_START_POST_C == event) {
/* C runtime environment and system clocks are setup. */

/* Configure pins. */
R_IOPORT_Open(&g_ioport_ctrl, &g_bsp_pin_cfg);
}
}

 

 

  • Ramu,
    I thing that your results are to be expected. Looking at you code I would expect some data to be transmitted and then for the code to fail.

    At 9600 BAUD at bit time is 1/9600 = 104.16us. Assuming you are transmitting 1 start bit, 8 bit data, no parity, 1 stop bit you will have 10 bits in total, so the time to transmit 1 character = 1.04166ms

    When you open the UART and then write, there is a small delay due to internal synchronization or the UART logic

    Therefore, when you delay for 1 microsecond, and then close the UART, the UART has only had time to transfer the start bit and the first data. Similarly, when you delay to 1millisecond, there is time for more data to be transmitted before you close the UART. When you delay for 1 second, there is time to transmit the entire data string, before you close the UART.

    Your while(1) loop now tries to transmit the data again. But you have closed the UART. It is not open to be Written, hence you end up in the default handler.
    2 options. Don't close the UART or Open it every time you need to use it.

    You could also consider using the callback function that will tell you when UART has finished transmitting data and wait on that condition, rather than using an arbitrary delay value.
    Also, consider checking the return value of the functions that you call. All FSP functions return FSP_SUCCESS, or an error.

    Regards,
  • Like Richard suggested, set your callback to something like uart_callback. Use the Developer's assistant to drag-n-drop the callback function into your code. Declare a global scope variable like: volatile bool finished; Just prior to your call to write set it to false. Set it to true in your callback function. After the call to write put in a while(!finished); BE SURE TO DECLARE THE VARIABLE VOLATILE! or the optimizer may prune it. If you move to an RTOS you can use a semaphore rather than a volatile bool, and replace the while(!finished) with a get semaphore and the finished = true in the interrupt handler to a put semaphore.
  • In reply to Dale Drinkard:

    Thanks Richard and Dale..

    I somehow managed to print it using a delay function.

    Thanks for suggesting me the uart_callback. Can you please share me any example for this to set, since im very new to this. It would help me if you can share any reference code available or any document to do it.
  • In reply to Ramu:

    Have a look at the attached project.

    EK-RA2A1_FSP_1_2_0_Uart_Callback.zip

  • Hello Ramu,

    Have you finally found the answer on your questions?

    Kind regards,
  • In reply to Sergey Sokol:

    HI Sergey,

    Yes, Jermey's post helped me.

    Can somebody help how to know the number of clock cycles used using e2Studio or any register available to check.
  • In reply to Ramu:

    Set the CPU frequency in the debug configuration :-

    Then in e2studio, when program execution is halted, the DWT is read to work out the execution time since the last halt in the bottom left hand corner:-

  • In reply to Jeremy:

    Hello Jermey,

    Any register is available to check?
  • In reply to Ramu:

    You could use the DWT CYCCNT register. This is the same register that e2studio will read (and it seems that e2studio stops and starts the DWT cycle counter).