SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
USART receive character and echo back

In this use case, the USART module is configured for:

  • Using USART0
  • Baudrate: 9600
  • Character length: 8 bit
  • Parity mode: Disabled
  • Stop bit: None
  • RS232 mode

The use case waits for a received character on the configured USART and echoes the character back to the same USART.

Setup steps

Prerequisites

  1. System Clock Management (sysclock)
  2. Common IOPORT API (ioport)

Example code

The following configuration must be added to the project (typically to a conf_usart.h file, but it can also be added to your main application file.):

#define USART_SERIAL USART0
#define USART_SERIAL_ID ID_USART0 //USART0 for sam4l
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT

A variable for the received byte must be added:

uint32_t received_byte;

Add to application initialization:

sysclk_init();
board_init();
const sam_usart_opt_t usart_console_settings = {
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
};
#if SAM4L
sysclk_enable_peripheral_clock(USART_SERIAL);
#else
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
#endif
usart_init_rs232(USART_SERIAL, &usart_console_settings,
sysclk_get_main_hz());
usart_enable_tx(USART_SERIAL);
usart_enable_rx(USART_SERIAL);
#define US_MR_CHMODE_NORMAL
(US_MR) Normal Mode
void usart_enable_tx(Usart *p_usart)
Enable USART transmitter.
Definition usart.c:1384
void usart_enable_rx(Usart *p_usart)
Enable USART receiver.
Definition usart.c:1426
uint32_t usart_init_rs232(Usart *p_usart, const sam_usart_opt_t *p_usart_opt, uint32_t ul_mck)
Configure USART to work in RS232 mode.
Definition usart.c:274
micro definition for LIN mode of SAMV71
Definition usart.h:85

Workflow

  1. Initialize system clock:
    sysclk_init();
  2. Configure the USART Tx and Rx pins by call the board init function:
    board_init();
    Note
    Set the following define in conf_board.h file to enable COM port,it will be used in board_init() function to set up IOPorts for the USART pins. For SAM4L:
    #define CONF_BOARD_COM_PORT
    For other SAM devices:
    #define CONF_BOARD_UART_CONSOLE
  3. Create USART options struct:
    const sam_usart_opt_t usart_console_settings = {
    USART_SERIAL_BAUDRATE,
    USART_SERIAL_CHAR_LENGTH,
    USART_SERIAL_PARITY,
    USART_SERIAL_STOP_BIT,
    };
  4. Enable the clock to the USART module:
    #if SAM4L
    sysclk_enable_peripheral_clock(USART_SERIAL);
    #else
    sysclk_enable_peripheral_clock(USART_SERIAL_ID);
    #endif
  5. Initialize the USART module in RS232 mode:
    usart_init_rs232(USART_SERIAL, &usart_console_settings,
    sysclk_get_main_hz());
  6. Enable the Rx and Tx modes of the USART module:
    usart_enable_tx(USART_SERIAL);
    usart_enable_rx(USART_SERIAL);

Usage steps

Example code

Add to, e.g., main loop in application C-file:

received_byte = usart_getchar(USART_SERIAL);
usart_putchar(USART_SERIAL, received_byte);
uint32_t usart_getchar(Usart *p_usart, uint32_t *c)
Read from USART Receive Holding Register.
Definition usart.c:1781
uint32_t usart_putchar(Usart *p_usart, uint32_t c)
Write to USART Transmit Holding Register.
Definition usart.c:1725

Workflow

  1. Wait for reception of a character:
    usart_getchar(USART_SERIAL, &received_byte);
  2. Echo the character back:
    usart_putchar(USART_SERIAL, received_byte);