Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
USART module (USART)

See Quick start guide for USART module.

This is a driver for configuring, enabling, disabling and use of the on-chip USART.

Dependencies

The USART module depends on the following modules:

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the USART module (USART).
 

Data Structures

struct  usart_rs232_options
 
struct  usart_spi_options
 
struct  USART_struct
 

Macros

#define USART_BAUD_1200   0x00
 
#define USART_BAUD_19200   0x04
 
#define USART_BAUD_2400   0x01
 
#define USART_BAUD_38400   0x05
 
#define USART_BAUD_4800   0x02
 
#define USART_BAUD_57600   0x06
 
#define USART_BAUD_9600   0x03
 
#define USART_BAUD_UNDEFINED   0xFF
 
#define USART_CHSIZE00_bm   0x02 /* Character Size 00 bit 1 mask. */
 
#define USART_CHSIZE00_bp   1 /* Character Size bit 00 position. */
 
#define USART_CHSIZE01_bm   0x04 /* Character Size 10 bit 1 mask. */
 
#define USART_CHSIZE01_bp   2 /* Character Size 10 bit position. */
 
#define USART_CHSIZE01C_gm   0x06 /* Character Size 10 bit 1 mask. */
 
#define USART_CHSIZE01C_gp   1 /* Character Size 10 bit position. */
 
#define USART_CHSIZE2_bm   0x04 /* Character Size bit mask. */
 
#define USART_CHSIZE2_bp   2 /* Character Size bit position. */
 
#define USART_DOR_bm   0x08 /* Data Over Run bit mask.*/
 
#define USART_DOR_bp   3 /* Data Over Run bit position.*/
 
#define USART_DORD_bm   0x04 /* Data order bit mask. */
 
#define USART_DORD_bp   2 /* Data order bit position. */
 
#define USART_DRE_bm   0x20 /* DATA Register Empty Bit mask.*/
 
#define USART_DRE_bp   5 /* DATA Register Empty bit position.*/
 
#define USART_DRIE_bm   0x20 /* Data register empty interrupt Enable bit mask.*/
 
#define USART_DRIE_bp   5 /* Data register empty interrupt Enable bit position.*/
 
#define USART_FE_bm   0x10 /* Frame Error bit mask.*/
 
#define USART_FE_bp   4 /*Frame error bit position.*/
 
#define USART_MPCM_bm   0x01 /* Multi Processor bit mask.*/
 
#define USART_MPMC_bp   0 /* Multi processor bit position.*/
 
#define USART_PMODE00_bm   0x10 /* USART Parity Mode Select 00 bit mask.*/
 
#define USART_PMODE00_bp   4 /* USART Parity Mode Select 00 bit position.*/
 
#define USART_PMODE01_bm   0x20 /* USART Parity Mode Select 01 bit mask.*/
 
#define USART_PMODE01_bp   5 /* USART Parity Mode Select 01 bit position.*/
 
#define USART_PMODE_gm   0x30 /* USART Parity Mode Select grp mask.*/
 
#define USART_PMODE_gp   4 /* USART Parity Mode Select grp position.*/
 
#define USART_RXB8_bm   0x02 /* Transmit bit 8 bit mask. */
 
#define USART_RXB8_bp   1 /* Transmit bit 8 bit position. */
 
#define USART_RXC_bm   0x80 /* RX complete bit mask.*/
 
#define USART_RXC_bp   7 /* RX complete bit position.*/
 
#define USART_RXCIE_bm   0x80 /* RX complete interrupt Enable bit mask.*/
 
#define USART_RXCIE_bp   7 /* RX complete interrupt Enable bit position.*/
 
#define USART_RXEN_bm   0x10 /* Receiver Enable bit mask. */
 
#define USART_RXEN_bp   4 /* Receiver Enable bit position. */
 
#define USART_STOPB_bm   0x08 /* USART stop bit mask.*/
 
#define USART_STOPB_bp   3 /* USART stop bit Position.*/
 
#define USART_TXB8_bm   0x01 /* Transmit bit 8 bit mask. */
 
#define USART_TXB8_bp   0 /* Transmit bit 8 bit position. */
 
#define USART_TXC_bm   0x40 /* TX complete bit mask.*/
 
#define USART_TXC_bp   6 /* TX complete bit position.*/
 
#define USART_TXCIE_bm   0x40 /* TX complete interrupt Enable bit mask.*/
 
#define USART_TXCIE_bp   6 /* TX complete interrupt Enable bit position.*/
 
#define USART_TXEN_bm   0x08 /* Transmitter Enable bit mask. */
 
#define USART_TXEN_bp   3 /* Transmitter Enable bit position. */
 
#define USART_U2X_bm   0x02 /* Double TX speed bit mask.*/
 
#define USART_U2X_bp   1 /* Double TX speed bit position.*/
 
#define USART_UCPHA_bm   0x02 /* Clock Phase bit mask. */
 
#define USART_UCPHA_bp   1 /* Clock Phase bit position. */
 
#define USART_UCPOL_bm   0x01 /* Sync mode Pol bit 1 mask. */
 
#define USART_UCPOL_bp   0 /*Sync mode Pol bit 0 position */
 
#define USART_UMSEL00_bm   0x40 /* USART Mode Select 00 bit mask.*/
 
#define USART_UMSEL00_bp   6 /* USART Mode Select 00 bit position.*/
 
#define USART_UMSEL01_bm   0x80 /* USART Mode Select 01 bit mask.*/
 
#define USART_UMSEL01_bp   7 /* USART Mode Select 01 bit position.*/
 
#define USART_UMSEL01_gm   0xC0 /* USART Mode Select 01 grp mask.*/
 
#define USART_UMSEL01_gp   6 /* USART Mode Select 01 grp position.*/
 
#define USART_UPE_bm   0x04 /* Parity error bit mask.*/
 
#define USART_UPE_bp   2 /* Parity error bit position.*/
 
#define USARTA0
 
#define USARTA1
 

Typedefs

typedef volatile uint16_t register16_t
 
typedef volatile uint8_t register8_t
 
typedef enum USART_CHSIZE_enum USART_CHSIZE_t
 
typedef enum USART_CMODE_enum USART_CMODE_t
 
typedef enum USART_PMODE_enum USART_PMODE_t
 
typedef struct usart_rs232_options usart_rs232_options_t
 
typedef struct usart_spi_options usart_spi_options_t
 
typedef struct USART_struct USART_t
 

Enumerations

enum  USART_CHSIZE_enum {
  USART_CHSIZE_5BIT_gc = (0x00),
  USART_CHSIZE_6BIT_gc = (0x01),
  USART_CHSIZE_7BIT_gc = (0x02),
  USART_CHSIZE_8BIT_gc = (0x03),
  USART_CHSIZE_9BIT_gc = (0x07),
  USART_CHSIZE_5BIT_gc = (0x00),
  USART_CHSIZE_6BIT_gc = (0x01),
  USART_CHSIZE_7BIT_gc = (0x02),
  USART_CHSIZE_8BIT_gc = (0x03),
  USART_CHSIZE_9BIT_gc = (0x07)
}
 
enum  USART_CMODE_enum {
  USART_CMODE_ASYNCHRONOUS_gc = (0x00 << USART_UMSEL01_gp),
  USART_CMODE_SYNCHRONOUS_gc = (0x01 << USART_UMSEL01_gp),
  USART_CMODE_IRDA_gc = (0x02 << USART_UMSEL01_gp),
  USART_CMODE_MSPI_gc = (0x03 << USART_UMSEL01_gp),
  USART_CMODE_ASYNCHRONOUS_gc = (0x00 << USART_UMSEL01_gp),
  USART_CMODE_SYNCHRONOUS_gc = (0x01 << USART_UMSEL01_gp),
  USART_CMODE_IRDA_gc = (0x02 << USART_UMSEL01_gp),
  USART_CMODE_MSPI_gc = (0x03 << USART_UMSEL01_gp)
}
 
enum  USART_PMODE_enum {
  USART_PMODE_DISABLED_gc = (0x00 << USART_PMODE_gp),
  USART_PMODE_EVEN_gc = (0x02 << USART_PMODE_gp),
  USART_PMODE_ODD_gc = (0x03 << USART_PMODE_gp),
  USART_PMODE_DISABLED_gc = (0x00 << USART_PMODE_gp),
  USART_PMODE_EVEN_gc = (0x02 << USART_PMODE_gp),
  USART_PMODE_ODD_gc = (0x03 << USART_PMODE_gp)
}
 

Functions

static PROGMEM_DECLARE (uint16_t, baudctrl_1mhz[])
 
static PROGMEM_DECLARE (uint16_t, baudctrl_8mhz[])
 
static PROGMEM_DECLARE (uint16_t, baudctrl_16mhz[])
 
static void usart_clear_tx_complete (USART_t *usart)
 Clear TX complete interrupt flag. More...
 
static void usart_data_empty_interrupt_disable (USART_t *usart)
 Disable USART data register empty interrupt. More...
 
static void usart_data_empty_interrupt_enable (USART_t *usart)
 Enable USART data register empty interrupt. More...
 
static bool usart_data_register_is_empty (USART_t *usart)
 Check if data register empty flag is set. More...
 
static void usart_double_baud_disable (USART_t *usart)
 Disable the Doubling of USART transmission speed. More...
 
static void usart_double_baud_enable (USART_t *usart)
 Double the USART transmission speed. More...
 
static void usart_format_set (USART_t *usart, USART_CHSIZE_t charSize, USART_PMODE_t parityMode, bool twoStopBits)
 Configure the USART frame format. More...
 
static uint8_t usart_get (USART_t *usart)
 Read a data to the USART data register. More...
 
uint8_t usart_getchar (USART_t *usart)
 Receive a data with the USART module. More...
 
bool usart_init_rs232 (USART_t *usart, const usart_rs232_options_t *opt)
 Initialize USART in RS232 mode. More...
 
void usart_init_spi (USART_t *usart, const usart_spi_options_t *opt)
 Initialize USART in SPI master mode. More...
 
static void usart_put (USART_t *usart, uint8_t txdata)
 Write a data to the USART data register. More...
 
status_code_t usart_putchar (USART_t *usart, uint8_t c)
 Send a data with the USART module. More...
 
static void usart_rx_complete_interrupt_disable (USART_t *usart)
 Disable USART receive complete interrupt. More...
 
static void usart_rx_complete_interrupt_enable (USART_t *usart)
 Enable USART receive complete interrupt. More...
 
static void usart_rx_disable (USART_t *usart)
 Disable USART receiver. More...
 
static void usart_rx_enable (USART_t *usart)
 Enable USART receiver. More...
 
static bool usart_rx_is_complete (USART_t *usart)
 Checks if the RX complete interrupt flag is set. More...
 
bool usart_set_baudrate (USART_t *usart, uint32_t baud, uint32_t cpu_hz)
 Set the baudrate value in the USART module. More...
 
void usart_set_baudrate_precalculated (USART_t *usart, uint32_t baud, uint32_t cpu_hz)
 Set the baudrate using pre calculated BAUDCTRL values stored in program memory. More...
 
static void usart_set_mode (USART_t *usart, USART_CMODE_t usartmode)
 Set the mode the USART run in. More...
 
void usart_spi_set_baudrate (USART_t *usart, uint32_t baud, uint32_t cpu_hz)
 Set the baudrate value in the USART_SPI module. More...
 
static uint8_t usart_spi_transmit (USART_t *usart, uint8_t txdata)
 Performs a data transfer on the USART in SPI mode. More...
 
static void usart_tx_complete_interrupt_disable (USART_t *usart)
 Disable USART transmit complete interrupt. More...
 
static void usart_tx_complete_interrupt_enable (USART_t *usart)
 Enable USART transmit complete interrupt. More...
 
static void usart_tx_disable (USART_t *usart)
 Disable USART transmitter. More...
 
static void usart_tx_enable (USART_t *usart)
 Enable USART transmitter. More...
 
static bool usart_tx_is_complete (USART_t *usart)
 Checks if the TX complete interrupt flag is set. More...
 

Quick start guide for USART module

This is the quick start guide for the USART module, with step-by-step instructions on how to configure and use the driver in a selection of use cases.The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case - transmit a character

USART use cases

Basic use case - transmit a character

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

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

Setup steps

Prerequisites

  1. System Clock Management

Example code

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

#define USART_SERIAL &USARTA1
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc
#define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc
#define USART_SERIAL_STOP_BIT false

Add to application initialization:

static usart_rs232_options_t USART_SERIAL_OPTIONS = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS);

Workflow

  1. Initialize system clock:
  2. Create USART options struct:
    • static usart_rs232_options_t USART_SERIAL_OPTIONS = {
      .baudrate = USART_SERIAL_BAUDRATE,
      .charlength = USART_SERIAL_CHAR_LENGTH,
      .paritytype = USART_SERIAL_PARITY,
      .stopbits = USART_SERIAL_STOP_BIT
      };
  3. Initialize in RS232 mode:

Usage steps

Example code

Add to application C-file:

usart_putchar(USART_SERIAL, 'a');

Workflow

  1. Send an 'a' character via USART

USART receive character and echo back

In this use case, the USART module is configured for:
  • Using USARTA1
  • 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

Example code

  1. 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 &USARTA1
    #define USART_SERIAL_BAUDRATE 9600
    #define USART_SERIAL_CHAR_LENGTH USART_CHSIZE_8BIT_gc
    #define USART_SERIAL_PARITY USART_PMODE_DISABLED_gc
    #define USART_SERIAL_STOP_BIT false

A variable for the received byte must be added:

uint8_t received_byte;

Add to application initialization:

static usart_rs232_options_t USART_SERIAL_OPTIONS = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS);

Workflow

  1. Initialize system clock:
  2. Create USART options struct:
    • static usart_rs232_options_t USART_SERIAL_OPTIONS = {
      .baudrate = USART_SERIAL_BAUDRATE,
      .charlength = USART_SERIAL_CHAR_LENGTH,
      .paritytype = USART_SERIAL_PARITY,
      .stopbits = USART_SERIAL_STOP_BIT
      };
  3. Initialize in RS232 mode:
    • megarf_usart_init_rs232(USART_SERIAL, &USART_SERIAL_OPTIONS);

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);

Workflow

  1. Wait for reception of a character:
  2. Echo the character back:

#define USART_BAUD_1200   0x00
#define USART_BAUD_19200   0x04
#define USART_BAUD_2400   0x01
#define USART_BAUD_38400   0x05
#define USART_BAUD_4800   0x02
#define USART_BAUD_57600   0x06
#define USART_BAUD_9600   0x03
#define USART_BAUD_UNDEFINED   0xFF
#define USART_CHSIZE00_bm   0x02 /* Character Size 00 bit 1 mask. */
#define USART_CHSIZE00_bp   1 /* Character Size bit 00 position. */
#define USART_CHSIZE01_bm   0x04 /* Character Size 10 bit 1 mask. */
#define USART_CHSIZE01_bp   2 /* Character Size 10 bit position. */
#define USART_CHSIZE01C_gm   0x06 /* Character Size 10 bit 1 mask. */

Referenced by usart_format_set().

#define USART_CHSIZE01C_gp   1 /* Character Size 10 bit position. */

Referenced by usart_format_set().

#define USART_CHSIZE2_bm   0x04 /* Character Size bit mask. */

Referenced by usart_format_set().

#define USART_CHSIZE2_bp   2 /* Character Size bit position. */

Referenced by usart_format_set().

#define USART_DOR_bm   0x08 /* Data Over Run bit mask.*/
#define USART_DOR_bp   3 /* Data Over Run bit position.*/
#define USART_DORD_bm   0x04 /* Data order bit mask. */
#define USART_DORD_bp   2 /* Data order bit position. */
#define USART_DRE_bm   0x20 /* DATA Register Empty Bit mask.*/
#define USART_DRE_bp   5 /* DATA Register Empty bit position.*/
#define USART_DRIE_bm   0x20 /* Data register empty interrupt Enable bit mask.*/
#define USART_DRIE_bp   5 /* Data register empty interrupt Enable bit position.*/
#define USART_FE_bm   0x10 /* Frame Error bit mask.*/
#define USART_FE_bp   4 /*Frame error bit position.*/
#define USART_MPCM_bm   0x01 /* Multi Processor bit mask.*/
#define USART_MPMC_bp   0 /* Multi processor bit position.*/
#define USART_PMODE00_bm   0x10 /* USART Parity Mode Select 00 bit mask.*/
#define USART_PMODE00_bp   4 /* USART Parity Mode Select 00 bit position.*/
#define USART_PMODE01_bm   0x20 /* USART Parity Mode Select 01 bit mask.*/
#define USART_PMODE01_bp   5 /* USART Parity Mode Select 01 bit position.*/
#define USART_PMODE_gm   0x30 /* USART Parity Mode Select grp mask.*/

Referenced by usart_format_set().

#define USART_PMODE_gp   4 /* USART Parity Mode Select grp position.*/
#define USART_RXB8_bm   0x02 /* Transmit bit 8 bit mask. */
#define USART_RXB8_bp   1 /* Transmit bit 8 bit position. */
#define USART_RXC_bm   0x80 /* RX complete bit mask.*/
#define USART_RXC_bp   7 /* RX complete bit position.*/
#define USART_RXCIE_bm   0x80 /* RX complete interrupt Enable bit mask.*/
#define USART_RXCIE_bp   7 /* RX complete interrupt Enable bit position.*/
#define USART_RXEN_bm   0x10 /* Receiver Enable bit mask. */
#define USART_RXEN_bp   4 /* Receiver Enable bit position. */
#define USART_STOPB_bm   0x08 /* USART stop bit mask.*/

Referenced by usart_format_set().

#define USART_STOPB_bp   3 /* USART stop bit Position.*/

Referenced by usart_format_set().

#define USART_TXB8_bm   0x01 /* Transmit bit 8 bit mask. */
#define USART_TXB8_bp   0 /* Transmit bit 8 bit position. */
#define USART_TXC_bm   0x40 /* TX complete bit mask.*/
#define USART_TXC_bp   6 /* TX complete bit position.*/
#define USART_TXCIE_bm   0x40 /* TX complete interrupt Enable bit mask.*/
#define USART_TXCIE_bp   6 /* TX complete interrupt Enable bit position.*/
#define USART_TXEN_bm   0x08 /* Transmitter Enable bit mask. */
#define USART_TXEN_bp   3 /* Transmitter Enable bit position. */
#define USART_U2X_bm   0x02 /* Double TX speed bit mask.*/
#define USART_U2X_bp   1 /* Double TX speed bit position.*/
#define USART_UCPHA_bm   0x02 /* Clock Phase bit mask. */
#define USART_UCPHA_bp   1 /* Clock Phase bit position. */
#define USART_UCPOL_bm   0x01 /* Sync mode Pol bit 1 mask. */
#define USART_UCPOL_bp   0 /*Sync mode Pol bit 0 position */
#define USART_UMSEL00_bm   0x40 /* USART Mode Select 00 bit mask.*/
#define USART_UMSEL00_bp   6 /* USART Mode Select 00 bit position.*/
#define USART_UMSEL01_bm   0x80 /* USART Mode Select 01 bit mask.*/
#define USART_UMSEL01_bp   7 /* USART Mode Select 01 bit position.*/
#define USART_UMSEL01_gm   0xC0 /* USART Mode Select 01 grp mask.*/

Referenced by usart_set_mode().

#define USART_UMSEL01_gp   6 /* USART Mode Select 01 grp position.*/
#define USART_UPE_bm   0x04 /* Parity error bit mask.*/
#define USART_UPE_bp   2 /* Parity error bit position.*/
#define USARTA0
Value:
(*(USART_t *)0xC0) /* Universal Asynchronous
* Receiver-Transmitter A0 */
Definition: usart_mega.h:86
#define USARTA1
Value:
(*(USART_t *)0XC8) /* Universal Asynchronous
* Receiver-Transmitter A1 */
Definition: usart_mega.h:86

typedef volatile uint16_t register16_t
typedef volatile uint8_t register8_t
typedef struct USART_struct USART_t

Enumerator
USART_CHSIZE_5BIT_gc 
USART_CHSIZE_6BIT_gc 
USART_CHSIZE_7BIT_gc 
USART_CHSIZE_8BIT_gc 
USART_CHSIZE_9BIT_gc 
USART_CHSIZE_5BIT_gc 
USART_CHSIZE_6BIT_gc 
USART_CHSIZE_7BIT_gc 
USART_CHSIZE_8BIT_gc 
USART_CHSIZE_9BIT_gc 
Enumerator
USART_CMODE_ASYNCHRONOUS_gc 
USART_CMODE_SYNCHRONOUS_gc 
USART_CMODE_IRDA_gc 
USART_CMODE_MSPI_gc 
USART_CMODE_ASYNCHRONOUS_gc 
USART_CMODE_SYNCHRONOUS_gc 
USART_CMODE_IRDA_gc 
USART_CMODE_MSPI_gc 
Enumerator
USART_PMODE_DISABLED_gc 
USART_PMODE_EVEN_gc 
USART_PMODE_ODD_gc 
USART_PMODE_DISABLED_gc 
USART_PMODE_EVEN_gc 
USART_PMODE_ODD_gc 

static PROGMEM_DECLARE ( uint16_t  ,
baudctrl_1mhz  [] 
)
static
static PROGMEM_DECLARE ( uint16_t  ,
baudctrl_8mhz  [] 
)
static
static PROGMEM_DECLARE ( uint16_t  ,
baudctrl_16mhz  [] 
)
static
static void usart_clear_tx_complete ( USART_t usart)
inlinestatic

Clear TX complete interrupt flag.

TX flag is clear after complete transmission, automatically.

Parameters
usartThe USART module.

References USART_struct::UCSRnA, and USART_TXC_bm.

Referenced by usart_spi_transmit().

static void usart_data_empty_interrupt_disable ( USART_t usart)
inlinestatic

Disable USART data register empty interrupt.

Parameters
usartPointer to the USART module.

References USART_DRIE_bm.

static void usart_data_empty_interrupt_enable ( USART_t usart)
inlinestatic

Enable USART data register empty interrupt.

Parameters
usartPointer to the USART module.

References USART_DRIE_bm.

static bool usart_data_register_is_empty ( USART_t usart)
inlinestatic

Check if data register empty flag is set.

Parameters
usartThe USART module.

References USART_DRE_bm.

Referenced by usart_spi_transmit().

static void usart_double_baud_disable ( USART_t usart)
inlinestatic

Disable the Doubling of USART transmission speed.

Parameters
usartThe USART module.

References USART_struct::UCSRnA, and USART_U2X_bm.

static void usart_double_baud_enable ( USART_t usart)
inlinestatic

Double the USART transmission speed.

Parameters
usartThe USART module.

References USART_struct::UCSRnA, and USART_U2X_bm.

static void usart_format_set ( USART_t usart,
USART_CHSIZE_t  charSize,
USART_PMODE_t  parityMode,
bool  twoStopBits 
)
inlinestatic

Configure the USART frame format.

Sets the frame format, Frame Size, parity mode and number of stop bits.

Parameters
usartPointer to the USART module
charSizeThe character size. Use USART_CHSIZE_t type.
parityModeThe parity Mode. Use USART_PMODE_t type.
twoStopBitsEnable two stop bit mode. Use bool type.

References USART_CHSIZE01C_gm, USART_CHSIZE01C_gp, USART_CHSIZE2_bm, USART_CHSIZE2_bp, USART_PMODE_gm, USART_STOPB_bm, and USART_STOPB_bp.

static uint8_t usart_get ( USART_t usart)
inlinestatic

Read a data to the USART data register.

Parameters
usartThe USART module.
Returns
The received data

Referenced by usart_spi_transmit().

uint8_t usart_getchar ( USART_t usart)

Receive a data with the USART module.

This function returns the received data from the USART module.

Parameters
usartThe USART module.
Returns
The received data.

References USART_struct::UDR, and usart_rx_is_complete().

Referenced by main(), run_loopback_syncmode_test(), run_loopback_test(), and usart_serial_getchar().

bool usart_init_rs232 ( USART_t usart,
const usart_rs232_options_t opt 
)

Initialize USART in RS232 mode.

This function initializes the USART module in RS232 mode using the usart_rs232_options_t configuration structure and CPU frequency.

Parameters
usartThe USART module.
optThe RS232 configuration option.
Return values
trueif the initialization was successful
falseif the initialization failed (error in baud rate calculation)

References usart_rs232_options::baudrate, usart_rs232_options::charlength, usart_rs232_options::paritytype, usart_rs232_options::stopbits, sysclk_get_source_clock_hz(), USART_CMODE_ASYNCHRONOUS_gc, usart_enable_module_clock(), usart_format_set(), usart_rx_enable(), usart_set_baudrate(), usart_set_mode(), and usart_tx_enable().

Referenced by main(), run_check_registers_test(), run_loopback_test(), and usart_serial_init().

void usart_init_spi ( USART_t usart,
const usart_spi_options_t opt 
)

Initialize USART in SPI master mode.

This function initializes the USART module in SPI master mode using the usart_spi_options_t configuration structure and CPU frequency.

Parameters
usartThe USART module.
optThe RS232 configuration option.

References usart_spi_options::baudrate, usart_spi_options::data_order, ioport_configure_port_pin(), IOPORT_CREATE_PIN, IOPORT_DIR_OUTPUT, IOPORT_INIT_HIGH, ioport_pin_to_mask(), ioport_pin_to_port(), usart_spi_options::spimode, sysclk_get_source_clock_hz(), USART_struct::UBRR, USART_struct::UCSRnC, USART_CMODE_MSPI_gc, USART_DORD_bm, usart_enable_module_clock(), usart_rx_enable(), usart_set_mode(), usart_spi_set_baudrate(), usart_tx_enable(), USART_UCPHA_bm, and USART_UCPOL_bm.

Referenced by usart_spi_setup_device().

static void usart_put ( USART_t usart,
uint8_t  txdata 
)
inlinestatic

Write a data to the USART data register.

Parameters
usartThe USART module.
txdataThe data to be transmitted.

Referenced by usart_spi_transmit().

status_code_t usart_putchar ( USART_t usart,
uint8_t  c 
)

Send a data with the USART module.

This function outputs a data using the USART module.

Parameters
usartThe USART module.
cThe data to send.
Returns
STATUS_OK

References STATUS_OK, USART_struct::UDR, and usart_data_register_is_empty().

Referenced by example_cmp1_int_cb(), example_cmp2_int_cb(), example_cmp3_int_cb(), example_ovf_int_cb(), main(), run_loopback_syncmode_test(), run_loopback_test(), and usart_serial_putchar().

static void usart_rx_complete_interrupt_disable ( USART_t usart)
inlinestatic

Disable USART receive complete interrupt.

Parameters
usartPointer to the USART module.

References USART_RXC_bm.

static void usart_rx_complete_interrupt_enable ( USART_t usart)
inlinestatic

Enable USART receive complete interrupt.

Parameters
usartPointer to the USART module.

References USART_RXC_bm.

static void usart_rx_disable ( USART_t usart)
inlinestatic

Disable USART receiver.

Parameters
usartPointer to the USART module.

References USART_RXEN_bm.

static void usart_rx_enable ( USART_t usart)
inlinestatic

Enable USART receiver.

Parameters
usartPointer to the USART module

References USART_RXEN_bm.

static bool usart_rx_is_complete ( USART_t usart)
inlinestatic

Checks if the RX complete interrupt flag is set.

Checks if the RX complete interrupt flag is set.

Parameters
usartThe USART module.

References USART_RXC_bm.

bool usart_set_baudrate ( USART_t usart,
uint32_t  baud,
uint32_t  cpu_hz 
)

Set the baudrate value in the USART module.

This function sets the baudrate register with scaling regarding the CPU frequency and makes sure the baud rate is supported by the hardware. The function can be used if you don't want to calculate the settings yourself or changes to baudrate at runtime is required.

Parameters
usartThe USART module.
baudThe baudrate.
cpu_hzThe CPU frequency.
Return values
trueif the hardware supports the baud rate
falseif the hardware does not support the baud rate (i.e. it's either too high or too low.)

References USART_struct::UBRR, USART_struct::UCSRnA, and USART_U2X_bm.

Referenced by run_baudrate_test(), and usart_init_rs232().

void usart_set_baudrate_precalculated ( USART_t usart,
uint32_t  baud,
uint32_t  cpu_hz 
)

Set the baudrate using pre calculated BAUDCTRL values stored in program memory.

Note
This function only works for cpu_hz 1Mhz, 8Mhz or 16Mhz and baudrate values 1200, 2400, 4800, 9600, 19200, 38400 and 57600.
Parameters
usartThe USART module.
baudThe baudrate.
cpu_hzThe CPU frequency.

References Assert, PROGMEM_READ_WORD, USART_struct::UBRR, USART_BAUD_UNDEFINED, and usart_get_baud_offset().

static void usart_set_mode ( USART_t usart,
USART_CMODE_t  usartmode 
)
inlinestatic

Set the mode the USART run in.

Set the mode the USART run in. The default mode is asynchronous mode.

Parameters
usartPointer to the USART module register section.
usartmodeSelects the USART mode. Use USART_CMODE_t type.

USART modes:

  • 0x0 : Asynchronous mode.
  • 0x1 : Synchronous mode.
  • 0x2 : IrDA mode.
  • 0x3 : Master SPI mode.

References USART_UMSEL01_gm.

void usart_spi_set_baudrate ( USART_t usart,
uint32_t  baud,
uint32_t  cpu_hz 
)

Set the baudrate value in the USART_SPI module.

This function sets the baudrate register regarding the CPU frequency.

Parameters
usartThe USART(SPI) module.
baudThe baudrate.
cpu_hzThe CPU frequency.

References USART_struct::UBRR.

Referenced by run_loopback_syncmode_test(), and usart_init_spi().

static uint8_t usart_spi_transmit ( USART_t usart,
uint8_t  txdata 
)
inlinestatic

Performs a data transfer on the USART in SPI mode.

Parameters
usartThe USART module.
txdataThe data to be transmitted.
Returns
The received data

References usart_clear_tx_complete(), usart_data_register_is_empty(), usart_get(), usart_put(), and usart_tx_is_complete().

static void usart_tx_complete_interrupt_disable ( USART_t usart)
inlinestatic

Disable USART transmit complete interrupt.

Parameters
usartPointer to the USART module.

References USART_TXC_bm.

static void usart_tx_complete_interrupt_enable ( USART_t usart)
inlinestatic

Enable USART transmit complete interrupt.

Parameters
usartPointer to the USART module.

References USART_TXC_bm.

static void usart_tx_disable ( USART_t usart)
inlinestatic

Disable USART transmitter.

Parameters
usartPointer to the USART module.

References USART_TXEN_bm.

static void usart_tx_enable ( USART_t usart)
inlinestatic

Enable USART transmitter.

Parameters
usartPointer to the USART module.

References USART_TXEN_bm.

static bool usart_tx_is_complete ( USART_t usart)
inlinestatic

Checks if the TX complete interrupt flag is set.

Checks if the TX complete interrupt flag is set.

Parameters
usartThe USART module.

References USART_TXC_bm.

Referenced by usart_spi_transmit().