This quick start will echo back characters typed into the terminal, using asynchronous TX and RX callbacks from the USART peripheral.
In this use case the USART will be configured with the following settings:
- Asynchronous mode
- 9600 Baudrate
- 8-bits, No Parity and one Stop Bit
- TX and RX enabled and connected to the Xplained Pro Embedded Debugger virtual COM port
Setup
Prerequisites
There are no special setup requirements for this use-case.
Code
Add to the main application source file, outside of any functions:
#define MAX_RX_BUFFER_LENGTH 5
volatile uint8_t rx_buffer[MAX_RX_BUFFER_LENGTH];
Copy-paste the following callback function code to your user application:
{
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
void usart_write_callback(struct usart_module *const usart_module)
{
}
Copy-paste the following setup code to your user application:
{
#if(SAMR30E)
{
config_usart.baudrate = 9600;
config_usart.mux_setting = CDC_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = CDC_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = CDC_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = CDC_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = CDC_SERCOM_PINMUX_PAD3;
}
}
#else
{
config_usart.baudrate = 9600;
}
}
#endif
}
void configure_usart_callbacks(void)
{
}
Add to user application initialization (typically the start of main()
):
configure_usart_callbacks();
Workflow
- Create a module software instance structure for the USART module to store the USART driver state while it is in use.
- Note
- This should never go out of scope as long as the module is in use. In most cases, this should be global.
- Configure the USART module.
- Create a USART module configuration struct, which can be filled out to adjust the configuration of a physical USART peripheral.
- Initialize the USART configuration struct with the module's default values.
- Note
- This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
- Alter the USART settings to configure the physical pinout, baudrate, and other relevant parameters.
}
}
#else
{
config_usart.baudrate = 9600;
- Configure the USART module with the desired settings, retrying while the driver is busy until the configuration is stressfully set.
- Enable the USART module.
- Configure the USART callbacks.
- Register the TX and RX callback functions with the driver.
- Enable the TX and RX callbacks so that they will be called by the driver when appropriate.
Use Case
Code
Copy-paste the following code to your user application:
uint8_t string[] = "Hello World!\r\n";
while (true) {
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
Workflow
- Enable global interrupts, so that the callbacks can be fired.
- Send a string to the USART to show the demo is running, blocking until all characters have been sent.
uint8_t string[] = "Hello World!\r\n";
- Enter an infinite loop to continuously echo received values on the USART.
- Perform an asynchronous read of the USART, which will fire the registered callback when characters are received.
(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);