Microchip® Advanced Software Framework

megarf_usart_spi/usart_spi.h File Reference

AVR MEGARF USART in SPI mode driver definitions.

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

#include "compiler.h"
#include "status_codes.h"
#include "ioport.h"
#include "usart_megarf.h"
#include "conf_usart_spi.h"

Data Structures

struct  usart_spi_device
 

USART in SPI mode Management Configuration

#define CONFIG_USART_SPI_DUMMY   0xFF
 
#define SPI_CPHA   (1 << 0)
 Clock phase. More...
 
#define SPI_CPOL   (1 << 1)
 Clock polarity. More...
 
#define SPI_MODE_0   0
 SPI mode 0. More...
 
#define SPI_MODE_1   (SPI_CPHA)
 SPI mode 1. More...
 
#define SPI_MODE_2   (SPI_CPOL)
 SPI mode 2. More...
 
#define SPI_MODE_3   (SPI_CPOL | SPI_CPHA)
 SPI mode 3. More...
 
typedef uint8_t spi_flags_t
 
typedef uint32_t board_spi_select_id_t
 
void usart_spi_init (USART_t *usart)
 Initializes the USART in SPI master mode. More...
 
void usart_spi_setup_device (USART_t *usart, struct usart_spi_device *device, spi_flags_t flags, unsigned long baud_rate, board_spi_select_id_t sel_id)
 Setup a USART in SPI mode device. More...
 
void usart_spi_enable (USART_t *usart)
 Enables the USART for the specified USART in SPI mode. More...
 
void usart_spi_disable (USART_t *usart)
 Disables the USART. More...
 
status_code_t usart_spi_write_packet (USART_t *usart, const uint8_t *data, size_t len)
 Send a sequence of bytes to a SPI device using USART in SPI mode. More...
 
status_code_t usart_spi_read_packet (USART_t *usart, uint8_t *data, size_t len)
 Receive a sequence of bytes from a USART in SPI mode device. More...
 
void usart_spi_select_device (USART_t *usart, struct usart_spi_device *device)
 Select given device on the SPI bus. More...
 
void usart_spi_deselect_device (USART_t *usart, struct usart_spi_device *device)
 Deselect given device on the SPI bus. More...
 
static __always_inline void usart_spi_write_single (USART_t *usart, uint8_t data)
 Write one byte to a SPI device using USART in SPI mode. More...
 
static void usart_spi_read_single (USART_t *usart, uint8_t *data)
 Receive one byte from a SPI device using USART in SPI mode. More...
 
static bool usart_spi_is_tx_empty (USART_t *usart)
 Check whether there are data in Transmit Holding Register or Transmit Shift Register in SPI master mode. More...
 
static bool usart_spi_is_rx_ready (USART_t *usart)
 Check whether the USART in SPI master mode contains a received character. More...
 
static bool usart_spi_is_tx_ready (USART_t *usart)
 Check if the USART Transmit Register is empty. More...
 
static bool usart_spi_is_rx_full (USART_t *usart)
 Tests if the USART in SPI mode contains a received character. More...
 

#define CONFIG_USART_SPI_DUMMY   0xFF
#define SPI_CPHA   (1 << 0)

Clock phase.

#define SPI_CPOL   (1 << 1)

Clock polarity.

#define SPI_MODE_0   0

SPI mode 0.

Referenced by epd_spi_init(), and main().

#define SPI_MODE_1   (SPI_CPHA)

SPI mode 1.

#define SPI_MODE_2   (SPI_CPOL)

SPI mode 2.

#define SPI_MODE_3   (SPI_CPOL | SPI_CPHA)

SPI mode 3.

typedef uint32_t board_spi_select_id_t
typedef uint8_t spi_flags_t

void usart_spi_deselect_device ( USART_t usart,
struct usart_spi_device device 
)

Deselect given device on the SPI bus.

Calls board chip deselect.

Parameters
usartBase address of the USART instance.
deviceSPI device

References usart_spi_device::id, and ioport_set_pin_high().

Referenced by spi_slave_transfer(), spi_usart_master_transfer(), and usart_spi_at45dbx_mem_check().

void usart_spi_disable ( USART_t usart)

Disables the USART.

Ensures that nothing is transferred while setting up buffers.

Parameters
usartBase address of the USART instance.

References usart_rx_disable(), and usart_tx_disable().

void usart_spi_enable ( USART_t usart)

Enables the USART for the specified USART in SPI mode.

Parameters
usartBase address of the USART instance.

References usart_rx_enable(), and usart_tx_enable().

Referenced by main().

void usart_spi_init ( USART_t usart)

Initializes the USART in SPI master mode.

Parameters
usartBase address of the USART instance.

References PRUSART0_bm, and sysclk_enable_module().

Referenced by at45dbx_spi_init(), and main().

static bool usart_spi_is_rx_full ( USART_t usart)
inlinestatic

Tests if the USART in SPI mode contains a received character.

Parameters
usartBase address of the USART instance.
Returns
1 if the USART Receive Holding Register is full, otherwise 0.

References usart_rx_is_complete().

static bool usart_spi_is_rx_ready ( USART_t usart)
inlinestatic

Check whether the USART in SPI master mode contains a received character.

Parameters
usartBase address of the USART instance.
Return values
1Some data have been received.
0No data has been received.

References usart_rx_is_complete().

static bool usart_spi_is_tx_empty ( USART_t usart)
inlinestatic

Check whether there are data in Transmit Holding Register or Transmit Shift Register in SPI master mode.

Parameters
usartBase address of the USART instance.
Return values
1The two registers are empty.
0One of the two registers contains data.

References usart_data_register_is_empty().

static bool usart_spi_is_tx_ready ( USART_t usart)
inlinestatic

Check if the USART Transmit Register is empty.

Parameters
usartBase address of the USART instance.
Return values
1There is no data in the Transmit Holding Register.
0There are data in the Transmit Holding Register.

References usart_data_register_is_empty().

status_code_t usart_spi_read_packet ( USART_t usart,
uint8_t *  data,
size_t  len 
)

Receive a sequence of bytes from a USART in SPI mode device.

All bytes sent out on usart bus are sent as value 0.

Parameters
usartBase address of the usart instance.
datadata buffer to read
lenLength of data
Precondition
usart device must be selected with usart_spi_select_device() first

References CONFIG_USART_SPI_DUMMY, STATUS_OK, and usart_spi_transmit().

Referenced by spi_slave_transfer(), and usart_spi_at45dbx_mem_check().

static void usart_spi_read_single ( USART_t usart,
uint8_t *  data 
)
inlinestatic

Receive one byte from a SPI device using USART in SPI mode.

Parameters
usartBase address of the USART instance.
dataPointer to the data byte where to store the received data.

References CONFIG_USART_SPI_DUMMY, and usart_spi_transmit().

void usart_spi_select_device ( USART_t usart,
struct usart_spi_device device 
)

Select given device on the SPI bus.

Set device specific setting and calls board chip select.

Parameters
usartBase address of the USART instance.
deviceSPI device

References usart_spi_device::id, and ioport_set_pin_low().

Referenced by spi_slave_transfer(), spi_usart_master_transfer(), and usart_spi_at45dbx_mem_check().

void usart_spi_setup_device ( USART_t usart,
struct usart_spi_device device,
spi_flags_t  flags,
unsigned long  baud_rate,
board_spi_select_id_t  sel_id 
)

Setup a USART in SPI mode device.

The returned device descriptor structure must be passed to the driver whenever that device should be used as current slave device.

Parameters
usartBase address of the USART instance.
devicePointer to usart device struct that should be initialized.
flagsUSART configuration flags. Common flags for all implementations are the usart modes SPI_MODE_0 ... SPI_MODE_3.
baud_rateBaud rate for communication with slave device in Hz.
sel_idBoard specific select id

References usart_spi_options::baudrate, usart_spi_options::data_order, usart_spi_options::spimode, and usart_init_spi().

Referenced by main().

status_code_t usart_spi_write_packet ( USART_t usart,
const uint8_t *  data,
size_t  len 
)

Send a sequence of bytes to a SPI device using USART in SPI mode.

Received bytes on the USART in SPI mode are discarded.

Parameters
usartBase address of the USART instance.
dataData buffer to write
lenLength of data
Precondition
usart device must be selected with usart_spi_select_device() first

References STATUS_OK, and usart_spi_transmit().

Referenced by spi_slave_transfer(), spi_usart_master_transfer(), and usart_spi_at45dbx_mem_check().

static __always_inline void usart_spi_write_single ( USART_t usart,
uint8_t  data 
)
static

Write one byte to a SPI device using USART in SPI mode.

Parameters
usartBase address of the usart instance.
dataThe data byte to be loaded

References usart_spi_transmit().