nrf_qspi.h 30 KB

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