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. The character reception is performed via an interrupt handler, rather than the polling method used in USART receive character and echo back.
Setup steps
Prerequisites
- System Clock Management (sysclock)
- Parallel Input/Output Controller (pio)
- Power Management Controller (pmc)
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_ISR_HANDLER USART0_Handler
#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:
Add to application initialization:
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
#if SAM4L
#else
#endif
Workflow
- Initialize system clock:
- Configure the USART Tx and Rx pins by call the board init function:
- 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
- Create USART options struct:
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
- Enable the clock to the USART module:
- Initialize the USART module in RS232 mode:
- Enable the Rx and Tx modes of the USART module:
- Enable the USART character reception interrupt, and general interrupts for the USART module.
Usage steps
Example code
Add to your main application C-file the USART interrupt handler:
void USART_SERIAL_ISR_HANDLER(void)
{
uint32_t received_byte;
}
}
Workflow
- When the USART ISR fires, retrieve the USART module interrupt flags:
- Check if the USART Receive Character interrupt has fired:
- If a character has been received, fetch it into a temporary variable:
- Echo the character back: