The serial port is one of the most common communication methods and is also the most common communication interface for microcontroller debugging. It is an essential communication interface for modern microcontrollers. The latest and most powerful RA8 series microcontroller with the Cortex-M85 core is no exception (as of May 2024).
Today, I will explain how to implement serial output using the Renesas Cortex-M85 core RA8 series microcontroller, a process that can be configured in just about a minute.
01
Preparation
To quickly use the Renesas Cortex-M85 core RA8 series microcontroller, some preparations are necessary.
01
Integrated Development Tools
There are many integrated development environments (IDEs) available for Renesas microcontrollers, such as Renesas’ own e2s, Keil, IAR, etc.
02
Prepare Hardware Board
Although simulation tools can implement some functions of microcontrollers, it is best to run programs on actual hardware to better learn the technology.
This tutorial uses the Renesas CPKCOR_RA8D1B evaluation board, which features the RA8D1 microcontroller and integrates J-link, allowing us to connect to a computer with just one cable for downloading and debugging.
02
Getting Started with Cortex-M85 Serial Port in One Minute
The e2s integrates many functions, allowing us to quickly create and generate a project with just a few mouse clicks.
01
Create Project
To quickly create a project based on e2s, open e2s, go to File -> New -> Renesas C/C++ Project -> Renesas RA:
Enter the project name, for example, we input RA8D1_UART:
Select the microcontroller model, for example, R7FA8D1BHEC:
Then, follow the prompts step by step to create an RA8 project:
02
Configure Serial Port
Here, you only need to configure the serial port using the graphical interface. In the Pins section, select Peripherals -> SLI4 and configure it to asynchronous communication mode:
In Stacks -> New Stacks, create a new UART:
Then, configure the UART parameters (attributes):
Here, modify a few main parameters according to your situation; most others can remain as defaults. If interrupts are needed, you will need to define an interrupt callback function:
If you want to use printf, you also need to configure two places: 1. heap stack size; 2. project settings.
Next, we need to add code. We will add two source files: bsp_debug_uart.h and bsp_debug_uart.c. The bsp_debug_uart.h file includes header files and declares functions.
#ifndef __BSP_DEBUG_UART_H
#define __BSP_DEBUG_UART_H
#include "hal_data.h"
#include <stdio.h>
void Debug_UART4_Init(void);
#endif
The bsp_debug_uart.c source file is also quite simple:
#include "bsp_debug_uart.h"
/* Debug UART4 Initialization */
void Debug_UART4_Init(void){
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_B_UART_Open(&g_uart4_ctrl, &g_uart4_cfg);
assert(FSP_SUCCESS == err);
}
/* Send complete flag */
volatile bool uart_send_complete_flag = false;
/* Serial port interrupt callback */
void debug_uart4_callback (uart_callback_args_t * p_args){
switch (p_args->event) {
case UART_EVENT_RX_CHAR: {
/* Echo back the received data */
R_SCI_B_UART_Write(&g_uart4_ctrl, (uint8_t *)&(p_args->data), 1);
break;
}
case UART_EVENT_TX_COMPLETE: {
uart_send_complete_flag = true;
break;
}
default: break;
}
}
/* Redirect printf output */
#if defined __GNUC__ && !defined __clang__
int _write(int fd, char *pBuffer, int size); // Prevent compiler warning
int _write(int fd, char *pBuffer, int size){
(void)fd;
R_SCI_B_UART_Write(&g_uart4_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return size;
}
#else
int fputc(int ch, FILE *f){
(void)f;
R_SCI_B_UART_Write(&g_uart4_ctrl, (uint8_t *)&ch, 1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif
With this setup, you can send any character to the microcontroller, and it will echo it back to you. For example, if you send the character “A”.
At this point, the configuration of the RA8 microcontroller’s serial port is complete. Isn’t it simple? For those with a certain foundation, it can basically be done in a minute.
1
END
1
To Be Continued
Recommended Reading
Renesas RA8 Series Tutorial | Common Documentation for Learning RA8
Renesas RA8 Series Tutorial | Setting Up the Renesas RA8 Development Environment and Lighting Up an LED
Renesas RA0E1 Development Board Review | IIC OLED Test