nrf_spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_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. /** @brief SPI events. */
  59. typedef enum
  60. {
  61. NRF_SPI_EVENT_READY = offsetof(NRF_SPI_Type, EVENTS_READY) ///< TXD byte sent and RXD byte received.
  62. } nrf_spi_event_t;
  63. /** @brief SPI interrupts. */
  64. typedef enum
  65. {
  66. NRF_SPI_INT_READY_MASK = SPI_INTENSET_READY_Msk, ///< Interrupt on READY event.
  67. NRF_SPI_ALL_INTS_MASK = SPI_INTENSET_READY_Msk ///< All SPI interrupts.
  68. } nrf_spi_int_mask_t;
  69. /** @brief SPI data rates. */
  70. typedef enum
  71. {
  72. NRF_SPI_FREQ_125K = SPI_FREQUENCY_FREQUENCY_K125, ///< 125 kbps.
  73. NRF_SPI_FREQ_250K = SPI_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  74. NRF_SPI_FREQ_500K = SPI_FREQUENCY_FREQUENCY_K500, ///< 500 kbps.
  75. NRF_SPI_FREQ_1M = SPI_FREQUENCY_FREQUENCY_M1, ///< 1 Mbps.
  76. NRF_SPI_FREQ_2M = SPI_FREQUENCY_FREQUENCY_M2, ///< 2 Mbps.
  77. NRF_SPI_FREQ_4M = SPI_FREQUENCY_FREQUENCY_M4, ///< 4 Mbps.
  78. // [conversion to 'int' needed to prevent compilers from complaining
  79. // that the provided value (0x80000000UL) is out of range of "int"]
  80. NRF_SPI_FREQ_8M = (int)SPI_FREQUENCY_FREQUENCY_M8 ///< 8 Mbps.
  81. } nrf_spi_frequency_t;
  82. /** @brief SPI modes. */
  83. typedef enum
  84. {
  85. NRF_SPI_MODE_0, ///< SCK active high, sample on leading edge of clock.
  86. NRF_SPI_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  87. NRF_SPI_MODE_2, ///< SCK active low, sample on leading edge of clock.
  88. NRF_SPI_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  89. } nrf_spi_mode_t;
  90. /** @brief SPI bit orders. */
  91. typedef enum
  92. {
  93. NRF_SPI_BIT_ORDER_MSB_FIRST = SPI_CONFIG_ORDER_MsbFirst, ///< Most significant bit shifted out first.
  94. NRF_SPI_BIT_ORDER_LSB_FIRST = SPI_CONFIG_ORDER_LsbFirst ///< Least significant bit shifted out first.
  95. } nrf_spi_bit_order_t;
  96. /**
  97. * @brief Function for clearing the specified SPI event.
  98. *
  99. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  100. * @param[in] event Event to be cleared.
  101. */
  102. __STATIC_INLINE void nrf_spi_event_clear(NRF_SPI_Type * p_reg,
  103. nrf_spi_event_t event);
  104. /**
  105. * @brief Function for retrieving the state of the SPI event.
  106. *
  107. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  108. * @param[in] event Event to be checked.
  109. *
  110. * @retval true The event has been generated.
  111. * @retval false The event has not been generated.
  112. */
  113. __STATIC_INLINE bool nrf_spi_event_check(NRF_SPI_Type * p_reg,
  114. nrf_spi_event_t event);
  115. /**
  116. * @brief Function for getting the address of the specified SPI event register.
  117. *
  118. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  119. * @param[in] event The specified event.
  120. *
  121. * @return Address of the specified event register.
  122. */
  123. __STATIC_INLINE uint32_t * nrf_spi_event_address_get(NRF_SPI_Type * p_reg,
  124. nrf_spi_event_t event);
  125. /**
  126. * @brief Function for enabling the specified interrupts.
  127. *
  128. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  129. * @param[in] mask Mask of interrupts to be enabled.
  130. */
  131. __STATIC_INLINE void nrf_spi_int_enable(NRF_SPI_Type * p_reg,
  132. uint32_t mask);
  133. /**
  134. * @brief Function for disabling the specified interrupts.
  135. *
  136. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  137. * @param[in] mask Mask of interrupts to be disabled.
  138. */
  139. __STATIC_INLINE void nrf_spi_int_disable(NRF_SPI_Type * p_reg,
  140. uint32_t mask);
  141. /**
  142. * @brief Function for retrieving the state of a given interrupt.
  143. *
  144. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  145. * @param[in] spi_int Interrupt to be checked.
  146. *
  147. * @retval true The interrupt is enabled.
  148. * @retval false The interrupt is not enabled.
  149. */
  150. __STATIC_INLINE bool nrf_spi_int_enable_check(NRF_SPI_Type * p_reg,
  151. nrf_spi_int_mask_t spi_int);
  152. /**
  153. * @brief Function for enabling the SPI peripheral.
  154. *
  155. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  156. */
  157. __STATIC_INLINE void nrf_spi_enable(NRF_SPI_Type * p_reg);
  158. /**
  159. * @brief Function for disabling the SPI peripheral.
  160. *
  161. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  162. */
  163. __STATIC_INLINE void nrf_spi_disable(NRF_SPI_Type * p_reg);
  164. /**
  165. * @brief Function for configuring SPI pins.
  166. *
  167. * If a given signal is not needed, pass the @ref NRF_SPI_PIN_NOT_CONNECTED
  168. * value instead of its pin number.
  169. *
  170. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  171. * @param[in] sck_pin SCK pin number.
  172. * @param[in] mosi_pin MOSI pin number.
  173. * @param[in] miso_pin MISO pin number.
  174. */
  175. __STATIC_INLINE void nrf_spi_pins_set(NRF_SPI_Type * p_reg,
  176. uint32_t sck_pin,
  177. uint32_t mosi_pin,
  178. uint32_t miso_pin);
  179. /**
  180. * @brief Function for writing data to the SPI transmitter register.
  181. *
  182. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  183. * @param[in] data TX data to send.
  184. */
  185. __STATIC_INLINE void nrf_spi_txd_set(NRF_SPI_Type * p_reg, uint8_t data);
  186. /**
  187. * @brief Function for reading data from the SPI receiver register.
  188. *
  189. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  190. *
  191. * @return RX data received.
  192. */
  193. __STATIC_INLINE uint8_t nrf_spi_rxd_get(NRF_SPI_Type * p_reg);
  194. /**
  195. * @brief Function for setting the SPI master data rate.
  196. *
  197. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  198. * @param[in] frequency SPI frequency.
  199. */
  200. __STATIC_INLINE void nrf_spi_frequency_set(NRF_SPI_Type * p_reg,
  201. nrf_spi_frequency_t frequency);
  202. /**
  203. * @brief Function for setting the SPI configuration.
  204. *
  205. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  206. * @param[in] spi_mode SPI mode.
  207. * @param[in] spi_bit_order SPI bit order.
  208. */
  209. __STATIC_INLINE void nrf_spi_configure(NRF_SPI_Type * p_reg,
  210. nrf_spi_mode_t spi_mode,
  211. nrf_spi_bit_order_t spi_bit_order);
  212. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  213. __STATIC_INLINE void nrf_spi_event_clear(NRF_SPI_Type * p_reg,
  214. nrf_spi_event_t event)
  215. {
  216. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  217. #if __CORTEX_M == 0x04
  218. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  219. (void)dummy;
  220. #endif
  221. }
  222. __STATIC_INLINE bool nrf_spi_event_check(NRF_SPI_Type * p_reg,
  223. nrf_spi_event_t event)
  224. {
  225. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  226. }
  227. __STATIC_INLINE uint32_t * nrf_spi_event_address_get(NRF_SPI_Type * p_reg,
  228. nrf_spi_event_t event)
  229. {
  230. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  231. }
  232. __STATIC_INLINE void nrf_spi_int_enable(NRF_SPI_Type * p_reg,
  233. uint32_t mask)
  234. {
  235. p_reg->INTENSET = mask;
  236. }
  237. __STATIC_INLINE void nrf_spi_int_disable(NRF_SPI_Type * p_reg,
  238. uint32_t mask)
  239. {
  240. p_reg->INTENCLR = mask;
  241. }
  242. __STATIC_INLINE bool nrf_spi_int_enable_check(NRF_SPI_Type * p_reg,
  243. nrf_spi_int_mask_t spi_int)
  244. {
  245. return (bool)(p_reg->INTENSET & spi_int);
  246. }
  247. __STATIC_INLINE void nrf_spi_enable(NRF_SPI_Type * p_reg)
  248. {
  249. p_reg->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
  250. }
  251. __STATIC_INLINE void nrf_spi_disable(NRF_SPI_Type * p_reg)
  252. {
  253. p_reg->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
  254. }
  255. __STATIC_INLINE void nrf_spi_pins_set(NRF_SPI_Type * p_reg,
  256. uint32_t sck_pin,
  257. uint32_t mosi_pin,
  258. uint32_t miso_pin)
  259. {
  260. #if defined(SPI_PSEL_SCK_CONNECT_Pos)
  261. p_reg->PSEL.SCK = sck_pin;
  262. #else
  263. p_reg->PSELSCK = sck_pin;
  264. #endif
  265. #if defined(SPI_PSEL_MOSI_CONNECT_Pos)
  266. p_reg->PSEL.MOSI = mosi_pin;
  267. #else
  268. p_reg->PSELMOSI = mosi_pin;
  269. #endif
  270. #if defined(SPI_PSEL_MISO_CONNECT_Pos)
  271. p_reg->PSEL.MISO = miso_pin;
  272. #else
  273. p_reg->PSELMISO = miso_pin;
  274. #endif
  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__