Microchip® Advanced Software Framework

usart_megarf.h File Reference

USART driver for AVR MEGARF.

This file contains basic functions for the AVR MEGA USART, with support for all modes, settings and clock speeds.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

#include "compiler.h"
#include "status_codes.h"

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