Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

ASF USART Serial Interface for UART Tx and Rx

Created on: 14 June 2016

Part 12 of the ASF ARM Tutorial

Communicating between a microcontroller and terminal window using the ASF USART serial interface service module. How to send and receive characters and/or packets of data using a UART or USART with Atmel Software Framework and demonstrated on an ARM Cortex board.

In this part of the ASF tutorial series a UART is used to transmit and receive characters and packets of data using the ASF USART serial interface module. This module provides functions for initializing a UART or USART for serial communications and provides functions for sending and receiving individual characters or packets of data.

Code is used to send and receive data over the virtual COM port which is part of the EDBG unit on an Atmel evaluation board. The same code works on an RS-232 serial port.

ASF USART Serial Interface Hardware and Software


A SAM4N Xplained Pro board is used which communicates with a PC over a USB cable. Hardware is the same as used in the previous part of this tutorial series.


As with the previous part of this tutorial series, a terminal emulator will be needed such as Tera Term for Windows.

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual

Creating the ASF USART Serial Interface Project

Create a new ASF project called uart in Atmel Studio as explained in the new ASF project quick start checklist.

ASF Modules

The ASF module that adds the UART and USART functionality to the project is the USART - Serial interface (service) module. Also add the IOPORT module which is needed to configure the UART or USART pins.

ASF modules used in the uart project
ASF Modules used in the UART Project

Hardware Definitions and Configuration (conf_board.h and conf_uart_serial.h)

UART0 is defined as the console UART as it is connected to the EDBG virtual COM port on the SAM4N Xplained Pro board.


#ifndef CONF_BOARD_H
#define CONF_BOARD_H

#define CONSOLE_UART                (Usart *)UART0

// clock resonators
#define BOARD_FREQ_SLCK_XTAL        (32768U)
#define BOARD_FREQ_SLCK_BYPASS      (32768U)
#define BOARD_FREQ_MAINCK_XTAL      (12000000U)
#define BOARD_FREQ_MAINCK_BYPASS    (12000000U)
#define BOARD_MCK                   CHIP_FREQ_CPU_MAX
#define BOARD_OSC_STARTUP_US        15625

#endif // CONF_BOARD_H

The conf_uart_serial.h file is added to the project when the USART Serial interface service module is added to the project. Uncomment the USART settings in this file and change them to the desired values. Be sure to set the UART name (CONF_UART) to point to the correct UART for the board.



/* A reference setting for UART */
/** UART Interface */
//#define CONF_UART            CONSOLE_UART
/** Baudrate setting */
//#define CONF_UART_BAUDRATE   115200
/** Parity setting */

/* A reference setting for USART */
/** USART Interface */
#define CONF_UART              CONSOLE_UART
/** Baudrate setting */
#define CONF_UART_BAUDRATE     115200
/** Character length setting */
/** Parity setting */
/** Stop bits setting */


Hardware Initialization (init.c)

Pins PA9 and PA10 are set to use internal peripheral A which is UART0 by calling ioport_set_port_mode(). These two UART pins are then disabled as I/O pins by calling ioport_disable_port() which dedicates them to the UART.


#include <asf.h>
#include <board.h>
#include <conf_board.h>

void board_init(void)
    WDT->WDT_MR = WDT_MR_WDDIS;     // disable watchdog
    ioport_init();                  // call before using IOPORT service
    // configure UART pins
    ioport_set_port_mode(IOPORT_PIOA, PIO_PA9A_URXD0 | PIO_PA10A_UTXD0, IOPORT_MODE_MUX_A);
    ioport_disable_port(IOPORT_PIOA, PIO_PA9A_URXD0 | PIO_PA10A_UTXD0);

Application Code (main.c)

The application demonstrates some of the UART transmit and receive functions available in the USART - Serial interface service module. Although these functions are more suited to applications where devices or systems may be communicating serially with each other, they are demonstrated using strings and characters in this example.

Using strings and characters makes it easier to demonstrate the functions by using a simple terminal emulator program running on a PC.


#include <asf.h>

int main (void)
    const char str1[] = "Type 'a' to continue...\r\n";
    uint8_t rx_char = 0;

    static usart_serial_options_t usart_options = {
        .baudrate =   CONF_UART_BAUDRATE,
        .charlength = CONF_UART_CHAR_LENGTH,
        .paritytype = CONF_UART_PARITY,
        .stopbits =   CONF_UART_STOP_BITS
    // initialize the UART
    usart_serial_init(CONF_UART, &usart_options);
    // send a string using the write packet function
    usart_serial_write_packet(CONF_UART, (const uint8_t*)str1, sizeof(str1) - 1);
    do {
        // get a single character
        usart_serial_getchar(CONF_UART, &rx_char);
    } while (rx_char != 'a');
    // send a single character
    usart_serial_putchar(CONF_UART, 'A');
    while (1) {

The main application code sends a string out of the UART using the usart_serial_write_packet() function. This function would typically be used to send some communication data packet from one system to another.

usart_serial_getchar() is used to receive a single character from the UART serial port.

usart_serial_putchar() sends a single character to the terminal program using the UART.

A function called usart_serial_read_packet() is also available as part of the USART service module. This function is not used in the example program. It simply receives a packet of data of a certain length from the UART.

The following video demonstrates the UART application running.

Can't see the video? View on YouTube →