nrf_spim.h 29 KB

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