Microchip® Advanced Software Framework

Quick Start Guide for SERCOM USART - Basic

This quick start will echo back characters typed into the terminal.

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:

struct usart_module usart_instance;

Copy-paste the following setup code to your user application:

void configure_usart(void)
{
struct usart_config config_usart;
usart_get_config_defaults(&config_usart);
#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;
while (usart_init(&usart_instance,
CDC_MODULE, &config_usart) != STATUS_OK) {
}
}
#else
{
config_usart.baudrate = 9600;
config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
while (usart_init(&usart_instance,
EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
}
}
#endif
usart_enable(&usart_instance);
}

Add to user application initialization (typically the start of main()):

configure_usart();

Workflow

  1. Create a module software instance structure for the USART module to store the USART driver state while it is in use.
    struct usart_module usart_instance;
    Note
    This should never go out of scope as long as the module is in use. In most cases, this should be global.
  2. Configure the USART module.
    1. Create a USART module configuration struct, which can be filled out to adjust the configuration of a physical USART peripheral.
      struct usart_config config_usart;
    2. Initialize the USART configuration struct with the module's default values.
      usart_get_config_defaults(&config_usart);
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Alter the USART settings to configure the physical pinout, baudrate, and other relevant parameters.
      #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;
    4. Configure the USART module with the desired settings, retrying while the driver is busy until the configuration is stressfully set.
      while (usart_init(&usart_instance,
      CDC_MODULE, &config_usart) != STATUS_OK) {
      }
    5. Enable the USART module.
      usart_enable(&usart_instance);

Use Case

Code

Copy-paste the following code to your user application:

uint8_t string[] = "Hello World!\r\n";
usart_write_buffer_wait(&usart_instance, string, sizeof(string));
uint16_t temp;
while (true) {
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
}
}
}

Workflow

  1. 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";
    usart_write_buffer_wait(&usart_instance, string, sizeof(string));
  2. Enter an infinite loop to continuously echo received values on the USART.
    while (true) {
    if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
    while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
    }
    }
    }
  3. Perform a blocking read of the USART, storing the received character into the previously declared temporary variable.
    if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
  4. Echo the received variable back to the USART via a blocking write.
    while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
    }