Microchip® Advanced Software Framework

sam_spi/spi_master.h File Reference

SPI master common service for SAM.

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

#include "compiler.h"
#include "sysclk.h"
#include "status_codes.h"
#include "spi.h"
#include "conf_spi_master.h"

Data Structures

struct  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...
 
#define SPI_TYPE_DEFS
 
SPI Master Management Configuration
#define CONFIG_SPI_MASTER_DELAY_BCS   0
 Default Configuration of SPI Master Delay BCS. More...
 
#define CONFIG_SPI_MASTER_BITS_PER_TRANSFER   SPI_CSR_BITS_8_BIT
 Default Configuration of SPI Master Bits per Transfer Definition. More...
 
#define CONFIG_SPI_MASTER_DELAY_BCT   0
 Default Configuration of SPI Master Delay BCT. More...
 
#define CONFIG_SPI_MASTER_DELAY_BS   0
 Default Configuration of SPI Master Delay BS. More...
 
#define CONFIG_SPI_MASTER_DUMMY   0xFF
 Default Configuration of SPI Master Dummy Field. More...
 

Typedefs

typedef uint32_t board_spi_select_id_t
 Board SPI Select Id Definition. More...
 
typedef uint8_t spi_flags_t
 SPI Flags Definition. More...
 

Functions

void spi_deselect_device (Spi *p_spi, struct spi_device *device)
 Deselect the given device on the SPI bus. More...
 
void spi_master_init (Spi *p_spi)
 Initialize the SPI in master mode. More...
 
void spi_master_setup_device (Spi *p_spi, struct spi_device *device, spi_flags_t flags, uint32_t baud_rate, board_spi_select_id_t sel_id)
 Set up an SPI device. More...
 
status_code_t spi_read_packet (Spi *p_spi, uint8_t *data, size_t len)
 Receive a sequence of bytes from an SPI device. More...
 
static void spi_read_single (Spi *p_spi, uint8_t *data)
 Receive one byte from an SPI device. More...
 
void spi_select_device (Spi *p_spi, struct spi_device *device)
 Select the given device on the SPI bus. More...
 
status_code_t spi_transceive_packet (Spi *p_spi, uint8_t *tx_data, uint8_t *rx_data, size_t len)
 Send and receive a sequence of bytes from an SPI device. More...
 
status_code_t spi_write_packet (Spi *p_spi, const uint8_t *data, size_t len)
 Send a sequence of bytes to an SPI device. More...
 
static void spi_write_single (Spi *p_spi, uint8_t data)
 Write one byte to an SPI device. More...
 

#define CONFIG_SPI_MASTER_BITS_PER_TRANSFER   SPI_CSR_BITS_8_BIT

Default Configuration of SPI Master Bits per Transfer Definition.

Referenced by spi_master_setup_device().

#define CONFIG_SPI_MASTER_DELAY_BCS   0

Default Configuration of SPI Master Delay BCS.

Referenced by spi_master_init().

#define CONFIG_SPI_MASTER_DELAY_BCT   0

Default Configuration of SPI Master Delay BCT.

Referenced by spi_master_setup_device().

#define CONFIG_SPI_MASTER_DELAY_BS   0

Default Configuration of SPI Master Delay BS.

Referenced by spi_master_setup_device().

#define CONFIG_SPI_MASTER_DUMMY   0xFF

Default Configuration of SPI Master Dummy Field.

Referenced by spi_read_packet().

#define SPI_CPHA   (1 << 0)

Clock phase.

#define SPI_CPOL   (1 << 1)

Clock polarity.

#define SPI_MODE_1   (SPI_CPHA)

SPI mode 1.

Referenced by usart_init_spi_master(), and usart_init_spi_slave().

#define SPI_MODE_2   (SPI_CPOL)

SPI mode 2.

Referenced by usart_init_spi_master(), and usart_init_spi_slave().

#define SPI_MODE_3   (SPI_CPOL | SPI_CPHA)

SPI mode 3.

Referenced by usart_init_spi_master(), and usart_init_spi_slave().

#define SPI_TYPE_DEFS

typedef uint32_t board_spi_select_id_t

Board SPI Select Id Definition.

typedef uint8_t spi_flags_t

SPI Flags Definition.

void spi_deselect_device ( Spi *  p_spi,
struct spi_device device 
)

Deselect the given device on the SPI bus.

Call board chip deselect.

Parameters
p_spiBase address of the SPI instance.
deviceSPI device.
Precondition
SPI device must be selected with spi_select_device() first.

References NONE_CHIP_SELECT_ID, spi_is_tx_empty(), spi_set_lastxfer(), spi_set_peripheral_chip_select_value(), and UNUSED.

Referenced by at25dfx_spi_deselect_device(), spi_at25df_mem_check(), ssd1306_write_command(), ssd1306_write_data(), trx_aes_wrrd(), trx_frame_read(), trx_frame_write(), trx_reg_read(), trx_reg_write(), trx_sram_read(), and trx_sram_write().

void spi_master_setup_device ( Spi *  p_spi,
struct spi_device device,
spi_flags_t  flags,
uint32_t  baud_rate,
board_spi_select_id_t  sel_id 
)

Set up an SPI device.

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

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

References Assert, CONFIG_SPI_MASTER_BITS_PER_TRANSFER, CONFIG_SPI_MASTER_DELAY_BCT, CONFIG_SPI_MASTER_DELAY_BS, spi_device::id, spi_calc_baudrate_div(), spi_configure_cs_behavior(), SPI_CS_KEEP_LOW, spi_set_baudrate_div(), spi_set_bits_per_transfer(), spi_set_clock_phase(), spi_set_clock_polarity(), spi_set_transfer_delay(), sysclk_get_pba_hz(), and UNUSED.

Referenced by at25dfx_spi_init(), main(), ssd1306_interface_init(), and trx_spi_init().

status_code_t spi_read_packet ( Spi *  p_spi,
uint8_t *  data,
size_t  len 
)

Receive a sequence of bytes from an SPI device.

All bytes sent out on SPI bus are sent as value 0xff.

Parameters
p_spiBase address of the SPI instance.
dataData buffer to read.
lenLength of data to be read.
Precondition
SPI device must be selected with spi_select_device() first.

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

Parameters
p_spiBase address of the SPI instance.
dataData buffer to read.
lenLength of data to be read.
Precondition
SPI device must be selected with spi_select_device() first.

References CONFIG_SPI_MASTER_DUMMY, ERR_TIMEOUT, spi_is_rx_ready(), spi_is_tx_ready(), spi_read_single(), SPI_TIMEOUT, spi_write_single(), and STATUS_OK.

Referenced by at25dfx_spi_read_byte(), at25dfx_spi_read_packet(), spi_at25df_mem_check(), trx_aes_wrrd(), trx_frame_read(), trx_reg_read(), and trx_sram_read().

static void spi_read_single ( Spi *  p_spi,
uint8_t *  data 
)
inlinestatic

Receive one byte from an SPI device.

Parameters
p_spiBase address of the SPI instance.
dataData to read.

References spi_get().

Referenced by spi_read_packet(), spi_transceive_packet(), and trx_aes_wrrd().

void spi_select_device ( Spi *  p_spi,
struct spi_device device 
)

Select the given device on the SPI bus.

Set device specific setting and call board chip select.

Parameters
p_spiBase address of the SPI instance.
deviceSPI device.

References spi_device::id, MAX_NUM_WITH_DECODER, MAX_NUM_WITHOUT_DECODER, spi_get_peripheral_select_decode_setting(), and spi_set_peripheral_chip_select_value().

Referenced by at25dfx_spi_select_device(), spi_at25df_mem_check(), ssd1306_write_command(), ssd1306_write_data(), trx_aes_wrrd(), trx_frame_read(), trx_frame_write(), trx_reg_read(), trx_reg_write(), trx_sram_read(), and trx_sram_write().

status_code_t spi_transceive_packet ( Spi *  p_spi,
uint8_t *  tx_data,
uint8_t *  rx_data,
size_t  len 
)

Send and receive a sequence of bytes from an SPI device.

Parameters
p_spiBase address of the SPI instance.
tx_dataData buffer to send.
rx_dataData buffer to read.
lenLength of data to be read.
Precondition
SPI device must be selected with spi_select_device() first.

References ERR_TIMEOUT, spi_is_rx_ready(), spi_is_tx_ready(), spi_read_single(), SPI_TIMEOUT, spi_write_single(), and STATUS_OK.

status_code_t spi_write_packet ( Spi *  p_spi,
const uint8_t *  data,
size_t  len 
)

Send a sequence of bytes to an SPI device.

Received bytes on the SPI bus are discarded.

Parameters
p_spiBase address of the SPI instance.
dataData buffer to write.
lenLength of data to be written.
Precondition
SPI device must be selected with spi_select_device() first.

References ERR_TIMEOUT, spi_is_tx_ready(), SPI_TIMEOUT, spi_write_single(), and STATUS_OK.

Referenced by at25dfx_spi_write_byte(), at25dfx_spi_write_packet(), spi_at25df_mem_check(), trx_aes_wrrd(), trx_frame_read(), trx_frame_write(), trx_reg_read(), trx_reg_write(), trx_sram_read(), and trx_sram_write().

static void spi_write_single ( Spi *  p_spi,
uint8_t  data 
)
inlinestatic

Write one byte to an SPI device.

Parameters
p_spiBase address of the SPI instance.
dataData to write.

References spi_put().

Referenced by spi_read_packet(), spi_transceive_packet(), spi_write_packet(), ssd1306_write_command(), and ssd1306_write_data().