nrfx_qspi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_QSPI_H__
  41. #define NRFX_QSPI_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_qspi.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_qspi QSPI driver
  49. * @{
  50. * @ingroup nrf_qspi
  51. * @brief Quad Serial Peripheral Interface (QSPI) peripheral driver.
  52. */
  53. /** @brief QSPI driver instance configuration structure. */
  54. typedef struct
  55. {
  56. uint32_t xip_offset; /**< Address offset into the external memory for Execute in Place operation. */
  57. nrf_qspi_pins_t pins; /**< Pin configuration structure. */
  58. nrf_qspi_prot_conf_t prot_if; /**< Protocol layer interface configuration structure. */
  59. nrf_qspi_phy_conf_t phy_if; /**< Physical layer interface configuration structure. */
  60. uint8_t irq_priority; /**< Interrupt priority. */
  61. } nrfx_qspi_config_t;
  62. /** @brief QSPI instance default configuration. */
  63. #define NRFX_QSPI_DEFAULT_CONFIG \
  64. { \
  65. .xip_offset = NRFX_QSPI_CONFIG_XIP_OFFSET, \
  66. .pins = { \
  67. .sck_pin = NRFX_QSPI_PIN_SCK, \
  68. .csn_pin = NRFX_QSPI_PIN_CSN, \
  69. .io0_pin = NRFX_QSPI_PIN_IO0, \
  70. .io1_pin = NRFX_QSPI_PIN_IO1, \
  71. .io2_pin = NRFX_QSPI_PIN_IO2, \
  72. .io3_pin = NRFX_QSPI_PIN_IO3, \
  73. }, \
  74. .prot_if = { \
  75. .readoc = (nrf_qspi_readoc_t)NRFX_QSPI_CONFIG_READOC, \
  76. .writeoc = (nrf_qspi_writeoc_t)NRFX_QSPI_CONFIG_WRITEOC, \
  77. .addrmode = (nrf_qspi_addrmode_t)NRFX_QSPI_CONFIG_ADDRMODE, \
  78. .dpmconfig = false, \
  79. }, \
  80. .phy_if = { \
  81. .sck_delay = (uint8_t)NRFX_QSPI_CONFIG_SCK_DELAY, \
  82. .dpmen = false, \
  83. .spi_mode = (nrf_qspi_spi_mode_t)NRFX_QSPI_CONFIG_MODE, \
  84. .sck_freq = (nrf_qspi_frequency_t)NRFX_QSPI_CONFIG_FREQUENCY, \
  85. }, \
  86. .irq_priority = (uint8_t)NRFX_QSPI_CONFIG_IRQ_PRIORITY, \
  87. }
  88. /** @brief QSPI custom instruction helper with the default configuration. */
  89. #define NRFX_QSPI_DEFAULT_CINSTR(opc, len) \
  90. { \
  91. .opcode = (opc), \
  92. .length = (len), \
  93. .io2_level = false, \
  94. .io3_level = false, \
  95. .wipwait = false, \
  96. .wren = false \
  97. }
  98. /**
  99. * @brief QSPI master driver event types, passed to the handler routine provided
  100. * during initialization.
  101. */
  102. typedef enum
  103. {
  104. NRFX_QSPI_EVENT_DONE, /**< Transfer done. */
  105. } nrfx_qspi_evt_t;
  106. /** @brief QSPI driver event handler type. */
  107. typedef void (*nrfx_qspi_handler_t)(nrfx_qspi_evt_t event, void * p_context);
  108. /**
  109. * @brief Function for initializing the QSPI driver instance.
  110. *
  111. * This function configures the peripheral and its interrupts, and activates it. During the
  112. * activation process, the internal clocks are started and the QSPI peripheral tries to read
  113. * the status byte to read the busy bit. Reading the status byte is done in a simple poll and wait
  114. * mechanism.
  115. * If the busy bit is 1, this indicates issues with the external memory device. As a result,
  116. * @ref nrfx_qspi_init returns NRFX_ERROR_TIMEOUT.
  117. *
  118. * In case of issues:
  119. * - Check the connection.
  120. * - Make sure that the memory device does not perform other operations like erasing or writing.
  121. * - Check if there is a short circuit.
  122. *
  123. * @param[in] p_config Pointer to the structure with the initial configuration.
  124. * @param[in] handler Event handler provided by the user. If NULL, transfers
  125. * will be performed in blocking mode.
  126. * @param[in] p_context Pointer to context. Use in the interrupt handler.
  127. *
  128. * @retval NRFX_SUCCESS Initialization was successful.
  129. * @retval NRFX_ERROR_TIMEOUT The peripheral cannot connect with external memory.
  130. * @retval NRFX_ERROR_INVALID_STATE The driver was already initialized.
  131. * @retval NRFX_ERROR_INVALID_PARAM The pin configuration was incorrect.
  132. */
  133. nrfx_err_t nrfx_qspi_init(nrfx_qspi_config_t const * p_config,
  134. nrfx_qspi_handler_t handler,
  135. void * p_context);
  136. /** @brief Function for uninitializing the QSPI driver instance. */
  137. void nrfx_qspi_uninit(void);
  138. /**
  139. * @brief Function for reading data from the QSPI memory.
  140. *
  141. * Write, read, and erase operations check memory device busy state before starting the operation.
  142. * If the memory is busy, the resulting action depends on the mode in which the read operation is used:
  143. * - blocking mode (without handler) - a delay occurs until the last operation runs and
  144. * until the operation data is being read.
  145. * - interrupt mode (with handler) - event emission occurs after the last operation
  146. * and reading of data are finished.
  147. *
  148. * @param[out] p_rx_buffer Pointer to the receive buffer.
  149. * @param[in] rx_buffer_length Size of the data to read.
  150. * @param[in] src_address Address in memory to read from.
  151. *
  152. * @retval NRFX_SUCCESS The operation was successful (blocking mode) or operation
  153. * was commissioned (handler mode).
  154. * @retval NRFX_ERROR_BUSY The driver currently handles another operation.
  155. * @retval NRFX_ERROR_INVALID_ADDR The provided buffer is not placed in the Data RAM region
  156. * or its address is not aligned to a 32-bit word.
  157. */
  158. nrfx_err_t nrfx_qspi_read(void * p_rx_buffer,
  159. size_t rx_buffer_length,
  160. uint32_t src_address);
  161. /**
  162. * @brief Function for writing data to QSPI memory.
  163. *
  164. * Write, read, and erase operations check memory device busy state before starting the operation.
  165. * If the memory is busy, the resulting action depends on the mode in which the write operation is used:
  166. * - blocking mode (without handler) - a delay occurs until the last operation runs or
  167. * until the operation data is being sent.
  168. * - interrupt mode (with handler) - event emission occurs after the last operation
  169. * and sending of operation data are finished.
  170. * To manually control operation execution in the memory device, use @ref nrfx_qspi_mem_busy_check
  171. * after executing the write function.
  172. * Remember that an incoming event signalizes only that data was sent to the memory device and the periheral
  173. * before the write operation checked if memory was busy.
  174. *
  175. * @param[in] p_tx_buffer Pointer to the writing buffer.
  176. * @param[in] tx_buffer_length Size of the data to write.
  177. * @param[in] dst_address Address in memory to write to.
  178. *
  179. * @retval NRFX_SUCCESS The operation was successful (blocking mode) or operation
  180. * was commissioned (handler mode).
  181. * @retval NRFX_ERROR_BUSY The driver currently handles other operation.
  182. * @retval NRFX_ERROR_INVALID_ADDR The provided buffer is not placed in the Data RAM region
  183. * or its address is not aligned to a 32-bit word.
  184. */
  185. nrfx_err_t nrfx_qspi_write(void const * p_tx_buffer,
  186. size_t tx_buffer_length,
  187. uint32_t dst_address);
  188. /**
  189. * @brief Function for starting erasing of one memory block - 4KB, 64KB, or the whole chip.
  190. *
  191. * Write, read, and erase operations check memory device busy state before starting the operation.
  192. * If the memory is busy, the resulting action depends on the mode in which the erase operation is used:
  193. * - blocking mode (without handler) - a delay occurs until the last operation runs or
  194. * until the operation data is being sent.
  195. * - interrupt mode (with handler) - event emission occurs after the last operation
  196. * and sending of operation data are finished.
  197. * To manually control operation execution in the memory device, use @ref nrfx_qspi_mem_busy_check
  198. * after executing the erase function.
  199. * Remember that an incoming event signalizes only that data was sent to the memory device and the periheral
  200. * before the erase operation checked if memory was busy.
  201. *
  202. * @param[in] length Size of data to erase. See @ref nrf_qspi_erase_len_t.
  203. * @param[in] start_address Memory address to start erasing. If chip erase is performed, address
  204. * field is ommited.
  205. *
  206. * @retval NRFX_SUCCESS The operation was successful (blocking mode) or operation
  207. * was commissioned (handler mode).
  208. * @retval NRFX_ERROR_INVALID_ADDR The provided start address is not aligned to a 32-bit word.
  209. * @retval NRFX_ERROR_BUSY The driver currently handles another operation.
  210. */
  211. nrfx_err_t nrfx_qspi_erase(nrf_qspi_erase_len_t length,
  212. uint32_t start_address);
  213. /**
  214. * @brief Function for starting an erase operation of the whole chip.
  215. *
  216. * @retval NRFX_SUCCESS The operation was successful (blocking mode) or operation
  217. * was commissioned (handler mode).
  218. * @retval NRFX_ERROR_BUSY The driver currently handles another operation.
  219. */
  220. nrfx_err_t nrfx_qspi_chip_erase(void);
  221. /**
  222. * @brief Function for getting the current driver status and status byte of memory device with
  223. * testing WIP (write in progress) bit.
  224. *
  225. * @retval NRFX_SUCCESS The driver and memory are ready to handle a new operation.
  226. * @retval NRFX_ERROR_BUSY The driver or memory currently handle another operation.
  227. */
  228. nrfx_err_t nrfx_qspi_mem_busy_check(void);
  229. /**
  230. * @brief Function for sending operation code, sending data, and receiving data from the memory device.
  231. *
  232. * Use this function to transfer configuration data to memory and to receive data from memory.
  233. * Pointers can be addresses from flash memory.
  234. * This function is a synchronous function and should be used only if necessary.
  235. *
  236. * @param[in] p_config Pointer to the structure with opcode and transfer configuration.
  237. * @param[in] p_tx_buffer Pointer to the array with data to send. Can be NULL if only opcode is transmitted.
  238. * @param[out] p_rx_buffer Pointer to the array for data to receive. Can be NULL if there is nothing to receive.
  239. *
  240. * @retval NRFX_SUCCESS The operation was successful.
  241. * @retval NRFX_ERROR_TIMEOUT The external memory is busy or there are connection issues.
  242. * @retval NRFX_ERROR_BUSY The driver currently handles other operation.
  243. */
  244. nrfx_err_t nrfx_qspi_cinstr_xfer(nrf_qspi_cinstr_conf_t const * p_config,
  245. void const * p_tx_buffer,
  246. void * p_rx_buffer);
  247. /**
  248. * @brief Function for sending operation code and data to the memory device with simpler configuration.
  249. *
  250. * Use this function to transfer configuration data to memory and to receive data from memory.
  251. * This function is a synchronous function and should be used only if necessary.
  252. *
  253. * @param[in] opcode Operation code. Sending first.
  254. * @param[in] length Length of the data to send and opcode. See @ref nrf_qspi_cinstr_len_t.
  255. * @param[in] p_tx_buffer Pointer to input data array.
  256. *
  257. * @retval NRFX_SUCCESS The operation was successful.
  258. * @retval NRFX_ERROR_BUSY The driver currently handles another operation.
  259. */
  260. nrfx_err_t nrfx_qspi_cinstr_quick_send(uint8_t opcode,
  261. nrf_qspi_cinstr_len_t length,
  262. void const * p_tx_buffer);
  263. /**
  264. * @brief Function for starting the custom instruction long frame mode.
  265. *
  266. * The long frame mode is a mechanism that allows for arbitrary byte length custom instructions.
  267. * Use this function to initiate a custom transaction by sending custom instruction opcode.
  268. * To send and receive data, use @ref nrfx_qspi_lfm_xfer.
  269. *
  270. * @param[in] p_config Pointer to the structure with custom instruction opcode and transfer
  271. * configuration. Transfer length must be set to @ref NRF_QSPI_CINSTR_LEN_1B.
  272. *
  273. * @retval NRFX_SUCCESS Operation was successful.
  274. * @retval NRFX_ERROR_BUSY Driver currently handles other operation.
  275. * @retval NRFX_ERROR_TIMEOUT External memory is busy or there are connection issues.
  276. */
  277. nrfx_err_t nrfx_qspi_lfm_start(nrf_qspi_cinstr_conf_t const * p_config);
  278. /**
  279. * @brief Function for sending and receiving data in the custom instruction long frame mode.
  280. *
  281. * Both specified buffers must be at least @p transfer_length bytes in size.
  282. *
  283. * @param[in] p_tx_buffer Pointer to the array with data to send.
  284. * Can be NULL if there is nothing to send.
  285. * @param[out] p_rx_buffer Pointer to the array for receiving data.
  286. * Can be NULL if there is nothing to receive.
  287. * @param[in] transfer_length Number of bytes to send and receive.
  288. * @param[in] finalize True if custom instruction long frame mode is to be finalized
  289. * after this transfer.
  290. *
  291. * @retval NRFX_SUCCESS Operation was successful.
  292. * @retval NRFX_ERROR_TIMEOUT External memory is busy or there are connection issues.
  293. * Long frame mode becomes deactivated.
  294. */
  295. nrfx_err_t nrfx_qspi_lfm_xfer(void const * p_tx_buffer,
  296. void * p_rx_buffer,
  297. size_t transfer_length,
  298. bool finalize);
  299. /** @} */
  300. void nrfx_qspi_irq_handler(void);
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif // NRFX_QSPI_H__