This is the quick start guide for the USART module, with step-by-step instructions on how to configure and use the driver 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.
- Note
- Some SAM devices contain both USART and UART modules, with the latter being a subset in functionality of the former but physically separate peripherals. UART modules are compatible with the USART driver, but only for the functions and modes supported by the base UART driver.
USART use cases
- Note
- The USART pins configuration are not included here. Please refer the related code in board_init() function.
Basic use case - transmit a character
In this use case, the USART module is configured for:
- Using USART0
- Baudrate: 9600
- Character length: 8 bit
- Parity mode: Disabled
- Stop bit: None
- RS232 mode
Setup steps
Prerequisites
- System Clock Management (sysclock)
- Common IOPORT API (ioport)
Example code
The following configuration must be added to the project (typically to a conf_usart.h file, but it can also be added to your main application file.)
#define USART_SERIAL USART0
#define USART_SERIAL_ID ID_USART0
#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 US_MR_NBSTOP_1_BIT
Add to application initialization:
sysclk_init();
board_init();
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
};
#if SAM4L
sysclk_enable_peripheral_clock(USART_SERIAL);
#else
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
#endif
sysclk_get_main_hz());
#define US_MR_CHMODE_NORMAL
(US_MR) Normal Mode
void usart_enable_tx(Usart *p_usart)
Enable USART transmitter.
void usart_enable_rx(Usart *p_usart)
Enable USART receiver.
uint32_t usart_init_rs232(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in RS232 mode.
micro definition for LIN mode of SAMV71
Workflow
- Initialize system clock:
- Configure the USART Tx and Rx pins by call the board init function:
- Note
- Set the following define in conf_board.h file to enable COM port,it will be used in board_init() function to set up IOPorts for the USART pins. For SAM4L:
#define CONF_BOARD_COM_PORT
For other SAM devices: #define CONF_BOARD_UART_CONSOLE
- Create USART options struct:
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
};
- Enable the clock to the USART module:
#if SAM4L
sysclk_enable_peripheral_clock(USART_SERIAL);
#else
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
#endif
- Initialize the USART module in RS232 mode:
- Enable the Rx and Tx modes of the USART module:
Usage steps
Example code
Add to application C-file:
uint32_t usart_putchar(Usart *p_usart, uint32_t c)
Write to USART Transmit Holding Register.
Workflow
- Send an 'a' character via USART