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.
|
| #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 |
| |
|
| 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) |
| |
◆ 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. |
◆ CYHAL_SPI_RSLT_BAD_ARGUMENT
◆ CYHAL_SPI_RSLT_CLOCK_ERROR
Failed to initialize SPI clock
◆ CYHAL_SPI_RSLT_TRANSFER_ERROR
Failed to Transfer SPI data
◆ CYHAL_SPI_RSLT_CLOCK_NOT_SUPPORTED
Provided clock is not supported by SPI
◆ CYHAL_SPI_RSLT_PIN_CONFIG_NOT_SUPPORTED
Provided PIN configuration is not supported by SPI
◆ CYHAL_SPI_RSLT_INVALID_PIN_API_NOT_SUPPORTED
Provided PIN configuration is not supported by SPI
◆ CYHAL_SPI_RSLT_ERR_INVALID_PIN
The requested resource type is invalid
◆ cyhal_spi_frequency
Compatibility define for cyhal_spi_set_frequency.
◆ cyhal_spi_event_callback_t
| typedef void(* cyhal_spi_event_callback_t) (void *callback_arg, cyhal_spi_event_t event) |
Handler for SPI interrupts
◆ 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
|
◆ cyhal_spi_init()
Initialize the SPI peripheral
Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
- Parameters
-
| [out] | obj | The SPI object to initialize |
| [in] | mosi | The pin to use for MOSI |
- Note
- At least MOSI or MISO pin should be non-NC
- Parameters
-
| [in] | miso | The pin to use for MISO |
- Note
- At least MOSI or MISO pin should be non-NC
- Parameters
-
| [in] | sclk | The pin to use for SCLK |
- Note
- This pin cannot be NC
- Parameters
-
| [in] | ssel | The pin to use for SSEL |
- Note
- This pin can be NC
- Parameters
-
| [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 |
- Note
- bits should be 8 or 16
- Parameters
-
| [in] | mode | The SPI mode (clock polarity, phase, and shift direction) |
| [in] | is_slave | false for master mode or true for slave mode operation |
- Returns
- The status of the init request
◆ cyhal_spi_free()
Release a SPI object
Return the peripheral, pins and clock owned by the SPI object to their reset state
- Parameters
-
| [in,out] | obj | The SPI object to deinitialize |
◆ cyhal_spi_set_frequency()
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] | obj | The SPI object to configure |
| [in] | hz | The baud rate in Hz |
- Returns
- The status of the set_frequency request
◆ cyhal_spi_recv()
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] | obj | The SPI peripheral to read |
| [in] | value | The 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()
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] | obj | The SPI peripheral to use for sending |
| [in] | value | The 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] | 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, udpated with the number actually read |
| [in] | write_fill | Default 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] | 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 |
- 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()
Checks if the specified SPI peripheral is in use
- Parameters
-
| [in] | obj | The SPI peripheral to check |
- Returns
- Indication of whether the SPI is still transmitting
◆ cyhal_spi_abort_async()
Abort an SPI transfer
- Parameters
-
| [in] | obj | The SPI peripheral to stop |
- Returns
- The status of the abort_async request
◆ cyhal_spi_register_callback()
The SPI callback handler registration
- Parameters
-
| [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 |
◆ cyhal_spi_enable_event()
Configure SPI interrupt. This function is used for word-approach
- Parameters
-
| [in] | obj | The SPI object |
| [in] | event | The SPI event type |
| [in] | intrPriority | The priority for NVIC interrupt events |
| [in] | enable | True to turn on interrupts, False to turn off |