High level interface for interacting with the Serial Peripheral Interface (SPI).
The SPI protocol is a synchronous serial interface protocol. Devices operate in either controller or target mode. The controller initiates the data transfer.
Initialise a SPI controller or target interface using the PDL. The data rate can be set using mtb_hal_spi_set_frequency().
See Code snippets for code snippets to send or receive the data.
The following code snippet initializes an SPI Controller or Target interface using the PDL.
The following code snippet initializes an SPI Controller interface using the PDL. The data rate of transfer is set using mtb_hal_spi_set_frequency(). The code snippet shows how to transfer a single byte of data using mtb_hal_spi_put() and mtb_hal_spi_get().
The following code snippet initializes an SPI Target interface using the PDL. The data rate of transfer is set using mtb_hal_spi_set_frequency. The code snippet shows how to transfer a single byte of data using mtb_hal_spi_put() and mtb_hal_spi_get.
The following snippet sends and receives an array of data in a single SPI transaction using mtb_hal_spi_transfer(). The example uses SPI controller to transmit 5 bytes of data and receive 5 bytes of data in a single transaction.
The following snippet sends an array of data in a single SPI transaction using mtb_hal_spi_target_write(). The example uses SPI target to transmit 5 bytes of data in a single transaction with 50 ms timeout values.
SPI interrupt events (mtb_hal_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. A callback function is registered using mtb_hal_spi_register_callback to notify whenever the SPI transfer is complete.
mtb-example-psoc6-spi-controller: This example project demonstrates use of SPI (HAL) resource in PSoC® 6 MCU in Controller mode to write data to an SPI target.
API Reference | |
SPI HAL Results | |
SPI specific return codes. | |
Typedefs | |
typedef void(* | mtb_hal_spi_event_callback_t) (void *callback_arg, mtb_hal_spi_event_t event) |
Handler for SPI interrupts. | |
Enumerations | |
enum | mtb_hal_spi_event_t { MTB_HAL_SPI_EVENT_NONE = 0 , MTB_HAL_SPI_IRQ_DATA_IN_FIFO = (MTB_HAL_MAP_SPI_IRQ_DATA_IN_FIFO) , MTB_HAL_SPI_IRQ_DONE = (MTB_HAL_MAP_SPI_IRQ_DONE) , MTB_HAL_SPI_IRQ_ERROR = (MTB_HAL_MAP_SPI_IRQ_ERROR) } |
SPI interrupt triggers. More... | |
enum | mtb_hal_spi_fifo_type_t { MTB_HAL_SPI_FIFO_RX , MTB_HAL_SPI_FIFO_TX } |
SPI FIFO type. More... | |
Functions | |
cy_rslt_t | mtb_hal_spi_set_frequency (mtb_hal_spi_t *obj, uint32_t hz) |
Set the SPI baud rate. More... | |
cy_rslt_t | mtb_hal_spi_set_fifo_level (mtb_hal_spi_t *obj, mtb_hal_spi_fifo_type_t type, uint16_t level) |
Sets a threshold level for a FIFO that will generate an interrupt and a trigger output. More... | |
cy_rslt_t | mtb_hal_spi_get (mtb_hal_spi_t *obj, uint32_t *value) |
Synchronously get a received value out of the SPI receive buffer. More... | |
cy_rslt_t | mtb_hal_spi_put (mtb_hal_spi_t *obj, uint32_t value) |
Synchronously send a byte out. More... | |
cy_rslt_t | mtb_hal_spi_controller_write (mtb_hal_spi_t *obj, const uint8_t *src_buff, uint16_t *size) |
Write data from the user-defined buffer to the controller TX FIFO. More... | |
cy_rslt_t | mtb_hal_spi_target_read (mtb_hal_spi_t *obj, uint8_t *dst_buff, uint16_t *size, uint32_t timeout) |
Wait for controller send data to RX buffer and store them to the user-defined buffer. More... | |
cy_rslt_t | mtb_hal_spi_target_write (mtb_hal_spi_t *obj, const uint8_t *src_buff, uint16_t *size, uint32_t timeout) |
Write data from the user-defined buffer to TX buffer and wait until either all data has been read from TX buffer and shift register, or timeout is hit. More... | |
cy_rslt_t | mtb_hal_spi_transfer (mtb_hal_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 | mtb_hal_spi_clear (mtb_hal_spi_t *obj) |
Clear the SPI buffers. More... | |
void | mtb_hal_spi_register_callback (mtb_hal_spi_t *obj, mtb_hal_spi_event_callback_t callback, void *callback_arg) |
Register a SPI callback handler. More... | |
void | mtb_hal_spi_enable_event (mtb_hal_spi_t *obj, mtb_hal_spi_event_t event, bool enable) |
Configure SPI interrupt. More... | |
cy_rslt_t | mtb_hal_spi_process_interrupt (mtb_hal_spi_t *obj) |
Process interrupts related to a SPI instance. More... | |
enum mtb_hal_spi_event_t |
cy_rslt_t mtb_hal_spi_set_frequency | ( | mtb_hal_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 mtb_hal_spi_set_fifo_level | ( | mtb_hal_spi_t * | obj, |
mtb_hal_spi_fifo_type_t | type, | ||
uint16_t | level | ||
) |
Sets a threshold level for a FIFO that will generate an interrupt and a trigger output.
The RX FIFO interrupt and trigger will be activated when the receive FIFO has more entries than the threshold. The TX FIFO interrupt and trigger will be activated when the transmit FIFO has less entries than the threshold.
[in] | obj | The SPI object |
[in] | type | FIFO type to set level for |
[in] | level | Level threshold to set. See the device datasheet for valid range. |
cy_rslt_t mtb_hal_spi_get | ( | mtb_hal_spi_t * | obj, |
uint32_t * | value | ||
) |
Synchronously get a received value out of the SPI receive buffer.
In Controller mode - transmits fill-in value and read the data from RxFifo In Target mode - Blocks until a value is available
[in] | obj | The SPI peripheral to read |
[in] | value | The value received |
cy_rslt_t mtb_hal_spi_put | ( | mtb_hal_spi_t * | obj, |
uint32_t | value | ||
) |
Synchronously send a byte out.
In Controller mode transmits value to target and read/drop a value from the RxFifo. In Target mode writes a value to TxFifo
[in] | obj | The SPI peripheral to use for sending |
[in] | value | The value to send |
cy_rslt_t mtb_hal_spi_controller_write | ( | mtb_hal_spi_t * | obj, |
const uint8_t * | src_buff, | ||
uint16_t * | size | ||
) |
Write data from the user-defined buffer to the controller TX FIFO.
Performs non-blocking controller-mode transfer of an array of data. NOTE: If the size of the actual data is less than the expected, the function will copy only the available data and return.
[in] | obj | The SPI object |
[in] | src_buff | Pointer on memory to copy the data to the controller TX buffer. |
[in,out] | size | [in] The number of bytes to send, [out] number actually sent. |
cy_rslt_t mtb_hal_spi_target_read | ( | mtb_hal_spi_t * | obj, |
uint8_t * | dst_buff, | ||
uint16_t * | size, | ||
uint32_t | timeout | ||
) |
Wait for controller send data to RX buffer and store them to the user-defined buffer.
NOTE: If size of actual data is less then expected the function copy only available data.
[in] | obj | The SPI object |
[in] | dst_buff | Pointer on memory to store the data from the target RX buffer. |
[in,out] | size | [in] The number of bytes to read, [out] number actually read. |
[in] | timeout | Timeout in millisecond, set this value to 0 if you don't want to wait at all. |
cy_rslt_t mtb_hal_spi_target_write | ( | mtb_hal_spi_t * | obj, |
const uint8_t * | src_buff, | ||
uint16_t * | size, | ||
uint32_t | timeout | ||
) |
Write data from the user-defined buffer to TX buffer and wait until either all data has been read from TX buffer and shift register, or timeout is hit.
The user needs to provide the interrupt handler and call mtb_hal_spi_process_interrupt to process the interrupt.
NOTE: If size of actual data is less then expected the function copy only available data.
[in] | obj | The SPI object |
[in] | src_buff | Pointer on memory to copy the data to the target TX buffer. |
[in,out] | size | [in] The number of bytes to send, [out] number actually sent. |
[in] | timeout | Timeout in millisecond, set this value to 0 if you don't want to wait at all. |
cy_rslt_t mtb_hal_spi_transfer | ( | mtb_hal_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. Using this function will block for the duration of the transfer. The user needs to provide the interrupt handler and call mtb_hal_spi_process_interrupt to process the interrupt.
[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 mtb_hal_spi_clear | ( | mtb_hal_spi_t * | obj | ) |
Clear the SPI buffers.
[in] | obj | The SPI object |
void mtb_hal_spi_register_callback | ( | mtb_hal_spi_t * | obj, |
mtb_hal_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 mtb_hal_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 mtb_hal_spi_enable_event | ( | mtb_hal_spi_t * | obj, |
mtb_hal_spi_event_t | event, | ||
bool | enable | ||
) |
Configure SPI interrupt.
This function is used for word-approach
When an enabled event occurs, the function specified by mtb_hal_spi_register_callback will be called.
[in] | obj | The SPI object |
[in] | event | The SPI event type |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t mtb_hal_spi_process_interrupt | ( | mtb_hal_spi_t * | obj | ) |
Process interrupts related to a SPI instance.
obj | HAL object for which the interrupt should be processed |