Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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
 Input parameters when initializing RS232 and similar modes. More...
 
struct  usart_spi_options
 Input parameters when initializing SPI master mode. More...
 

Macros

#define USART_BAUD_1200   0x00
 Offset in lookup table for baudrate 1200. More...
 
#define USART_BAUD_19200   0x04
 Offset in lookup table for baudrate 19200. More...
 
#define USART_BAUD_2400   0x01
 Offset in lookup table for baudrate 2400. More...
 
#define USART_BAUD_38400   0x05
 Offset in lookup table for baudrate 38400. More...
 
#define USART_BAUD_4800   0x02
 Offset in lookup table for baudrate 4800. More...
 
#define USART_BAUD_57600   0x06
 Offset in lookup table for baudrate 57600. More...
 
#define USART_BAUD_9600   0x03
 Offset in lookup table for baudrate 9600. More...
 
#define USART_BAUD_UNDEFINED   0xFF
 Baudrate not in lookup table. More...
 

Typedefs

typedef struct usart_rs232_options usart_rs232_options_t
 Input parameters when initializing RS232 and similar modes. More...
 
typedef struct usart_spi_options usart_spi_options_t
 Input parameters when initializing SPI master mode. More...
 

Enumerations

enum  usart_int_level_t {
  USART_INT_LVL_OFF = 0x00,
  USART_INT_LVL_LO = 0x01,
  USART_INT_LVL_MED = 0x02,
  USART_INT_LVL_HI = 0x03
}
 USART interrupt levels. More...
 

Functions

static PROGMEM_DECLARE (uint16_t, baudctrl_2mhz[])
 Lookup table containing baudctrl values for CPU frequency 2 Mhz. More...
 
static PROGMEM_DECLARE (uint16_t, baudctrl_32mhz[])
 Lookup table containing baudctrl values for CPU frequency 32 Mhz. More...
 
static void usart_clear_rx_complete (USART_t *usart)
 Clear RX complete interrupt flag. More...
 
static void usart_clear_tx_complete (USART_t *usart)
 Clear TX complete interrupt flag. More...
 
static bool usart_data_register_is_empty (USART_t *usart)
 Check if data register empty flag is set. 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...
 
enum status_code usart_putchar (USART_t *usart, uint8_t c)
 Send a data with the USART module. 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 precalculated BAUDCTRL values from PROGMEM. More...
 
void usart_set_bsel_bscale_value (USART_t *usart, uint16_t bsel, uint8_t bscale)
 Set the baudrate by setting the BSEL and BSCALE values in the USART. More...
 
static void usart_set_dre_interrupt_level (USART_t *usart, enum usart_int_level_t level)
 Set USART DRE interrupt level. More...
 
static void usart_set_mode (USART_t *usart, USART_CMODE_t usartmode)
 Set the mode the USART run in. More...
 
static void usart_set_rx_interrupt_level (USART_t *usart, enum usart_int_level_t level)
 Set USART RXD interrupt level. More...
 
static void usart_set_tx_interrupt_level (USART_t *usart, enum usart_int_level_t level)
 Set USART TXD interrupt level. 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_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...
 

#define USART_BAUD_1200   0x00

Offset in lookup table for baudrate 1200.

Referenced by usart_get_baud_offset().

#define USART_BAUD_19200   0x04

Offset in lookup table for baudrate 19200.

Referenced by usart_get_baud_offset().

#define USART_BAUD_2400   0x01

Offset in lookup table for baudrate 2400.

Referenced by usart_get_baud_offset().

#define USART_BAUD_38400   0x05

Offset in lookup table for baudrate 38400.

Referenced by usart_get_baud_offset().

#define USART_BAUD_4800   0x02

Offset in lookup table for baudrate 4800.

Referenced by usart_get_baud_offset().

#define USART_BAUD_57600   0x06

Offset in lookup table for baudrate 57600.

Referenced by usart_get_baud_offset().

#define USART_BAUD_9600   0x03

Offset in lookup table for baudrate 9600.

Referenced by usart_get_baud_offset().

#define USART_BAUD_UNDEFINED   0xFF

Baudrate not in lookup table.

Referenced by usart_get_baud_offset(), and usart_set_baudrate_precalculated().

Input parameters when initializing RS232 and similar modes.

Input parameters when initializing SPI master mode.

USART interrupt levels.

Enumerator
USART_INT_LVL_OFF 
USART_INT_LVL_LO 
USART_INT_LVL_MED 
USART_INT_LVL_HI 

static PROGMEM_DECLARE ( uint16_t  ,
baudctrl_2mhz  [] 
)
static

Lookup table containing baudctrl values for CPU frequency 2 Mhz.

static PROGMEM_DECLARE ( uint16_t  ,
baudctrl_32mhz  [] 
)
static

Lookup table containing baudctrl values for CPU frequency 32 Mhz.

static void usart_clear_rx_complete ( USART_t *  usart)
inlinestatic

Clear RX complete interrupt flag.

Parameters
usartThe USART module.
static void usart_clear_tx_complete ( USART_t *  usart)
inlinestatic

Clear TX complete interrupt flag.

Parameters
usartThe USART module.

Referenced by configure_sensor_platform(), ili9341_send_byte(), ili9341_wait_for_send_done(), and usart_spi_transmit().

static bool usart_data_register_is_empty ( USART_t *  usart)
inlinestatic

Check if data register empty flag is set.

Parameters
usartThe USART module.

Referenced by ili9341_send_byte(), usart_putchar(), usart_spi_is_tx_empty(), usart_spi_is_tx_ready(), and usart_spi_transmit().

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.

Referenced by usart_init_rs232().

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 ili9341_read_byte(), and 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_rx_is_complete().

Referenced by main(), 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 successfull
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_enable_peripheral_clock(), sysclk_get_per_hz(), usart_format_set(), usart_rx_enable(), usart_set_baudrate(), usart_set_mode(), and usart_tx_enable().

Referenced by main(), 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_CREATE_PIN, IOPORT_DIR_OUTPUT, IOPORT_MODE_INVERT_PIN, IOPORT_MODE_TOTEM, IOPORT_PIN_LEVEL_HIGH, ioport_set_pin_dir(), ioport_set_pin_level(), ioport_set_pin_mode(), usart_spi_options::spimode, sysclk_enable_peripheral_clock(), sysclk_get_per_hz(), UNUSED, USART_DORD_bm, usart_rx_disable(), usart_rx_enable(), usart_set_mode(), usart_spi_set_baudrate(), usart_tx_enable(), and USART_UCPHA_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 ili9341_read_byte(), ili9341_send_byte(), and usart_spi_transmit().

enum status_code 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, and usart_data_register_is_empty().

Referenced by gfx_mono_screen_dump(), log_sha204(), main(), and usart_serial_putchar().

static void usart_rx_disable ( USART_t *  usart)
inlinestatic

Disable USART receiver.

Parameters
usartPointer to the USART module.

Referenced by ili9341_read_byte(), and usart_init_spi().

static void usart_rx_enable ( USART_t *  usart)
inlinestatic

Enable USART receiver.

Parameters
usartPointer to the USART module

Referenced by ili9341_read_byte(), usart_init_rs232(), and usart_init_spi().

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.

Referenced by ili9341_read_byte(), usart_getchar(), usart_spi_is_rx_full(), and usart_spi_is_rx_ready().

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

Referenced by usart_init_rs232().

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

Set the baudrate using precalculated BAUDCTRL values from PROGMEM.

Note
This function only works for cpu_hz 2Mhz or 32Mhz 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_BAUD_UNDEFINED, and usart_get_baud_offset().

void usart_set_bsel_bscale_value ( USART_t *  usart,
uint16_t  bsel,
uint8_t  bscale 
)

Set the baudrate by setting the BSEL and BSCALE values in the USART.

This function sets the selected BSEL and BSCALE value in the BAUDCTRL registers with BSCALE 0. For calculation options, see table 21-1 in XMEGA A manual.

Parameters
usartThe USART module.
bselCalculated BSEL value.
bscaleCalculated BSEL value.
static void usart_set_dre_interrupt_level ( USART_t *  usart,
enum usart_int_level_t  level 
)
inlinestatic

Set USART DRE interrupt level.

Sets the interrupt level on Data Register interrupt.

Parameters
usartPointer to the USART module.
levelInterrupt level of the DRE interrupt. Use USART_DREINTLVL_t type.
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.

Referenced by usart_init_rs232(), and usart_init_spi().

static void usart_set_rx_interrupt_level ( USART_t *  usart,
enum usart_int_level_t  level 
)
inlinestatic

Set USART RXD interrupt level.

Sets the interrupt level on RX Complete interrupt.

Parameters
usartPointer to the USART module.
levelInterrupt level of the RXD interrupt.
static void usart_set_tx_interrupt_level ( USART_t *  usart,
enum usart_int_level_t  level 
)
inlinestatic

Set USART TXD interrupt level.

Sets the interrupt level on TX Complete interrupt.

Parameters
usartPointer to the USART module.
levelInterrupt level of the TXD interrupt.
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.

Referenced by 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().

Referenced by ssd1306_write_command(), ssd1306_write_data(), st7565r_write_command(), st7565r_write_data(), usart_spi_read_packet(), usart_spi_read_single(), usart_spi_write_packet(), and usart_spi_write_single().

static void usart_tx_disable ( USART_t *  usart)
inlinestatic

Disable USART transmitter.

Parameters
usartPointer to the USART module.
static void usart_tx_enable ( USART_t *  usart)
inlinestatic

Enable USART transmitter.

Parameters
usartPointer to the USART module.

Referenced by usart_init_rs232(), and usart_init_spi().

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.

Referenced by configure_sensor_platform(), ili9341_wait_for_send_done(), and usart_spi_transmit().