This is the quick start guide for the Serial Interface module, with step-by-step instructions on how to configure and use the serial in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
Serial use cases
Basic use case - transmit a character
In this use case, the serial module is configured for:
- Using USARTD0
- Baudrate: 9600
- Character length: 8 bit
- Parity mode: Disabled
- Stop bit: None
- RS232 mode
The use case waits for a received character on the configured USART and echoes the character back to the same USART.
Setup steps
Prerequisites
- System Clock Management (sysclk)
Example code
The following configuration must be added to the project (typically to a conf_uart_serial.h file, but it can also be added to your main application file.)
- Note
- The following takes SAM3X configuration for example, other devices have similar configuration, but their parameters may be different, refer to corresponding header files.
#define USART_SERIAL &USARTD0
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT false
A variable for the received byte must be added:
Add to application initialization:
sysclk_init();
static usart_serial_options_t usart_options = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
Workflow
- Initialize system clock:
- Create serial USART options struct:
static usart_serial_options_t usart_options = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
- Initialize the serial service:
Usage steps
Example code
Add to application C-file:
Workflow
- Wait for reception of a character:
- Echo the character back: