SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
Quick Start Guide for USART STDIO Integration

This guide shows how to use the STDIO Serial API on top of the Serial Interface service, so printf() and scanf() use a USART/UART as console I/O.

What STDIO Does

The STDIO serial layer connects C standard I/O (printf, putchar, scanf, getchar) to ASF serial functions. Internally, stdio_serial_init() binds stdio to usart_serial_putchar() / usart_serial_getchar().

Basic use case - USART console with printf/scanf

In this use case:

Setup steps

Prerequisites

  1. Serial Interface service
  2. STDIO serial API
  3. Board pin mux configured for the selected console peripheral
  4. conf_uart_serial.h (or equivalent) with baud/parity/format macros

Example code

#include <asf.h>
#include <stdio_serial.h>
#include <conf_uart_serial.h>
static const usart_serial_options_t stdio_options = {
.baudrate = CONF_UART_BAUDRATE,
.charlength = US_MR_CHRL_8_BIT,
.paritytype = CONF_UART_PARITY,
.stopbits = US_MR_NBSTOP_1_BIT,
};
int main(void)
{
uint32_t ch;
sysclk_init();
board_init();
stdio_serial_init(CONF_UART, &stdio_options);
printf("\\n\\rUSART STDIO ready\\n\\r");
while (1) {
scanf("%c", (char *)&ch); // blocking read
printf("%c", (char)ch); // echo back
}
}
#define US_MR_NBSTOP_1_BIT
(US_MR) 1 stop bit
#define US_MR_CHRL_8_BIT
(US_MR) Character length is 8 bits
static void stdio_serial_init(volatile void *usart, const usart_serial_options_t *opt)
Initializes the stdio in Serial Mode.
Common Standard I/O Serial Management.
usart_rs232_options_t usart_serial_options_t
Definition uart_serial.h:76

Workflow

  1. Initialize clocks with sysclk_init()
  2. Initialize pins/board with board_init()
  3. Fill usart_serial_options_t with baud and frame format
  4. Call stdio_serial_init(CONF_UART, &stdio_options)
  5. Use printf() / scanf() in the main loop

Notes

  • scanf()/getchar() are blocking unless you add your own non-blocking logic.
  • For SAM targets, CONF_UART usually maps to CONSOLE_UART in conf_uart_serial.h.
  • Reference example: common/utils/stdio/stdio_serial/stdio_serial_example/stdio_serial_example.c