In this tutorial, we will make a short RA FSP project that changes UART parameters (e.g. baud rate, parity, stop bits, channel etc.) while the application is running.
In many use cases it is necessary to change some UART settings runtime through the application code. Without the need of using the FSP GUI configurator and re-generating code.
In this example we will change the UART channel runtime, by using the same UART instance in FSP, without the need of adding an extra stack in the GUI configurator.
Parts & ToolsFind below the list with the HW and SW tools, which will be used for this tutorial:
- Evaluation Kit for RA6M4 MCU Group
- e2studio + FSP installed
- USB to UART board
- Terminal App (Tera Term, Docklight etc.)
ProjectCreation
1. Start e2studio and create a new RA FSP Project.
Go to File->New->Renesas C/C++ Project-> Renesas RA and select:
And Click Next.
2. Name your project and click Next.
3. Select the board EK-RA6M4 and Toolchain to be used in your project.
For this tutorial, e2studio 2025-07 and FSP 6.1.0 is used.
And click Next.
4. Select Flat (Non-TrustZone) Project. Click Next.
5. If using FSP version later than 5.9.0 select as Preceding Project None. Click Next.
6. Select NoRTOS. Click Next.
7. As project type select Bare Metal - Minimal. Click Finish.
This is it you created you RA FSP project.
Setting-up UART1. Open the configuration.xml file of your project.
2. Go to Stacks tab. Add the stack for UART communication.
3. Select the stack you added and go to Properties tab. We will configure channel 0 and we will use the already set baud rate.
4. We will go to Pins tab and enable the SCI0 peripheral as Asynchronous UART.
If the pins we already use are used from another peripheral. Disable the other peripheral, to configure the pins to be used for SCI0 UART.
5. You will see the pins have been also set in UART stack Properties.
6. Configure the name of the uart user callback function
7. Press Generate Code.
AddingCode
1. Open the hal_entry.c file under src folder. The hal_entry function is called by main() function. And is the function, where you will put you main code.
2. Add the following code in hal_entry.c
#include "hal_data.h"
#include <stdio.h>
/*Callback flag to check if UART transition completed*/
volatile bool transmitComplete = false;
/*User functions*/
void init_uart_channel0(uart_cfg_t *uart_cfg);
void deinit_uart(void);
/*UART Init Function*/
void init_uart_channel0(uart_cfg_t *uart_cfg)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open(&g_uart0_ctrl, uart_cfg);
if (err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*UART De-init Function*/
void deinit_uart(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Close(&g_uart0_ctrl);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*Function to redirect printf to UART*/
int puts(const char *str)
{
// Set Transmit Flag to false
transmitComplete = false;
fsp_err_t err = FSP_SUCCESS;
uint8_t tx_data[256]={0};
int len =0;
len=sprintf((char*)tx_data,str);
// Write String to UART
err = R_SCI_UART_Write(&g_uart0_ctrl, tx_data,(uint32_t)len);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
return 0;
}
/*******************************************************************************************************************//**
* 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 */
/*Initialize UART for SCI0*/
init_uart_channel0(&g_uart0_cfg);
/*Transmit for example 10 messages with SCI0 UART*/
/*For this channel pins configured are TXD:P411,RXD:P410
*
*/
for(uint8_t i=0; i<10; i++)
{
printf("Channel 0 transmitting messages...\r\n");
R_BSP_SoftwareDelay(500,BSP_DELAY_UNITS_MILLISECONDS);
}
while (1)
{
R_BSP_SoftwareDelay(500,BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/* Callback function */
void g_uart0_cb(uart_callback_args_t *p_args)
{
/* TODO: add your own code here */
if((p_args->event = UART_EVENT_TX_COMPLETE))
{
transmitComplete = true;
return;
}
}Build the project and now it's time to test the UART transition. This sample code transmits 10 test UART messages.
Hardware Connections- Connect the USB-A to Micro-USB cable on J10 USB Debug port of EK-RA6M4.
For USB to UART and EK-RA6M4 connect:
- USB-UART TX- >RXD (P410, RA6M4)
- USB-UART RX->TXD (P411, RA6M4)
1. Build the project.
2. Launch a Debug session to download the code on RA6M4.
3. This is it you see the MCU transmitting 10 test messages through UART SCI0 on serial Terminal.
As already mentioned the purpose of this tutorial is to change the UART parameters runtime. In this example we will change runtime the uart channel.
And by using the same UART instance in our FSP code, we will switch from UART SCI0 to UART SCI9.
To do this, please follow the steps below.
1. Go to Pins tab and configure SCI9 to Asynchronous UART.
If the pins we already use are used from another peripheral. Disable the other peripheral, to configure the pins to be used for SCI9 UART.
2. Configure the UART ISRs for the proper operation of UART channel9. Go to Interrupts Tab and add the following:
By doing this, we link the already existent UART interrupt routines with the specific UART events on UART SCI9 peripheral.
3. Press Generate Code
4. Open ra_gen/vector_data.c, ra_gen/vector_data.h files. And you will see the code generated for the SCI9 ISRs.
5. Open again hal_entry.c file and add some extra code. Modify your existing hal_entry.c file, by copying pasting the code below:
#include "hal_data.h"
#include <stdio.h>
/*Callback flag to check if UART transition completed*/
volatile bool transmitComplete = false;
/*Structure Object to hold the configurations of UART Interface*/
static uart_cfg_t uart0_cfg_alt;
/*User functions*/
void init_uart_channel0(uart_cfg_t *uart_cfg);
void deinit_uart(void);
void setUARTparameters (uart_cfg_t *uart_cfg,uint8_t channel,uart_data_bits_t data_bits,uart_parity_t uart_parity,uart_stop_bits_t uart_stop_bits,
uint8_t priority,uint8_t vectorNumberRXI,uint8_t vectorNumberTXI, uint8_t vectorNumberTEI,uint8_t vectorNumberERI);
void init_uart_channel9(uart_cfg_t *uart_cfg);
/*UART Init Function*/
void init_uart_channel0(uart_cfg_t *uart_cfg)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open(&g_uart0_ctrl, uart_cfg);
if (err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*UART De-init Function*/
void deinit_uart(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Close(&g_uart0_ctrl);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*Function to modify the UART parameters*/
void setUARTparameters (uart_cfg_t *uart_cfg,uint8_t channel,uart_data_bits_t data_bits,uart_parity_t uart_parity,uart_stop_bits_t uart_stop_bits,
uint8_t priority,uint8_t vectorNumberRXI,uint8_t vectorNumberTXI, uint8_t vectorNumberTEI,uint8_t vectorNumberERI)
{
uart_cfg->channel = channel; //Set SCI channel
uart_cfg->data_bits = data_bits; //Set data bit length
uart_cfg->parity = uart_parity; //Set parity check
uart_cfg ->stop_bits = uart_stop_bits; //Stop bit Length
uart_cfg->p_callback = g_uart0_cb; //UART Callback functions
uart_cfg->p_context = NULL; //Set User defined context passed into callback function
uart_cfg->p_extend = &g_uart0_cfg_extend; //UART extended configuration structure
uart_cfg->p_transfer_tx = NULL; //Optional transfer instance to transmit bytes without interrupts, Set NULL if not used
uart_cfg->p_transfer_rx = NULL; //Optional transfer instance to receive bytes without interrupts, Set NULL if not used
uart_cfg->rxi_ipl = priority; //Set Receive Interrupt Priority
uart_cfg->txi_ipl = priority; //Set Transmit Interrupt Priority
uart_cfg->tei_ipl = priority; //Set Transmit End Interrupt Priority
uart_cfg->eri_ipl = priority; //Set Error Interrupt Priority
uart_cfg->rxi_irq = vectorNumberRXI; //Receive interrupt IRQ number
uart_cfg->txi_irq = vectorNumberTXI; //Transmit interrupt IRQ number
uart_cfg->tei_irq = vectorNumberTEI; //Transmit end interrupt IRQ number
uart_cfg->eri_irq = vectorNumberERI; //Error interrupt IRQ number
}
/*Function to initialize UART-SCI9*/
void init_uart_channel9(uart_cfg_t *uart_cfg)
{
fsp_err_t err = FSP_SUCCESS;
setUARTparameters (&uart0_cfg_alt,9,UART_DATA_BITS_8,UART_PARITY_OFF,UART_STOP_BITS_1,2,VECTOR_NUMBER_SCI9_RXI,VECTOR_NUMBER_SCI9_TXI,VECTOR_NUMBER_SCI9_TEI,VECTOR_NUMBER_SCI9_ERI);
err = R_SCI_UART_Open(&g_uart0_ctrl, uart_cfg);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*Function to redirect printf to UART*/
int puts(const char *str)
{
// Set Transmit Flag to false
transmitComplete = false;
fsp_err_t err = FSP_SUCCESS;
uint8_t tx_data[256]={0};
int len =0;
len=sprintf((char*)tx_data,str);
// Write String to UART
err = R_SCI_UART_Write(&g_uart0_ctrl, tx_data,(uint32_t)len);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
return 0;
}
/*******************************************************************************************************************//**
* 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 */
/*Initialize UART for SCI0*/
init_uart_channel0(&g_uart0_cfg);
/*Transmit for example 10 messages with SCI0 UART*/
/*For this channel pins configured are TXD:P411,RXD:P410
*
*/
for(uint8_t i=0; i<10; i++)
{
printf("Channel 0 transmitting messages...\r\n");
R_BSP_SoftwareDelay(500,BSP_DELAY_UNITS_MILLISECONDS);
}
/*Close UART with the settings for SCI0*/
deinit_uart();
/*Initialize UART for SCI9*/
init_uart_channel9(&uart0_cfg_alt);
/*Now transmit data from SCI9 UART for the rest of the program*/
/* To see the messages on Serial Monitor don't forget to change connections
* on the USB to UART Converter. For channel SCI9 : TXD:P203, RXD:P202
*/
while (1)
{
printf("Channel 9 transmitting messages...\r\n");
R_BSP_SoftwareDelay(500,BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/* Callback function */
void g_uart0_cb(uart_callback_args_t *p_args)
{
/* TODO: add your own code here */
if((p_args->event = UART_EVENT_TX_COMPLETE))
{
transmitComplete = true;
return;
}
}With the code added as shown above. We switch from UART SCI0 to UART SCI9 runtime. The RA6M4 transmits 10 test messages from UART SCI0 and then continues transmitting messages to serial Terminal from SCI9.
Run the project1. Build the project.
2. Launch a Debug session to download the code on RA6M4.
3. This is it you see the MCU transmitting 10 test messages through UART SCI0 on serial Terminal.
4. To see messages being transmitted also from UART SCI9 you need to change the connections between the UART-USB board and RA6M4 as below:
- USB-UART TX- >RXD (P202, RA6M4)
- USB-UART RX->TXD (P203, RA6M4)
This project shows how to safely change UART parameters at runtime on a Renesas RA board using FSP, enabling flexible and reliable serial communication.




Comments