nrf_spim.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /**
  2. * Copyright (c) 2015 - 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_SPIM_H__
  41. #define NRF_SPIM_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_spim_hal SPIM HAL
  48. * @{
  49. * @ingroup nrf_spim
  50. * @brief Hardware access layer for managing the SPIM peripheral.
  51. */
  52. /**
  53. * @brief This value can be used as a parameter for the @ref nrf_spim_pins_set
  54. * function to specify that a given SPI signal (SCK, MOSI, or MISO)
  55. * shall not be connected to a physical pin.
  56. */
  57. #define NRF_SPIM_PIN_NOT_CONNECTED 0xFFFFFFFF
  58. #if defined(SPIM_DCXCNT_DCXCNT_Msk) || defined(__NRFX_DOXYGEN__)
  59. /**
  60. * @brief This value specified in the DCX line configuration causes this line
  61. * to be set low during whole transmission (all transmitted bytes are
  62. * marked as command bytes). Any lower value causes the DCX line to be
  63. * switched from low to high after this number of bytes is transmitted
  64. * (all remaining bytes are marked as data bytes).
  65. */
  66. #define NRF_SPIM_DCX_CNT_ALL_CMD 0xF
  67. #endif
  68. /** @brief Macro for checking if the hardware chip select function is available. */
  69. #define NRF_SPIM_HW_CSN_PRESENT \
  70. (NRFX_CHECK(SPIM0_FEATURE_HARDWARE_CSN_PRESENT) || \
  71. NRFX_CHECK(SPIM1_FEATURE_HARDWARE_CSN_PRESENT) || \
  72. NRFX_CHECK(SPIM2_FEATURE_HARDWARE_CSN_PRESENT) || \
  73. NRFX_CHECK(SPIM3_FEATURE_HARDWARE_CSN_PRESENT))
  74. /** @brief SPIM tasks. */
  75. typedef enum
  76. {
  77. NRF_SPIM_TASK_START = offsetof(NRF_SPIM_Type, TASKS_START), ///< Start SPI transaction.
  78. NRF_SPIM_TASK_STOP = offsetof(NRF_SPIM_Type, TASKS_STOP), ///< Stop SPI transaction.
  79. NRF_SPIM_TASK_SUSPEND = offsetof(NRF_SPIM_Type, TASKS_SUSPEND), ///< Suspend SPI transaction.
  80. NRF_SPIM_TASK_RESUME = offsetof(NRF_SPIM_Type, TASKS_RESUME) ///< Resume SPI transaction.
  81. } nrf_spim_task_t;
  82. /** @brief SPIM events. */
  83. typedef enum
  84. {
  85. NRF_SPIM_EVENT_STOPPED = offsetof(NRF_SPIM_Type, EVENTS_STOPPED), ///< SPI transaction has stopped.
  86. NRF_SPIM_EVENT_ENDRX = offsetof(NRF_SPIM_Type, EVENTS_ENDRX), ///< End of RXD buffer reached.
  87. NRF_SPIM_EVENT_END = offsetof(NRF_SPIM_Type, EVENTS_END), ///< End of RXD buffer and TXD buffer reached.
  88. NRF_SPIM_EVENT_ENDTX = offsetof(NRF_SPIM_Type, EVENTS_ENDTX), ///< End of TXD buffer reached.
  89. NRF_SPIM_EVENT_STARTED = offsetof(NRF_SPIM_Type, EVENTS_STARTED) ///< Transaction started.
  90. } nrf_spim_event_t;
  91. /**
  92. * @brief SPIM shortcuts.
  93. */
  94. typedef enum
  95. {
  96. NRF_SPIM_SHORT_END_START_MASK = SPIM_SHORTS_END_START_Msk, ///< Shortcut between END event and START task.
  97. NRF_SPIM_ALL_SHORTS_MASK = SPIM_SHORTS_END_START_Msk ///< All SPIM shortcuts.
  98. } nrf_spim_short_mask_t;
  99. /** @brief SPIM interrupts. */
  100. typedef enum
  101. {
  102. NRF_SPIM_INT_STOPPED_MASK = SPIM_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  103. NRF_SPIM_INT_ENDRX_MASK = SPIM_INTENSET_ENDRX_Msk, ///< Interrupt on ENDRX event.
  104. NRF_SPIM_INT_END_MASK = SPIM_INTENSET_END_Msk, ///< Interrupt on END event.
  105. NRF_SPIM_INT_ENDTX_MASK = SPIM_INTENSET_ENDTX_Msk, ///< Interrupt on ENDTX event.
  106. NRF_SPIM_INT_STARTED_MASK = SPIM_INTENSET_STARTED_Msk, ///< Interrupt on STARTED event.
  107. NRF_SPIM_ALL_INTS_MASK = SPIM_INTENSET_STOPPED_Msk |
  108. SPIM_INTENSET_ENDRX_Msk |
  109. SPIM_INTENSET_END_Msk |
  110. SPIM_INTENSET_ENDTX_Msk |
  111. SPIM_INTENSET_STARTED_Msk ///< All SPIM interrupts.
  112. } nrf_spim_int_mask_t;
  113. /** @brief SPI master data rates. */
  114. typedef enum
  115. {
  116. NRF_SPIM_FREQ_125K = SPIM_FREQUENCY_FREQUENCY_K125, ///< 125 kbps.
  117. NRF_SPIM_FREQ_250K = SPIM_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  118. NRF_SPIM_FREQ_500K = SPIM_FREQUENCY_FREQUENCY_K500, ///< 500 kbps.
  119. NRF_SPIM_FREQ_1M = SPIM_FREQUENCY_FREQUENCY_M1, ///< 1 Mbps.
  120. NRF_SPIM_FREQ_2M = SPIM_FREQUENCY_FREQUENCY_M2, ///< 2 Mbps.
  121. NRF_SPIM_FREQ_4M = SPIM_FREQUENCY_FREQUENCY_M4, ///< 4 Mbps.
  122. // [conversion to 'int' needed to prevent compilers from complaining
  123. // that the provided value (0x80000000UL) is out of range of "int"]
  124. NRF_SPIM_FREQ_8M = (int)SPIM_FREQUENCY_FREQUENCY_M8, ///< 8 Mbps.
  125. #if defined(SPIM_FREQUENCY_FREQUENCY_M16) || defined(__NRFX_DOXYGEN__)
  126. NRF_SPIM_FREQ_16M = SPIM_FREQUENCY_FREQUENCY_M16, ///< 16 Mbps.
  127. #endif
  128. #if defined(SPIM_FREQUENCY_FREQUENCY_M32) || defined(__NRFX_DOXYGEN__)
  129. NRF_SPIM_FREQ_32M = SPIM_FREQUENCY_FREQUENCY_M32 ///< 32 Mbps.
  130. #endif
  131. } nrf_spim_frequency_t;
  132. /** @brief SPI modes. */
  133. typedef enum
  134. {
  135. NRF_SPIM_MODE_0, ///< SCK active high, sample on leading edge of clock.
  136. NRF_SPIM_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  137. NRF_SPIM_MODE_2, ///< SCK active low, sample on leading edge of clock.
  138. NRF_SPIM_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  139. } nrf_spim_mode_t;
  140. /** @brief SPI bit orders. */
  141. typedef enum
  142. {
  143. NRF_SPIM_BIT_ORDER_MSB_FIRST = SPIM_CONFIG_ORDER_MsbFirst, ///< Most significant bit shifted out first.
  144. NRF_SPIM_BIT_ORDER_LSB_FIRST = SPIM_CONFIG_ORDER_LsbFirst ///< Least significant bit shifted out first.
  145. } nrf_spim_bit_order_t;
  146. #if (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  147. /** @brief SPI CSN pin polarity. */
  148. typedef enum
  149. {
  150. NRF_SPIM_CSN_POL_LOW = SPIM_CSNPOL_CSNPOL_LOW, ///< Active low (idle state high).
  151. NRF_SPIM_CSN_POL_HIGH = SPIM_CSNPOL_CSNPOL_HIGH ///< Active high (idle state low).
  152. } nrf_spim_csn_pol_t;
  153. #endif // (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  154. /**
  155. * @brief Function for activating the specified SPIM task.
  156. *
  157. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  158. * @param[in] task Task to be activated.
  159. */
  160. __STATIC_INLINE void nrf_spim_task_trigger(NRF_SPIM_Type * p_reg,
  161. nrf_spim_task_t task);
  162. /**
  163. * @brief Function for getting the address of the specified SPIM task register.
  164. *
  165. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  166. * @param[in] task The specified task.
  167. *
  168. * @return Address of the specified task register.
  169. */
  170. __STATIC_INLINE uint32_t nrf_spim_task_address_get(NRF_SPIM_Type * p_reg,
  171. nrf_spim_task_t task);
  172. /**
  173. * @brief Function for clearing the specified SPIM event.
  174. *
  175. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  176. * @param[in] event Event to be cleared.
  177. */
  178. __STATIC_INLINE void nrf_spim_event_clear(NRF_SPIM_Type * p_reg,
  179. nrf_spim_event_t event);
  180. /**
  181. * @brief Function for retrieving the state of the SPIM event.
  182. *
  183. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  184. * @param[in] event Event to be checked.
  185. *
  186. * @retval true The event has been generated.
  187. * @retval false The event has not been generated.
  188. */
  189. __STATIC_INLINE bool nrf_spim_event_check(NRF_SPIM_Type * p_reg,
  190. nrf_spim_event_t event);
  191. /**
  192. * @brief Function for getting the address of the specified SPIM event register.
  193. *
  194. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  195. * @param[in] event The specified event.
  196. *
  197. * @return Address of the specified event register.
  198. */
  199. __STATIC_INLINE uint32_t nrf_spim_event_address_get(NRF_SPIM_Type * p_reg,
  200. nrf_spim_event_t event);
  201. /**
  202. * @brief Function for enabling the specified shortcuts.
  203. *
  204. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  205. * @param[in] mask Shortcuts to be enabled.
  206. */
  207. __STATIC_INLINE void nrf_spim_shorts_enable(NRF_SPIM_Type * p_reg,
  208. uint32_t mask);
  209. /**
  210. * @brief Function for disabling the specified shortcuts.
  211. *
  212. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  213. * @param[in] mask Shortcuts to be disabled.
  214. */
  215. __STATIC_INLINE void nrf_spim_shorts_disable(NRF_SPIM_Type * p_reg,
  216. uint32_t mask);
  217. /**
  218. * @brief Function for getting the shortcut setting.
  219. *
  220. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  221. *
  222. * @return Current shortcut configuration.
  223. */
  224. __STATIC_INLINE uint32_t nrf_spim_shorts_get(NRF_SPIM_Type * p_reg);
  225. /**
  226. * @brief Function for enabling the specified interrupts.
  227. *
  228. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  229. * @param[in] mask Mask of interrupts to be enabled.
  230. */
  231. __STATIC_INLINE void nrf_spim_int_enable(NRF_SPIM_Type * p_reg,
  232. uint32_t mask);
  233. /**
  234. * @brief Function for disabling the specified interrupts.
  235. *
  236. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  237. * @param[in] mask Mask of interrupts to be disabled.
  238. */
  239. __STATIC_INLINE void nrf_spim_int_disable(NRF_SPIM_Type * p_reg,
  240. uint32_t mask);
  241. /**
  242. * @brief Function for retrieving the state of a given interrupt.
  243. *
  244. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  245. * @param[in] spim_int Interrupt to be checked.
  246. *
  247. * @retval true The interrupt is enabled.
  248. * @retval false The interrupt is not enabled.
  249. */
  250. __STATIC_INLINE bool nrf_spim_int_enable_check(NRF_SPIM_Type * p_reg,
  251. nrf_spim_int_mask_t spim_int);
  252. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  253. /**
  254. * @brief Function for setting the subscribe configuration for a given
  255. * SPIM task.
  256. *
  257. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  258. * @param[in] task Task for which to set the configuration.
  259. * @param[in] channel Channel through which to subscribe events.
  260. */
  261. __STATIC_INLINE void nrf_spim_subscribe_set(NRF_SPIM_Type * p_reg,
  262. nrf_spim_task_t task,
  263. uint8_t channel);
  264. /**
  265. * @brief Function for clearing the subscribe configuration for a given
  266. * SPIM task.
  267. *
  268. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  269. * @param[in] task Task for which to clear the configuration.
  270. */
  271. __STATIC_INLINE void nrf_spim_subscribe_clear(NRF_SPIM_Type * p_reg,
  272. nrf_spim_task_t task);
  273. /**
  274. * @brief Function for setting the publish configuration for a given
  275. * SPIM event.
  276. *
  277. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  278. * @param[in] event Event for which to set the configuration.
  279. * @param[in] channel Channel through which to publish the event.
  280. */
  281. __STATIC_INLINE void nrf_spim_publish_set(NRF_SPIM_Type * p_reg,
  282. nrf_spim_event_t event,
  283. uint8_t channel);
  284. /**
  285. * @brief Function for clearing the publish configuration for a given
  286. * SPIM event.
  287. *
  288. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  289. * @param[in] event Event for which to clear the configuration.
  290. */
  291. __STATIC_INLINE void nrf_spim_publish_clear(NRF_SPIM_Type * p_reg,
  292. nrf_spim_event_t event);
  293. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  294. /**
  295. * @brief Function for enabling the SPIM peripheral.
  296. *
  297. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  298. */
  299. __STATIC_INLINE void nrf_spim_enable(NRF_SPIM_Type * p_reg);
  300. /**
  301. * @brief Function for disabling the SPIM peripheral.
  302. *
  303. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  304. */
  305. __STATIC_INLINE void nrf_spim_disable(NRF_SPIM_Type * p_reg);
  306. /**
  307. * @brief Function for configuring SPIM pins.
  308. *
  309. * If a given signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  310. * value instead of its pin number.
  311. *
  312. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  313. * @param[in] sck_pin SCK pin number.
  314. * @param[in] mosi_pin MOSI pin number.
  315. * @param[in] miso_pin MISO pin number.
  316. */
  317. __STATIC_INLINE void nrf_spim_pins_set(NRF_SPIM_Type * p_reg,
  318. uint32_t sck_pin,
  319. uint32_t mosi_pin,
  320. uint32_t miso_pin);
  321. #if (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  322. /**
  323. * @brief Function for configuring the SPIM hardware CSN pin.
  324. *
  325. * If this signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  326. * value instead of its pin number.
  327. *
  328. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  329. * @param[in] pin CSN pin number.
  330. * @param[in] polarity CSN pin polarity.
  331. * @param[in] duration Minimum duration between the edge of CSN and the edge of SCK
  332. * and minimum duration of CSN must stay unselected between transactions.
  333. * The value is specified in number of 64 MHz clock cycles (15.625 ns).
  334. */
  335. __STATIC_INLINE void nrf_spim_csn_configure(NRF_SPIM_Type * p_reg,
  336. uint32_t pin,
  337. nrf_spim_csn_pol_t polarity,
  338. uint32_t duration);
  339. #endif // (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  340. #if defined(SPIM_PSELDCX_CONNECT_Msk) || defined(__NRFX_DOXYGEN__)
  341. /**
  342. * @brief Function for configuring the SPIM DCX pin.
  343. *
  344. * If this signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  345. * value instead of its pin number.
  346. *
  347. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  348. * @param[in] dcx_pin DCX pin number.
  349. */
  350. __STATIC_INLINE void nrf_spim_dcx_pin_set(NRF_SPIM_Type * p_reg,
  351. uint32_t dcx_pin);
  352. /**
  353. * @brief Function for configuring the number of command bytes.
  354. *
  355. * Maximum value available for dividing the transmitted bytes into command
  356. * bytes and data bytes is @ref NRF_SPIM_DCX_CNT_ALL_CMD - 1.
  357. * The @ref NRF_SPIM_DCX_CNT_ALL_CMD value passed as the @c count parameter
  358. * causes all transmitted bytes to be marked as command bytes.
  359. *
  360. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  361. * @param[in] count Number of command bytes preceding the data bytes.
  362. */
  363. __STATIC_INLINE void nrf_spim_dcx_cnt_set(NRF_SPIM_Type * p_reg,
  364. uint32_t count);
  365. #endif // defined(SPIM_PSELDCX_CONNECT_Msk) || defined(__NRFX_DOXYGEN__)
  366. #if defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk) || defined(__NRFX_DOXYGEN__)
  367. /**
  368. * @brief Function for configuring the extended SPIM interface.
  369. *
  370. * @param p_reg Pointer to the structure of registers of the peripheral.
  371. * @param rxdelay Sample delay for input serial data on MISO,
  372. * specified in 64 MHz clock cycles (15.625 ns) from the sampling edge of SCK.
  373. */
  374. __STATIC_INLINE void nrf_spim_iftiming_set(NRF_SPIM_Type * p_reg,
  375. uint32_t rxdelay);
  376. #endif // defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk) || defined(__NRFX_DOXYGEN__)
  377. #if defined(SPIM_STALLSTAT_RX_Msk) || defined(__NRFX_DOXYGEN__)
  378. /**
  379. * @brief Function for clearing stall status for RX EasyDMA RAM accesses.
  380. *
  381. * @param p_reg Pointer to the structure of registers of the peripheral.
  382. */
  383. __STATIC_INLINE void nrf_spim_stallstat_rx_clear(NRF_SPIM_Type * p_reg);
  384. /**
  385. * @brief Function for getting stall status for RX EasyDMA RAM accesses.
  386. *
  387. * @param p_reg Pointer to the structure of registers of the peripheral.
  388. *
  389. * @return Stall status of RX EasyDMA RAM accesses.
  390. */
  391. __STATIC_INLINE bool nrf_spim_stallstat_rx_get(NRF_SPIM_Type * p_reg);
  392. #endif // defined(SPIM_STALLSTAT_RX_Msk) || defined(__NRFX_DOXYGEN__)
  393. #if defined(SPIM_STALLSTAT_TX_Msk) || defined(__NRFX_DOXYGEN__)
  394. /**
  395. * @brief Function for clearing stall status for TX EasyDMA RAM accesses.
  396. *
  397. * @param p_reg Pointer to the structure of registers of the peripheral.
  398. */
  399. __STATIC_INLINE void nrf_spim_stallstat_tx_clear(NRF_SPIM_Type * p_reg);
  400. /**
  401. * @brief Function for getting stall status for TX EasyDMA RAM accesses.
  402. *
  403. * @param p_reg Pointer to the structure of registers of the peripheral.
  404. *
  405. * @return Stall status of TX EasyDMA RAM accesses.
  406. */
  407. __STATIC_INLINE bool nrf_spim_stallstat_tx_get(NRF_SPIM_Type * p_reg);
  408. #endif // defined(SPIM_STALLSTAT_TX_Msk) || defined(__NRFX_DOXYGEN__)
  409. /**
  410. * @brief Function for setting the SPI master data rate.
  411. *
  412. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  413. * @param[in] frequency SPI frequency.
  414. */
  415. __STATIC_INLINE void nrf_spim_frequency_set(NRF_SPIM_Type * p_reg,
  416. nrf_spim_frequency_t frequency);
  417. /**
  418. * @brief Function for setting the transmit buffer.
  419. *
  420. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  421. * @param[in] p_buffer Pointer to the buffer with data to send.
  422. * @param[in] length Maximum number of data bytes to transmit.
  423. */
  424. __STATIC_INLINE void nrf_spim_tx_buffer_set(NRF_SPIM_Type * p_reg,
  425. uint8_t const * p_buffer,
  426. size_t length);
  427. /**
  428. * @brief Function for setting the receive buffer.
  429. *
  430. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  431. * @param[in] p_buffer Pointer to the buffer for received data.
  432. * @param[in] length Maximum number of data bytes to receive.
  433. */
  434. __STATIC_INLINE void nrf_spim_rx_buffer_set(NRF_SPIM_Type * p_reg,
  435. uint8_t * p_buffer,
  436. size_t length);
  437. /**
  438. * @brief Function for setting the SPI configuration.
  439. *
  440. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  441. * @param[in] spi_mode SPI mode.
  442. * @param[in] spi_bit_order SPI bit order.
  443. */
  444. __STATIC_INLINE void nrf_spim_configure(NRF_SPIM_Type * p_reg,
  445. nrf_spim_mode_t spi_mode,
  446. nrf_spim_bit_order_t spi_bit_order);
  447. /**
  448. * @brief Function for setting the over-read character.
  449. *
  450. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  451. * @param[in] orc Over-read character that is clocked out in case of
  452. * an over-read of the TXD buffer.
  453. */
  454. __STATIC_INLINE void nrf_spim_orc_set(NRF_SPIM_Type * p_reg,
  455. uint8_t orc);
  456. /**
  457. * @brief Function for enabling the TX list feature.
  458. *
  459. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  460. */
  461. __STATIC_INLINE void nrf_spim_tx_list_enable(NRF_SPIM_Type * p_reg);
  462. /**
  463. * @brief Function for disabling the TX list feature.
  464. *
  465. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  466. */
  467. __STATIC_INLINE void nrf_spim_tx_list_disable(NRF_SPIM_Type * p_reg);
  468. /**
  469. * @brief Function for enabling the RX list feature.
  470. *
  471. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  472. */
  473. __STATIC_INLINE void nrf_spim_rx_list_enable(NRF_SPIM_Type * p_reg);
  474. /**
  475. * @brief Function for disabling the RX list feature.
  476. *
  477. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  478. */
  479. __STATIC_INLINE void nrf_spim_rx_list_disable(NRF_SPIM_Type * p_reg);
  480. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  481. __STATIC_INLINE void nrf_spim_task_trigger(NRF_SPIM_Type * p_reg,
  482. nrf_spim_task_t task)
  483. {
  484. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  485. }
  486. __STATIC_INLINE uint32_t nrf_spim_task_address_get(NRF_SPIM_Type * p_reg,
  487. nrf_spim_task_t task)
  488. {
  489. return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
  490. }
  491. __STATIC_INLINE void nrf_spim_event_clear(NRF_SPIM_Type * p_reg,
  492. nrf_spim_event_t event)
  493. {
  494. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  495. #if __CORTEX_M == 0x04
  496. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  497. (void)dummy;
  498. #endif
  499. }
  500. __STATIC_INLINE bool nrf_spim_event_check(NRF_SPIM_Type * p_reg,
  501. nrf_spim_event_t event)
  502. {
  503. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  504. }
  505. __STATIC_INLINE uint32_t nrf_spim_event_address_get(NRF_SPIM_Type * p_reg,
  506. nrf_spim_event_t event)
  507. {
  508. return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
  509. }
  510. __STATIC_INLINE void nrf_spim_shorts_enable(NRF_SPIM_Type * p_reg,
  511. uint32_t mask)
  512. {
  513. p_reg->SHORTS |= mask;
  514. }
  515. __STATIC_INLINE void nrf_spim_shorts_disable(NRF_SPIM_Type * p_reg,
  516. uint32_t mask)
  517. {
  518. p_reg->SHORTS &= ~(mask);
  519. }
  520. __STATIC_INLINE uint32_t nrf_spim_shorts_get(NRF_SPIM_Type * p_reg)
  521. {
  522. return p_reg->SHORTS;
  523. }
  524. __STATIC_INLINE void nrf_spim_int_enable(NRF_SPIM_Type * p_reg,
  525. uint32_t mask)
  526. {
  527. p_reg->INTENSET = mask;
  528. }
  529. __STATIC_INLINE void nrf_spim_int_disable(NRF_SPIM_Type * p_reg,
  530. uint32_t mask)
  531. {
  532. p_reg->INTENCLR = mask;
  533. }
  534. __STATIC_INLINE bool nrf_spim_int_enable_check(NRF_SPIM_Type * p_reg,
  535. nrf_spim_int_mask_t spim_int)
  536. {
  537. return (bool)(p_reg->INTENSET & spim_int);
  538. }
  539. #if defined(DPPI_PRESENT)
  540. __STATIC_INLINE void nrf_spim_subscribe_set(NRF_SPIM_Type * p_reg,
  541. nrf_spim_task_t task,
  542. uint8_t channel)
  543. {
  544. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  545. ((uint32_t)channel | SPIM_SUBSCRIBE_START_EN_Msk);
  546. }
  547. __STATIC_INLINE void nrf_spim_subscribe_clear(NRF_SPIM_Type * p_reg,
  548. nrf_spim_task_t task)
  549. {
  550. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  551. }
  552. __STATIC_INLINE void nrf_spim_publish_set(NRF_SPIM_Type * p_reg,
  553. nrf_spim_event_t event,
  554. uint8_t channel)
  555. {
  556. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  557. ((uint32_t)channel | SPIM_PUBLISH_STARTED_EN_Msk);
  558. }
  559. __STATIC_INLINE void nrf_spim_publish_clear(NRF_SPIM_Type * p_reg,
  560. nrf_spim_event_t event)
  561. {
  562. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  563. }
  564. #endif // defined(DPPI_PRESENT)
  565. __STATIC_INLINE void nrf_spim_enable(NRF_SPIM_Type * p_reg)
  566. {
  567. p_reg->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
  568. }
  569. __STATIC_INLINE void nrf_spim_disable(NRF_SPIM_Type * p_reg)
  570. {
  571. p_reg->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos);
  572. }
  573. __STATIC_INLINE void nrf_spim_pins_set(NRF_SPIM_Type * p_reg,
  574. uint32_t sck_pin,
  575. uint32_t mosi_pin,
  576. uint32_t miso_pin)
  577. {
  578. p_reg->PSEL.SCK = sck_pin;
  579. p_reg->PSEL.MOSI = mosi_pin;
  580. p_reg->PSEL.MISO = miso_pin;
  581. }
  582. #if (NRF_SPIM_HW_CSN_PRESENT)
  583. __STATIC_INLINE void nrf_spim_csn_configure(NRF_SPIM_Type * p_reg,
  584. uint32_t pin,
  585. nrf_spim_csn_pol_t polarity,
  586. uint32_t duration)
  587. {
  588. p_reg->PSEL.CSN = pin;
  589. p_reg->CSNPOL = polarity;
  590. p_reg->IFTIMING.CSNDUR = duration;
  591. }
  592. #endif // defined(NRF_SPIM_HW_CSN_PRESENT)
  593. #if defined(SPIM_PSELDCX_CONNECT_Msk)
  594. __STATIC_INLINE void nrf_spim_dcx_pin_set(NRF_SPIM_Type * p_reg,
  595. uint32_t dcx_pin)
  596. {
  597. p_reg->PSELDCX = dcx_pin;
  598. }
  599. __STATIC_INLINE void nrf_spim_dcx_cnt_set(NRF_SPIM_Type * p_reg,
  600. uint32_t dcx_cnt)
  601. {
  602. p_reg->DCXCNT = dcx_cnt;
  603. }
  604. #endif // defined(SPIM_PSELDCX_CONNECT_Msk)
  605. #if defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk)
  606. __STATIC_INLINE void nrf_spim_iftiming_set(NRF_SPIM_Type * p_reg,
  607. uint32_t rxdelay)
  608. {
  609. p_reg->IFTIMING.RXDELAY = rxdelay;
  610. }
  611. #endif // defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk)
  612. #if defined(SPIM_STALLSTAT_RX_Msk)
  613. __STATIC_INLINE void nrf_spim_stallstat_rx_clear(NRF_SPIM_Type * p_reg)
  614. {
  615. p_reg->STALLSTAT &= ~(SPIM_STALLSTAT_RX_Msk);
  616. }
  617. __STATIC_INLINE bool nrf_spim_stallstat_rx_get(NRF_SPIM_Type * p_reg)
  618. {
  619. return (p_reg->STALLSTAT & SPIM_STALLSTAT_RX_Msk) != 0;
  620. }
  621. #endif // defined(SPIM_STALLSTAT_RX_Msk)
  622. #if defined(SPIM_STALLSTAT_TX_Msk)
  623. __STATIC_INLINE void nrf_spim_stallstat_tx_clear(NRF_SPIM_Type * p_reg)
  624. {
  625. p_reg->STALLSTAT &= ~(SPIM_STALLSTAT_TX_Msk);
  626. }
  627. __STATIC_INLINE bool nrf_spim_stallstat_tx_get(NRF_SPIM_Type * p_reg)
  628. {
  629. return (p_reg->STALLSTAT & SPIM_STALLSTAT_TX_Msk) != 0;
  630. }
  631. #endif // defined(SPIM_STALLSTAT_TX_Msk)
  632. __STATIC_INLINE void nrf_spim_frequency_set(NRF_SPIM_Type * p_reg,
  633. nrf_spim_frequency_t frequency)
  634. {
  635. p_reg->FREQUENCY = (uint32_t)frequency;
  636. }
  637. __STATIC_INLINE void nrf_spim_tx_buffer_set(NRF_SPIM_Type * p_reg,
  638. uint8_t const * p_buffer,
  639. size_t length)
  640. {
  641. p_reg->TXD.PTR = (uint32_t)p_buffer;
  642. p_reg->TXD.MAXCNT = length;
  643. }
  644. __STATIC_INLINE void nrf_spim_rx_buffer_set(NRF_SPIM_Type * p_reg,
  645. uint8_t * p_buffer,
  646. size_t length)
  647. {
  648. p_reg->RXD.PTR = (uint32_t)p_buffer;
  649. p_reg->RXD.MAXCNT = length;
  650. }
  651. __STATIC_INLINE void nrf_spim_configure(NRF_SPIM_Type * p_reg,
  652. nrf_spim_mode_t spi_mode,
  653. nrf_spim_bit_order_t spi_bit_order)
  654. {
  655. uint32_t config = (spi_bit_order == NRF_SPIM_BIT_ORDER_MSB_FIRST ?
  656. SPIM_CONFIG_ORDER_MsbFirst : SPIM_CONFIG_ORDER_LsbFirst);
  657. switch (spi_mode)
  658. {
  659. default:
  660. case NRF_SPIM_MODE_0:
  661. config |= (SPIM_CONFIG_CPOL_ActiveHigh << SPIM_CONFIG_CPOL_Pos) |
  662. (SPIM_CONFIG_CPHA_Leading << SPIM_CONFIG_CPHA_Pos);
  663. break;
  664. case NRF_SPIM_MODE_1:
  665. config |= (SPIM_CONFIG_CPOL_ActiveHigh << SPIM_CONFIG_CPOL_Pos) |
  666. (SPIM_CONFIG_CPHA_Trailing << SPIM_CONFIG_CPHA_Pos);
  667. break;
  668. case NRF_SPIM_MODE_2:
  669. config |= (SPIM_CONFIG_CPOL_ActiveLow << SPIM_CONFIG_CPOL_Pos) |
  670. (SPIM_CONFIG_CPHA_Leading << SPIM_CONFIG_CPHA_Pos);
  671. break;
  672. case NRF_SPIM_MODE_3:
  673. config |= (SPIM_CONFIG_CPOL_ActiveLow << SPIM_CONFIG_CPOL_Pos) |
  674. (SPIM_CONFIG_CPHA_Trailing << SPIM_CONFIG_CPHA_Pos);
  675. break;
  676. }
  677. p_reg->CONFIG = config;
  678. }
  679. __STATIC_INLINE void nrf_spim_orc_set(NRF_SPIM_Type * p_reg,
  680. uint8_t orc)
  681. {
  682. p_reg->ORC = orc;
  683. }
  684. __STATIC_INLINE void nrf_spim_tx_list_enable(NRF_SPIM_Type * p_reg)
  685. {
  686. p_reg->TXD.LIST = 1;
  687. }
  688. __STATIC_INLINE void nrf_spim_tx_list_disable(NRF_SPIM_Type * p_reg)
  689. {
  690. p_reg->TXD.LIST = 0;
  691. }
  692. __STATIC_INLINE void nrf_spim_rx_list_enable(NRF_SPIM_Type * p_reg)
  693. {
  694. p_reg->RXD.LIST = 1;
  695. }
  696. __STATIC_INLINE void nrf_spim_rx_list_disable(NRF_SPIM_Type * p_reg)
  697. {
  698. p_reg->RXD.LIST = 0;
  699. }
  700. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  701. /** @} */
  702. #ifdef __cplusplus
  703. }
  704. #endif
  705. #endif // NRF_SPIM_H__