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
- Serial Interface service
- STDIO serial API
- Board pin mux configured for the selected console peripheral
- conf_uart_serial.h (or equivalent) with baud/parity/format macros
Example code
#include <asf.h>
#include <conf_uart_serial.h>
.baudrate = CONF_UART_BAUDRATE,
.paritytype = CONF_UART_PARITY,
};
int main(void)
{
uint32_t ch;
sysclk_init();
board_init();
printf("\\n\\rUSART STDIO ready\\n\\r");
while (1) {
scanf("%c", (char *)&ch);
printf("%c", (char)ch);
}
}
#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
Workflow
- Initialize clocks with sysclk_init()
- Initialize pins/board with board_init()
- Fill usart_serial_options_t with baud and frame format
- Call stdio_serial_init(CONF_UART, &stdio_options)
- 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