High level interface for interacting with the Quad-SPI interface.
QSPI is an SPI-based communication interface, often used with external memory devices. The QSPI driver supports sending and receiving commands to/from from another device via a single, dual, quad, or octal SPI interface.
The following code snip demonstrates an example for initializing the cyhal_qspi_command_t structure for any given flash command. The cyhal_qspi_command_t.mode_bits structure has several other components which should be set as per the command. Mode bits are not required for single SPI read command, hence, mode_bits.disabled is set to TRUE in the below example code.
This example function demonstrates the initialization of the QSPI component and use of the cyhal_qspi_read() function to complete the read operation and receive the read data in a buffer.
The following code snippet demonstrates the use of cyhal_qspi_transfer() API for sending single byte instruction that may or may not need any address or data bytes. It also shows the usage of status register read command within a while loop to poll the WIP bit status.
This code snippet demonstrates the usage cyhal_qspi_write() API for executing program operation on flash memory.
API Reference | |
QSPI HAL Results | |
QSPI specific return codes. | |
Data Structures | |
struct | cyhal_qspi_command_t |
QSPI command settings. More... | |
struct | cyhal_qspi_command_t.instruction |
struct | cyhal_qspi_command_t.address |
struct | cyhal_qspi_command_t.mode_bits |
struct | cyhal_qspi_command_t.data |
Typedefs | |
typedef void(* | cyhal_qspi_event_callback_t) (void *callback_arg, cyhal_qspi_event_t event) |
Handler for QSPI callbacks. | |
Enumerations | |
enum | cyhal_qspi_bus_width_t { CYHAL_QSPI_CFG_BUS_SINGLE = 1, CYHAL_QSPI_CFG_BUS_DUAL = 2, CYHAL_QSPI_CFG_BUS_QUAD = 4, CYHAL_QSPI_CFG_BUS_OCTAL = 8 } |
QSPI Bus width. More... | |
enum | cyhal_qspi_size_t { CYHAL_QSPI_CFG_SIZE_8 = 8, CYHAL_QSPI_CFG_SIZE_16 = 16, CYHAL_QSPI_CFG_SIZE_24 = 24, CYHAL_QSPI_CFG_SIZE_32 = 32 } |
Address size in bits. More... | |
enum | cyhal_qspi_event_t { CYHAL_QSPI_EVENT_NONE = 0, CYHAL_QSPI_IRQ_TRANSMIT_DONE = 1 << 0, CYHAL_QSPI_IRQ_RECEIVE_DONE = 1 << 1 } |
QSPI interrupt triggers. More... | |
Functions | |
cy_rslt_t | cyhal_qspi_init (cyhal_qspi_t *obj, cyhal_gpio_t io0, cyhal_gpio_t io1, cyhal_gpio_t io2, cyhal_gpio_t io3, cyhal_gpio_t io4, cyhal_gpio_t io5, cyhal_gpio_t io6, cyhal_gpio_t io7, cyhal_gpio_t sclk, cyhal_gpio_t ssel, uint32_t hz, uint8_t mode) |
Initialize QSPI peripheral. More... | |
void | cyhal_qspi_free (cyhal_qspi_t *obj) |
Deinitilize QSPI peripheral. More... | |
cy_rslt_t | cyhal_qspi_set_frequency (cyhal_qspi_t *obj, uint32_t hz) |
Set the QSPI baud rate. More... | |
uint32_t | cyhal_qspi_get_frequency (cyhal_qspi_t *obj) |
Get the actual frequency that QSPI is configured for. More... | |
cy_rslt_t | cyhal_qspi_slave_select_config (cyhal_qspi_t *obj, cyhal_gpio_t ssel) |
Configures provided pin to work as QSPI slave select (SSEL) More... | |
cy_rslt_t | cyhal_qspi_select_active_ssel (cyhal_qspi_t *obj, cyhal_gpio_t ssel) |
Selects an active slave select (SSEL) line from one of available. More... | |
cy_rslt_t | cyhal_qspi_read (cyhal_qspi_t *obj, const cyhal_qspi_command_t *command, void *data, size_t *length) |
Receive a command and block of data, synchronously. More... | |
cy_rslt_t | cyhal_qspi_read_async (cyhal_qspi_t *obj, const cyhal_qspi_command_t *command, void *data, size_t *length) |
Receive a command and block of data in asynchronous mode. More... | |
cy_rslt_t | cyhal_qspi_write (cyhal_qspi_t *obj, const cyhal_qspi_command_t *command, const void *data, size_t *length) |
Send a command and block of data, synchronously. More... | |
cy_rslt_t | cyhal_qspi_write_async (cyhal_qspi_t *obj, const cyhal_qspi_command_t *command, const void *data, size_t *length) |
Send a command and block of data in asynchronous mode. More... | |
cy_rslt_t | cyhal_qspi_transfer (cyhal_qspi_t *obj, const cyhal_qspi_command_t *command, const void *tx_data, size_t tx_size, void *rx_data, size_t rx_size) |
Send a command (and optionally data) and get the response. More... | |
void | cyhal_qspi_register_callback (cyhal_qspi_t *obj, cyhal_qspi_event_callback_t callback, void *callback_arg) |
Register a QSPI event handler. More... | |
void | cyhal_qspi_enable_event (cyhal_qspi_t *obj, cyhal_qspi_event_t event, uint8_t intr_priority, bool enable) |
Configure QSPI interrupt enablement. More... | |
struct cyhal_qspi_command_t |
Data Fields | ||
---|---|---|
struct cyhal_qspi_command_t | instruction | Instruction structure. |
struct cyhal_qspi_command_t | address | Address structure. |
struct cyhal_qspi_command_t | mode_bits | Mode bits structure. |
uint8_t | dummy_count | Dummy cycles count. |
struct cyhal_qspi_command_t | data | Data structure. |
struct cyhal_qspi_command_t.instruction |
Data Fields | ||
---|---|---|
cyhal_qspi_bus_width_t | bus_width | Bus width for the instruction. |
uint8_t | value | Instruction value. |
bool | disabled | Instruction phase skipped if disabled is set to true. |
struct cyhal_qspi_command_t.address |
Data Fields | ||
---|---|---|
cyhal_qspi_bus_width_t | bus_width | Bus width for the address. |
cyhal_qspi_size_t | size | Address size. |
uint32_t | value | Address value. |
bool | disabled | Address phase skipped if disabled is set to true. |
struct cyhal_qspi_command_t.mode_bits |
Data Fields | ||
---|---|---|
cyhal_qspi_bus_width_t | bus_width | Bus width for mode bits. |
cyhal_qspi_size_t | size | Mode bits size. |
uint32_t | value | Mode bits value. |
bool | disabled | Mode bits phase skipped if disabled is set to true. |
struct cyhal_qspi_command_t.data |
Data Fields | ||
---|---|---|
cyhal_qspi_bus_width_t | bus_width | Bus width for data. |
enum cyhal_qspi_size_t |
enum cyhal_qspi_event_t |
cy_rslt_t cyhal_qspi_init | ( | cyhal_qspi_t * | obj, |
cyhal_gpio_t | io0, | ||
cyhal_gpio_t | io1, | ||
cyhal_gpio_t | io2, | ||
cyhal_gpio_t | io3, | ||
cyhal_gpio_t | io4, | ||
cyhal_gpio_t | io5, | ||
cyhal_gpio_t | io6, | ||
cyhal_gpio_t | io7, | ||
cyhal_gpio_t | sclk, | ||
cyhal_gpio_t | ssel, | ||
uint32_t | hz, | ||
uint8_t | mode | ||
) |
Initialize QSPI peripheral.
It should initialize QSPI pins (io0-io7, sclk and ssel), set frequency, clock polarity and phase mode. The clock for the peripheral should be enabled
[out] | obj | Pointer to a QSPI object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | io0 | Data pin 0 |
[in] | io1 | Data pin 1 |
[in] | io2 | Data pin 2 |
[in] | io3 | Data pin 3 |
[in] | io4 | Data pin 4 |
[in] | io5 | Data pin 5 |
[in] | io6 | Data pin 6 |
[in] | io7 | Data pin 7 |
[in] | sclk | The clock pin |
[in] | ssel | The chip select pin |
[in] | hz | The bus frequency |
[in] | mode | Clock polarity and phase mode (0 - 3) |
void cyhal_qspi_free | ( | cyhal_qspi_t * | obj | ) |
Deinitilize QSPI peripheral.
It should release pins that are associated with the QSPI object, and disable clocks for QSPI peripheral module that was associated with the object
[in,out] | obj | QSPI object |
cy_rslt_t cyhal_qspi_set_frequency | ( | cyhal_qspi_t * | obj, |
uint32_t | hz | ||
) |
Set the QSPI baud rate.
Actual frequency may differ from the desired frequency due to available dividers and the bus clock. Function will apply achieved frequency only if it is in +0% /-10% deviation bounds from desired. Use cyhal_qspi_get_frequency function to get actual frequency value that was achieved and set.
[in] | obj | The QSPI object to configure |
[in] | hz | The baud rate in Hz |
uint32_t cyhal_qspi_get_frequency | ( | cyhal_qspi_t * | obj | ) |
Get the actual frequency that QSPI is configured for.
[in] | obj | The QSPI object to configure |
cy_rslt_t cyhal_qspi_slave_select_config | ( | cyhal_qspi_t * | obj, |
cyhal_gpio_t | ssel | ||
) |
Configures provided pin to work as QSPI slave select (SSEL)
Multiple pins can be configured as QSPI slave select pins. Please refer to device datasheet for details. Switching between configured slave select pins is done by cyhal_qspi_select_active_ssel function. Unless modified with this function, the SSEL pin provided as part of cyhal_qspi_init is the default.
[in] | obj | The QSPI object to configure |
[in] | ssel | Pin to be configured as QSPI SSEL |
cy_rslt_t cyhal_qspi_select_active_ssel | ( | cyhal_qspi_t * | obj, |
cyhal_gpio_t | ssel | ||
) |
Selects an active slave select (SSEL) line from one of available.
SSEL pin should be configured by cyhal_qspi_slave_select_config or cyhal_qspi_init functions prior to selecting it as active.
[in] | obj | The QSPI object to configure |
[in] | ssel | SSEL pin to be set as active |
cy_rslt_t cyhal_qspi_read | ( | cyhal_qspi_t * | obj, |
const cyhal_qspi_command_t * | command, | ||
void * | data, | ||
size_t * | length | ||
) |
Receive a command and block of data, synchronously.
This will read either length
bytes or the number of bytes that are currently available in the receive buffer, whichever is less, then return. The value pointed to by length
will be updated to reflect the number of bytes that were actually read.
[in] | obj | QSPI object |
[in] | command | QSPI command |
[out] | data | RX buffer |
[in] | length | RX buffer length in bytes |
cy_rslt_t cyhal_qspi_read_async | ( | cyhal_qspi_t * | obj, |
const cyhal_qspi_command_t * | command, | ||
void * | data, | ||
size_t * | length | ||
) |
Receive a command and block of data in asynchronous mode.
This will transfer length
bytes into the buffer pointed to by data
in the background. When the requested quantity of data has been read, the CYHAL_QSPI_IRQ_RECEIVE_DONE event will be raised. See cyhal_qspi_register_callback and cyhal_qspi_enable_event.
[in] | obj | QSPI object |
[in] | command | QSPI command |
[out] | data | RX buffer |
[in] | length | RX buffer length in bytes |
cy_rslt_t cyhal_qspi_write | ( | cyhal_qspi_t * | obj, |
const cyhal_qspi_command_t * | command, | ||
const void * | data, | ||
size_t * | length | ||
) |
Send a command and block of data, synchronously.
This will write either length
bytes or until the write buffer is full, whichever is less, then return. The value pointed to by length
will be updated to reflect the number of bytes that were actually written.
[in] | obj | QSPI object |
[in] | command | QSPI command |
[in] | data | TX buffer |
[in] | length | TX buffer length in bytes |
cy_rslt_t cyhal_qspi_write_async | ( | cyhal_qspi_t * | obj, |
const cyhal_qspi_command_t * | command, | ||
const void * | data, | ||
size_t * | length | ||
) |
Send a command and block of data in asynchronous mode.
This will transfer length
bytes into the tx buffer in the background. When the requested quantity of data has been queued in the transmit buffer, the CYHAL_QSPI_IRQ_TRANSMIT_DONE event will be raised. See cyhal_qspi_register_callback and cyhal_qspi_enable_event.
[in] | obj | QSPI object |
[in] | command | QSPI command |
[in] | data | TX buffer |
[in] | length | TX buffer length in bytes |
cy_rslt_t cyhal_qspi_transfer | ( | cyhal_qspi_t * | obj, |
const cyhal_qspi_command_t * | command, | ||
const void * | tx_data, | ||
size_t | tx_size, | ||
void * | rx_data, | ||
size_t | rx_size | ||
) |
Send a command (and optionally data) and get the response.
Can be used to send/receive device specific commands
[in] | obj | QSPI object |
[in] | command | QSPI command |
[in] | tx_data | TX buffer |
[in] | tx_size | TX buffer length in bytes |
[out] | rx_data | RX buffer |
[in] | rx_size | RX buffer length in bytes |
void cyhal_qspi_register_callback | ( | cyhal_qspi_t * | obj, |
cyhal_qspi_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a QSPI event handler.
This function will be called when one of the events enabled by cyhal_qspi_enable_event occurs.
[in] | obj | The QSPI 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 handler when called |
void cyhal_qspi_enable_event | ( | cyhal_qspi_t * | obj, |
cyhal_qspi_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Configure QSPI interrupt enablement.
When an enabled event occurs, the function specified by cyhal_qspi_register_callback will be called.
[in] | obj | The QSPI object |
[in] | event | The QSPI event type |
[in] | intr_priority | The priority for NVIC interrupt events |
[in] | enable | True to turn on interrupts, False to turn off |