nrf_spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_SPI_H__
  41. #define NRF_SPI_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_spi_hal SPI HAL
  48. * @{
  49. * @ingroup nrf_spi
  50. * @brief Hardware access layer for managing the SPI peripheral.
  51. */
  52. /**
  53. * @brief This value can be used as a parameter for the @ref nrf_spi_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_SPI_PIN_NOT_CONNECTED 0xFFFFFFFF
  58. /**
  59. * @brief SPI events.
  60. */
  61. typedef enum
  62. {
  63. /*lint -save -e30*/
  64. NRF_SPI_EVENT_READY = offsetof(NRF_SPI_Type, EVENTS_READY) ///< TXD byte sent and RXD byte received.
  65. /*lint -restore*/
  66. } nrf_spi_event_t;
  67. /**
  68. * @brief SPI interrupts.
  69. */
  70. typedef enum
  71. {
  72. NRF_SPI_INT_READY_MASK = SPI_INTENSET_READY_Msk, ///< Interrupt on READY event.
  73. NRF_SPI_ALL_INTS_MASK = SPI_INTENSET_READY_Msk ///< All SPI interrupts.
  74. } nrf_spi_int_mask_t;
  75. /**
  76. * @brief SPI data rates.
  77. */
  78. typedef enum
  79. {
  80. NRF_SPI_FREQ_125K = SPI_FREQUENCY_FREQUENCY_K125, ///< 125 kbps.
  81. NRF_SPI_FREQ_250K = SPI_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  82. NRF_SPI_FREQ_500K = SPI_FREQUENCY_FREQUENCY_K500, ///< 500 kbps.
  83. NRF_SPI_FREQ_1M = SPI_FREQUENCY_FREQUENCY_M1, ///< 1 Mbps.
  84. NRF_SPI_FREQ_2M = SPI_FREQUENCY_FREQUENCY_M2, ///< 2 Mbps.
  85. NRF_SPI_FREQ_4M = SPI_FREQUENCY_FREQUENCY_M4, ///< 4 Mbps.
  86. // [conversion to 'int' needed to prevent compilers from complaining
  87. // that the provided value (0x80000000UL) is out of range of "int"]
  88. NRF_SPI_FREQ_8M = (int)SPI_FREQUENCY_FREQUENCY_M8 ///< 8 Mbps.
  89. } nrf_spi_frequency_t;
  90. /**
  91. * @brief SPI modes.
  92. */
  93. typedef enum
  94. {
  95. NRF_SPI_MODE_0, ///< SCK active high, sample on leading edge of clock.
  96. NRF_SPI_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  97. NRF_SPI_MODE_2, ///< SCK active low, sample on leading edge of clock.
  98. NRF_SPI_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  99. } nrf_spi_mode_t;
  100. /**
  101. * @brief SPI bit orders.
  102. */
  103. typedef enum
  104. {
  105. NRF_SPI_BIT_ORDER_MSB_FIRST = SPI_CONFIG_ORDER_MsbFirst, ///< Most significant bit shifted out first.
  106. NRF_SPI_BIT_ORDER_LSB_FIRST = SPI_CONFIG_ORDER_LsbFirst ///< Least significant bit shifted out first.
  107. } nrf_spi_bit_order_t;
  108. /**
  109. * @brief Function for clearing a specific SPI event.
  110. *
  111. * @param[in] p_reg Pointer to the peripheral registers structure.
  112. * @param[in] spi_event Event to clear.
  113. */
  114. __STATIC_INLINE void nrf_spi_event_clear(NRF_SPI_Type * p_reg,
  115. nrf_spi_event_t spi_event);
  116. /**
  117. * @brief Function for checking the state of a specific SPI event.
  118. *
  119. * @param[in] p_reg Pointer to the peripheral registers structure.
  120. * @param[in] spi_event Event to check.
  121. *
  122. * @retval true If the event is set.
  123. * @retval false If the event is not set.
  124. */
  125. __STATIC_INLINE bool nrf_spi_event_check(NRF_SPI_Type * p_reg,
  126. nrf_spi_event_t spi_event);
  127. /**
  128. * @brief Function for getting the address of a specific SPI event register.
  129. *
  130. * @param[in] p_reg Pointer to the peripheral registers structure.
  131. * @param[in] spi_event Requested event.
  132. *
  133. * @return Address of the specified event register.
  134. */
  135. __STATIC_INLINE uint32_t * nrf_spi_event_address_get(NRF_SPI_Type * p_reg,
  136. nrf_spi_event_t spi_event);
  137. /**
  138. * @brief Function for enabling specified interrupts.
  139. *
  140. * @param[in] p_reg Pointer to the peripheral registers structure.
  141. * @param[in] spi_int_mask Interrupts to enable.
  142. */
  143. __STATIC_INLINE void nrf_spi_int_enable(NRF_SPI_Type * p_reg,
  144. uint32_t spi_int_mask);
  145. /**
  146. * @brief Function for disabling specified interrupts.
  147. *
  148. * @param[in] p_reg Pointer to the peripheral registers structure.
  149. * @param[in] spi_int_mask Interrupts to disable.
  150. */
  151. __STATIC_INLINE void nrf_spi_int_disable(NRF_SPI_Type * p_reg,
  152. uint32_t spi_int_mask);
  153. /**
  154. * @brief Function for retrieving the state of a given interrupt.
  155. *
  156. * @param[in] p_reg Pointer to the peripheral registers structure.
  157. * @param[in] spi_int Interrupt to check.
  158. *
  159. * @retval true If the interrupt is enabled.
  160. * @retval false If the interrupt is not enabled.
  161. */
  162. __STATIC_INLINE bool nrf_spi_int_enable_check(NRF_SPI_Type * p_reg,
  163. nrf_spi_int_mask_t spi_int);
  164. /**
  165. * @brief Function for enabling the SPI peripheral.
  166. *
  167. * @param[in] p_reg Pointer to the peripheral registers structure.
  168. */
  169. __STATIC_INLINE void nrf_spi_enable(NRF_SPI_Type * p_reg);
  170. /**
  171. * @brief Function for disabling the SPI peripheral.
  172. *
  173. * @param[in] p_reg Pointer to the peripheral registers structure.
  174. */
  175. __STATIC_INLINE void nrf_spi_disable(NRF_SPI_Type * p_reg);
  176. /**
  177. * @brief Function for configuring SPI pins.
  178. *
  179. * If a given signal is not needed, pass the @ref NRF_SPI_PIN_NOT_CONNECTED
  180. * value instead of its pin number.
  181. *
  182. * @param[in] p_reg Pointer to the peripheral registers structure.
  183. * @param[in] sck_pin SCK pin number.
  184. * @param[in] mosi_pin MOSI pin number.
  185. * @param[in] miso_pin MISO pin number.
  186. */
  187. __STATIC_INLINE void nrf_spi_pins_set(NRF_SPI_Type * p_reg,
  188. uint32_t sck_pin,
  189. uint32_t mosi_pin,
  190. uint32_t miso_pin);
  191. /**
  192. * @brief Function for writing data to the SPI transmitter register.
  193. *
  194. * @param[in] p_reg Pointer to the peripheral registers structure.
  195. * @param[in] data TX data to send.
  196. */
  197. __STATIC_INLINE void nrf_spi_txd_set(NRF_SPI_Type * p_reg, uint8_t data);
  198. /**
  199. * @brief Function for reading data from the SPI receiver register.
  200. *
  201. * @param[in] p_reg Pointer to the peripheral registers structure.
  202. *
  203. * @return RX data received.
  204. */
  205. __STATIC_INLINE uint8_t nrf_spi_rxd_get(NRF_SPI_Type * p_reg);
  206. /**
  207. * @brief Function for setting the SPI master data rate.
  208. *
  209. * @param[in] p_reg Pointer to the peripheral registers structure.
  210. * @param[in] frequency SPI frequency.
  211. */
  212. __STATIC_INLINE void nrf_spi_frequency_set(NRF_SPI_Type * p_reg,
  213. nrf_spi_frequency_t frequency);
  214. /**
  215. * @brief Function for setting the SPI configuration.
  216. *
  217. * @param[in] p_reg Pointer to the peripheral registers structure.
  218. * @param[in] spi_mode SPI mode.
  219. * @param[in] spi_bit_order SPI bit order.
  220. */
  221. __STATIC_INLINE void nrf_spi_configure(NRF_SPI_Type * p_reg,
  222. nrf_spi_mode_t spi_mode,
  223. nrf_spi_bit_order_t spi_bit_order);
  224. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  225. __STATIC_INLINE void nrf_spi_event_clear(NRF_SPI_Type * p_reg,
  226. nrf_spi_event_t spi_event)
  227. {
  228. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spi_event)) = 0x0UL;
  229. #if __CORTEX_M == 0x04
  230. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spi_event));
  231. (void)dummy;
  232. #endif
  233. }
  234. __STATIC_INLINE bool nrf_spi_event_check(NRF_SPI_Type * p_reg,
  235. nrf_spi_event_t spi_event)
  236. {
  237. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)spi_event);
  238. }
  239. __STATIC_INLINE uint32_t * nrf_spi_event_address_get(NRF_SPI_Type * p_reg,
  240. nrf_spi_event_t spi_event)
  241. {
  242. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)spi_event);
  243. }
  244. __STATIC_INLINE void nrf_spi_int_enable(NRF_SPI_Type * p_reg,
  245. uint32_t spi_int_mask)
  246. {
  247. p_reg->INTENSET = spi_int_mask;
  248. }
  249. __STATIC_INLINE void nrf_spi_int_disable(NRF_SPI_Type * p_reg,
  250. uint32_t spi_int_mask)
  251. {
  252. p_reg->INTENCLR = spi_int_mask;
  253. }
  254. __STATIC_INLINE bool nrf_spi_int_enable_check(NRF_SPI_Type * p_reg,
  255. nrf_spi_int_mask_t spi_int)
  256. {
  257. return (bool)(p_reg->INTENSET & spi_int);
  258. }
  259. __STATIC_INLINE void nrf_spi_enable(NRF_SPI_Type * p_reg)
  260. {
  261. p_reg->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
  262. }
  263. __STATIC_INLINE void nrf_spi_disable(NRF_SPI_Type * p_reg)
  264. {
  265. p_reg->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
  266. }
  267. __STATIC_INLINE void nrf_spi_pins_set(NRF_SPI_Type * p_reg,
  268. uint32_t sck_pin,
  269. uint32_t mosi_pin,
  270. uint32_t miso_pin)
  271. {
  272. p_reg->PSELSCK = sck_pin;
  273. p_reg->PSELMOSI = mosi_pin;
  274. p_reg->PSELMISO = miso_pin;
  275. }
  276. __STATIC_INLINE void nrf_spi_txd_set(NRF_SPI_Type * p_reg, uint8_t data)
  277. {
  278. p_reg->TXD = data;
  279. }
  280. __STATIC_INLINE uint8_t nrf_spi_rxd_get(NRF_SPI_Type * p_reg)
  281. {
  282. return p_reg->RXD;
  283. }
  284. __STATIC_INLINE void nrf_spi_frequency_set(NRF_SPI_Type * p_reg,
  285. nrf_spi_frequency_t frequency)
  286. {
  287. p_reg->FREQUENCY = frequency;
  288. }
  289. __STATIC_INLINE void nrf_spi_configure(NRF_SPI_Type * p_reg,
  290. nrf_spi_mode_t spi_mode,
  291. nrf_spi_bit_order_t spi_bit_order)
  292. {
  293. uint32_t config = (spi_bit_order == NRF_SPI_BIT_ORDER_MSB_FIRST ?
  294. SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst);
  295. switch (spi_mode)
  296. {
  297. default:
  298. case NRF_SPI_MODE_0:
  299. config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos) |
  300. (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos);
  301. break;
  302. case NRF_SPI_MODE_1:
  303. config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos) |
  304. (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos);
  305. break;
  306. case NRF_SPI_MODE_2:
  307. config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos) |
  308. (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos);
  309. break;
  310. case NRF_SPI_MODE_3:
  311. config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos) |
  312. (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos);
  313. break;
  314. }
  315. p_reg->CONFIG = config;
  316. }
  317. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  318. /** @} */
  319. #ifdef __cplusplus
  320. }
  321. #endif
  322. #endif // NRF_SPI_H__