nrf_spis.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @defgroup nrf_spis_hal SPIS HAL
  14. * @{
  15. * @ingroup nrf_spis
  16. *
  17. * @brief Hardware access layer for accessing the SPIS peripheral.
  18. */
  19. #ifndef NRF_SPIS_H__
  20. #define NRF_SPIS_H__
  21. #include <stddef.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include "nrf.h"
  25. /**
  26. * @brief This value can be used as a parameter for the @ref nrf_spis_pins_set
  27. * function to specify that a given SPI signal (SCK, MOSI, or MISO)
  28. * shall not be connected to a physical pin.
  29. */
  30. #define NRF_SPIS_PIN_NOT_CONNECTED 0xFFFFFFFF
  31. /**
  32. * @brief SPIS tasks.
  33. */
  34. typedef enum
  35. {
  36. /*lint -save -e30*/
  37. NRF_SPIS_TASK_ACQUIRE = offsetof(NRF_SPIS_Type, TASKS_ACQUIRE), ///< Acquire SPI semaphore.
  38. NRF_SPIS_TASK_RELEASE = offsetof(NRF_SPIS_Type, TASKS_RELEASE), ///< Release SPI semaphore, enabling the SPI slave to acquire it.
  39. /*lint -restore*/
  40. } nrf_spis_task_t;
  41. /**
  42. * @brief SPIS events.
  43. */
  44. typedef enum
  45. {
  46. /*lint -save -e30*/
  47. NRF_SPIS_EVENT_END = offsetof(NRF_SPIS_Type, EVENTS_END), ///< Granted transaction completed.
  48. NRF_SPIS_EVENT_ACQUIRED = offsetof(NRF_SPIS_Type, EVENTS_ACQUIRED) ///< Semaphore acquired.
  49. /*lint -restore*/
  50. } nrf_spis_event_t;
  51. /**
  52. * @brief SPIS shortcuts.
  53. */
  54. typedef enum
  55. {
  56. NRF_SPIS_SHORT_END_ACQUIRE = SPIS_SHORTS_END_ACQUIRE_Msk ///< Shortcut between END event and ACQUIRE task.
  57. } nrf_spis_short_mask_t;
  58. /**
  59. * @brief SPIS interrupts.
  60. */
  61. typedef enum
  62. {
  63. NRF_SPIS_INT_END_MASK = SPIS_INTENSET_END_Msk, ///< Interrupt on END event.
  64. NRF_SPIS_INT_ACQUIRED_MASK = SPIS_INTENSET_ACQUIRED_Msk ///< Interrupt on ACQUIRED event.
  65. } nrf_spis_int_mask_t;
  66. /**
  67. * @brief SPI modes.
  68. */
  69. typedef enum
  70. {
  71. NRF_SPIS_MODE_0, ///< SCK active high, sample on leading edge of clock.
  72. NRF_SPIS_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  73. NRF_SPIS_MODE_2, ///< SCK active low, sample on leading edge of clock.
  74. NRF_SPIS_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  75. } nrf_spis_mode_t;
  76. /**
  77. * @brief SPI bit orders.
  78. */
  79. typedef enum
  80. {
  81. NRF_SPIS_BIT_ORDER_MSB_FIRST = SPIS_CONFIG_ORDER_MsbFirst, ///< Most significant bit shifted out first.
  82. NRF_SPIS_BIT_ORDER_LSB_FIRST = SPIS_CONFIG_ORDER_LsbFirst ///< Least significant bit shifted out first.
  83. } nrf_spis_bit_order_t;
  84. /**
  85. * @brief SPI semaphore status.
  86. */
  87. typedef enum
  88. {
  89. NRF_SPIS_SEMSTAT_FREE = 0, ///< Semaphore is free.
  90. NRF_SPIS_SEMSTAT_CPU = 1, ///< Semaphore is assigned to the CPU.
  91. NRF_SPIS_SEMSTAT_SPIS = 2, ///< Semaphore is assigned to the SPI slave.
  92. NRF_SPIS_SEMSTAT_CPUPENDING = 3 ///< Semaphore is assigned to the SPI, but a handover to the CPU is pending.
  93. } nrf_spis_semstat_t;
  94. /**
  95. * @brief SPIS status.
  96. */
  97. typedef enum
  98. {
  99. NRF_SPIS_STATUS_OVERREAD = SPIS_STATUS_OVERREAD_Msk, ///< TX buffer over-read detected and prevented.
  100. NRF_SPIS_STATUS_OVERFLOW = SPIS_STATUS_OVERFLOW_Msk ///< RX buffer overflow detected and prevented.
  101. } nrf_spis_status_mask_t;
  102. /**
  103. * @brief Function for activating a specific SPIS task.
  104. *
  105. * @param[in] p_spis SPIS instance.
  106. * @param[in] spis_task Task to activate.
  107. */
  108. __STATIC_INLINE void nrf_spis_task_trigger(NRF_SPIS_Type * p_spis,
  109. nrf_spis_task_t spis_task);
  110. /**
  111. * @brief Function for getting the address of a specific SPIS task register.
  112. *
  113. * @param[in] p_spis SPIS instance.
  114. * @param[in] spis_task Requested task.
  115. *
  116. * @return Address of the specified task register.
  117. */
  118. __STATIC_INLINE uint32_t nrf_spis_task_address_get(NRF_SPIS_Type const * p_spis,
  119. nrf_spis_task_t spis_task);
  120. /**
  121. * @brief Function for clearing a specific SPIS event.
  122. *
  123. * @param[in] p_spis SPIS instance.
  124. * @param[in] spis_event Event to clear.
  125. */
  126. __STATIC_INLINE void nrf_spis_event_clear(NRF_SPIS_Type * p_spis,
  127. nrf_spis_event_t spis_event);
  128. /**
  129. * @brief Function for checking the state of a specific SPIS event.
  130. *
  131. * @param[in] p_spis SPIS instance.
  132. * @param[in] spis_event Event to check.
  133. *
  134. * @retval true If the event is set.
  135. * @retval false If the event is not set.
  136. */
  137. __STATIC_INLINE bool nrf_spis_event_check(NRF_SPIS_Type const * p_spis,
  138. nrf_spis_event_t spis_event);
  139. /**
  140. * @brief Function for getting the address of a specific SPIS event register.
  141. *
  142. * @param[in] p_spis SPIS instance.
  143. * @param[in] spis_event Requested event.
  144. *
  145. * @return Address of the specified event register.
  146. */
  147. __STATIC_INLINE uint32_t nrf_spis_event_address_get(NRF_SPIS_Type const * p_spis,
  148. nrf_spis_event_t spis_event);
  149. /**
  150. * @brief Function for enabling specified shortcuts.
  151. *
  152. * @param[in] p_spis SPIS instance.
  153. * @param[in] spis_shorts_mask Shortcuts to enable.
  154. */
  155. __STATIC_INLINE void nrf_spis_shorts_enable(NRF_SPIS_Type * p_spis,
  156. uint32_t spis_shorts_mask);
  157. /**
  158. * @brief Function for disabling specified shortcuts.
  159. *
  160. * @param[in] p_spis SPIS instance.
  161. * @param[in] spis_shorts_mask Shortcuts to disable.
  162. */
  163. __STATIC_INLINE void nrf_spis_shorts_disable(NRF_SPIS_Type * p_spis,
  164. uint32_t spis_shorts_mask);
  165. /**
  166. * @brief Function for enabling specified interrupts.
  167. *
  168. * @param[in] p_spis SPIS instance.
  169. * @param[in] spis_int_mask Interrupts to enable.
  170. */
  171. __STATIC_INLINE void nrf_spis_int_enable(NRF_SPIS_Type * p_spis,
  172. uint32_t spis_int_mask);
  173. /**
  174. * @brief Function for disabling specified interrupts.
  175. *
  176. * @param[in] p_spis SPIS instance.
  177. * @param[in] spis_int_mask Interrupts to disable.
  178. */
  179. __STATIC_INLINE void nrf_spis_int_disable(NRF_SPIS_Type * p_spis,
  180. uint32_t spis_int_mask);
  181. /**
  182. * @brief Function for retrieving the state of a given interrupt.
  183. *
  184. * @param[in] p_spis SPIS instance.
  185. * @param[in] spis_int Interrupt to check.
  186. *
  187. * @retval true If the interrupt is enabled.
  188. * @retval false If the interrupt is not enabled.
  189. */
  190. __STATIC_INLINE bool nrf_spis_int_enable_check(NRF_SPIS_Type const * p_spis,
  191. nrf_spis_int_mask_t spis_int);
  192. /**
  193. * @brief Function for enabling the SPIS peripheral.
  194. *
  195. * @param[in] p_spis SPIS instance.
  196. */
  197. __STATIC_INLINE void nrf_spis_enable(NRF_SPIS_Type * p_spis);
  198. /**
  199. * @brief Function for disabling the SPIS peripheral.
  200. *
  201. * @param[in] p_spis SPIS instance.
  202. */
  203. __STATIC_INLINE void nrf_spis_disable(NRF_SPIS_Type * p_spis);
  204. /**
  205. * @brief Function for retrieving the SPIS semaphore status.
  206. *
  207. * @param[in] p_spis SPIS instance.
  208. *
  209. * @returns Current semaphore status.
  210. */
  211. __STATIC_INLINE nrf_spis_semstat_t nrf_spis_semaphore_status_get(NRF_SPIS_Type * p_spis);
  212. /**
  213. * @brief Function for retrieving the SPIS status.
  214. *
  215. * @param[in] p_spis SPIS instance.
  216. *
  217. * @returns Current SPIS status.
  218. */
  219. __STATIC_INLINE nrf_spis_status_mask_t nrf_spis_status_get(NRF_SPIS_Type * p_spis);
  220. /**
  221. * @brief Function for configuring SPIS pins.
  222. *
  223. * If a given signal is not needed, pass the @ref NRF_SPIS_PIN_NOT_CONNECTED
  224. * value instead of its pin number.
  225. *
  226. * @param[in] p_spis SPIS instance.
  227. * @param[in] sck_pin SCK pin number.
  228. * @param[in] mosi_pin MOSI pin number.
  229. * @param[in] miso_pin MISO pin number.
  230. * @param[in] csn_pin CSN pin number.
  231. */
  232. __STATIC_INLINE void nrf_spis_pins_set(NRF_SPIS_Type * p_spis,
  233. uint32_t sck_pin,
  234. uint32_t mosi_pin,
  235. uint32_t miso_pin,
  236. uint32_t csn_pin);
  237. /**
  238. * @brief Function for setting the transmit buffer.
  239. *
  240. * @param[in] p_spis SPIS instance.
  241. * @param[in] p_buffer Pointer to the buffer that contains the data to send.
  242. * @param[in] length Maximum number of data bytes to transmit.
  243. */
  244. __STATIC_INLINE void nrf_spis_tx_buffer_set(NRF_SPIS_Type * p_spis,
  245. uint8_t const * p_buffer,
  246. uint8_t length);
  247. /**
  248. * @brief Function for setting the receive buffer.
  249. *
  250. * @param[in] p_spis SPIS instance.
  251. * @param[in] p_buffer Pointer to the buffer for received data.
  252. * @param[in] length Maximum number of data bytes to receive.
  253. */
  254. __STATIC_INLINE void nrf_spis_rx_buffer_set(NRF_SPIS_Type * p_spis,
  255. uint8_t * p_buffer,
  256. uint8_t length);
  257. /**
  258. * @brief Function for getting the number of bytes transmitted
  259. * in the last granted transaction.
  260. *
  261. * @param[in] p_spis SPIS instance.
  262. *
  263. * @returns Number of bytes transmitted.
  264. */
  265. __STATIC_INLINE uint8_t nrf_spis_tx_amount_get(NRF_SPIS_Type const * p_spis);
  266. /**
  267. * @brief Function for getting the number of bytes received
  268. * in the last granted transaction.
  269. *
  270. * @param[in] p_spis SPIS instance.
  271. *
  272. * @returns Number of bytes received.
  273. */
  274. __STATIC_INLINE uint8_t nrf_spis_rx_amount_get(NRF_SPIS_Type const * p_spis);
  275. /**
  276. * @brief Function for setting the SPI configuration.
  277. *
  278. * @param[in] p_spis SPIS instance.
  279. * @param[in] spi_mode SPI mode.
  280. * @param[in] spi_bit_order SPI bit order.
  281. */
  282. __STATIC_INLINE void nrf_spis_configure(NRF_SPIS_Type * p_spis,
  283. nrf_spis_mode_t spi_mode,
  284. nrf_spis_bit_order_t spi_bit_order);
  285. /**
  286. * @brief Function for setting the default character.
  287. *
  288. * @param[in] p_spis SPIS instance.
  289. * @param[in] def Default character that is clocked out in case of
  290. * an overflow of the RXD buffer.
  291. */
  292. __STATIC_INLINE void nrf_spis_def_set(NRF_SPIS_Type * p_spis,
  293. uint8_t def);
  294. /**
  295. * @brief Function for setting the over-read character.
  296. *
  297. * @param[in] p_spis SPIS instance.
  298. * @param[in] orc Over-read character that is clocked out in case of
  299. * an over-read of the TXD buffer.
  300. */
  301. __STATIC_INLINE void nrf_spis_orc_set(NRF_SPIS_Type * p_spis,
  302. uint8_t orc);
  303. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  304. __STATIC_INLINE void nrf_spis_task_trigger(NRF_SPIS_Type * p_spis,
  305. nrf_spis_task_t spis_task)
  306. {
  307. *((volatile uint32_t *)((uint8_t *)p_spis + (uint32_t)spis_task)) = 0x1UL;
  308. }
  309. __STATIC_INLINE uint32_t nrf_spis_task_address_get(NRF_SPIS_Type const * p_spis,
  310. nrf_spis_task_t spis_task)
  311. {
  312. return (uint32_t)p_spis + (uint32_t)spis_task;
  313. }
  314. __STATIC_INLINE void nrf_spis_event_clear(NRF_SPIS_Type * p_spis,
  315. nrf_spis_event_t spis_event)
  316. {
  317. *((volatile uint32_t *)((uint8_t *)p_spis + (uint32_t)spis_event)) = 0x0UL;
  318. }
  319. __STATIC_INLINE bool nrf_spis_event_check(NRF_SPIS_Type const * p_spis,
  320. nrf_spis_event_t spis_event)
  321. {
  322. return (bool)*(volatile uint32_t *)((uint8_t *)p_spis + (uint32_t)spis_event);
  323. }
  324. __STATIC_INLINE uint32_t nrf_spis_event_address_get(NRF_SPIS_Type const * p_spis,
  325. nrf_spis_event_t spis_event)
  326. {
  327. return (uint32_t)p_spis + (uint32_t)spis_event;
  328. }
  329. __STATIC_INLINE void nrf_spis_shorts_enable(NRF_SPIS_Type * p_spis,
  330. uint32_t spis_shorts_mask)
  331. {
  332. p_spis->SHORTS |= spis_shorts_mask;
  333. }
  334. __STATIC_INLINE void nrf_spis_shorts_disable(NRF_SPIS_Type * p_spis,
  335. uint32_t spis_shorts_mask)
  336. {
  337. p_spis->SHORTS &= ~(spis_shorts_mask);
  338. }
  339. __STATIC_INLINE void nrf_spis_int_enable(NRF_SPIS_Type * p_spis,
  340. uint32_t spis_int_mask)
  341. {
  342. p_spis->INTENSET = spis_int_mask;
  343. }
  344. __STATIC_INLINE void nrf_spis_int_disable(NRF_SPIS_Type * p_spis,
  345. uint32_t spis_int_mask)
  346. {
  347. p_spis->INTENCLR = spis_int_mask;
  348. }
  349. __STATIC_INLINE bool nrf_spis_int_enable_check(NRF_SPIS_Type const * p_spis,
  350. nrf_spis_int_mask_t spis_int)
  351. {
  352. return (bool)(p_spis->INTENSET & spis_int);
  353. }
  354. __STATIC_INLINE void nrf_spis_enable(NRF_SPIS_Type * p_spis)
  355. {
  356. p_spis->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos);
  357. }
  358. __STATIC_INLINE void nrf_spis_disable(NRF_SPIS_Type * p_spis)
  359. {
  360. p_spis->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos);
  361. }
  362. __STATIC_INLINE nrf_spis_semstat_t nrf_spis_semaphore_status_get(NRF_SPIS_Type * p_spis)
  363. {
  364. return (nrf_spis_semstat_t) ((p_spis->SEMSTAT & SPIS_SEMSTAT_SEMSTAT_Msk)
  365. >> SPIS_SEMSTAT_SEMSTAT_Pos);
  366. }
  367. __STATIC_INLINE nrf_spis_status_mask_t nrf_spis_status_get(NRF_SPIS_Type * p_spis)
  368. {
  369. return (nrf_spis_status_mask_t) p_spis->STATUS;
  370. }
  371. __STATIC_INLINE void nrf_spis_pins_set(NRF_SPIS_Type * p_spis,
  372. uint32_t sck_pin,
  373. uint32_t mosi_pin,
  374. uint32_t miso_pin,
  375. uint32_t csn_pin)
  376. {
  377. p_spis->PSELSCK = sck_pin;
  378. p_spis->PSELMOSI = mosi_pin;
  379. p_spis->PSELMISO = miso_pin;
  380. p_spis->PSELCSN = csn_pin;
  381. }
  382. __STATIC_INLINE void nrf_spis_tx_buffer_set(NRF_SPIS_Type * p_spis,
  383. uint8_t const * p_buffer,
  384. uint8_t length)
  385. {
  386. p_spis->TXDPTR = (uint32_t)p_buffer;
  387. p_spis->MAXTX = length;
  388. }
  389. __STATIC_INLINE void nrf_spis_rx_buffer_set(NRF_SPIS_Type * p_spis,
  390. uint8_t * p_buffer,
  391. uint8_t length)
  392. {
  393. p_spis->RXDPTR = (uint32_t)p_buffer;
  394. p_spis->MAXRX = length;
  395. }
  396. __STATIC_INLINE uint8_t nrf_spis_tx_amount_get(NRF_SPIS_Type const * p_spis)
  397. {
  398. //return (uint8_t) p_spis->AMOUNTRX;
  399. return (uint8_t) p_spis->AMOUNTTX;
  400. }
  401. __STATIC_INLINE uint8_t nrf_spis_rx_amount_get(NRF_SPIS_Type const * p_spis)
  402. {
  403. //return (uint8_t) p_spis->AMOUNTTX;
  404. return (uint8_t) p_spis->AMOUNTRX;
  405. }
  406. __STATIC_INLINE void nrf_spis_configure(NRF_SPIS_Type * p_spis,
  407. nrf_spis_mode_t spi_mode,
  408. nrf_spis_bit_order_t spi_bit_order)
  409. {
  410. uint32_t config = (spi_bit_order == NRF_SPIS_BIT_ORDER_MSB_FIRST ?
  411. SPIS_CONFIG_ORDER_MsbFirst : SPIS_CONFIG_ORDER_LsbFirst);
  412. switch (spi_mode)
  413. {
  414. default:
  415. case NRF_SPIS_MODE_0:
  416. config |= (SPIS_CONFIG_CPOL_ActiveHigh << SPIS_CONFIG_CPOL_Pos) |
  417. (SPIS_CONFIG_CPHA_Leading << SPIS_CONFIG_CPHA_Pos);
  418. break;
  419. case NRF_SPIS_MODE_1:
  420. config |= (SPIS_CONFIG_CPOL_ActiveHigh << SPIS_CONFIG_CPOL_Pos) |
  421. (SPIS_CONFIG_CPHA_Trailing << SPIS_CONFIG_CPHA_Pos);
  422. break;
  423. case NRF_SPIS_MODE_2:
  424. config |= (SPIS_CONFIG_CPOL_ActiveLow << SPIS_CONFIG_CPOL_Pos) |
  425. (SPIS_CONFIG_CPHA_Leading << SPIS_CONFIG_CPHA_Pos);
  426. break;
  427. case NRF_SPIS_MODE_3:
  428. config |= (SPIS_CONFIG_CPOL_ActiveLow << SPIS_CONFIG_CPOL_Pos) |
  429. (SPIS_CONFIG_CPHA_Trailing << SPIS_CONFIG_CPHA_Pos);
  430. break;
  431. }
  432. p_spis->CONFIG = config;
  433. }
  434. __STATIC_INLINE void nrf_spis_orc_set(NRF_SPIS_Type * p_spis,
  435. uint8_t orc)
  436. {
  437. p_spis->ORC = orc;
  438. }
  439. __STATIC_INLINE void nrf_spis_def_set(NRF_SPIS_Type * p_spis,
  440. uint8_t def)
  441. {
  442. p_spis->DEF = def;
  443. }
  444. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  445. #endif // NRF_SPIS_H__
  446. /** @} */