PSoC 6 Hardware Abstraction Layer (HAL)
SPI (Serial Peripheral Interface)

General Description

High level interface for interacting with the Cypress SPI.

The SPI protocol is a synchronous serial interface protocol. Devices operate in either master or slave mode. The master initiates the data transfer.

Motorola SPI modes 0, 1, 2, and 3 are supported, with either MSB or LSB first. The operating mode and data frame size can be configured via cyhal_spi_cfg_t.

Data Structures

struct  cyhal_spi_cfg_t
 Initial SPI configuration. More...
 

Macros

#define CYHAL_SPI_RSLT_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 0))
 
#define CYHAL_SPI_RSLT_CLOCK_ERROR   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 1))
 
#define CYHAL_SPI_RSLT_TRANSFER_ERROR   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 2))
 
#define CYHAL_SPI_RSLT_CLOCK_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 3))
 
#define CYHAL_SPI_RSLT_PIN_CONFIG_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 5))
 
#define CYHAL_SPI_RSLT_INVALID_PIN_API_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 6))
 
#define CYHAL_SPI_RSLT_ERR_INVALID_PIN   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 7))
 
#define cyhal_spi_frequency   cyhal_spi_set_frequency
 

Typedefs

typedef void(* cyhal_spi_event_callback_t) (void *callback_arg, cyhal_spi_event_t event)
 

Enumerations

enum  cyhal_spi_event_t { CYHAL_SPI_IRQ_DATA_IN_FIFO = 1 << 1, CYHAL_SPI_IRQ_DONE = 1 << 2, CYHAL_SPI_IRQ_ERROR = 1 << 3 }
 
enum  cyhal_spi_mode_t {
  CYHAL_SPI_MODE_00_MSB, CYHAL_SPI_MODE_00_LSB, CYHAL_SPI_MODE_01_MSB, CYHAL_SPI_MODE_01_LSB,
  CYHAL_SPI_MODE_10_MSB, CYHAL_SPI_MODE_10_LSB, CYHAL_SPI_MODE_11_MSB, CYHAL_SPI_MODE_11_LSB
}
 

Functions

cy_rslt_t cyhal_spi_init (cyhal_spi_t *obj, cyhal_gpio_t mosi, cyhal_gpio_t miso, cyhal_gpio_t sclk, cyhal_gpio_t ssel, const cyhal_clock_divider_t *clk, uint8_t bits, cyhal_spi_mode_t mode, bool is_slave)
 
void cyhal_spi_free (cyhal_spi_t *obj)
 
cy_rslt_t cyhal_spi_set_frequency (cyhal_spi_t *obj, uint32_t hz)
 
cy_rslt_t cyhal_spi_recv (cyhal_spi_t *obj, uint32_t *value)
 
cy_rslt_t cyhal_spi_send (cyhal_spi_t *obj, uint32_t value)
 
cy_rslt_t cyhal_spi_transfer (cyhal_spi_t *obj, const uint8_t *tx, size_t tx_length, uint8_t *rx, size_t rx_length, uint8_t write_fill)
 
cy_rslt_t cyhal_spi_transfer_async (cyhal_spi_t *obj, const uint8_t *tx, size_t tx_length, uint8_t *rx, size_t rx_length)
 
bool cyhal_spi_is_busy (cyhal_spi_t *obj)
 
cy_rslt_t cyhal_spi_abort_async (cyhal_spi_t *obj)
 
void cyhal_spi_register_callback (cyhal_spi_t *obj, cyhal_spi_event_callback_t callback, void *callback_arg)
 
void cyhal_spi_enable_event (cyhal_spi_t *obj, cyhal_spi_event_t event, uint8_t intrPriority, bool enable)
 

Data Structure Documentation

◆ cyhal_spi_cfg_t

struct cyhal_spi_cfg_t
Data Fields
cyhal_spi_mode_t mode The operating mode.
uint8_t data_bits The number of bits per transfer.
bool is_slave Whether the peripheral is operating as slave or master.

Macro Definition Documentation

◆ CYHAL_SPI_RSLT_BAD_ARGUMENT

#define CYHAL_SPI_RSLT_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 0))

Bad argument

◆ CYHAL_SPI_RSLT_CLOCK_ERROR

#define CYHAL_SPI_RSLT_CLOCK_ERROR   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 1))

Failed to initialize SPI clock

◆ CYHAL_SPI_RSLT_TRANSFER_ERROR

#define CYHAL_SPI_RSLT_TRANSFER_ERROR   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 2))

Failed to Transfer SPI data

◆ CYHAL_SPI_RSLT_CLOCK_NOT_SUPPORTED

#define CYHAL_SPI_RSLT_CLOCK_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 3))

Provided clock is not supported by SPI

◆ CYHAL_SPI_RSLT_PIN_CONFIG_NOT_SUPPORTED

#define CYHAL_SPI_RSLT_PIN_CONFIG_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 5))

Provided PIN configuration is not supported by SPI

◆ CYHAL_SPI_RSLT_INVALID_PIN_API_NOT_SUPPORTED

#define CYHAL_SPI_RSLT_INVALID_PIN_API_NOT_SUPPORTED   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 6))

Provided PIN configuration is not supported by SPI

◆ CYHAL_SPI_RSLT_ERR_INVALID_PIN

#define CYHAL_SPI_RSLT_ERR_INVALID_PIN   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_SPI, 7))

The requested resource type is invalid

◆ cyhal_spi_frequency

#define cyhal_spi_frequency   cyhal_spi_set_frequency

Compatibility define for cyhal_spi_set_frequency.

Typedef Documentation

◆ cyhal_spi_event_callback_t

typedef void(* cyhal_spi_event_callback_t) (void *callback_arg, cyhal_spi_event_t event)

Handler for SPI interrupts

Enumeration Type Documentation

◆ cyhal_spi_event_t

SPI interrupt triggers

Enumerator
CYHAL_SPI_IRQ_DATA_IN_FIFO 

All transfer data has been moved into data FIFO

CYHAL_SPI_IRQ_DONE 

Transfer complete.

CYHAL_SPI_IRQ_ERROR 

An error occurred while transferring data

◆ cyhal_spi_mode_t

SPI operating modes

Enumerator
CYHAL_SPI_MODE_00_MSB 

Standard motorola SPI CPOL=0, CPHA=0 with MSB first operation

CYHAL_SPI_MODE_00_LSB 

Standard motorola SPI CPOL=0, CPHA=0 with LSB first operation

CYHAL_SPI_MODE_01_MSB 

Standard motorola SPI CPOL=0, CPHA=1 with MSB first operation

CYHAL_SPI_MODE_01_LSB 

Standard motorola SPI CPOL=0, CPHA=1 with LSB first operation

CYHAL_SPI_MODE_10_MSB 

Standard motorola SPI CPOL=1, CPHA=0 with MSB first operation

CYHAL_SPI_MODE_10_LSB 

Standard motorola SPI CPOL=1, CPHA=0 with LSB first operation

CYHAL_SPI_MODE_11_MSB 

Standard motorola SPI CPOL=1, CPHA=1 with MSB first operation

CYHAL_SPI_MODE_11_LSB 

Standard motorola SPI CPOL=1, CPHA=1 with LSB first operation

Function Documentation

◆ cyhal_spi_init()

cy_rslt_t cyhal_spi_init ( cyhal_spi_t *  obj,
cyhal_gpio_t  mosi,
cyhal_gpio_t  miso,
cyhal_gpio_t  sclk,
cyhal_gpio_t  ssel,
const cyhal_clock_divider_t *  clk,
uint8_t  bits,
cyhal_spi_mode_t  mode,
bool  is_slave 
)

Initialize the SPI peripheral

Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral

Parameters
[out]objThe SPI object to initialize
[in]mosiThe pin to use for MOSI
Note
At least MOSI or MISO pin should be non-NC
Parameters
[in]misoThe pin to use for MISO
Note
At least MOSI or MISO pin should be non-NC
Parameters
[in]sclkThe pin to use for SCLK
Note
This pin cannot be NC
Parameters
[in]sselThe pin to use for SSEL
Note
This pin can be NC
Parameters
[in]clkThe clock to use can be shared, if not provided a new clock will be allocated
[in]bitsThe number of bits per frame
Note
bits should be 8 or 16
Parameters
[in]modeThe SPI mode (clock polarity, phase, and shift direction)
[in]is_slavefalse for master mode or true for slave mode operation
Returns
The status of the init request

◆ cyhal_spi_free()

void cyhal_spi_free ( cyhal_spi_t *  obj)

Release a SPI object

Return the peripheral, pins and clock owned by the SPI object to their reset state

Parameters
[in,out]objThe SPI object to deinitialize

◆ cyhal_spi_set_frequency()

cy_rslt_t cyhal_spi_set_frequency ( cyhal_spi_t *  obj,
uint32_t  hz 
)

Set the SPI baud rate

Actual frequency may differ from the desired frequency due to available dividers and bus clock Configures the SPI peripheral's baud rate

Parameters
[in,out]objThe SPI object to configure
[in]hzThe baud rate in Hz
Returns
The status of the set_frequency request

◆ cyhal_spi_recv()

cy_rslt_t cyhal_spi_recv ( cyhal_spi_t *  obj,
uint32_t *  value 
)

Get a received value out of the SPI receive buffer

In Master mode - transmits fill-in value and read the data from RxFifo In Slave mode - Blocks until a value is available

Parameters
[in]objThe SPI peripheral to read
[in]valueThe value received
Returns
The status of the read request
Note
  • In Master mode, MISO pin required to be non-NC for this API to operate
  • In Slave mode, MOSI pin required to be non-NC for this API to operate

◆ cyhal_spi_send()

cy_rslt_t cyhal_spi_send ( cyhal_spi_t *  obj,
uint32_t  value 
)

Send a byte out

In Master mode transmits value to slave and read/drop a value from the RxFifo. In Slave mode writes a value to TxFifo

Parameters
[in]objThe SPI peripheral to use for sending
[in]valueThe value to send
Returns
The status of the write request
Note
  • In Master mode, MOSI pin required to be non-NC for this API to operate
  • In Slave mode, MISO pin required to be non-NC for this API to operate

◆ cyhal_spi_transfer()

cy_rslt_t cyhal_spi_transfer ( cyhal_spi_t *  obj,
const uint8_t *  tx,
size_t  tx_length,
uint8_t *  rx,
size_t  rx_length,
uint8_t  write_fill 
)

Write a block out and receive a value

The total number of bytes sent and received will be the maximum of tx_length and rx_length. The bytes written will be padded with the value 0xff.

Parameters
[in]objThe SPI peripheral to use for sending
[in]txPointer to the byte-array of data to write to the device
[in,out]tx_lengthNumber of bytes to write, updated with the number actually written
[out]rxPointer to the byte-array of data to read from the device
[in,out]rx_lengthNumber of bytes to read, udpated with the number actually read
[in]write_fillDefault data transmitted while performing a read
Returns
The status of the transfer request
Note
Both MOSI and MISO pins required to be non-NC for this API to operate

◆ cyhal_spi_transfer_async()

cy_rslt_t cyhal_spi_transfer_async ( cyhal_spi_t *  obj,
const uint8_t *  tx,
size_t  tx_length,
uint8_t *  rx,
size_t  rx_length 
)

Begin the SPI transfer. Buffer pointers and lengths are specified in tx_buff and rx_buff

Parameters
[in]objThe SPI object that holds the transfer information
[in]txThe transmit buffer
[in,out]tx_lengthThe number of bytes to transmit
[out]rxThe receive buffer
[in,out]rx_lengthThe number of bytes to receive
Returns
The status of the transfer_async request
Note
Both MOSI and MISO pins required to be non-NC for this API to operate

◆ cyhal_spi_is_busy()

bool cyhal_spi_is_busy ( cyhal_spi_t *  obj)

Checks if the specified SPI peripheral is in use

Parameters
[in]objThe SPI peripheral to check
Returns
Indication of whether the SPI is still transmitting

◆ cyhal_spi_abort_async()

cy_rslt_t cyhal_spi_abort_async ( cyhal_spi_t *  obj)

Abort an SPI transfer

Parameters
[in]objThe SPI peripheral to stop
Returns
The status of the abort_async request

◆ cyhal_spi_register_callback()

void cyhal_spi_register_callback ( cyhal_spi_t *  obj,
cyhal_spi_event_callback_t  callback,
void *  callback_arg 
)

The SPI callback handler registration

Parameters
[in]objThe SPI object
[in]callbackThe callback handler which will be invoked when the interrupt fires
[in]callback_argGeneric argument that will be provided to the callback when called

◆ cyhal_spi_enable_event()

void cyhal_spi_enable_event ( cyhal_spi_t *  obj,
cyhal_spi_event_t  event,
uint8_t  intrPriority,
bool  enable 
)

Configure SPI interrupt. This function is used for word-approach

Parameters
[in]objThe SPI object
[in]eventThe SPI event type
[in]intrPriorityThe priority for NVIC interrupt events
[in]enableTrue to turn on interrupts, False to turn off