High level interface for interacting with the Serial Peripheral Interface (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.
Initialise a SPI master or slave interface using cyhal_spi_init() and provide the SPI pins (mosi, miso, sclk, ssel), number of bits per frame (data_bits) and SPI Motorola mode. The data rate can be set using cyhal_spi_set_frequency().
See Code snippets for code snippets to send or receive the data.
The following code snippet initializes an SPI Master interface using the cyhal_spi_init(). The data rate of transfer is set using cyhal_spi_set_frequency(). The code snippet shows how to transfer a single byte of data using cyhal_spi_send() and cyhal_spi_recv().
The following code snippet initializes an SPI Slave interface using the cyhal_spi_init(). The data rate of transfer is set using cyhal_spi_set_frequency. The code snippet shows how to transfer a single byte of data using cyhal_spi_send() and cyhal_spi_recv.
The following snippet sends and receives an array of data in a single SPI transaction using cyhal_spi_transfer(). The example uses SPI master to transmit 5 bytes of data and receive 5 bytes of data in a single transaction.
SPI interrupt events ( cyhal_spi_event_t) can be mapped to an interrupt and assigned to a callback function. The callback function needs to be first registered and then the event needs to be enabled. The following snippet initialises a SPI master to perform a block transfer using cyhal_spi_transfer_async(). This is a non-blocking function. A callback function is registered using cyhal_spi_register_callback to notify whenever the SPI transfer is complete.
API Reference | |
SPI HAL Results | |
SPI specific return codes. | |
Data Structures | |
struct | cyhal_spi_cfg_t |
Initial SPI configuration. More... | |
Macros | |
#define | cyhal_spi_frequency cyhal_spi_set_frequency |
Compatibility define for cyhal_spi_set_frequency. More... | |
#define | CYHAL_SPI_MODE_FLAG_LSB (0x01u) |
Flag for SPI cyhal_spi_mode_t values indicating that the LSB is sent first. More... | |
#define | CYHAL_SPI_MODE_FLAG_CPHA (0x02u) |
Flag for SPI cyhal_spi_mode_t values indicating that the CPHA=1. More... | |
#define | CYHAL_SPI_MODE_FLAG_CPOL (0x04u) |
Flag for SPI cyhal_spi_mode_t values indicating that the CPOL=1. More... | |
#define | CYHAL_SPI_MODE(cpol, cpha, lsb) |
Creates a cyhal_spi_mode_t value given the cpol, cpha, lsb values. More... | |
Typedefs | |
typedef void(* | cyhal_spi_event_callback_t) (void *callback_arg, cyhal_spi_event_t event) |
Handler for SPI interrupts. | |
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 } |
SPI interrupt triggers. More... | |
enum | cyhal_spi_ssel_polarity_t { CYHAL_SPI_SSEL_ACTIVE_LOW = 0, CYHAL_SPI_SSEL_ACTIVE_HIGH = 1 } |
SPI Slave Select polarity. More... | |
enum | cyhal_spi_mode_t { CYHAL_SPI_MODE_00_MSB = CYHAL_SPI_MODE(0, 0, 0), CYHAL_SPI_MODE_00_LSB = CYHAL_SPI_MODE(0, 0, 1), CYHAL_SPI_MODE_01_MSB = CYHAL_SPI_MODE(0, 1, 0), CYHAL_SPI_MODE_01_LSB = CYHAL_SPI_MODE(0, 1, 1), CYHAL_SPI_MODE_10_MSB = CYHAL_SPI_MODE(1, 0, 0), CYHAL_SPI_MODE_10_LSB = CYHAL_SPI_MODE(1, 0, 1), CYHAL_SPI_MODE_11_MSB = CYHAL_SPI_MODE(1, 1, 0), CYHAL_SPI_MODE_11_LSB = CYHAL_SPI_MODE(1, 1, 1) } |
SPI operating modes. More... | |
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_t *clk, uint8_t bits, cyhal_spi_mode_t mode, bool is_slave) |
Initialize the SPI peripheral. More... | |
void | cyhal_spi_free (cyhal_spi_t *obj) |
Release a SPI object. More... | |
cy_rslt_t | cyhal_spi_set_frequency (cyhal_spi_t *obj, uint32_t hz) |
Set the SPI baud rate. More... | |
cy_rslt_t | cyhal_spi_slave_select_config (cyhal_spi_t *obj, cyhal_gpio_t ssel, cyhal_spi_ssel_polarity_t polarity) |
Configures provided ssel pin to work as SPI slave select with specified polarity. More... | |
cy_rslt_t | cyhal_spi_select_active_ssel (cyhal_spi_t *obj, cyhal_gpio_t ssel) |
Selects an active slave select line from one of available. More... | |
cy_rslt_t | cyhal_spi_recv (cyhal_spi_t *obj, uint32_t *value) |
Synchronously get a received value out of the SPI receive buffer. More... | |
cy_rslt_t | cyhal_spi_send (cyhal_spi_t *obj, uint32_t value) |
Synchronously send a byte out. More... | |
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) |
Synchronously Write a block out and receive a value. More... | |
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) |
Start an asynchronous SPI transfer. More... | |
bool | cyhal_spi_is_busy (cyhal_spi_t *obj) |
Checks if the specified SPI peripheral is in use. More... | |
cy_rslt_t | cyhal_spi_abort_async (cyhal_spi_t *obj) |
Abort an SPI transfer. More... | |
void | cyhal_spi_register_callback (cyhal_spi_t *obj, cyhal_spi_event_callback_t callback, void *callback_arg) |
Register a SPI callback handler. More... | |
void | cyhal_spi_enable_event (cyhal_spi_t *obj, cyhal_spi_event_t event, uint8_t intr_priority, bool enable) |
Configure SPI interrupt. More... | |
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. |
#define cyhal_spi_frequency cyhal_spi_set_frequency |
Compatibility define for cyhal_spi_set_frequency.
#define CYHAL_SPI_MODE_FLAG_LSB (0x01u) |
Flag for SPI cyhal_spi_mode_t values indicating that the LSB is sent first.
#define CYHAL_SPI_MODE_FLAG_CPHA (0x02u) |
Flag for SPI cyhal_spi_mode_t values indicating that the CPHA=1.
#define CYHAL_SPI_MODE_FLAG_CPOL (0x04u) |
Flag for SPI cyhal_spi_mode_t values indicating that the CPOL=1.
#define CYHAL_SPI_MODE | ( | cpol, | |
cpha, | |||
lsb | |||
) |
Creates a cyhal_spi_mode_t value given the cpol, cpha, lsb values.
enum cyhal_spi_event_t |
enum cyhal_spi_mode_t |
SPI operating modes.
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_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
[out] | obj | Pointer to a SPI object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | mosi | The pin to use for MOSI |
[in] | miso | The pin to use for MISO |
[in] | sclk | The pin to use for SCLK |
[in] | ssel | The pin to use for SSEL |
[in] | clk | The clock to use can be shared, if not provided a new clock will be allocated |
[in] | bits | The number of bits per frame |
[in] | mode | The SPI mode (clock polarity, phase, and shift direction) |
[in] | is_slave | false for master mode or true for slave mode operation |
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
[in,out] | obj | The SPI object to deinitialize |
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
[in,out] | obj | The SPI object to configure |
[in] | hz | The baud rate in Hz |
cy_rslt_t cyhal_spi_slave_select_config | ( | cyhal_spi_t * | obj, |
cyhal_gpio_t | ssel, | ||
cyhal_spi_ssel_polarity_t | polarity | ||
) |
Configures provided ssel pin to work as SPI slave select with specified polarity.
Multiple pins can be configured as SPI slave select pins. Please refer to device datasheet for details. Switching between configured slave select pins is done by cyhal_spi_select_active_ssel function. Unless modified with this function, the SSEL pin provided as part of cyhal_spi_init is the default.
[in] | obj | The SPI object to add slave select for |
[in] | ssel | Slave select pin to be added |
[in] | polarity | Polarity of slave select |
cy_rslt_t cyhal_spi_select_active_ssel | ( | cyhal_spi_t * | obj, |
cyhal_gpio_t | ssel | ||
) |
Selects an active slave select line from one of available.
This function is applicable for the master and slave. SSEL pin should be configured by cyhal_spi_slave_select_config or cyhal_spi_init functions prior to selecting it as active. The active slave select line will automatically be toggled as part of any transfer.
[in] | obj | The SPI object for switching |
[in] | ssel | Slave select pin to be set as active |
cy_rslt_t cyhal_spi_recv | ( | cyhal_spi_t * | obj, |
uint32_t * | value | ||
) |
Synchronously 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
[in] | obj | The SPI peripheral to read |
[in] | value | The value received |
cy_rslt_t cyhal_spi_send | ( | cyhal_spi_t * | obj, |
uint32_t | value | ||
) |
Synchronously 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
[in] | obj | The SPI peripheral to use for sending |
[in] | value | The value to send |
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 | ||
) |
Synchronously 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 (at the end) with the value given by write_fill.
This function will block for the duration of the transfer. cyhal_spi_transfer_async can be used for non-blocking transfers.
[in] | obj | The SPI peripheral to use for sending |
[in] | tx | Pointer to the byte-array of data to write to the device |
[in,out] | tx_length | Number of bytes to write, updated with the number actually written |
[out] | rx | Pointer to the byte-array of data to read from the device |
[in,out] | rx_length | Number of bytes to read, updated with the number actually read |
[in] | write_fill | Default data transmitted while performing a read |
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 | ||
) |
Start an asynchronous SPI transfer.
This will transfer rx_length
bytes into the buffer pointed to by rx
, while simultaneously transfering tx_length
bytes of data from the buffer pointed to by tx
, both in the background. When the transfer is complete, the CYHAL_SPI_IRQ_DONE event will be raised. See cyhal_spi_register_callback and cyhal_spi_enable_event.
[in] | obj | The SPI object that holds the transfer information |
[in] | tx | The transmit buffer |
[in,out] | tx_length | The number of bytes to transmit |
[out] | rx | The receive buffer |
[in,out] | rx_length | The number of bytes to receive |
bool cyhal_spi_is_busy | ( | cyhal_spi_t * | obj | ) |
Checks if the specified SPI peripheral is in use.
[in] | obj | The SPI peripheral to check |
cy_rslt_t cyhal_spi_abort_async | ( | cyhal_spi_t * | obj | ) |
Abort an SPI transfer.
[in] | obj | The SPI peripheral to stop |
void cyhal_spi_register_callback | ( | cyhal_spi_t * | obj, |
cyhal_spi_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a SPI callback handler.
This function will be called when one of the events enabled by cyhal_spi_enable_event occurs.
[in] | obj | The SPI object |
[in] | callback | The callback handler which will be invoked when the interrupt fires |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void cyhal_spi_enable_event | ( | cyhal_spi_t * | obj, |
cyhal_spi_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Configure SPI interrupt.
This function is used for word-approach
When an enabled event occurs, the function specified by cyhal_spi_register_callback will be called.
[in] | obj | The SPI object |
[in] | event | The SPI event type |
[in] | intr_priority | The priority for NVIC interrupt events |
[in] | enable | True to turn on interrupts, False to turn off |