nrf_spis.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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_SPIS_H__
  41. #define NRF_SPIS_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_spis_hal SPIS HAL
  48. * @{
  49. * @ingroup nrf_spis
  50. * @brief Hardware access layer for managing the SPIS peripheral.
  51. */
  52. /**
  53. * @brief This value can be used as a parameter for the @ref nrf_spis_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_SPIS_PIN_NOT_CONNECTED 0xFFFFFFFF
  58. /** @brief SPIS tasks. */
  59. typedef enum
  60. {
  61. NRF_SPIS_TASK_ACQUIRE = offsetof(NRF_SPIS_Type, TASKS_ACQUIRE), ///< Acquire SPI semaphore.
  62. NRF_SPIS_TASK_RELEASE = offsetof(NRF_SPIS_Type, TASKS_RELEASE), ///< Release SPI semaphore, enabling the SPI slave to acquire it.
  63. } nrf_spis_task_t;
  64. /** @brief SPIS events. */
  65. typedef enum
  66. {
  67. NRF_SPIS_EVENT_END = offsetof(NRF_SPIS_Type, EVENTS_END), ///< Granted transaction completed.
  68. NRF_SPIS_EVENT_ACQUIRED = offsetof(NRF_SPIS_Type, EVENTS_ACQUIRED) ///< Semaphore acquired.
  69. } nrf_spis_event_t;
  70. /** @brief SPIS shortcuts. */
  71. typedef enum
  72. {
  73. NRF_SPIS_SHORT_END_ACQUIRE = SPIS_SHORTS_END_ACQUIRE_Msk ///< Shortcut between END event and ACQUIRE task.
  74. } nrf_spis_short_mask_t;
  75. /** @brief SPIS interrupts. */
  76. typedef enum
  77. {
  78. NRF_SPIS_INT_END_MASK = SPIS_INTENSET_END_Msk, ///< Interrupt on END event.
  79. NRF_SPIS_INT_ACQUIRED_MASK = SPIS_INTENSET_ACQUIRED_Msk ///< Interrupt on ACQUIRED event.
  80. } nrf_spis_int_mask_t;
  81. /** @brief SPI modes. */
  82. typedef enum
  83. {
  84. NRF_SPIS_MODE_0, ///< SCK active high, sample on leading edge of clock.
  85. NRF_SPIS_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  86. NRF_SPIS_MODE_2, ///< SCK active low, sample on leading edge of clock.
  87. NRF_SPIS_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  88. } nrf_spis_mode_t;
  89. /** @brief SPI bit orders. */
  90. typedef enum
  91. {
  92. NRF_SPIS_BIT_ORDER_MSB_FIRST = SPIS_CONFIG_ORDER_MsbFirst, ///< Most significant bit shifted out first.
  93. NRF_SPIS_BIT_ORDER_LSB_FIRST = SPIS_CONFIG_ORDER_LsbFirst ///< Least significant bit shifted out first.
  94. } nrf_spis_bit_order_t;
  95. /** @brief SPI semaphore status. */
  96. typedef enum
  97. {
  98. NRF_SPIS_SEMSTAT_FREE = 0, ///< Semaphore is free.
  99. NRF_SPIS_SEMSTAT_CPU = 1, ///< Semaphore is assigned to the CPU.
  100. NRF_SPIS_SEMSTAT_SPIS = 2, ///< Semaphore is assigned to the SPI slave.
  101. NRF_SPIS_SEMSTAT_CPUPENDING = 3 ///< Semaphore is assigned to the SPI, but a handover to the CPU is pending.
  102. } nrf_spis_semstat_t;
  103. /** @brief SPIS status. */
  104. typedef enum
  105. {
  106. NRF_SPIS_STATUS_OVERREAD = SPIS_STATUS_OVERREAD_Msk, ///< TX buffer over-read detected and prevented.
  107. NRF_SPIS_STATUS_OVERFLOW = SPIS_STATUS_OVERFLOW_Msk ///< RX buffer overflow detected and prevented.
  108. } nrf_spis_status_mask_t;
  109. /**
  110. * @brief Function for activating the specified SPIS task.
  111. *
  112. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  113. * @param[in] task Task to be activated.
  114. */
  115. __STATIC_INLINE void nrf_spis_task_trigger(NRF_SPIS_Type * p_reg,
  116. nrf_spis_task_t task);
  117. /**
  118. * @brief Function for getting the address of the specified SPIS task register.
  119. *
  120. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  121. * @param[in] task The specified task.
  122. *
  123. * @return Address of the specified task register.
  124. */
  125. __STATIC_INLINE uint32_t nrf_spis_task_address_get(NRF_SPIS_Type const * p_reg,
  126. nrf_spis_task_t task);
  127. /**
  128. * @brief Function for clearing the specified SPIS event.
  129. *
  130. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  131. * @param[in] event Event to be cleared.
  132. */
  133. __STATIC_INLINE void nrf_spis_event_clear(NRF_SPIS_Type * p_reg,
  134. nrf_spis_event_t event);
  135. /**
  136. * @brief Function for retrieving the state of the SPIS event.
  137. *
  138. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  139. * @param[in] event Event to be checked.
  140. *
  141. * @retval true The event has been generated.
  142. * @retval false The event has not been generated.
  143. */
  144. __STATIC_INLINE bool nrf_spis_event_check(NRF_SPIS_Type const * p_reg,
  145. nrf_spis_event_t event);
  146. /**
  147. * @brief Function for getting the address of the specified SPIS event register.
  148. *
  149. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  150. * @param[in] event The specified event.
  151. *
  152. * @return Address of the specified event register.
  153. */
  154. __STATIC_INLINE uint32_t nrf_spis_event_address_get(NRF_SPIS_Type const * p_reg,
  155. nrf_spis_event_t event);
  156. /**
  157. * @brief Function for enabling the specified shortcuts.
  158. *
  159. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  160. * @param[in] mask Shortcuts to be enabled.
  161. */
  162. __STATIC_INLINE void nrf_spis_shorts_enable(NRF_SPIS_Type * p_reg,
  163. uint32_t mask);
  164. /**
  165. * @brief Function for disabling the specified shortcuts.
  166. *
  167. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  168. * @param[in] mask Shortcuts to be disabled.
  169. */
  170. __STATIC_INLINE void nrf_spis_shorts_disable(NRF_SPIS_Type * p_reg,
  171. uint32_t mask);
  172. /**
  173. * @brief Function for enabling the specified interrupts.
  174. *
  175. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  176. * @param[in] mask Mask of interrupts to be enabled.
  177. */
  178. __STATIC_INLINE void nrf_spis_int_enable(NRF_SPIS_Type * p_reg,
  179. uint32_t mask);
  180. /**
  181. * @brief Function for disabling the specified interrupts.
  182. *
  183. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  184. * @param[in] mask Mask of interrupts to be disabled.
  185. */
  186. __STATIC_INLINE void nrf_spis_int_disable(NRF_SPIS_Type * p_reg,
  187. uint32_t mask);
  188. /**
  189. * @brief Function for retrieving the state of a given interrupt.
  190. *
  191. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  192. * @param[in] spis_int Interrupt to be checked.
  193. *
  194. * @retval true The interrupt is enabled.
  195. * @retval false The interrupt is not enabled.
  196. */
  197. __STATIC_INLINE bool nrf_spis_int_enable_check(NRF_SPIS_Type const * p_reg,
  198. nrf_spis_int_mask_t spis_int);
  199. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  200. /**
  201. * @brief Function for setting the subscribe configuration for a given
  202. * SPIS task.
  203. *
  204. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  205. * @param[in] task Task for which to set the configuration.
  206. * @param[in] channel Channel through which to subscribe events.
  207. */
  208. __STATIC_INLINE void nrf_spis_subscribe_set(NRF_SPIS_Type * p_reg,
  209. nrf_spis_task_t task,
  210. uint8_t channel);
  211. /**
  212. * @brief Function for clearing the subscribe configuration for a given
  213. * SPIS task.
  214. *
  215. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  216. * @param[in] task Task for which to clear the configuration.
  217. */
  218. __STATIC_INLINE void nrf_spis_subscribe_clear(NRF_SPIS_Type * p_reg,
  219. nrf_spis_task_t task);
  220. /**
  221. * @brief Function for setting the publish configuration for a given
  222. * SPIS event.
  223. *
  224. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  225. * @param[in] event Event for which to set the configuration.
  226. * @param[in] channel Channel through which to publish the event.
  227. */
  228. __STATIC_INLINE void nrf_spis_publish_set(NRF_SPIS_Type * p_reg,
  229. nrf_spis_event_t event,
  230. uint8_t channel);
  231. /**
  232. * @brief Function for clearing the publish configuration for a given
  233. * SPIS event.
  234. *
  235. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  236. * @param[in] event Event for which to clear the configuration.
  237. */
  238. __STATIC_INLINE void nrf_spis_publish_clear(NRF_SPIS_Type * p_reg,
  239. nrf_spis_event_t event);
  240. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  241. /**
  242. * @brief Function for enabling the SPIS peripheral.
  243. *
  244. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  245. */
  246. __STATIC_INLINE void nrf_spis_enable(NRF_SPIS_Type * p_reg);
  247. /**
  248. * @brief Function for disabling the SPIS peripheral.
  249. *
  250. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  251. */
  252. __STATIC_INLINE void nrf_spis_disable(NRF_SPIS_Type * p_reg);
  253. /**
  254. * @brief Function for retrieving the SPIS semaphore status.
  255. *
  256. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  257. *
  258. * @returns Current semaphore status.
  259. */
  260. __STATIC_INLINE nrf_spis_semstat_t nrf_spis_semaphore_status_get(NRF_SPIS_Type * p_reg);
  261. /**
  262. * @brief Function for retrieving the SPIS status.
  263. *
  264. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  265. *
  266. * @returns Current SPIS status.
  267. */
  268. __STATIC_INLINE nrf_spis_status_mask_t nrf_spis_status_get(NRF_SPIS_Type * p_reg);
  269. /**
  270. * @brief Function for configuring SPIS pins.
  271. *
  272. * If a given signal is not needed, pass the @ref NRF_SPIS_PIN_NOT_CONNECTED
  273. * value instead of its pin number.
  274. *
  275. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  276. * @param[in] sck_pin SCK pin number.
  277. * @param[in] mosi_pin MOSI pin number.
  278. * @param[in] miso_pin MISO pin number.
  279. * @param[in] csn_pin CSN pin number.
  280. */
  281. __STATIC_INLINE void nrf_spis_pins_set(NRF_SPIS_Type * p_reg,
  282. uint32_t sck_pin,
  283. uint32_t mosi_pin,
  284. uint32_t miso_pin,
  285. uint32_t csn_pin);
  286. /**
  287. * @brief Function for setting the transmit buffer.
  288. *
  289. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  290. * @param[in] p_buffer Pointer to the buffer that contains the data to send.
  291. * @param[in] length Maximum number of data bytes to transmit.
  292. */
  293. __STATIC_INLINE void nrf_spis_tx_buffer_set(NRF_SPIS_Type * p_reg,
  294. uint8_t const * p_buffer,
  295. size_t length);
  296. /**
  297. * @brief Function for setting the receive buffer.
  298. *
  299. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  300. * @param[in] p_buffer Pointer to the buffer for received data.
  301. * @param[in] length Maximum number of data bytes to receive.
  302. */
  303. __STATIC_INLINE void nrf_spis_rx_buffer_set(NRF_SPIS_Type * p_reg,
  304. uint8_t * p_buffer,
  305. size_t length);
  306. /**
  307. * @brief Function for getting the number of bytes transmitted
  308. * in the last granted transaction.
  309. *
  310. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  311. *
  312. * @returns Number of bytes transmitted.
  313. */
  314. __STATIC_INLINE size_t nrf_spis_tx_amount_get(NRF_SPIS_Type const * p_reg);
  315. /**
  316. * @brief Function for getting the number of bytes received
  317. * in the last granted transaction.
  318. *
  319. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  320. *
  321. * @returns Number of bytes received.
  322. */
  323. __STATIC_INLINE size_t nrf_spis_rx_amount_get(NRF_SPIS_Type const * p_reg);
  324. /**
  325. * @brief Function for setting the SPI configuration.
  326. *
  327. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  328. * @param[in] spi_mode SPI mode.
  329. * @param[in] spi_bit_order SPI bit order.
  330. */
  331. __STATIC_INLINE void nrf_spis_configure(NRF_SPIS_Type * p_reg,
  332. nrf_spis_mode_t spi_mode,
  333. nrf_spis_bit_order_t spi_bit_order);
  334. /**
  335. * @brief Function for setting the default character.
  336. *
  337. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  338. * @param[in] def Default character that is clocked out in case of
  339. * an overflow of the RXD buffer.
  340. */
  341. __STATIC_INLINE void nrf_spis_def_set(NRF_SPIS_Type * p_reg,
  342. uint8_t def);
  343. /**
  344. * @brief Function for setting the over-read character.
  345. *
  346. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  347. * @param[in] orc Over-read character that is clocked out in case of
  348. * an over-read of the TXD buffer.
  349. */
  350. __STATIC_INLINE void nrf_spis_orc_set(NRF_SPIS_Type * p_reg,
  351. uint8_t orc);
  352. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  353. __STATIC_INLINE void nrf_spis_task_trigger(NRF_SPIS_Type * p_reg,
  354. nrf_spis_task_t task)
  355. {
  356. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  357. }
  358. __STATIC_INLINE uint32_t nrf_spis_task_address_get(NRF_SPIS_Type const * p_reg,
  359. nrf_spis_task_t task)
  360. {
  361. return (uint32_t)p_reg + (uint32_t)task;
  362. }
  363. __STATIC_INLINE void nrf_spis_event_clear(NRF_SPIS_Type * p_reg,
  364. nrf_spis_event_t event)
  365. {
  366. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  367. #if __CORTEX_M == 0x04
  368. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  369. (void)dummy;
  370. #endif
  371. }
  372. __STATIC_INLINE bool nrf_spis_event_check(NRF_SPIS_Type const * p_reg,
  373. nrf_spis_event_t event)
  374. {
  375. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  376. }
  377. __STATIC_INLINE uint32_t nrf_spis_event_address_get(NRF_SPIS_Type const * p_reg,
  378. nrf_spis_event_t event)
  379. {
  380. return (uint32_t)p_reg + (uint32_t)event;
  381. }
  382. __STATIC_INLINE void nrf_spis_shorts_enable(NRF_SPIS_Type * p_reg,
  383. uint32_t mask)
  384. {
  385. p_reg->SHORTS |= mask;
  386. }
  387. __STATIC_INLINE void nrf_spis_shorts_disable(NRF_SPIS_Type * p_reg,
  388. uint32_t mask)
  389. {
  390. p_reg->SHORTS &= ~(mask);
  391. }
  392. __STATIC_INLINE void nrf_spis_int_enable(NRF_SPIS_Type * p_reg,
  393. uint32_t mask)
  394. {
  395. p_reg->INTENSET = mask;
  396. }
  397. __STATIC_INLINE void nrf_spis_int_disable(NRF_SPIS_Type * p_reg,
  398. uint32_t mask)
  399. {
  400. p_reg->INTENCLR = mask;
  401. }
  402. __STATIC_INLINE bool nrf_spis_int_enable_check(NRF_SPIS_Type const * p_reg,
  403. nrf_spis_int_mask_t spis_int)
  404. {
  405. return (bool)(p_reg->INTENSET & spis_int);
  406. }
  407. #if defined(DPPI_PRESENT)
  408. __STATIC_INLINE void nrf_spis_subscribe_set(NRF_SPIS_Type * p_reg,
  409. nrf_spis_task_t task,
  410. uint8_t channel)
  411. {
  412. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  413. ((uint32_t)channel | SPIS_SUBSCRIBE_ACQUIRE_EN_Msk);
  414. }
  415. __STATIC_INLINE void nrf_spis_subscribe_clear(NRF_SPIS_Type * p_reg,
  416. nrf_spis_task_t task)
  417. {
  418. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  419. }
  420. __STATIC_INLINE void nrf_spis_publish_set(NRF_SPIS_Type * p_reg,
  421. nrf_spis_event_t event,
  422. uint8_t channel)
  423. {
  424. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  425. ((uint32_t)channel | SPIS_PUBLISH_END_EN_Msk);
  426. }
  427. __STATIC_INLINE void nrf_spis_publish_clear(NRF_SPIS_Type * p_reg,
  428. nrf_spis_event_t event)
  429. {
  430. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  431. }
  432. #endif // defined(DPPI_PRESENT)
  433. __STATIC_INLINE void nrf_spis_enable(NRF_SPIS_Type * p_reg)
  434. {
  435. p_reg->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos);
  436. }
  437. __STATIC_INLINE void nrf_spis_disable(NRF_SPIS_Type * p_reg)
  438. {
  439. p_reg->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos);
  440. }
  441. __STATIC_INLINE nrf_spis_semstat_t nrf_spis_semaphore_status_get(NRF_SPIS_Type * p_reg)
  442. {
  443. return (nrf_spis_semstat_t) ((p_reg->SEMSTAT & SPIS_SEMSTAT_SEMSTAT_Msk)
  444. >> SPIS_SEMSTAT_SEMSTAT_Pos);
  445. }
  446. __STATIC_INLINE nrf_spis_status_mask_t nrf_spis_status_get(NRF_SPIS_Type * p_reg)
  447. {
  448. return (nrf_spis_status_mask_t) p_reg->STATUS;
  449. }
  450. __STATIC_INLINE void nrf_spis_pins_set(NRF_SPIS_Type * p_reg,
  451. uint32_t sck_pin,
  452. uint32_t mosi_pin,
  453. uint32_t miso_pin,
  454. uint32_t csn_pin)
  455. {
  456. #if defined (NRF51)
  457. p_reg->PSELSCK = sck_pin;
  458. p_reg->PSELMOSI = mosi_pin;
  459. p_reg->PSELMISO = miso_pin;
  460. p_reg->PSELCSN = csn_pin;
  461. #else
  462. p_reg->PSEL.SCK = sck_pin;
  463. p_reg->PSEL.MOSI = mosi_pin;
  464. p_reg->PSEL.MISO = miso_pin;
  465. p_reg->PSEL.CSN = csn_pin;
  466. #endif
  467. }
  468. __STATIC_INLINE void nrf_spis_tx_buffer_set(NRF_SPIS_Type * p_reg,
  469. uint8_t const * p_buffer,
  470. size_t length)
  471. {
  472. #if defined (NRF51)
  473. p_reg->TXDPTR = (uint32_t)p_buffer;
  474. p_reg->MAXTX = length;
  475. #else
  476. p_reg->TXD.PTR = (uint32_t)p_buffer;
  477. p_reg->TXD.MAXCNT = length;
  478. #endif
  479. }
  480. __STATIC_INLINE void nrf_spis_rx_buffer_set(NRF_SPIS_Type * p_reg,
  481. uint8_t * p_buffer,
  482. size_t length)
  483. {
  484. #if defined (NRF51)
  485. p_reg->RXDPTR = (uint32_t)p_buffer;
  486. p_reg->MAXRX = length;
  487. #else
  488. p_reg->RXD.PTR = (uint32_t)p_buffer;
  489. p_reg->RXD.MAXCNT = length;
  490. #endif
  491. }
  492. __STATIC_INLINE size_t nrf_spis_tx_amount_get(NRF_SPIS_Type const * p_reg)
  493. {
  494. #if defined (NRF51)
  495. return p_reg->AMOUNTTX;
  496. #else
  497. return p_reg->TXD.AMOUNT;
  498. #endif
  499. }
  500. __STATIC_INLINE size_t nrf_spis_rx_amount_get(NRF_SPIS_Type const * p_reg)
  501. {
  502. #if defined (NRF51)
  503. return p_reg->AMOUNTRX;
  504. #else
  505. return p_reg->RXD.AMOUNT;
  506. #endif
  507. }
  508. __STATIC_INLINE void nrf_spis_configure(NRF_SPIS_Type * p_reg,
  509. nrf_spis_mode_t spi_mode,
  510. nrf_spis_bit_order_t spi_bit_order)
  511. {
  512. uint32_t config = (spi_bit_order == NRF_SPIS_BIT_ORDER_MSB_FIRST ?
  513. SPIS_CONFIG_ORDER_MsbFirst : SPIS_CONFIG_ORDER_LsbFirst);
  514. switch (spi_mode)
  515. {
  516. default:
  517. case NRF_SPIS_MODE_0:
  518. config |= (SPIS_CONFIG_CPOL_ActiveHigh << SPIS_CONFIG_CPOL_Pos) |
  519. (SPIS_CONFIG_CPHA_Leading << SPIS_CONFIG_CPHA_Pos);
  520. break;
  521. case NRF_SPIS_MODE_1:
  522. config |= (SPIS_CONFIG_CPOL_ActiveHigh << SPIS_CONFIG_CPOL_Pos) |
  523. (SPIS_CONFIG_CPHA_Trailing << SPIS_CONFIG_CPHA_Pos);
  524. break;
  525. case NRF_SPIS_MODE_2:
  526. config |= (SPIS_CONFIG_CPOL_ActiveLow << SPIS_CONFIG_CPOL_Pos) |
  527. (SPIS_CONFIG_CPHA_Leading << SPIS_CONFIG_CPHA_Pos);
  528. break;
  529. case NRF_SPIS_MODE_3:
  530. config |= (SPIS_CONFIG_CPOL_ActiveLow << SPIS_CONFIG_CPOL_Pos) |
  531. (SPIS_CONFIG_CPHA_Trailing << SPIS_CONFIG_CPHA_Pos);
  532. break;
  533. }
  534. p_reg->CONFIG = config;
  535. }
  536. __STATIC_INLINE void nrf_spis_orc_set(NRF_SPIS_Type * p_reg,
  537. uint8_t orc)
  538. {
  539. p_reg->ORC = orc;
  540. }
  541. __STATIC_INLINE void nrf_spis_def_set(NRF_SPIS_Type * p_reg,
  542. uint8_t def)
  543. {
  544. p_reg->DEF = def;
  545. }
  546. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  547. /** @} */
  548. #ifdef __cplusplus
  549. }
  550. #endif
  551. #endif // NRF_SPIS_H__