nrf_qspi.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 NRF_QSPI_H__
  41. #define NRF_QSPI_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_qspi_hal QSPI HAL
  48. * @{
  49. * @ingroup nrf_qspi
  50. * @brief Hardware access layer for managing the QSPI peripheral.
  51. */
  52. /**
  53. * @brief This value can be used as a parameter for the @ref nrf_qspi_pins_set
  54. * function to specify that a given QSPI signal (SCK, CSN, IO0, IO1, IO2, or IO3)
  55. * will not be connected to a physical pin.
  56. */
  57. #define NRF_QSPI_PIN_NOT_CONNECTED 0xFF
  58. /** @brief Macro for setting proper values to pin registers. */
  59. #define NRF_QSPI_PIN_VAL(pin) (pin) == NRF_QSPI_PIN_NOT_CONNECTED ? 0xFFFFFFFF : (pin)
  60. /** @brief QSPI tasks. */
  61. typedef enum
  62. {
  63. NRF_QSPI_TASK_ACTIVATE = offsetof(NRF_QSPI_Type, TASKS_ACTIVATE), /**< Activate the QSPI interface. */
  64. NRF_QSPI_TASK_READSTART = offsetof(NRF_QSPI_Type, TASKS_READSTART), /**< Start transfer from external flash memory to internal RAM. */
  65. NRF_QSPI_TASK_WRITESTART = offsetof(NRF_QSPI_Type, TASKS_WRITESTART), /**< Start transfer from internal RAM to external flash memory. */
  66. NRF_QSPI_TASK_ERASESTART = offsetof(NRF_QSPI_Type, TASKS_ERASESTART), /**< Start external flash memory erase operation. */
  67. NRF_QSPI_TASK_DEACTIVATE = offsetof(NRF_QSPI_Type, TASKS_DEACTIVATE), /**< Deactivate the QSPI interface. */
  68. } nrf_qspi_task_t;
  69. /** @brief QSPI events. */
  70. typedef enum
  71. {
  72. NRF_QSPI_EVENT_READY = offsetof(NRF_QSPI_Type, EVENTS_READY) /**< QSPI peripheral is ready after it executes any task. */
  73. } nrf_qspi_event_t;
  74. /** @brief QSPI interrupts. */
  75. typedef enum
  76. {
  77. NRF_QSPI_INT_READY_MASK = QSPI_INTENSET_READY_Msk /**< Interrupt on READY event. */
  78. } nrf_qspi_int_mask_t;
  79. /** @brief QSPI frequency divider values. */
  80. typedef enum
  81. {
  82. NRF_QSPI_FREQ_32MDIV1, /**< 32.0 MHz. */
  83. NRF_QSPI_FREQ_32MDIV2, /**< 16.0 MHz. */
  84. NRF_QSPI_FREQ_32MDIV3, /**< 10.6 MHz. */
  85. NRF_QSPI_FREQ_32MDIV4, /**< 8.00 MHz. */
  86. NRF_QSPI_FREQ_32MDIV5, /**< 6.40 MHz. */
  87. NRF_QSPI_FREQ_32MDIV6, /**< 5.33 MHz. */
  88. NRF_QSPI_FREQ_32MDIV7, /**< 4.57 MHz. */
  89. NRF_QSPI_FREQ_32MDIV8, /**< 4.00 MHz. */
  90. NRF_QSPI_FREQ_32MDIV9, /**< 3.55 MHz. */
  91. NRF_QSPI_FREQ_32MDIV10, /**< 3.20 MHz. */
  92. NRF_QSPI_FREQ_32MDIV11, /**< 2.90 MHz. */
  93. NRF_QSPI_FREQ_32MDIV12, /**< 2.66 MHz. */
  94. NRF_QSPI_FREQ_32MDIV13, /**< 2.46 MHz. */
  95. NRF_QSPI_FREQ_32MDIV14, /**< 2.29 MHz. */
  96. NRF_QSPI_FREQ_32MDIV15, /**< 2.13 MHz. */
  97. NRF_QSPI_FREQ_32MDIV16, /**< 2.00 MHz. */
  98. } nrf_qspi_frequency_t;
  99. /** @brief Interface configuration for a read operation. */
  100. typedef enum
  101. {
  102. NRF_QSPI_READOC_FASTREAD = QSPI_IFCONFIG0_READOC_FASTREAD, /**< Single data line SPI. FAST_READ (opcode 0x0B). */
  103. NRF_QSPI_READOC_READ2O = QSPI_IFCONFIG0_READOC_READ2O, /**< Dual data line SPI. READ2O (opcode 0x3B). */
  104. NRF_QSPI_READOC_READ2IO = QSPI_IFCONFIG0_READOC_READ2IO, /**< Dual data line SPI. READ2IO (opcode 0xBB). */
  105. NRF_QSPI_READOC_READ4O = QSPI_IFCONFIG0_READOC_READ4O, /**< Quad data line SPI. READ4O (opcode 0x6B). */
  106. NRF_QSPI_READOC_READ4IO = QSPI_IFCONFIG0_READOC_READ4IO /**< Quad data line SPI. READ4IO (opcode 0xEB). */
  107. } nrf_qspi_readoc_t;
  108. /** @brief Interface configuration for a write operation. */
  109. typedef enum
  110. {
  111. NRF_QSPI_WRITEOC_PP = QSPI_IFCONFIG0_WRITEOC_PP, /**< Single data line SPI. PP (opcode 0x02). */
  112. NRF_QSPI_WRITEOC_PP2O = QSPI_IFCONFIG0_WRITEOC_PP2O, /**< Dual data line SPI. PP2O (opcode 0xA2). */
  113. NRF_QSPI_WRITEOC_PP4O = QSPI_IFCONFIG0_WRITEOC_PP4O, /**< Quad data line SPI. PP4O (opcode 0x32). */
  114. NRF_QSPI_WRITEOC_PP4IO = QSPI_IFCONFIG0_WRITEOC_PP4IO, /**< Quad data line SPI. READ4O (opcode 0x38). */
  115. } nrf_qspi_writeoc_t;
  116. /** @brief Interface configuration for addressing mode. */
  117. typedef enum
  118. {
  119. NRF_QSPI_ADDRMODE_24BIT = QSPI_IFCONFIG0_ADDRMODE_24BIT, /**< 24-bit addressing. */
  120. NRF_QSPI_ADDRMODE_32BIT = QSPI_IFCONFIG0_ADDRMODE_32BIT /**< 32-bit addressing. */
  121. } nrf_qspi_addrmode_t;
  122. /** @brief QSPI SPI mode. Polarization and phase configuration. */
  123. typedef enum
  124. {
  125. NRF_QSPI_MODE_0 = QSPI_IFCONFIG1_SPIMODE_MODE0, /**< Mode 0 (CPOL=0, CPHA=0). */
  126. NRF_QSPI_MODE_1 = QSPI_IFCONFIG1_SPIMODE_MODE3 /**< Mode 1 (CPOL=1, CPHA=1). */
  127. } nrf_qspi_spi_mode_t;
  128. /** @brief Addressing configuration mode. */
  129. typedef enum
  130. {
  131. NRF_QSPI_ADDRCONF_MODE_NOINSTR = QSPI_ADDRCONF_MODE_NoInstr, /**< Do not send any instruction. */
  132. NRF_QSPI_ADDRCONF_MODE_OPCODE = QSPI_ADDRCONF_MODE_Opcode, /**< Send opcode. */
  133. NRF_QSPI_ADDRCONF_MODE_OPBYTE0 = QSPI_ADDRCONF_MODE_OpByte0, /**< Send opcode, byte0. */
  134. NRF_QSPI_ADDRCONF_MODE_ALL = QSPI_ADDRCONF_MODE_All /**< Send opcode, byte0, byte1. */
  135. } nrf_qspi_addrconfig_mode_t;
  136. /** @brief Erasing data length. */
  137. typedef enum
  138. {
  139. NRF_QSPI_ERASE_LEN_4KB = QSPI_ERASE_LEN_LEN_4KB, /**< Erase 4 kB block (flash command 0x20). */
  140. NRF_QSPI_ERASE_LEN_64KB = QSPI_ERASE_LEN_LEN_64KB, /**< Erase 64 kB block (flash command 0xD8). */
  141. NRF_QSPI_ERASE_LEN_ALL = QSPI_ERASE_LEN_LEN_All /**< Erase all (flash command 0xC7). */
  142. } nrf_qspi_erase_len_t;
  143. /** @brief Custom instruction length. */
  144. typedef enum
  145. {
  146. NRF_QSPI_CINSTR_LEN_1B = QSPI_CINSTRCONF_LENGTH_1B, /**< Send opcode only. */
  147. NRF_QSPI_CINSTR_LEN_2B = QSPI_CINSTRCONF_LENGTH_2B, /**< Send opcode, CINSTRDAT0.BYTE0. */
  148. NRF_QSPI_CINSTR_LEN_3B = QSPI_CINSTRCONF_LENGTH_3B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT0.BYTE1. */
  149. NRF_QSPI_CINSTR_LEN_4B = QSPI_CINSTRCONF_LENGTH_4B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT0.BYTE2. */
  150. NRF_QSPI_CINSTR_LEN_5B = QSPI_CINSTRCONF_LENGTH_5B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT0.BYTE3. */
  151. NRF_QSPI_CINSTR_LEN_6B = QSPI_CINSTRCONF_LENGTH_6B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT1.BYTE4. */
  152. NRF_QSPI_CINSTR_LEN_7B = QSPI_CINSTRCONF_LENGTH_7B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT1.BYTE5. */
  153. NRF_QSPI_CINSTR_LEN_8B = QSPI_CINSTRCONF_LENGTH_8B, /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT1.BYTE6. */
  154. NRF_QSPI_CINSTR_LEN_9B = QSPI_CINSTRCONF_LENGTH_9B /**< Send opcode, CINSTRDAT0.BYTE0 -> CINSTRDAT1.BYTE7. */
  155. } nrf_qspi_cinstr_len_t;
  156. /** @brief Pin configuration. */
  157. typedef struct
  158. {
  159. uint8_t sck_pin; /**< SCK pin number. */
  160. uint8_t csn_pin; /**< Chip select pin number. */
  161. uint8_t io0_pin; /**< IO0/MOSI pin number. */
  162. uint8_t io1_pin; /**< IO1/MISO pin number. */
  163. uint8_t io2_pin; /**< IO2 pin number (optional).
  164. * Set to @ref NRF_QSPI_PIN_NOT_CONNECTED if this signal is not needed.
  165. */
  166. uint8_t io3_pin; /**< IO3 pin number (optional).
  167. * Set to @ref NRF_QSPI_PIN_NOT_CONNECTED if this signal is not needed.
  168. */
  169. } nrf_qspi_pins_t;
  170. /** @brief Custom instruction configuration. */
  171. typedef struct
  172. {
  173. uint8_t opcode; /**< Opcode used in custom instruction transmission. */
  174. nrf_qspi_cinstr_len_t length; /**< Length of the custom instruction data. */
  175. bool io2_level; /**< I/O line level during transmission. */
  176. bool io3_level; /**< I/O line level during transmission. */
  177. bool wipwait; /**< Wait if a Wait in Progress bit is set in the memory status byte. */
  178. bool wren; /**< Send write enable before instruction. */
  179. } nrf_qspi_cinstr_conf_t;
  180. /** @brief Addressing mode register configuration. See @ref nrf_qspi_addrconfig_set */
  181. typedef struct
  182. {
  183. uint8_t opcode; /**< Opcode used to enter the proper addressing mode. */
  184. uint8_t byte0; /**< Byte following the opcode. */
  185. uint8_t byte1; /**< Byte following byte0. */
  186. nrf_qspi_addrconfig_mode_t mode; /**< Extended addresing mode. */
  187. bool wipwait; /**< Enable or disable waiting for complete operation execution. */
  188. bool wren; /**< Send write enable before instruction. */
  189. } nrf_qspi_addrconfig_conf_t;
  190. /** @brief Structure with QSPI protocol interface configuration. */
  191. typedef struct
  192. {
  193. nrf_qspi_readoc_t readoc; /**< Read operation code. */
  194. nrf_qspi_writeoc_t writeoc; /**< Write operation code. */
  195. nrf_qspi_addrmode_t addrmode; /**< Addresing mode (24-bit or 32-bit). */
  196. bool dpmconfig; /**< Enable the Deep Power-down Mode (DPM) feature. */
  197. } nrf_qspi_prot_conf_t;
  198. /** @brief QSPI physical interface configuration. */
  199. typedef struct
  200. {
  201. uint8_t sck_delay; /**< tSHSL, tWHSL, and tSHWL in number of 16 MHz periods (62.5ns). */
  202. bool dpmen; /**< Enable the DPM feature. */
  203. nrf_qspi_spi_mode_t spi_mode; /**< SPI phase and polarization. */
  204. nrf_qspi_frequency_t sck_freq; /**< SCK frequency given as enum @ref nrf_qspi_frequency_t. */
  205. } nrf_qspi_phy_conf_t;
  206. /**
  207. * @brief Function for activating the specified QSPI task.
  208. *
  209. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  210. * @param[in] task Task to be activated.
  211. */
  212. __STATIC_INLINE void nrf_qspi_task_trigger(NRF_QSPI_Type * p_reg, nrf_qspi_task_t task);
  213. /**
  214. * @brief Function for getting the address of the specified QSPI task register.
  215. *
  216. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  217. * @param[in] task QSPI task.
  218. *
  219. * @return Address of the specified task register.
  220. */
  221. __STATIC_INLINE uint32_t nrf_qspi_task_address_get(NRF_QSPI_Type const * p_reg,
  222. nrf_qspi_task_t task);
  223. /**
  224. * @brief Function for clearing the specified QSPI event.
  225. *
  226. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  227. * @param[in] event Event to be cleared.
  228. */
  229. __STATIC_INLINE void nrf_qspi_event_clear(NRF_QSPI_Type * p_reg, nrf_qspi_event_t event);
  230. /**
  231. * @brief Function for retrieving the state of the QSPI event.
  232. *
  233. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  234. * @param[in] event Event to be checked.
  235. *
  236. * @retval true The event has been generated.
  237. * @retval false The event has not been generated.
  238. */
  239. __STATIC_INLINE bool nrf_qspi_event_check(NRF_QSPI_Type const * p_reg, nrf_qspi_event_t event);
  240. /**
  241. * @brief Function for getting the address of the specified QSPI event register.
  242. *
  243. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  244. * @param[in] event The specified event.
  245. *
  246. * @return Address of the specified event register.
  247. */
  248. __STATIC_INLINE uint32_t * nrf_qspi_event_address_get(NRF_QSPI_Type const * p_reg,
  249. nrf_qspi_event_t event);
  250. /**
  251. * @brief Function for enabling specified interrupts.
  252. *
  253. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  254. * @param[in] mask Mask of interrupts to be enabled.
  255. */
  256. __STATIC_INLINE void nrf_qspi_int_enable(NRF_QSPI_Type * p_reg, uint32_t mask);
  257. /**
  258. * @brief Function for disabling specified interrupts.
  259. *
  260. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  261. * @param[in] mask Mask of interrupts to be disabled.
  262. */
  263. __STATIC_INLINE void nrf_qspi_int_disable(NRF_QSPI_Type * p_reg, uint32_t mask);
  264. /**
  265. * @brief Function for retrieving the state of a given interrupt.
  266. *
  267. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  268. * @param[in] qspi_int Interrupt to be checked.
  269. *
  270. * @retval true The interrupt is enabled.
  271. * @retval false The interrupt is not enabled.
  272. */
  273. __STATIC_INLINE bool nrf_qspi_int_enable_check(NRF_QSPI_Type const * p_reg,
  274. nrf_qspi_int_mask_t qspi_int);
  275. /**
  276. * @brief Function for enabling the QSPI peripheral.
  277. *
  278. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  279. */
  280. __STATIC_INLINE void nrf_qspi_enable(NRF_QSPI_Type * p_reg);
  281. /**
  282. * @brief Function for disabling the QSPI peripheral.
  283. *
  284. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  285. */
  286. __STATIC_INLINE void nrf_qspi_disable(NRF_QSPI_Type * p_reg);
  287. /**
  288. * @brief Function for configuring QSPI pins.
  289. *
  290. * If a given signal is not needed, pass the @ref NRF_QSPI_PIN_NOT_CONNECTED
  291. * value instead of its pin number.
  292. *
  293. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  294. * @param[in] p_pins Pointer to the pins configuration structure. See @ref nrf_qspi_pins_t.
  295. */
  296. __STATIC_INLINE void nrf_qspi_pins_set(NRF_QSPI_Type * p_reg,
  297. const nrf_qspi_pins_t * p_pins);
  298. /**
  299. * @brief Function for setting the QSPI XIPOFFSET register.
  300. *
  301. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  302. * @param[in] xip_offset Address offset in the external memory for Execute in Place operation.
  303. */
  304. __STATIC_INLINE void nrf_qspi_xip_offset_set(NRF_QSPI_Type * p_reg,
  305. uint32_t xip_offset);
  306. /**
  307. * @brief Function for setting the QSPI IFCONFIG0 register.
  308. *
  309. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  310. * @param[in] p_config Pointer to the QSPI protocol interface configuration structure.
  311. * See @ref nrf_qspi_prot_conf_t.
  312. */
  313. __STATIC_INLINE void nrf_qspi_ifconfig0_set(NRF_QSPI_Type * p_reg,
  314. const nrf_qspi_prot_conf_t * p_config);
  315. /**
  316. * @brief Function for setting the QSPI IFCONFIG1 register.
  317. *
  318. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  319. * @param[in] p_config Pointer to the QSPI physical interface configuration structure.
  320. * See @ref nrf_qspi_phy_conf_t.
  321. */
  322. __STATIC_INLINE void nrf_qspi_ifconfig1_set(NRF_QSPI_Type * p_reg,
  323. const nrf_qspi_phy_conf_t * p_config);
  324. /**
  325. * @brief Function for setting the QSPI ADDRCONF register.
  326. *
  327. * This function must be executed before sending task NRF_QSPI_TASK_ACTIVATE. Data stored in the structure
  328. * is sent during the start of the peripheral. Remember that the reset instruction can set
  329. * addressing mode to default in the memory device. If memory reset is necessary before configuring
  330. * the addressing mode, use custom instruction feature instead of this function.
  331. * Case with reset: Enable the peripheral without setting ADDRCONF register, send reset instructions
  332. * using a custom instruction feature (reset enable and then reset), set proper addressing mode
  333. * using the custom instruction feature.
  334. *
  335. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  336. * @param[in] p_config Pointer to the addressing mode configuration structure.
  337. * See @ref nrf_qspi_addrconfig_conf_t.
  338. */
  339. __STATIC_INLINE void nrf_qspi_addrconfig_set(NRF_QSPI_Type * p_reg,
  340. const nrf_qspi_addrconfig_conf_t * p_config);
  341. /**
  342. * @brief Function for setting write data into the peripheral register (without starting the process).
  343. *
  344. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  345. * @param[in] p_buffer Pointer to the writing buffer.
  346. * @param[in] length Lenght of the writing data.
  347. * @param[in] dest_addr Address in memory to write to.
  348. */
  349. __STATIC_INLINE void nrf_qspi_write_buffer_set(NRF_QSPI_Type * p_reg,
  350. void const * p_buffer,
  351. uint32_t length,
  352. uint32_t dest_addr);
  353. /**
  354. * @brief Function for setting read data into the peripheral register (without starting the process).
  355. *
  356. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  357. * @param[out] p_buffer Pointer to the reading buffer.
  358. * @param[in] length Length of the read data.
  359. * @param[in] src_addr Address in memory to read from.
  360. */
  361. __STATIC_INLINE void nrf_qspi_read_buffer_set(NRF_QSPI_Type * p_reg,
  362. void * p_buffer,
  363. uint32_t length,
  364. uint32_t src_addr);
  365. /**
  366. * @brief Function for setting erase data into the peripheral register (without starting the process).
  367. *
  368. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  369. * @param[in] erase_addr Start address to erase. Address must have padding set to 4 bytes.
  370. * @param[in] len Size of erasing area.
  371. */
  372. __STATIC_INLINE void nrf_qspi_erase_ptr_set(NRF_QSPI_Type * p_reg,
  373. uint32_t erase_addr,
  374. nrf_qspi_erase_len_t len);
  375. /**
  376. * @brief Function for getting the peripheral status register.
  377. *
  378. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  379. *
  380. * @return Peripheral status register.
  381. */
  382. __STATIC_INLINE uint32_t nrf_qspi_status_reg_get(NRF_QSPI_Type const * p_reg);
  383. /**
  384. * @brief Function for getting the device status register stored in the peripheral status register.
  385. *
  386. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  387. *
  388. * @return Device status register (lower byte).
  389. */
  390. __STATIC_INLINE uint8_t nrf_qspi_sreg_get(NRF_QSPI_Type const * p_reg);
  391. /**
  392. * @brief Function for checking if the peripheral is busy or not.
  393. *
  394. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  395. *
  396. * @retval true The QSPI is busy.
  397. * @retval false The QSPI is ready.
  398. */
  399. __STATIC_INLINE bool nrf_qspi_busy_check(NRF_QSPI_Type const * p_reg);
  400. /**
  401. * @brief Function for setting registers sending with custom instruction transmission.
  402. *
  403. * This function can be ommited when using NRF_QSPI_CINSTR_LEN_1B as the length argument
  404. * (sending only opcode without data).
  405. *
  406. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  407. * @param[in] length Length of the custom instruction data.
  408. * @param[in] p_tx_data Pointer to the data to send with the custom instruction.
  409. */
  410. __STATIC_INLINE void nrf_qspi_cinstrdata_set(NRF_QSPI_Type * p_reg,
  411. nrf_qspi_cinstr_len_t length,
  412. void const * p_tx_data);
  413. /**
  414. * @brief Function for getting data from register after custom instruction transmission.
  415. *
  416. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  417. * @param[in] length Length of the custom instruction data.
  418. * @param[in] p_rx_data Pointer to the reading buffer.
  419. */
  420. __STATIC_INLINE void nrf_qspi_cinstrdata_get(NRF_QSPI_Type const * p_reg,
  421. nrf_qspi_cinstr_len_t length,
  422. void * p_rx_data);
  423. /**
  424. * @brief Function for sending custom instruction to external memory.
  425. *
  426. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  427. * @param[in] p_config Pointer to the custom instruction configuration structure.
  428. * See @ref nrf_qspi_cinstr_conf_t.
  429. */
  430. __STATIC_INLINE void nrf_qspi_cinstr_transfer_start(NRF_QSPI_Type * p_reg,
  431. const nrf_qspi_cinstr_conf_t * p_config);
  432. /**
  433. * @brief Function for starting a custom instruction long transfer.
  434. *
  435. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  436. * @param[in] p_config Pointer to the custom instruction configuration structure.
  437. * See @ref nrf_qspi_cinstr_conf_t.
  438. */
  439. __STATIC_INLINE void nrf_qspi_cinstr_long_transfer_start(NRF_QSPI_Type * p_reg,
  440. const nrf_qspi_cinstr_conf_t * p_config);
  441. /**
  442. * @brief Function for checking whether a custom instruction long transfer is ongoing.
  443. *
  444. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  445. *
  446. * @retval true Custom instruction long transfer is ongoing.
  447. * @retval false Custom instruction long transfer is not ongoing.
  448. */
  449. __STATIC_INLINE bool nrf_qspi_cinstr_long_transfer_is_ongoing(NRF_QSPI_Type const * p_reg);
  450. /**
  451. * @brief Function for continuing a custom instruction long transfer.
  452. *
  453. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  454. * @param[in] length Length of the custom instruction data.
  455. * @param[in] finalize True if the custom instruction long transfer is to be finalized.
  456. * False if the custom instruction long transfer is to be continued.
  457. */
  458. __STATIC_INLINE void nrf_qspi_cinstr_long_transfer_continue(NRF_QSPI_Type * p_reg,
  459. nrf_qspi_cinstr_len_t length,
  460. bool finalize);
  461. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  462. __STATIC_INLINE void nrf_qspi_task_trigger(NRF_QSPI_Type * p_reg, nrf_qspi_task_t task)
  463. {
  464. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  465. }
  466. __STATIC_INLINE uint32_t nrf_qspi_task_address_get(NRF_QSPI_Type const * p_reg,
  467. nrf_qspi_task_t task)
  468. {
  469. return ((uint32_t)p_reg + (uint32_t)task);
  470. }
  471. __STATIC_INLINE void nrf_qspi_event_clear(NRF_QSPI_Type * p_reg, nrf_qspi_event_t event)
  472. {
  473. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  474. }
  475. __STATIC_INLINE bool nrf_qspi_event_check(NRF_QSPI_Type const * p_reg, nrf_qspi_event_t event)
  476. {
  477. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  478. }
  479. __STATIC_INLINE uint32_t * nrf_qspi_event_address_get(NRF_QSPI_Type const * p_reg,
  480. nrf_qspi_event_t event)
  481. {
  482. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  483. }
  484. __STATIC_INLINE void nrf_qspi_int_enable(NRF_QSPI_Type * p_reg, uint32_t mask)
  485. {
  486. p_reg->INTENSET = mask;
  487. }
  488. __STATIC_INLINE void nrf_qspi_int_disable(NRF_QSPI_Type * p_reg, uint32_t mask)
  489. {
  490. p_reg->INTENCLR = mask;
  491. }
  492. __STATIC_INLINE bool nrf_qspi_int_enable_check(NRF_QSPI_Type const * p_reg,
  493. nrf_qspi_int_mask_t qspi_int)
  494. {
  495. return (bool)(p_reg->INTENSET & qspi_int);
  496. }
  497. __STATIC_INLINE void nrf_qspi_enable(NRF_QSPI_Type * p_reg)
  498. {
  499. p_reg->ENABLE = (QSPI_ENABLE_ENABLE_Enabled << QSPI_ENABLE_ENABLE_Pos);
  500. }
  501. __STATIC_INLINE void nrf_qspi_disable(NRF_QSPI_Type * p_reg)
  502. {
  503. // Workaround for nRF52840 anomaly 122: Current consumption is too high.
  504. *(volatile uint32_t *)0x40029054ul = 1ul;
  505. p_reg->ENABLE = (QSPI_ENABLE_ENABLE_Disabled << QSPI_ENABLE_ENABLE_Pos);
  506. }
  507. __STATIC_INLINE void nrf_qspi_pins_set(NRF_QSPI_Type * p_reg, const nrf_qspi_pins_t * p_pins)
  508. {
  509. p_reg->PSEL.SCK = NRF_QSPI_PIN_VAL(p_pins->sck_pin);
  510. p_reg->PSEL.CSN = NRF_QSPI_PIN_VAL(p_pins->csn_pin);
  511. p_reg->PSEL.IO0 = NRF_QSPI_PIN_VAL(p_pins->io0_pin);
  512. p_reg->PSEL.IO1 = NRF_QSPI_PIN_VAL(p_pins->io1_pin);
  513. p_reg->PSEL.IO2 = NRF_QSPI_PIN_VAL(p_pins->io2_pin);
  514. p_reg->PSEL.IO3 = NRF_QSPI_PIN_VAL(p_pins->io3_pin);
  515. }
  516. __STATIC_INLINE void nrf_qspi_xip_offset_set(NRF_QSPI_Type * p_reg,
  517. uint32_t xip_offset)
  518. {
  519. p_reg->XIPOFFSET = xip_offset;
  520. }
  521. __STATIC_INLINE void nrf_qspi_ifconfig0_set(NRF_QSPI_Type * p_reg,
  522. const nrf_qspi_prot_conf_t * p_config)
  523. {
  524. uint32_t config = p_config->readoc;
  525. config |= ((uint32_t)p_config->writeoc) << QSPI_IFCONFIG0_WRITEOC_Pos;
  526. config |= ((uint32_t)p_config->addrmode) << QSPI_IFCONFIG0_ADDRMODE_Pos;
  527. config |= (p_config->dpmconfig ? 1U : 0U ) << QSPI_IFCONFIG0_DPMENABLE_Pos;
  528. p_reg->IFCONFIG0 = config;
  529. }
  530. __STATIC_INLINE void nrf_qspi_ifconfig1_set(NRF_QSPI_Type * p_reg,
  531. const nrf_qspi_phy_conf_t * p_config)
  532. {
  533. // IFCONFIG1 mask for reserved fields in the register.
  534. uint32_t config = p_reg->IFCONFIG1 & 0x00FFFF00;
  535. config |= p_config->sck_delay;
  536. config |= (p_config->dpmen ? 1U : 0U) << QSPI_IFCONFIG1_DPMEN_Pos;
  537. config |= ((uint32_t)(p_config->spi_mode)) << QSPI_IFCONFIG1_SPIMODE_Pos;
  538. config |= ((uint32_t)(p_config->sck_freq)) << QSPI_IFCONFIG1_SCKFREQ_Pos;
  539. p_reg->IFCONFIG1 = config;
  540. }
  541. __STATIC_INLINE void nrf_qspi_addrconfig_set(NRF_QSPI_Type * p_reg,
  542. const nrf_qspi_addrconfig_conf_t * p_config)
  543. {
  544. uint32_t config = p_config->opcode;
  545. config |= ((uint32_t)p_config->byte0) << QSPI_ADDRCONF_BYTE0_Pos;
  546. config |= ((uint32_t)p_config->byte1) << QSPI_ADDRCONF_BYTE1_Pos;
  547. config |= ((uint32_t)(p_config->mode)) << QSPI_ADDRCONF_MODE_Pos;
  548. config |= (p_config->wipwait ? 1U : 0U) << QSPI_ADDRCONF_WIPWAIT_Pos;
  549. config |= (p_config->wren ? 1U : 0U) << QSPI_ADDRCONF_WREN_Pos;
  550. p_reg->ADDRCONF = config;
  551. }
  552. __STATIC_INLINE void nrf_qspi_write_buffer_set(NRF_QSPI_Type * p_reg,
  553. void const * p_buffer,
  554. uint32_t length,
  555. uint32_t dest_addr)
  556. {
  557. p_reg->WRITE.DST = dest_addr;
  558. p_reg->WRITE.SRC = (uint32_t) p_buffer;
  559. p_reg->WRITE.CNT = length;
  560. }
  561. __STATIC_INLINE void nrf_qspi_read_buffer_set(NRF_QSPI_Type * p_reg,
  562. void * p_buffer,
  563. uint32_t length,
  564. uint32_t src_addr)
  565. {
  566. p_reg->READ.SRC = src_addr;
  567. p_reg->READ.DST = (uint32_t) p_buffer;
  568. p_reg->READ.CNT = length;
  569. }
  570. __STATIC_INLINE void nrf_qspi_erase_ptr_set(NRF_QSPI_Type * p_reg,
  571. uint32_t erase_addr,
  572. nrf_qspi_erase_len_t len)
  573. {
  574. p_reg->ERASE.PTR = erase_addr;
  575. p_reg->ERASE.LEN = len;
  576. }
  577. __STATIC_INLINE uint32_t nrf_qspi_status_reg_get(NRF_QSPI_Type const * p_reg)
  578. {
  579. return p_reg->STATUS;
  580. }
  581. __STATIC_INLINE uint8_t nrf_qspi_sreg_get(NRF_QSPI_Type const * p_reg)
  582. {
  583. return (uint8_t)(p_reg->STATUS & QSPI_STATUS_SREG_Msk) >> QSPI_STATUS_SREG_Pos;
  584. }
  585. __STATIC_INLINE bool nrf_qspi_busy_check(NRF_QSPI_Type const * p_reg)
  586. {
  587. return ((p_reg->STATUS & QSPI_STATUS_READY_Msk) >>
  588. QSPI_STATUS_READY_Pos) == QSPI_STATUS_READY_BUSY;
  589. }
  590. __STATIC_INLINE void nrf_qspi_cinstrdata_set(NRF_QSPI_Type * p_reg,
  591. nrf_qspi_cinstr_len_t length,
  592. void const * p_tx_data)
  593. {
  594. uint32_t reg = 0;
  595. uint8_t const *p_tx_data_8 = (uint8_t const *) p_tx_data;
  596. // Load custom instruction.
  597. switch (length)
  598. {
  599. case NRF_QSPI_CINSTR_LEN_9B:
  600. reg |= ((uint32_t)p_tx_data_8[7]) << QSPI_CINSTRDAT1_BYTE7_Pos;
  601. /* fall-through */
  602. case NRF_QSPI_CINSTR_LEN_8B:
  603. reg |= ((uint32_t)p_tx_data_8[6]) << QSPI_CINSTRDAT1_BYTE6_Pos;
  604. /* fall-through */
  605. case NRF_QSPI_CINSTR_LEN_7B:
  606. reg |= ((uint32_t)p_tx_data_8[5]) << QSPI_CINSTRDAT1_BYTE5_Pos;
  607. /* fall-through */
  608. case NRF_QSPI_CINSTR_LEN_6B:
  609. reg |= ((uint32_t)p_tx_data_8[4]);
  610. p_reg->CINSTRDAT1 = reg;
  611. reg = 0;
  612. /* fall-through */
  613. case NRF_QSPI_CINSTR_LEN_5B:
  614. reg |= ((uint32_t)p_tx_data_8[3]) << QSPI_CINSTRDAT0_BYTE3_Pos;
  615. /* fall-through */
  616. case NRF_QSPI_CINSTR_LEN_4B:
  617. reg |= ((uint32_t)p_tx_data_8[2]) << QSPI_CINSTRDAT0_BYTE2_Pos;
  618. /* fall-through */
  619. case NRF_QSPI_CINSTR_LEN_3B:
  620. reg |= ((uint32_t)p_tx_data_8[1]) << QSPI_CINSTRDAT0_BYTE1_Pos;
  621. /* fall-through */
  622. case NRF_QSPI_CINSTR_LEN_2B:
  623. reg |= ((uint32_t)p_tx_data_8[0]);
  624. p_reg->CINSTRDAT0 = reg;
  625. /* fall-through */
  626. case NRF_QSPI_CINSTR_LEN_1B:
  627. /* Send only opcode. Case to avoid compiler warnings. */
  628. break;
  629. default:
  630. break;
  631. }
  632. }
  633. __STATIC_INLINE void nrf_qspi_cinstrdata_get(NRF_QSPI_Type const * p_reg,
  634. nrf_qspi_cinstr_len_t length,
  635. void * p_rx_data)
  636. {
  637. uint8_t *p_rx_data_8 = (uint8_t *) p_rx_data;
  638. uint32_t reg1 = p_reg->CINSTRDAT1;
  639. uint32_t reg0 = p_reg->CINSTRDAT0;
  640. switch (length)
  641. {
  642. case NRF_QSPI_CINSTR_LEN_9B:
  643. p_rx_data_8[7] = (uint8_t)(reg1 >> QSPI_CINSTRDAT1_BYTE7_Pos);
  644. /* fall-through */
  645. case NRF_QSPI_CINSTR_LEN_8B:
  646. p_rx_data_8[6] = (uint8_t)(reg1 >> QSPI_CINSTRDAT1_BYTE6_Pos);
  647. /* fall-through */
  648. case NRF_QSPI_CINSTR_LEN_7B:
  649. p_rx_data_8[5] = (uint8_t)(reg1 >> QSPI_CINSTRDAT1_BYTE5_Pos);
  650. /* fall-through */
  651. case NRF_QSPI_CINSTR_LEN_6B:
  652. p_rx_data_8[4] = (uint8_t)(reg1);
  653. /* fall-through */
  654. case NRF_QSPI_CINSTR_LEN_5B:
  655. p_rx_data_8[3] = (uint8_t)(reg0 >> QSPI_CINSTRDAT0_BYTE3_Pos);
  656. /* fall-through */
  657. case NRF_QSPI_CINSTR_LEN_4B:
  658. p_rx_data_8[2] = (uint8_t)(reg0 >> QSPI_CINSTRDAT0_BYTE2_Pos);
  659. /* fall-through */
  660. case NRF_QSPI_CINSTR_LEN_3B:
  661. p_rx_data_8[1] = (uint8_t)(reg0 >> QSPI_CINSTRDAT0_BYTE1_Pos);
  662. /* fall-through */
  663. case NRF_QSPI_CINSTR_LEN_2B:
  664. p_rx_data_8[0] = (uint8_t)(reg0);
  665. /* fall-through */
  666. case NRF_QSPI_CINSTR_LEN_1B:
  667. /* Send only opcode. Case to avoid compiler warnings. */
  668. break;
  669. default:
  670. break;
  671. }
  672. }
  673. __STATIC_INLINE void nrf_qspi_cinstr_transfer_start(NRF_QSPI_Type * p_reg,
  674. const nrf_qspi_cinstr_conf_t * p_config)
  675. {
  676. p_reg->CINSTRCONF = (((uint32_t)p_config->opcode << QSPI_CINSTRCONF_OPCODE_Pos) |
  677. ((uint32_t)p_config->length << QSPI_CINSTRCONF_LENGTH_Pos) |
  678. ((uint32_t)p_config->io2_level << QSPI_CINSTRCONF_LIO2_Pos) |
  679. ((uint32_t)p_config->io3_level << QSPI_CINSTRCONF_LIO3_Pos) |
  680. ((uint32_t)p_config->wipwait << QSPI_CINSTRCONF_WIPWAIT_Pos) |
  681. ((uint32_t)p_config->wren << QSPI_CINSTRCONF_WREN_Pos));
  682. }
  683. __STATIC_INLINE void nrf_qspi_cinstr_long_transfer_start(NRF_QSPI_Type * p_reg,
  684. const nrf_qspi_cinstr_conf_t * p_config)
  685. {
  686. p_reg->CINSTRCONF = (((uint32_t)p_config->opcode << QSPI_CINSTRCONF_OPCODE_Pos) |
  687. ((uint32_t)p_config->length << QSPI_CINSTRCONF_LENGTH_Pos) |
  688. ((uint32_t)p_config->io2_level << QSPI_CINSTRCONF_LIO2_Pos) |
  689. ((uint32_t)p_config->io3_level << QSPI_CINSTRCONF_LIO3_Pos) |
  690. ((uint32_t)p_config->wipwait << QSPI_CINSTRCONF_WIPWAIT_Pos) |
  691. ((uint32_t)p_config->wren << QSPI_CINSTRCONF_WREN_Pos) |
  692. (QSPI_CINSTRCONF_LFEN_Msk));
  693. }
  694. __STATIC_INLINE bool nrf_qspi_cinstr_long_transfer_is_ongoing(NRF_QSPI_Type const * p_reg)
  695. {
  696. return (bool)((p_reg->CINSTRCONF & (QSPI_CINSTRCONF_LFEN_Msk | QSPI_CINSTRCONF_LFSTOP_Msk))
  697. == QSPI_CINSTRCONF_LFEN_Msk);
  698. }
  699. __STATIC_INLINE void nrf_qspi_cinstr_long_transfer_continue(NRF_QSPI_Type * p_reg,
  700. nrf_qspi_cinstr_len_t length,
  701. bool finalize)
  702. {
  703. uint32_t mask = (((uint32_t)length << QSPI_CINSTRCONF_LENGTH_Pos) | (QSPI_CINSTRCONF_LFEN_Msk));
  704. mask |= (finalize ? QSPI_CINSTRCONF_LFSTOP_Msk : 0);
  705. p_reg->CINSTRCONF = mask;
  706. }
  707. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  708. /** @} */
  709. #ifdef __cplusplus
  710. }
  711. #endif
  712. #endif // NRF_QSPI_H__