nrf_spi.h 9.9 KB

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