Microchip® Advanced Software Framework

xmega_usart_spi/usart_spi.h File Reference

AVR XMEGA USART in SPI mode driver functions.

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

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

Data Structures

struct  usart_spi_device
 Polled SPI device definition. More...
 

Macros

#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...
 
USART in SPI mode Management Configuration
#define CONFIG_USART_SPI_DUMMY   0xFF
 Default Config Spi Master Dummy Field. More...
 

Typedefs

typedef uint32_t board_spi_select_id_t
 
typedef uint8_t spi_flags_t
 

Functions

void usart_spi_deselect_device (USART_t *usart, struct usart_spi_device *device)
 Deselect given device on the SPI bus. More...
 
void usart_spi_disable (USART_t *usart)
 Disables the USART. More...
 
void usart_spi_enable (USART_t *usart)
 Enables the USART for the specified USART in SPI mode. More...
 
void usart_spi_init (USART_t *usart)
 Initializes the USART in SPI master mode. More...
 
bool usart_spi_is_enabled (USART_t *usart)
 Tests if the USART in SPI mode is enabled. More...
 
static bool usart_spi_is_rx_full (USART_t *usart)
 Tests if the USART in SPI mode contains a received character. 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_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_tx_ready (USART_t *usart)
 Check if the USART Transmit Register is empty. 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...
 
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...
 
void usart_spi_select_device (USART_t *usart, struct usart_spi_device *device)
 Select given device on the SPI bus. 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...
 
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...
 
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...
 

#define CONFIG_USART_SPI_DUMMY   0xFF

Default Config Spi Master Dummy Field.

Referenced by usart_spi_read_packet(), and usart_spi_read_single().

#define SPI_CPHA   (1 << 0)

Clock phase.

#define SPI_CPOL   (1 << 1)

Clock polarity.

#define SPI_MODE_0   0
#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.

Referenced by hx8347a_interface_init(), and st7565r_interface_init().

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
Precondition
SPI device must be selected with usart_spi_select_device() first

References usart_spi_device::id, and ioport_set_pin_high().

Referenced by at25dfx_spi_deselect_device(), ssd1306_write_command(), ssd1306_write_data(), st7565r_write_command(), and st7565r_write_data().

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.
void usart_spi_enable ( USART_t *  usart)

Enables the USART for the specified USART in SPI mode.

Parameters
usartBase address of the USART instance.
void usart_spi_init ( USART_t *  usart)

Initializes the USART in SPI master mode.

Parameters
usartBase address of the USART instance.

References sysclk_enable_module(), SYSCLK_PORT_C, SYSCLK_PORT_D, SYSCLK_PORT_E, and SYSCLK_PORT_F.

Referenced by at25dfx_spi_init(), at45dbx_spi_init(), hx8347a_interface_init(), ili9341_interface_init(), ssd1306_interface_init(), and st7565r_interface_init().

bool usart_spi_is_enabled ( USART_t *  usart)

Tests if the USART in SPI mode is enabled.

Parameters
usartBase address of the usart instance.
Returns
1 if the usart is enabled, otherwise 0.
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().

Referenced by ili9341_wait_for_send_done().

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 at25dfx_spi_read_byte(), at25dfx_spi_read_packet(), and hx8347a_read_byte().

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

Referenced by hx8347a_read_byte(), and ili9341_read_byte().

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 at25dfx_spi_select_device(), ssd1306_write_command(), ssd1306_write_data(), st7565r_write_command(), and st7565r_write_data().

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 at25dfx_spi_init(), hx8347a_interface_init(), ili9341_interface_init(), ssd1306_interface_init(), and st7565r_interface_init().

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 at25dfx_spi_write_byte(), at25dfx_spi_write_packet(), hx8347a_send_byte(), and hx8347a_write_packet().

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

Referenced by hx8347a_send_byte(), and ili9341_send_byte().