nrf_spim.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. /**
  268. * @brief Function for enabling the SPIM peripheral.
  269. *
  270. * @param[in] p_reg Pointer to the peripheral registers structure.
  271. */
  272. __STATIC_INLINE void nrf_spim_enable(NRF_SPIM_Type * p_reg);
  273. /**
  274. * @brief Function for disabling the SPIM peripheral.
  275. *
  276. * @param[in] p_reg Pointer to the peripheral registers structure.
  277. */
  278. __STATIC_INLINE void nrf_spim_disable(NRF_SPIM_Type * p_reg);
  279. /**
  280. * @brief Function for configuring SPIM pins.
  281. *
  282. * If a given signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  283. * value instead of its pin number.
  284. *
  285. * @param[in] p_reg Pointer to the peripheral registers structure.
  286. * @param[in] sck_pin SCK pin number.
  287. * @param[in] mosi_pin MOSI pin number.
  288. * @param[in] miso_pin MISO pin number.
  289. */
  290. __STATIC_INLINE void nrf_spim_pins_set(NRF_SPIM_Type * p_reg,
  291. uint32_t sck_pin,
  292. uint32_t mosi_pin,
  293. uint32_t miso_pin);
  294. #if (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  295. /**
  296. * @brief Function for configuring the SPIM hardware CSN pin.
  297. *
  298. * If this signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  299. * value instead of its pin number.
  300. *
  301. * @param[in] p_reg Pointer to the peripheral registers structure.
  302. * @param[in] pin CSN pin number.
  303. * @param[in] polarity CSN pin polarity.
  304. * @param[in] duration Minimum duration between the edge of CSN and the edge of SCK
  305. * and minimum duration of CSN must stay unselected between transactions.
  306. * The value is specified in number of 64 MHz clock cycles (15.625 ns).
  307. */
  308. __STATIC_INLINE void nrf_spim_csn_configure(NRF_SPIM_Type * p_reg,
  309. uint32_t pin,
  310. nrf_spim_csn_pol_t polarity,
  311. uint32_t duration);
  312. #endif // (NRF_SPIM_HW_CSN_PRESENT) || defined(__NRFX_DOXYGEN__)
  313. #if defined(SPIM_PSELDCX_CONNECT_Msk) || defined(__NRFX_DOXYGEN__)
  314. /**
  315. * @brief Function for configuring the SPIM DCX pin.
  316. *
  317. * If this signal is not needed, pass the @ref NRF_SPIM_PIN_NOT_CONNECTED
  318. * value instead of its pin number.
  319. *
  320. * @param[in] p_reg Pointer to the peripheral registers structure.
  321. * @param[in] dcx_pin DCX pin number.
  322. */
  323. __STATIC_INLINE void nrf_spim_dcx_pin_set(NRF_SPIM_Type * p_reg,
  324. uint32_t dcx_pin);
  325. /**
  326. * @brief Function for configuring the number of command bytes.
  327. *
  328. * Maximum value available for dividing the transmitted bytes into command
  329. * bytes and data bytes is @ref NRF_SPIM_DCX_CNT_ALL_CMD - 1.
  330. * The @ref NRF_SPIM_DCX_CNT_ALL_CMD value passed as the @c count parameter
  331. * causes all transmitted bytes to be marked as command bytes.
  332. *
  333. * @param[in] p_reg Pointer to the peripheral registers structure.
  334. * @param[in] count Number of command bytes preceding the data bytes.
  335. */
  336. __STATIC_INLINE void nrf_spim_dcx_cnt_set(NRF_SPIM_Type * p_reg,
  337. uint32_t count);
  338. #endif // defined(SPIM_PSELDCX_CONNECT_Msk) || defined(__NRFX_DOXYGEN__)
  339. #if defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk) || defined(__NRFX_DOXYGEN__)
  340. /**
  341. * @brief Function for configuring the extended SPIM interface.
  342. * @param p_reg Pointer to the peripheral registers structure.
  343. * @param rxdelay Sample delay for input serial data on MISO,
  344. * specified in 64 MHz clock cycles (15.625 ns) from the sampling edge of SCK.
  345. */
  346. __STATIC_INLINE void nrf_spim_iftiming_set(NRF_SPIM_Type * p_reg,
  347. uint32_t rxdelay);
  348. #endif // defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk) || defined(__NRFX_DOXYGEN__)
  349. #if defined(SPIM_STALLSTAT_RX_Msk) || defined(__NRFX_DOXYGEN__)
  350. /**
  351. * @brief Function for clearing stall status for RX EasyDMA RAM accesses.
  352. *
  353. * @param p_reg Pointer to the peripheral registers structure.
  354. */
  355. __STATIC_INLINE void nrf_spim_stallstat_rx_clear(NRF_SPIM_Type * p_reg);
  356. /**
  357. * @brief Function for getting stall status for RX EasyDMA RAM accesses.
  358. *
  359. * @param p_reg Pointer to the peripheral registers structure.
  360. *
  361. * @return Stall status of RX EasyDMA RAM accesses.
  362. */
  363. __STATIC_INLINE bool nrf_spim_stallstat_rx_get(NRF_SPIM_Type * p_reg);
  364. #endif // defined(SPIM_STALLSTAT_RX_Msk) || defined(__NRFX_DOXYGEN__)
  365. #if defined(SPIM_STALLSTAT_TX_Msk) || defined(__NRFX_DOXYGEN__)
  366. /**
  367. * @brief Function for clearing stall status for TX EasyDMA RAM accesses.
  368. *
  369. * @param p_reg Pointer to the peripheral registers structure.
  370. */
  371. __STATIC_INLINE void nrf_spim_stallstat_tx_clear(NRF_SPIM_Type * p_reg);
  372. /**
  373. * @brief Function for getting stall status for TX EasyDMA RAM accesses.
  374. *
  375. * @param p_reg Pointer to the peripheral registers structure.
  376. *
  377. * @return Stall status of TX EasyDMA RAM accesses.
  378. */
  379. __STATIC_INLINE bool nrf_spim_stallstat_tx_get(NRF_SPIM_Type * p_reg);
  380. #endif // defined(SPIM_STALLSTAT_TX_Msk) || defined(__NRFX_DOXYGEN__)
  381. /**
  382. * @brief Function for setting the SPI master data rate.
  383. *
  384. * @param[in] p_reg Pointer to the peripheral registers structure.
  385. * @param[in] frequency SPI frequency.
  386. */
  387. __STATIC_INLINE void nrf_spim_frequency_set(NRF_SPIM_Type * p_reg,
  388. nrf_spim_frequency_t frequency);
  389. /**
  390. * @brief Function for setting the transmit buffer.
  391. *
  392. * @param[in] p_reg Pointer to the peripheral registers structure.
  393. * @param[in] p_buffer Pointer to the buffer with data to send.
  394. * @param[in] length Maximum number of data bytes to transmit.
  395. */
  396. __STATIC_INLINE void nrf_spim_tx_buffer_set(NRF_SPIM_Type * p_reg,
  397. uint8_t const * p_buffer,
  398. size_t length);
  399. /**
  400. * @brief Function for setting the receive buffer.
  401. *
  402. * @param[in] p_reg Pointer to the peripheral registers structure.
  403. * @param[in] p_buffer Pointer to the buffer for received data.
  404. * @param[in] length Maximum number of data bytes to receive.
  405. */
  406. __STATIC_INLINE void nrf_spim_rx_buffer_set(NRF_SPIM_Type * p_reg,
  407. uint8_t * p_buffer,
  408. size_t length);
  409. /**
  410. * @brief Function for setting the SPI configuration.
  411. *
  412. * @param[in] p_reg Pointer to the peripheral registers structure.
  413. * @param[in] spi_mode SPI mode.
  414. * @param[in] spi_bit_order SPI bit order.
  415. */
  416. __STATIC_INLINE void nrf_spim_configure(NRF_SPIM_Type * p_reg,
  417. nrf_spim_mode_t spi_mode,
  418. nrf_spim_bit_order_t spi_bit_order);
  419. /**
  420. * @brief Function for setting the over-read character.
  421. *
  422. * @param[in] p_reg Pointer to the peripheral registers structure.
  423. * @param[in] orc Over-read character that is clocked out in case of
  424. * an over-read of the TXD buffer.
  425. */
  426. __STATIC_INLINE void nrf_spim_orc_set(NRF_SPIM_Type * p_reg,
  427. uint8_t orc);
  428. /**
  429. * @brief Function for enabling the TX list feature.
  430. *
  431. * @param[in] p_reg Pointer to the peripheral registers structure.
  432. */
  433. __STATIC_INLINE void nrf_spim_tx_list_enable(NRF_SPIM_Type * p_reg);
  434. /**
  435. * @brief Function for disabling the TX list feature.
  436. *
  437. * @param[in] p_reg Pointer to the peripheral registers structure.
  438. */
  439. __STATIC_INLINE void nrf_spim_tx_list_disable(NRF_SPIM_Type * p_reg);
  440. /**
  441. * @brief Function for enabling the RX list feature.
  442. *
  443. * @param[in] p_reg Pointer to the peripheral registers structure.
  444. */
  445. __STATIC_INLINE void nrf_spim_rx_list_enable(NRF_SPIM_Type * p_reg);
  446. /**
  447. * @brief Function for disabling the RX list feature.
  448. *
  449. * @param[in] p_reg Pointer to the peripheral registers structure.
  450. */
  451. __STATIC_INLINE void nrf_spim_rx_list_disable(NRF_SPIM_Type * p_reg);
  452. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  453. __STATIC_INLINE void nrf_spim_task_trigger(NRF_SPIM_Type * p_reg,
  454. nrf_spim_task_t spim_task)
  455. {
  456. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spim_task)) = 0x1UL;
  457. }
  458. __STATIC_INLINE uint32_t nrf_spim_task_address_get(NRF_SPIM_Type * p_reg,
  459. nrf_spim_task_t spim_task)
  460. {
  461. return (uint32_t)((uint8_t *)p_reg + (uint32_t)spim_task);
  462. }
  463. __STATIC_INLINE void nrf_spim_event_clear(NRF_SPIM_Type * p_reg,
  464. nrf_spim_event_t spim_event)
  465. {
  466. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spim_event)) = 0x0UL;
  467. #if __CORTEX_M == 0x04
  468. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spim_event));
  469. (void)dummy;
  470. #endif
  471. }
  472. __STATIC_INLINE bool nrf_spim_event_check(NRF_SPIM_Type * p_reg,
  473. nrf_spim_event_t spim_event)
  474. {
  475. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spim_event);
  476. }
  477. __STATIC_INLINE uint32_t nrf_spim_event_address_get(NRF_SPIM_Type * p_reg,
  478. nrf_spim_event_t spim_event)
  479. {
  480. return (uint32_t)((uint8_t *)p_reg + (uint32_t)spim_event);
  481. }
  482. __STATIC_INLINE void nrf_spim_shorts_enable(NRF_SPIM_Type * p_reg,
  483. uint32_t spim_shorts_mask)
  484. {
  485. p_reg->SHORTS |= spim_shorts_mask;
  486. }
  487. __STATIC_INLINE void nrf_spim_shorts_disable(NRF_SPIM_Type * p_reg,
  488. uint32_t spim_shorts_mask)
  489. {
  490. p_reg->SHORTS &= ~(spim_shorts_mask);
  491. }
  492. __STATIC_INLINE uint32_t nrf_spim_shorts_get(NRF_SPIM_Type * p_reg)
  493. {
  494. return p_reg->SHORTS;
  495. }
  496. __STATIC_INLINE void nrf_spim_int_enable(NRF_SPIM_Type * p_reg,
  497. uint32_t spim_int_mask)
  498. {
  499. p_reg->INTENSET = spim_int_mask;
  500. }
  501. __STATIC_INLINE void nrf_spim_int_disable(NRF_SPIM_Type * p_reg,
  502. uint32_t spim_int_mask)
  503. {
  504. p_reg->INTENCLR = spim_int_mask;
  505. }
  506. __STATIC_INLINE bool nrf_spim_int_enable_check(NRF_SPIM_Type * p_reg,
  507. nrf_spim_int_mask_t spim_int)
  508. {
  509. return (bool)(p_reg->INTENSET & spim_int);
  510. }
  511. __STATIC_INLINE void nrf_spim_enable(NRF_SPIM_Type * p_reg)
  512. {
  513. p_reg->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
  514. }
  515. __STATIC_INLINE void nrf_spim_disable(NRF_SPIM_Type * p_reg)
  516. {
  517. p_reg->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos);
  518. }
  519. __STATIC_INLINE void nrf_spim_pins_set(NRF_SPIM_Type * p_reg,
  520. uint32_t sck_pin,
  521. uint32_t mosi_pin,
  522. uint32_t miso_pin)
  523. {
  524. p_reg->PSEL.SCK = sck_pin;
  525. p_reg->PSEL.MOSI = mosi_pin;
  526. p_reg->PSEL.MISO = miso_pin;
  527. }
  528. #if (NRF_SPIM_HW_CSN_PRESENT)
  529. __STATIC_INLINE void nrf_spim_csn_configure(NRF_SPIM_Type * p_reg,
  530. uint32_t pin,
  531. nrf_spim_csn_pol_t polarity,
  532. uint32_t duration)
  533. {
  534. p_reg->PSEL.CSN = pin;
  535. p_reg->CSNPOL = polarity;
  536. p_reg->IFTIMING.CSNDUR = duration;
  537. }
  538. #endif // defined(NRF_SPIM_HW_CSN_PRESENT)
  539. #if defined(SPIM_PSELDCX_CONNECT_Msk)
  540. __STATIC_INLINE void nrf_spim_dcx_pin_set(NRF_SPIM_Type * p_reg,
  541. uint32_t dcx_pin)
  542. {
  543. p_reg->PSELDCX = dcx_pin;
  544. }
  545. __STATIC_INLINE void nrf_spim_dcx_cnt_set(NRF_SPIM_Type * p_reg,
  546. uint32_t dcx_cnt)
  547. {
  548. p_reg->DCXCNT = dcx_cnt;
  549. }
  550. #endif // defined(SPIM_PSELDCX_CONNECT_Msk)
  551. #if defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk)
  552. __STATIC_INLINE void nrf_spim_iftiming_set(NRF_SPIM_Type * p_reg,
  553. uint32_t rxdelay)
  554. {
  555. p_reg->IFTIMING.RXDELAY = rxdelay;
  556. }
  557. #endif // defined(SPIM_IFTIMING_RXDELAY_RXDELAY_Msk)
  558. #if defined(SPIM_STALLSTAT_RX_Msk)
  559. __STATIC_INLINE void nrf_spim_stallstat_rx_clear(NRF_SPIM_Type * p_reg)
  560. {
  561. p_reg->STALLSTAT &= ~(SPIM_STALLSTAT_RX_Msk);
  562. }
  563. __STATIC_INLINE bool nrf_spim_stallstat_rx_get(NRF_SPIM_Type * p_reg)
  564. {
  565. return (p_reg->STALLSTAT & SPIM_STALLSTAT_RX_Msk) != 0;
  566. }
  567. #endif // defined(SPIM_STALLSTAT_RX_Msk)
  568. #if defined(SPIM_STALLSTAT_TX_Msk)
  569. __STATIC_INLINE void nrf_spim_stallstat_tx_clear(NRF_SPIM_Type * p_reg)
  570. {
  571. p_reg->STALLSTAT &= ~(SPIM_STALLSTAT_TX_Msk);
  572. }
  573. __STATIC_INLINE bool nrf_spim_stallstat_tx_get(NRF_SPIM_Type * p_reg)
  574. {
  575. return (p_reg->STALLSTAT & SPIM_STALLSTAT_TX_Msk) != 0;
  576. }
  577. #endif // defined(SPIM_STALLSTAT_TX_Msk)
  578. __STATIC_INLINE void nrf_spim_frequency_set(NRF_SPIM_Type * p_reg,
  579. nrf_spim_frequency_t frequency)
  580. {
  581. p_reg->FREQUENCY = frequency;
  582. }
  583. __STATIC_INLINE void nrf_spim_tx_buffer_set(NRF_SPIM_Type * p_reg,
  584. uint8_t const * p_buffer,
  585. size_t length)
  586. {
  587. p_reg->TXD.PTR = (uint32_t)p_buffer;
  588. p_reg->TXD.MAXCNT = length;
  589. }
  590. __STATIC_INLINE void nrf_spim_rx_buffer_set(NRF_SPIM_Type * p_reg,
  591. uint8_t * p_buffer,
  592. size_t length)
  593. {
  594. p_reg->RXD.PTR = (uint32_t)p_buffer;
  595. p_reg->RXD.MAXCNT = length;
  596. }
  597. __STATIC_INLINE void nrf_spim_configure(NRF_SPIM_Type * p_reg,
  598. nrf_spim_mode_t spi_mode,
  599. nrf_spim_bit_order_t spi_bit_order)
  600. {
  601. uint32_t config = (spi_bit_order == NRF_SPIM_BIT_ORDER_MSB_FIRST ?
  602. SPIM_CONFIG_ORDER_MsbFirst : SPIM_CONFIG_ORDER_LsbFirst);
  603. switch (spi_mode)
  604. {
  605. default:
  606. case NRF_SPIM_MODE_0:
  607. config |= (SPIM_CONFIG_CPOL_ActiveHigh << SPIM_CONFIG_CPOL_Pos) |
  608. (SPIM_CONFIG_CPHA_Leading << SPIM_CONFIG_CPHA_Pos);
  609. break;
  610. case NRF_SPIM_MODE_1:
  611. config |= (SPIM_CONFIG_CPOL_ActiveHigh << SPIM_CONFIG_CPOL_Pos) |
  612. (SPIM_CONFIG_CPHA_Trailing << SPIM_CONFIG_CPHA_Pos);
  613. break;
  614. case NRF_SPIM_MODE_2:
  615. config |= (SPIM_CONFIG_CPOL_ActiveLow << SPIM_CONFIG_CPOL_Pos) |
  616. (SPIM_CONFIG_CPHA_Leading << SPIM_CONFIG_CPHA_Pos);
  617. break;
  618. case NRF_SPIM_MODE_3:
  619. config |= (SPIM_CONFIG_CPOL_ActiveLow << SPIM_CONFIG_CPOL_Pos) |
  620. (SPIM_CONFIG_CPHA_Trailing << SPIM_CONFIG_CPHA_Pos);
  621. break;
  622. }
  623. p_reg->CONFIG = config;
  624. }
  625. __STATIC_INLINE void nrf_spim_orc_set(NRF_SPIM_Type * p_reg,
  626. uint8_t orc)
  627. {
  628. p_reg->ORC = orc;
  629. }
  630. __STATIC_INLINE void nrf_spim_tx_list_enable(NRF_SPIM_Type * p_reg)
  631. {
  632. p_reg->TXD.LIST = 1;
  633. }
  634. __STATIC_INLINE void nrf_spim_tx_list_disable(NRF_SPIM_Type * p_reg)
  635. {
  636. p_reg->TXD.LIST = 0;
  637. }
  638. __STATIC_INLINE void nrf_spim_rx_list_enable(NRF_SPIM_Type * p_reg)
  639. {
  640. p_reg->RXD.LIST = 1;
  641. }
  642. __STATIC_INLINE void nrf_spim_rx_list_disable(NRF_SPIM_Type * p_reg)
  643. {
  644. p_reg->RXD.LIST = 0;
  645. }
  646. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  647. /** @} */
  648. #ifdef __cplusplus
  649. }
  650. #endif
  651. #endif // NRF_SPIM_H__