In this tutorial we will create a short RA project to learn how we can redirect printf to UART.
In many use cases is necessary for developers to print messages from the CPU on a PC Serial Terminal. It is perfect for debugging purposes and for monitoring the application code.
Parts& Tools
Find 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.)
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 your 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.
Adding Code1. 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>
/*User flag to check UART transition completed*/
volatile bool transmitComplete = false;
/*User Functions*/
void init_uart(void);
void deinit_uart(void);
/*Initialize UART SCI0*/
void init_uart(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
if (err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*De-initialize UART SCI0*/
void deinit_uart(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Close(&g_uart0_ctrl);
if(err != FSP_SUCCESS)
{
__BKPT(0);
}
}
/*Redirecting printf to UART SCI0*/
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();
while (1)
{
printf ("Hello from RA6M4- Test message from UART...\r\n");
R_BSP_SoftwareDelay(500,BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/* User Callback function- Checking UART events */
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 test UART messages, using printf.
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 test messages through UART SCI0 on Serial Terminal every 1 s.
In this tutorial, you learned how to output UART messages from Renesas RA6M4 to a PC terminal, using printf. This is perfect for monitoring and debugging your application program.




Comments