본문으로 건너뛰기

SAM4S2A stdio

ASF Wizard

  • Standard serial I/O (stdio) (driver): Common standard serial I/O management driver that implements a stdio serial interface on SAM devices.
  • USART - Serial interface (service): Common API which abstracts the user from directly accessing the register and provides a generic serial interface using USART.
  • PIO - Parallel Input/Output Controller (driver): The Parallel Input/Output Controller manages up to 32 fully programmable input/output lines. Each I/O line may be dedicated as a general-purpose I/O or be assigned to a function of an embedded peripheral. This assures effective optimization of the pins of a product.

src\config\conf_board.h

#ifndef CONF_BOARD_H
#define CONF_BOARD_H

#define CONSOLE_UART (Usart *)USART0

/** Enable Com Port. */
#define CONF_BOARD_UART_CONSOLE

#endif // CONF_BOARD_H

src\config\conf_uart_serial.h

#ifndef CONF_USART_SERIAL_H
#define CONF_USART_SERIAL_H

/* A reference setting for USART */
/** USART Interface */
#define CONF_UART CONSOLE_UART
/** Baudrate setting */
#define CONF_UART_BAUDRATE 115200
/** Character length setting */
#define CONF_UART_CHAR_LENGTH US_MR_CHRL_8_BIT
/** Parity setting */
#define CONF_UART_PARITY US_MR_PAR_NO
/** Stop bits setting */
#define CONF_UART_STOP_BITS US_MR_NBSTOP_1_BIT

#define USART_SERIAL CONF_UART
#define USART_SERIAL_ID ID_USART0
#define USART_SERIAL_BAUDRATE CONF_UART_BAUDRATE
#define USART_SERIAL_CHAR_LENGTH CONF_UART_CHAR_LENGTH
#define USART_SERIAL_PARITY CONF_UART_PARITY
#define USART_SERIAL_STOP_BIT CONF_UART_STOP_BITS

#define PINS_USART0_PIO PIOA
#define PINS_USART0_ID USART_SERIAL_ID
#define PINS_USART0_TYPE PIO_PERIPH_A
#define PINS_USART0_ATTR PIO_DEFAULT
#define PINS_USART0_MASK (PIO_PA5A_RXD0 | PIO_PA6A_TXD0)

#endif /* CONF_USART_SERIAL_H_INCLUDED */

Examples

#include "conf_uart_serial.h"

#include <asf.h>

void serial_init(void) {
pio_configure(
PINS_USART0_PIO, PINS_USART0_TYPE, PINS_USART0_MASK, PINS_USART0_ATTR);
pmc_enable_periph_clk(USART_SERIAL_ID);
sysclk_enable_peripheral_clock(USART_SERIAL_ID);

const usart_serial_options_t usart_serial_options
= {.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT};

stdio_serial_init(USART_SERIAL, &usart_serial_options);
}

int main(void) {
sysclk_init();
board_init();
/* Insert application code here, after the board has been initialized. */
wdt_disable(WDT);

serial_init();

for(;;) {
uint16_t temp;
scanf("%d", &temp);
printf("%d\r\n", temp);
}
}