nrfx_qspi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. /**
  54. * @brief QSPI driver instance configuration structure.
  55. */
  56. typedef struct
  57. {
  58. uint32_t xip_offset; /**< Address offset into the external memory for Execute in Place operation. */
  59. nrf_qspi_pins_t pins; /**< Pins configuration structure. */
  60. nrf_qspi_prot_conf_t prot_if; /**< Protocol layer interface configuration structure. */
  61. nrf_qspi_phy_conf_t phy_if; /**< Physical layer interface configuration structure. */
  62. uint8_t irq_priority; /**< Interrupt priority. */
  63. } nrfx_qspi_config_t;
  64. /**
  65. * @brief QSPI instance default configuration.
  66. */
  67. #define NRFX_QSPI_DEFAULT_CONFIG \
  68. { \
  69. .xip_offset = NRFX_QSPI_CONFIG_XIP_OFFSET, \
  70. .pins = { \
  71. .sck_pin = NRFX_QSPI_PIN_SCK, \
  72. .csn_pin = NRFX_QSPI_PIN_CSN, \
  73. .io0_pin = NRFX_QSPI_PIN_IO0, \
  74. .io1_pin = NRFX_QSPI_PIN_IO1, \
  75. .io2_pin = NRFX_QSPI_PIN_IO2, \
  76. .io3_pin = NRFX_QSPI_PIN_IO3, \
  77. }, \
  78. .irq_priority = (uint8_t)NRFX_QSPI_CONFIG_IRQ_PRIORITY, \
  79. .prot_if = { \
  80. .readoc = (nrf_qspi_readoc_t)NRFX_QSPI_CONFIG_READOC, \
  81. .writeoc = (nrf_qspi_writeoc_t)NRFX_QSPI_CONFIG_WRITEOC, \
  82. .addrmode = (nrf_qspi_addrmode_t)NRFX_QSPI_CONFIG_ADDRMODE, \
  83. .dpmconfig = false, \
  84. }, \
  85. .phy_if = { \
  86. .sck_freq = (nrf_qspi_frequency_t)NRFX_QSPI_CONFIG_FREQUENCY, \
  87. .sck_delay = (uint8_t)NRFX_QSPI_CONFIG_SCK_DELAY, \
  88. .spi_mode = (nrf_qspi_spi_mode_t)NRFX_QSPI_CONFIG_MODE, \
  89. .dpmen = false \
  90. }, \
  91. }
  92. /**
  93. * @brief QSPI custom instruction helper with default configuration.
  94. */
  95. #define NRFX_QSPI_DEFAULT_CINSTR(opc, len) \
  96. { \
  97. .opcode = (opc), \
  98. .length = (len), \
  99. .io2_level = false, \
  100. .io3_level = false, \
  101. .wipwait = false, \
  102. .wren = false \
  103. }
  104. /**
  105. * @brief QSPI master driver event types, passed to the handler routine provided
  106. * during initialization.
  107. */
  108. typedef enum
  109. {
  110. NRFX_QSPI_EVENT_DONE, /**< Transfer done. */
  111. } nrfx_qspi_evt_t;
  112. /**
  113. * @brief QSPI driver event handler type.
  114. */
  115. typedef void (*nrfx_qspi_handler_t)(nrfx_qspi_evt_t event, void * p_context);
  116. /**
  117. * @brief Function for initializing the QSPI driver instance.
  118. *
  119. * This function configures the peripheral and its interrupts and activates it. During the
  120. * activation process, the internal clocks are started and the QSPI peripheral tries to read
  121. * the status byte to read the busy bit. Reading the status byte is done in a simple poll and wait
  122. * mechanism.
  123. * If the busy bit is 1, this indicates issues with the external memory device. As a result,
  124. * @ref nrfx_qspi_init returns NRFX_ERROR_TIMEOUT.
  125. *
  126. * In case of issues:
  127. * - Check the connection.
  128. * - Make sure that the memory device does not perform other operations like erasing or writing.
  129. * - Check if there is a short circuit.
  130. *
  131. * @param[in] p_config Pointer to the structure with initial configuration.
  132. * @param[in] handler Event handler provided by the user. If NULL, transfers
  133. * will be performed in blocking mode.
  134. * @param[in] p_context Pointer to context. Use in interrupt handler.
  135. *
  136. * @retval NRFX_SUCCESS If initialization was successful.
  137. * @retval NRFX_ERROR_TIMEOUT If the peripheral cannot connect with external memory.
  138. * @retval NRFX_ERROR_INVALID_STATE If the driver was already initialized.
  139. * @retval NRFX_ERROR_INVALID_PARAM If the pin configuration was incorrect.
  140. */
  141. nrfx_err_t nrfx_qspi_init(nrfx_qspi_config_t const * p_config,
  142. nrfx_qspi_handler_t handler,
  143. void * p_context);
  144. /**
  145. * @brief Function for uninitializing the QSPI driver instance.
  146. */
  147. void nrfx_qspi_uninit(void);
  148. /**
  149. * @brief Function for reading data from QSPI memory.
  150. *
  151. * Write, read, and erase operations check memory device busy state before starting the operation.
  152. * If the memory is busy, the resulting action depends on the mode in which the read operation is used:
  153. * - blocking mode (without handler) - a delay occurs until the last operation still runs and
  154. * until operation data is still being read.
  155. * - interrupt mode (with handler) - event emission occurs after the last operation
  156. * and reading of data are finished.
  157. *
  158. * @param[out] p_rx_buffer Pointer to the receive buffer.
  159. * @param[in] rx_buffer_length Size of the data to read.
  160. * @param[in] src_address Address in memory to read from.
  161. *
  162. * @retval NRFX_SUCCESS If the operation was successful (blocking mode) or operation
  163. * was commissioned (handler mode).
  164. * @retval NRFX_ERROR_BUSY If the driver currently handles another operation.
  165. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffer is not placed in the Data RAM region.
  166. */
  167. nrfx_err_t nrfx_qspi_read(void * p_rx_buffer,
  168. size_t rx_buffer_length,
  169. uint32_t src_address);
  170. /**
  171. * @brief Function for writing data to QSPI memory.
  172. *
  173. * Write, read, and erase operations check memory device busy state before starting the operation.
  174. * If the memory is busy, the resulting action depends on the mode in which the write operation is used:
  175. * - blocking mode (without handler) - a delay occurs until the last operation still runs and
  176. * until operation data is still being sent.
  177. * - interrupt mode (with handler) - event emission occurs after the last operation
  178. * and sending of operation data are finished.
  179. * To manually control operation execution in the memory device, use @ref nrfx_qspi_mem_busy_check
  180. * after executing the write function.
  181. * Remember that an incoming event signalizes only that data was sent to the memory device and the periheral
  182. * before the write operation checked if memory was busy.
  183. *
  184. * @param[in] p_tx_buffer Pointer to the writing buffer.
  185. * @param[in] tx_buffer_length Size of the data to write.
  186. * @param[in] dst_address Address in memory to write to.
  187. *
  188. * @retval NRFX_SUCCESS If the operation was successful (blocking mode) or operation
  189. * was commissioned (handler mode).
  190. * @retval NRFX_ERROR_BUSY If the driver currently handles other operation.
  191. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffer is not placed in the Data RAM region.
  192. */
  193. nrfx_err_t nrfx_qspi_write(void const * p_tx_buffer,
  194. size_t tx_buffer_length,
  195. uint32_t dst_address);
  196. /**
  197. * @brief Function for starting erasing of one memory block - 4KB, 64KB, or the whole chip.
  198. *
  199. * Write, read, and erase operations check memory device busy state before starting the operation.
  200. * If the memory is busy, the resulting action depends on the mode in which the erase operation is used:
  201. * - blocking mode (without handler) - a delay occurs until the last operation still runs and
  202. * until operation data is still being sent.
  203. * - interrupt mode (with handler) - event emission occurs after the last operation
  204. * and sending of operation data are finished.
  205. * To manually control operation execution in the memory device, use @ref nrfx_qspi_mem_busy_check
  206. * after executing the erase function.
  207. * Remember that an incoming event signalizes only that data was sent to the memory device and the periheral
  208. * before the erase operation checked if memory was busy.
  209. *
  210. * @param[in] length Size of data to erase. See @ref nrf_qspi_erase_len_t.
  211. * @param[in] start_address Memory address to start erasing. If chip erase is performed, address
  212. * field is ommited.
  213. *
  214. * @retval NRFX_SUCCESS If the operation was successful (blocking mode) or operation
  215. * was commissioned (handler mode).
  216. * @retval NRFX_ERROR_BUSY If the driver currently handles another operation.
  217. */
  218. nrfx_err_t nrfx_qspi_erase(nrf_qspi_erase_len_t length,
  219. uint32_t start_address);
  220. /**
  221. * @brief Function for starting an erase operation of the whole chip.
  222. *
  223. * @retval NRFX_SUCCESS If the operation was successful (blocking mode) or operation
  224. * was commissioned (handler mode).
  225. * @retval NRFX_ERROR_BUSY If the driver currently handles another operation.
  226. */
  227. nrfx_err_t nrfx_qspi_chip_erase(void);
  228. /**
  229. * @brief Function for getting the current driver status and status byte of memory device with
  230. * testing WIP (write in progress) bit.
  231. *
  232. * @retval NRFX_SUCCESS If the driver and memory are ready to handle a new operation.
  233. * @retval NRFX_ERROR_BUSY If the driver or memory currently handle another operation.
  234. */
  235. nrfx_err_t nrfx_qspi_mem_busy_check(void);
  236. /**
  237. * @brief Function for sending operation code, sending data, and receiving data from the memory device.
  238. *
  239. * Use this function to transfer configuration data to memory and to receive data from memory.
  240. * Pointers can be addresses from flash memory.
  241. * This function is a synchronous function and should be used only if necessary.
  242. *
  243. * @param[in] p_config Pointer to the structure with opcode and transfer configuration.
  244. * @param[in] p_tx_buffer Pointer to the array with data to send. Can be NULL if only opcode is transmitted.
  245. * @param[out] p_rx_buffer Pointer to the array for data to receive. Can be NULL if there is nothing to receive.
  246. *
  247. * @retval NRFX_SUCCESS If the operation was successful.
  248. * @retval NRFX_ERROR_TIMEOUT If the external memory is busy or there are connection issues.
  249. * @retval NRFX_ERROR_BUSY If the driver currently handles other operation.
  250. */
  251. nrfx_err_t nrfx_qspi_cinstr_xfer(nrf_qspi_cinstr_conf_t const * p_config,
  252. void const * p_tx_buffer,
  253. void * p_rx_buffer);
  254. /**
  255. * @brief Function for sending operation code and data to the memory device with simpler configuration.
  256. *
  257. * Use this function to transfer configuration data to memory and to receive data from memory.
  258. * This function is a synchronous function and should be used only if necessary.
  259. *
  260. * @param[in] opcode Operation code. Sending first.
  261. * @param[in] length Length of the data to send and opcode. See @ref nrf_qspi_cinstr_len_t.
  262. * @param[in] p_tx_buffer Pointer to input data array.
  263. *
  264. * @retval NRFX_SUCCESS If the operation was successful.
  265. * @retval NRFX_ERROR_BUSY If the driver currently handles another operation.
  266. */
  267. nrfx_err_t nrfx_qspi_cinstr_quick_send(uint8_t opcode,
  268. nrf_qspi_cinstr_len_t length,
  269. void const * p_tx_buffer);
  270. void nrfx_qspi_irq_handler(void);
  271. /** @} */
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif // NRFX_QSPI_H__