RA Flexible Software Package Documentation  Release v5.7.0

 
UART (r_uarta)

Functions

fsp_err_t R_UARTA_Open (uart_ctrl_t *const p_api_ctrl, uart_cfg_t const *const p_cfg)
 
fsp_err_t R_UARTA_Close (uart_ctrl_t *const p_api_ctrl)
 
fsp_err_t R_UARTA_Read (uart_ctrl_t *const p_api_ctrl, uint8_t *const p_dest, uint32_t const bytes)
 
fsp_err_t R_UARTA_Write (uart_ctrl_t *const p_api_ctrl, uint8_t const *const p_src, uint32_t const bytes)
 
fsp_err_t R_UARTA_CallbackSet (uart_ctrl_t *const p_api_ctrl, void(*p_callback)(uart_callback_args_t *), void const *const p_context, uart_callback_args_t *const p_callback_memory)
 
fsp_err_t R_UARTA_BaudSet (uart_ctrl_t *const p_api_ctrl, void const *const p_baud_setting)
 
fsp_err_t R_UARTA_InfoGet (uart_ctrl_t *const p_api_ctrl, uart_info_t *const p_info)
 
fsp_err_t R_UARTA_Abort (uart_ctrl_t *const p_api_ctrl, uart_dir_t communication_to_abort)
 
fsp_err_t R_UARTA_ReadStop (uart_ctrl_t *const p_api_ctrl, uint32_t *p_remaining_bytes)
 
fsp_err_t R_UARTA_BaudCalculate (uint32_t baudrate, uint32_t baud_rate_percent_error_x1000, uarta_clock_source_t clock_source, uarta_baud_setting_t *const p_baud_setting)
 

Detailed Description

Driver for the UARTA peripheral on RA MCUs. This module implements the UART Interface.

Overview

Features

The UARTA module supports the following features:

Configuration

Build Time Configurations for r_uarta

The following build time configurations are defined in fsp_cfg/r_uarta_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
DTC SupportMCU Specific OptionsEnable DTC support for the UARTA module.
Receive Error Interrupt Mode
  • Disabled
  • Enabled
Enabled Selection receive interrupt mode .
Disabled: The UARTA0_ERRI interrupt is generated when a reception error occurs.
Enabled: The UARTA0_RXI interrupt is generated when a reception error occurs.

Configurations for Connectivity > UART (r_uarta)

This module can be added to the Stacks tab via New Stack > Connectivity > UART (r_uarta).

ConfigurationOptionsDefaultDescription
General
NameName must be a valid C symbolg_uart0 Module name.
ChannelValue must be a non-negative integer0 Select the UARTA channel.
Data Bits
  • 8bits
  • 7bits
  • 5bits
8bits Select the number of bits per word.
Parity
  • None
  • Zero
  • Odd
  • Even
None Select the parity mode.
Stop Bits
  • 1bit
  • 2bits
1bit Select the number of stop bits.
Note: For the receive data, only the first 1 bit of the stop bits is checked regardless of the stop bit length.
Baud
Baud RateValue must be an integer greater than 0115200 Enter the desired baud rate.

If the requested baud rate cannot be achieved, the settings with the smallest percent error are used. The theoretical calculated baud rate and percent error are printed in a comment in the generated baud_setting_t structure.
Extra
Transfer Order
  • LSB first
  • MSB first
LSB first Selection of the transmission/reception order.
Transfer level
  • Positive logic
  • Negative logic
Positive logic Selection of the transmission/reception level.
Clock outputMCU Specific OptionsEnable Clock output
Interrupts
CallbackName must be a valid C symbolNULL A user callback function can be provided. If this callback function is provided, it will be called from the interrupt service routine (ISR).
Receive Interrupt PriorityMCU Specific OptionsSelect the receive interrupt priority.
Transmit Interrupt PriorityMCU Specific OptionsSelect the transmit interrupt priority.
Error Interrupt PriorityMCU Specific OptionsSelect the error interrupt priority.

Clock Configuration

The baud-rate clock generator can be selected from the clock source MOSC, HOCO, MOCO, SOSC/LOCO.

Pin Configuration

This module uses TXDA and RXDA pin to communicate to external devices.

Usage Notes

Limitations

DTC Limitations

Examples

UARTA Example

uint8_t g_dest[TRANSFER_LENGTH];
uint8_t g_src[TRANSFER_LENGTH];
uint8_t g_out_of_band_received[TRANSFER_LENGTH];
volatile uint32_t g_transfer_complete = 0;
volatile uint32_t g_receive_complete = 0;
uint32_t g_out_of_band_index = 0;
void r_uarta_basic_example (void)
{
/* Initialize p_src to known data */
for (uint32_t i = 0; i < TRANSFER_LENGTH; i++)
{
g_src[i] = (uint8_t) ('A' + (i % 26));
}
/* Open the transfer instance with initial configuration. */
fsp_err_t err = R_UARTA_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
err = R_UARTA_Read(&g_uart0_ctrl, g_dest, TRANSFER_LENGTH);
assert(FSP_SUCCESS == err);
err = R_UARTA_Write(&g_uart0_ctrl, g_src, TRANSFER_LENGTH);
assert(FSP_SUCCESS == err);
while (!g_transfer_complete)
{
}
while (!g_receive_complete)
{
}
}
void example_callback (uart_callback_args_t * p_args)
{
/* Handle the UART event */
switch (p_args->event)
{
/* Received a character */
{
/* Only put the next character in the receive buffer if there is space for it */
if (sizeof(g_out_of_band_received) > g_out_of_band_index)
{
/* Write either the next one or two bytes depending on the receive data size */
if (UART_DATA_BITS_8 >= g_uart0_cfg.data_bits)
{
g_out_of_band_received[g_out_of_band_index++] = (uint8_t) p_args->data;
}
else
{
uint16_t * p_dest = (uint16_t *) &g_out_of_band_received[g_out_of_band_index];
*p_dest = (uint16_t) p_args->data;
g_out_of_band_index += 2;
}
}
break;
}
/* Receive complete */
{
g_receive_complete = 1;
break;
}
/* Transmit complete */
{
g_transfer_complete = 1;
break;
}
default:
{
}
}
}

UARTA Baud Set Example

#define UARTA_BAUDRATE_19200 (19200)
#define UARTA_BAUDRATE_ERROR_PERCENT_4740 (4740)
void r_uarta_baud_example (void)
{
uarta_baud_setting_t baud_setting;
uint32_t baud_rate = UARTA_BAUDRATE_19200;
uint32_t error_rate_x_1000 = UARTA_BAUDRATE_ERROR_PERCENT_4740;
fsp_err_t err = R_UARTA_BaudCalculate(baud_rate, error_rate_x_1000, clock_source, &baud_setting);
assert(FSP_SUCCESS == err);
err = R_UARTA_BaudSet(&g_uart0_ctrl, (void *) &baud_setting);
assert(FSP_SUCCESS == err);
}

Data Structures

struct  uarta_baud_setting_t
 
struct  uarta_extended_cfg_t
 
struct  uarta_instance_ctrl_t
 

Enumerations

enum  uarta_clock_source_t
 
enum  uarta_clock_div_t
 
enum  uarta_clock_out_t
 
enum  uarta_dir_bit_t
 
enum  uarta_alv_bit_t
 

Data Structure Documentation

◆ uarta_baud_setting_t

struct uarta_baud_setting_t

Register settings to acheive a desired baud rate and modulation duty.

Data Fields
union uarta_baud_setting_t __unnamed__
uint8_t brgca Baud rate generator control setting.
uint16_t delay_time Delay time (us) required to enable TX at open.

◆ uarta_extended_cfg_t

struct uarta_extended_cfg_t

UART on UARTA device Configuration

Data Fields
uarta_dir_bit_t transfer_dir Transmission/reception order configuration.
uarta_alv_bit_t transfer_level Transmission/reception level configuration.
uarta_clock_out_t clock_output Disable/Enable clock output.
uarta_baud_setting_t * p_baud_setting Register settings for a desired baud rate.

◆ uarta_instance_ctrl_t

struct uarta_instance_ctrl_t

UARTA instance control block.

Enumeration Type Documentation

◆ uarta_clock_source_t

Enumeration for UARTA clock source

Enumerator
UARTA_CLOCK_SOURCE_SOSC_LOCO 

SOSC/LOCO.

UARTA_CLOCK_SOURCE_LOCO 

LOCO.

UARTA_CLOCK_SOURCE_MOSC 

MOSC.

UARTA_CLOCK_SOURCE_HOCO 

HOCO.

UARTA_CLOCK_SOURCE_MOCO 

MOCO.

UARTA_CLOCK_SOURCE_SOSC 

SOSC.

◆ uarta_clock_div_t

Enumeration for UARTA clock divider

Enumerator
UARTA_CLOCK_DIV_1 

fSEL/1

UARTA_CLOCK_DIV_2 

fSEL/2

UARTA_CLOCK_DIV_4 

fSEL/4

UARTA_CLOCK_DIV_8 

fSEL/8

UARTA_CLOCK_DIV_16 

fSEL/16

UARTA_CLOCK_DIV_32 

fSEL/32

UARTA_CLOCK_DIV_64 

fSEL/64

UARTA_CLOCK_DIV_COUNT 

Total number of clock divider options.

◆ uarta_clock_out_t

Enabled/Disabled Clock output

Enumerator
UARTA_CLOCK_OUTPUT_DISABLED 

Disables CLKAn output.

UARTA_CLOCK_OUTPUT_ENABLED 

Enables CLKAn output.

◆ uarta_dir_bit_t

Transmission/reception order configuration.

Enumerator
UARTA_DIR_BIT_MSB_FIRST 

MSB first.

UARTA_DIR_BIT_LSB_FIRST 

LSB first.

◆ uarta_alv_bit_t

Transmission/reception level configuration.

Enumerator
UARTA_ALV_BIT_POSITIVE_LOGIC 

Positive logic (wait state = high level, start bit = low level, stop bit = high level)

UARTA_ALV_BIT_NEGATIVE_LOGIC 

Negative logic (wait state = low level, start bit = high level, stop bit = low level)

Function Documentation

◆ R_UARTA_Open()

fsp_err_t R_UARTA_Open ( uart_ctrl_t *const  p_api_ctrl,
uart_cfg_t const *const  p_cfg 
)

Configures the UARTA driver based on the input configurations. If transmission/reception is enabled at compile time, transmission/reception is enabled at the end of this function. Implements uart_api_t::open

Return values
FSP_SUCCESSChannel opened successfully.
FSP_ERR_ASSERTIONPointer to UARTA control block or configuration structure is NULL.
FSP_ERR_IP_CHANNEL_NOT_PRESENTThe requested channel does not exist on this MCU.
FSP_ERR_INVALID_ARGUMENTInvalid clock select (f_UTA0) and baudrate configuration.
FSP_ERR_ALREADY_OPENControl block has already been opened or channel is being used by another instance. Call close() then open() to reconfigure.
Returns
See Common Error Codes or functions called by this function for other possible return codes. This function calls:

◆ R_UARTA_Close()

fsp_err_t R_UARTA_Close ( uart_ctrl_t *const  p_api_ctrl)

Aborts any in progress transfers. Disables interrupts, receiver, and transmitter. Closes lower level transfer drivers if used. Removes power. Implements uart_api_t::close

Return values
FSP_SUCCESSChannel successfully closed.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened

◆ R_UARTA_Read()

fsp_err_t R_UARTA_Read ( uart_ctrl_t *const  p_api_ctrl,
uint8_t *const  p_dest,
uint32_t const  bytes 
)

Receives user specified number of bytes into destination buffer pointer. Implements uart_api_t::read

Return values
FSP_SUCCESSData reception successfully ends.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL. Number of transfers outside the max or min boundary when transfer instance used
FSP_ERR_NOT_OPENThe control block has not been opened
FSP_ERR_IN_USEA previous read operation is still in progress.
FSP_ERR_UNSUPPORTEDUARTA_CFG_RX_ENABLE is set to 0
Returns
See Common Error Codes or functions called by this function for other possible return codes. This function calls:

◆ R_UARTA_Write()

fsp_err_t R_UARTA_Write ( uart_ctrl_t *const  p_api_ctrl,
uint8_t const *const  p_src,
uint32_t const  bytes 
)

Transmits user specified number of bytes from the source buffer pointer. Implements uart_api_t::write

Return values
FSP_SUCCESSData transmission finished successfully.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL. Number of transfers outside the max or min boundary when transfer instance used
FSP_ERR_NOT_OPENThe control block has not been opened
FSP_ERR_IN_USEA UARTA transmission is in progress
FSP_ERR_UNSUPPORTEDUART_CFG_TX_ENABLE is set to 0
Returns
See Common Error Codes or functions called by this function for other possible return codes. This function calls:

◆ R_UARTA_CallbackSet()

fsp_err_t R_UARTA_CallbackSet ( uart_ctrl_t *const  p_api_ctrl,
void(*)(uart_callback_args_t *)  p_callback,
void const *const  p_context,
uart_callback_args_t *const  p_callback_memory 
)

Updates the user callback and has option of providing memory for callback structure. Implements uart_api_t::callbackSet

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_UARTA_BaudSet()

fsp_err_t R_UARTA_BaudSet ( uart_ctrl_t *const  p_api_ctrl,
void const *const  p_baud_setting 
)

Updates the baud rate using the clock selected in Open. p_baud_setting is a pointer to a uarta_baud_setting_t structure. Implements uart_api_t::baudSet

Warning
This terminates any in-progress transmission.
Return values
FSP_SUCCESSBaud rate was successfully changed.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL or the UART is not configured to use the internal clock.
FSP_ERR_INVALID_ARGUMENTInvalid clock select (f_UTA0) and baudrate configuration.
FSP_ERR_NOT_OPENThe control block has not been opened

◆ R_UARTA_InfoGet()

fsp_err_t R_UARTA_InfoGet ( uart_ctrl_t *const  p_api_ctrl,
uart_info_t *const  p_info 
)

Provides the driver information, including the maximum number of bytes that can be received or transmitted at a time. Implements uart_api_t::infoGet

Return values
FSP_SUCCESSInformation stored in provided p_info.
FSP_ERR_ASSERTIONPointer to UART control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened

◆ R_UARTA_Abort()

fsp_err_t R_UARTA_Abort ( uart_ctrl_t *const  p_api_ctrl,
uart_dir_t  communication_to_abort 
)

Provides API to abort ongoing transfer. Transmission is aborted after the current character is transmitted. Reception is still enabled after abort(). Any characters received after abort() and before the transfer is reset in the next call to read(), will arrive via the callback function with event UART_EVENT_RX_CHAR. Implements uart_api_t::communicationAbort

Return values
FSP_SUCCESSUARTA transaction aborted successfully.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_UNSUPPORTEDThe requested Abort direction is unsupported.
Returns
See Common Error Codes or functions called by this function for other possible return codes. This function calls: transfer_api_t::disable

◆ R_UARTA_ReadStop()

fsp_err_t R_UARTA_ReadStop ( uart_ctrl_t *const  p_api_ctrl,
uint32_t *  p_remaining_bytes 
)

Provides API to abort ongoing read. Reception is still enabled after abort(). Any characters received after abort() and before the transfer is reset in the next call to read(), will arrive via the callback function with event UART_EVENT_RX_CHAR. Implements uart_api_t::readStop

Return values
FSP_SUCCESSUARTA transaction aborted successfully.
FSP_ERR_ASSERTIONPointer to UARTA control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_UNSUPPORTEDThe requested Abort direction is unsupported.
Returns
See Common Error Codes or functions called by this function for other possible return codes. This function calls: transfer_api_t::disable

◆ R_UARTA_BaudCalculate()

fsp_err_t R_UARTA_BaudCalculate ( uint32_t  baudrate,
uint32_t  baud_rate_percent_error_x1000,
uarta_clock_source_t  clock_source,
uarta_baud_setting_t *const  p_baud_setting 
)

Calculates baud rate register settings. Evaluates and determines the best possible settings set to the baud rate related registers.

Parameters
[in]baudrateBaud rate [bps]. For example, 19200, 57600, 115200, etc.
[in]baud_rate_percent_error_x1000Max baud rate error. At most baud_rate_percent_error x 1000 required for module to function. Absolute max baud_rate_error is 4740 (4.74%).
[in]clock_sourceClock Source. Required for module to generate baudrate. The clock sources can be select include UARTA_CLOCK_SOURCE_MOSC, UARTA_CLOCK_SOURCE_HOCO, UARTA_CLOCK_SOURCE_MOCO, UARTA_CLOCK_SOURCE_SOSC_LOCO.
[out]p_baud_settingBaud setting information stored here if successful
Return values
FSP_SUCCESSBaud rate is set successfully
FSP_ERR_ASSERTIONNull pointer
FSP_ERR_INVALID_ARGUMENTArgument is out of available range, baud rate is '0'.
FSP_ERR_INVALID_RATEBaud rate error is outside the range or the baud rate could not be set given the current clock source.