High level interface for interacting with the Memory SPI interface.
MemorySPI is an SPI-based communication interface, often used with external memory devices. The MemorySPI 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 mtb_hal_memoryspi_command_t structure for any given flash command. The mtb_hal_memoryspi_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 MemorySPI component and use of the mtb_hal_memoryspi_read() function to complete the read operation and receive the read data in a buffer.
The following code snippet demonstrates the use of mtb_hal_memoryspi_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 mtb_hal_memoryspi_write() API for executing program operation on flash memory.
This code snippet demonstrates the usage and mtb_hal_memoryspi_select_active_csel() API for switching between memories.
Some devices execute code from external memory rather than internal memory and therefore at boot already initialize the Hardware to correctly enable read, write, execute operations from the MCU to the external memory. In these cases, the MemorySPI interface can still be used to interact with the same external memory on which code is executing from but special attention is needed to avoid impacting the normal code operation. In these cases, the SMIF interface will have already been configured and set to XIP mode. It is still possible to execute MMIO operations to other parts of the memory that are free. In order to do so, the APIs in this driver need to be stored in a different storage (either RAM or internal memory of the device) so that there is no risk of trying to access the external memory while it's actively engaged in a different operation.

API Reference | |
| MemorySPI HAL Results | |
| MemorySPI specific return codes. | |
Typedefs | |
| typedef void(* | mtb_hal_memoryspi_event_callback_t) (void *callback_arg, mtb_hal_memoryspi_event_t event) |
| Handler for MemorySPI callbacks. | |
Functions | |
| cy_rslt_t | mtb_hal_memoryspi_setup (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_configurator_t *config, cy_stc_smif_context_t *context) |
| Sets up a HAL instance to use the specified hardware resource. More... | |
| uint32_t | mtb_hal_memoryspi_get_frequency (mtb_hal_memoryspi_t *obj) |
| Get the actual frequency that MemorySPI is configured for. More... | |
| cy_rslt_t | mtb_hal_memoryspi_chip_configure (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_chip_select_t csel, mtb_hal_memoryspi_data_select_t data_select) |
| Configure data select pin for the specified memory. More... | |
| cy_rslt_t | mtb_hal_memoryspi_select_active_csel (mtb_hal_memoryspi_t *obj, mtb_hal_memoryspi_chip_select_t csel) |
| Selects an active chip select (CSEL) line from one of available and configured. More... | |
| cy_rslt_t | mtb_hal_memoryspi_read (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_command_t *command, uint32_t address, void *data, size_t *length) |
| Sends a command to initiate a read, waits for the command to be accepted then reads a block of data in a blocking fashion. More... | |
| cy_rslt_t | mtb_hal_memoryspi_read_async (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_command_t *command, uint32_t address, void *data, size_t *length) |
| Sends a command to initiate a read, waits for the command to be accepted then reads a block of data in a non-blocking fashion. More... | |
| cy_rslt_t | mtb_hal_memoryspi_write (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_command_t *command, uint32_t address, const void *data, size_t *length) |
| Sends a command to initiate a write, waits for the command to be accepted then writes a block of data in a blocking fashion. More... | |
| cy_rslt_t | mtb_hal_memoryspi_write_async (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_command_t *command, uint32_t address, const void *data, size_t *length) |
| Sends a command to initiate a write, waits for the command to be accepted then writes a block of data in a non-blocking fashion. More... | |
| cy_rslt_t | mtb_hal_memoryspi_transfer (mtb_hal_memoryspi_t *obj, const mtb_hal_memoryspi_command_t *command, uint32_t address, const void *tx_data, size_t tx_size, void *rx_data, size_t rx_size) |
| Send a command (and optionally data , if tx_buffer is specified) and get the response (and optionally data if trx_buffer is specified). More... | |
| void | mtb_hal_memoryspi_register_callback (mtb_hal_memoryspi_t *obj, mtb_hal_memoryspi_event_callback_t callback, void *callback_arg) |
| Register a MemorySPI event handler. More... | |
| void | mtb_hal_memoryspi_enable_event (mtb_hal_memoryspi_t *obj, mtb_hal_memoryspi_event_t event, bool enable) |
| Configure MemorySPI interrupt enablement. More... | |
| cy_rslt_t | mtb_hal_memoryspi_process_interrupt (mtb_hal_memoryspi_t *obj) |
| Process interrupts related related to a MemorySPI instance. More... | |
| bool | mtb_hal_memoryspi_is_async_in_progress (mtb_hal_memoryspi_t *obj) |
| Checks if an async operation is in progress. More... | |
| bool | mtb_hal_memoryspi_is_busy (mtb_hal_memoryspi_t *obj) |
| Checks if the specified MemorySPI peripheral is in use. More... | |
MemorySPI Chip Select Each chip select is represented by an enumeration that has the bit corresponding to the chip select number set.
The data line-selection options for a chip device.
| cy_rslt_t mtb_hal_memoryspi_setup | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_configurator_t * | config, | ||
| cy_stc_smif_context_t * | context | ||
| ) |
Sets up a HAL instance to use the specified hardware resource.
This hardware resource must have already been configured via the PDL.
| [out] | obj | The HAL driver instance object. The caller must allocate the memory for this object, but the HAL will initialize its contents |
| [in] | config | The configurator-generated HAL config structure for this peripheral instance |
| [in] | context | The pointer to the context structure for the SMIF driver |
| uint32_t mtb_hal_memoryspi_get_frequency | ( | mtb_hal_memoryspi_t * | obj | ) |
Get the actual frequency that MemorySPI is configured for.
| [in] | obj | The MemorySPI object |
| cy_rslt_t mtb_hal_memoryspi_chip_configure | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_chip_select_t | csel, | ||
| mtb_hal_memoryspi_data_select_t | data_select | ||
| ) |
Configure data select pin for the specified memory.
Multiple pins can be configured as MemorySPI chip select pins as well as IO pins may be (or may not be) shared and used to service multiple connected memories. This function can be called multiple times - each call for each additional memory. Please refer to device datasheet for details. Switching between configured chip select pins is done by mtb_hal_memoryspi_select_active_csel function. Unless modified with that function, the first CSEL pin provided as part of mtb_hal_memoryspi_setup is the default. Please refer to Code Snippet 5: Configuring multiple memories for example of configuration multiple memory devices and switching between them.
| [in] | obj | The MemorySPI object to configure |
| [in] | csel | CSEL pin for which we want to configure the data select |
| [in] | data_select | Data select value we want to set for the csel |
| cy_rslt_t mtb_hal_memoryspi_select_active_csel | ( | mtb_hal_memoryspi_t * | obj, |
| mtb_hal_memoryspi_chip_select_t | csel | ||
| ) |
Selects an active chip select (CSEL) line from one of available and configured.
Memories can be added with help of mtb_hal_memoryspi_chip_configure function.
| [in] | obj | The MemorySPI object to configure |
| [in] | csel | CSEL pin to be set as active |
| cy_rslt_t mtb_hal_memoryspi_read | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_command_t * | command, | ||
| uint32_t | address, | ||
| void * | data, | ||
| size_t * | length | ||
| ) |
Sends a command to initiate a read, waits for the command to be accepted then reads a block of data in a blocking fashion.
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 | MemorySPI object |
| [in] | command | MemorySPI command |
| [in] | address | Address to access to |
| [out] | data | RX buffer |
| [in] | length | RX buffer length in bytes |
| cy_rslt_t mtb_hal_memoryspi_read_async | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_command_t * | command, | ||
| uint32_t | address, | ||
| void * | data, | ||
| size_t * | length | ||
| ) |
Sends a command to initiate a read, waits for the command to be accepted then reads a block of data in a non-blocking fashion.
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 MTB_HAL_MEMORYSPI_IRQ_RECEIVE_DONE event will be raised. See mtb_hal_memoryspi_register_callback and mtb_hal_memoryspi_enable_event.
| [in] | obj | MemorySPI object |
| [in] | command | MemorySPI command |
| [in] | address | Address to access to |
| [out] | data | RX buffer |
| [in] | length | RX buffer length in bytes |
| cy_rslt_t mtb_hal_memoryspi_write | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_command_t * | command, | ||
| uint32_t | address, | ||
| const void * | data, | ||
| size_t * | length | ||
| ) |
Sends a command to initiate a write, waits for the command to be accepted then writes a block of data in a blocking fashion.
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. If length is 0, the function simply returns and does not send the command as for some memories the write commands has to be followed by at least one byte of data and otherwise the memory will be stuck in a perpetual busy state.
| [in] | obj | MemorySPI object |
| [in] | command | MemorySPI command |
| [in] | address | Address to access to |
| [in] | data | TX buffer |
| [in] | length | TX buffer length in bytes |
| cy_rslt_t mtb_hal_memoryspi_write_async | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_command_t * | command, | ||
| uint32_t | address, | ||
| const void * | data, | ||
| size_t * | length | ||
| ) |
Sends a command to initiate a write, waits for the command to be accepted then writes a block of data in a non-blocking fashion.
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 MTB_HAL_MEMORYSPI_IRQ_TRANSMIT_DONE event will be raised. See mtb_hal_memoryspi_register_callback and mtb_hal_memoryspi_enable_event. If length is 0, the function simply returns and does not send the command as for some memories the write commands has to be followed by at least one byte of data and otherwise the memory will be stuck in a perpetual busy state.
| [in] | obj | MemorySPI object |
| [in] | command | MemorySPI command |
| [in] | address | Address to access to |
| [in] | data | TX buffer |
| [in] | length | TX buffer length in bytes |
| cy_rslt_t mtb_hal_memoryspi_transfer | ( | mtb_hal_memoryspi_t * | obj, |
| const mtb_hal_memoryspi_command_t * | command, | ||
| uint32_t | address, | ||
| const void * | tx_data, | ||
| size_t | tx_size, | ||
| void * | rx_data, | ||
| size_t | rx_size | ||
| ) |
Send a command (and optionally data , if tx_buffer is specified) and get the response (and optionally data if trx_buffer is specified).
Can be used to send/receive device specific commands
| [in] | obj | MemorySPI object |
| [in] | command | MemorySPI command |
| [in] | address | Address to access to |
| [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 mtb_hal_memoryspi_register_callback | ( | mtb_hal_memoryspi_t * | obj, |
| mtb_hal_memoryspi_event_callback_t | callback, | ||
| void * | callback_arg | ||
| ) |
Register a MemorySPI event handler.
This function will be called when one of the events enabled by mtb_hal_memoryspi_enable_event occurs.
| [in] | obj | The MemorySPI 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 mtb_hal_memoryspi_enable_event | ( | mtb_hal_memoryspi_t * | obj, |
| mtb_hal_memoryspi_event_t | event, | ||
| bool | enable | ||
| ) |
Configure MemorySPI interrupt enablement.
When an enabled event occurs, the function specified by mtb_hal_memoryspi_register_callback will be called.
| [in] | obj | The MemorySPI object |
| [in] | event | The MemorySPI event type |
| [in] | enable | True to turn on interrupts, False to turn off |
| cy_rslt_t mtb_hal_memoryspi_process_interrupt | ( | mtb_hal_memoryspi_t * | obj | ) |
Process interrupts related related to a MemorySPI instance.
| obj | HAL object for which the interrupt should be processed |
| bool mtb_hal_memoryspi_is_async_in_progress | ( | mtb_hal_memoryspi_t * | obj | ) |
Checks if an async operation is in progress.
| [in] | obj | The MemorySPI peripheral to check |
| bool mtb_hal_memoryspi_is_busy | ( | mtb_hal_memoryspi_t * | obj | ) |
Checks if the specified MemorySPI peripheral is in use.
| [in] | obj | The MemorySPI peripheral to check |