nrf_twi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #ifndef NRF_TWI_H__
  13. #define NRF_TWI_H__
  14. /**
  15. * @defgroup nrf_twi_hal TWI HAL
  16. * @{
  17. * @ingroup nrf_twi_master
  18. *
  19. * @brief Hardware access layer for managing the TWI peripheral.
  20. */
  21. #include <stdint.h>
  22. #include <stddef.h>
  23. #include <stdbool.h>
  24. #include "nrf.h"
  25. /**
  26. * @brief TWI tasks.
  27. */
  28. typedef enum
  29. {
  30. /*lint -save -e30*/
  31. NRF_TWI_TASK_STARTRX = offsetof(NRF_TWI_Type, TASKS_STARTRX), ///< Start TWI receive sequence.
  32. NRF_TWI_TASK_STARTTX = offsetof(NRF_TWI_Type, TASKS_STARTTX), ///< Start TWI transmit sequence.
  33. NRF_TWI_TASK_STOP = offsetof(NRF_TWI_Type, TASKS_STOP), ///< Stop TWI transaction.
  34. NRF_TWI_TASK_SUSPEND = offsetof(NRF_TWI_Type, TASKS_SUSPEND), ///< Suspend TWI transaction.
  35. NRF_TWI_TASK_RESUME = offsetof(NRF_TWI_Type, TASKS_RESUME) ///< Resume TWI transaction.
  36. /*lint -restore*/
  37. } nrf_twi_task_t;
  38. /**
  39. * @brief TWI events.
  40. */
  41. typedef enum
  42. {
  43. /*lint -save -e30*/
  44. NRF_TWI_EVENT_STOPPED = offsetof(NRF_TWI_Type, EVENTS_STOPPED), ///< TWI stopped.
  45. NRF_TWI_EVENT_RXDREADY = offsetof(NRF_TWI_Type, EVENTS_RXDREADY), ///< TWI RXD byte received.
  46. NRF_TWI_EVENT_TXDSENT = offsetof(NRF_TWI_Type, EVENTS_TXDSENT), ///< TWI TXD byte sent.
  47. NRF_TWI_EVENT_ERROR = offsetof(NRF_TWI_Type, EVENTS_ERROR), ///< TWI error.
  48. NRF_TWI_EVENT_BB = offsetof(NRF_TWI_Type, EVENTS_BB), ///< TWI byte boundary, generated before each byte that is sent or received.
  49. NRF_TWI_EVENT_SUSPENDED = offsetof(NRF_TWI_Type, EVENTS_SUSPENDED) ///< TWI entered the suspended state.
  50. /*lint -restore*/
  51. } nrf_twi_event_t;
  52. /**
  53. * @brief TWI shortcuts.
  54. */
  55. typedef enum
  56. {
  57. NRF_TWI_SHORT_BB_SUSPEND_MASK = TWI_SHORTS_BB_SUSPEND_Msk, ///< Shortcut between BB event and SUSPEND task.
  58. NRF_TWI_SHORT_BB_STOP_MASK = TWI_SHORTS_BB_STOP_Msk, ///< Shortcut between BB event and STOP task.
  59. } nrf_twi_short_mask_t;
  60. /**
  61. * @brief TWI interrupts.
  62. */
  63. typedef enum
  64. {
  65. NRF_TWI_INT_STOPPED_MASK = TWI_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  66. NRF_TWI_INT_RXDREADY_MASK = TWI_INTENSET_RXDREADY_Msk, ///< Interrupt on RXDREADY event.
  67. NRF_TWI_INT_TXDSENT_MASK = TWI_INTENSET_TXDSENT_Msk, ///< Interrupt on TXDSENT event.
  68. NRF_TWI_INT_ERROR_MASK = TWI_INTENSET_ERROR_Msk, ///< Interrupt on ERROR event.
  69. NRF_TWI_INT_BB_MASK = TWI_INTENSET_BB_Msk, ///< Interrupt on BB event.
  70. NRF_TWI_INT_SUSPENDED_MASK = TWI_INTENSET_SUSPENDED_Msk ///< Interrupt on SUSPENDED event.
  71. } nrf_twi_int_mask_t;
  72. /**
  73. * @brief TWI error source.
  74. */
  75. typedef enum
  76. {
  77. NRF_TWI_ERROR_ADDRESS_NACK = TWI_ERRORSRC_ANACK_Msk, ///< NACK received after sending the address.
  78. NRF_TWI_ERROR_DATA_NACK = TWI_ERRORSRC_DNACK_Msk, ///< NACK received after sending a data byte.
  79. NRF_TWI_ERROR_OVERRUN = TWI_ERRORSRC_OVERRUN_Msk ///< Overrun error.
  80. /**< A new byte was received before the previous byte was read
  81. * from the RXD register (previous data is lost). */
  82. } nrf_twi_error_t;
  83. /**
  84. * @brief TWI master clock frequency.
  85. */
  86. typedef enum
  87. {
  88. NRF_TWI_FREQ_100K = TWI_FREQUENCY_FREQUENCY_K100, ///< 100 kbps.
  89. NRF_TWI_FREQ_250K = TWI_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  90. NRF_TWI_FREQ_400K = TWI_FREQUENCY_FREQUENCY_K400 ///< 400 kbps.
  91. } nrf_twi_frequency_t;
  92. /**
  93. * @brief Function for activating a specific TWI task.
  94. *
  95. * @param[in] p_twi TWI instance.
  96. * @param[in] task Task to activate.
  97. */
  98. __STATIC_INLINE void nrf_twi_task_trigger(NRF_TWI_Type * p_twi,
  99. nrf_twi_task_t task);
  100. /**
  101. * @brief Function for getting the address of a specific TWI task register.
  102. *
  103. * @param[in] p_twi TWI instance.
  104. * @param[in] task Requested task.
  105. *
  106. * @return Address of the specified task register.
  107. */
  108. __STATIC_INLINE uint32_t * nrf_twi_task_address_get(NRF_TWI_Type * p_twi,
  109. nrf_twi_task_t task);
  110. /**
  111. * @brief Function for clearing a specific TWI event.
  112. *
  113. * @param[in] p_twi TWI instance.
  114. * @param[in] event Event to clear.
  115. */
  116. __STATIC_INLINE void nrf_twi_event_clear(NRF_TWI_Type * p_twi,
  117. nrf_twi_event_t event);
  118. /**
  119. * @brief Function for checking the state of a specific event.
  120. *
  121. * @param[in] p_twi TWI instance.
  122. * @param[in] event Event to check.
  123. *
  124. * @retval true If the event is set.
  125. * @retval false If the event is not set.
  126. */
  127. __STATIC_INLINE bool nrf_twi_event_check(NRF_TWI_Type * p_twi,
  128. nrf_twi_event_t event);
  129. /**
  130. * @brief Function for getting the address of a specific TWI event register.
  131. *
  132. * @param[in] p_twi TWI instance.
  133. * @param[in] event Requested event.
  134. *
  135. * @return Address of the specified event register.
  136. */
  137. __STATIC_INLINE uint32_t * nrf_twi_event_address_get(NRF_TWI_Type * p_twi,
  138. nrf_twi_event_t event);
  139. /**
  140. * @brief Function for enabling specified shortcuts.
  141. *
  142. * @param[in] p_twi TWI instance.
  143. * @param[in] shorts_mask Shortcuts to enable.
  144. */
  145. __STATIC_INLINE void nrf_twi_shorts_enable(NRF_TWI_Type * p_twi,
  146. uint32_t shorts_mask);
  147. /**
  148. * @brief Function for disabling specified shortcuts.
  149. *
  150. * @param[in] p_twi TWI instance.
  151. * @param[in] shorts_mask Shortcuts to disable.
  152. */
  153. __STATIC_INLINE void nrf_twi_shorts_disable(NRF_TWI_Type * p_twi,
  154. uint32_t shorts_mask);
  155. /**
  156. * @brief Function for enabling specified interrupts.
  157. *
  158. * @param[in] p_twi TWI instance.
  159. * @param[in] int_mask Interrupts to enable.
  160. */
  161. __STATIC_INLINE void nrf_twi_int_enable(NRF_TWI_Type * p_twi,
  162. uint32_t int_mask);
  163. /**
  164. * @brief Function for disabling specified interrupts.
  165. *
  166. * @param[in] p_twi TWI instance.
  167. * @param[in] int_mask Interrupts to disable.
  168. */
  169. __STATIC_INLINE void nrf_twi_int_disable(NRF_TWI_Type * p_twi,
  170. uint32_t int_mask);
  171. /**
  172. * @brief Function for retrieving the state of a given interrupt.
  173. *
  174. * @param[in] p_twi TWI instance.
  175. * @param[in] int_mask Interrupt to check.
  176. *
  177. * @retval true If the interrupt is enabled.
  178. * @retval false If the interrupt is not enabled.
  179. */
  180. __STATIC_INLINE bool nrf_twi_int_enable_check(NRF_TWI_Type * p_twi,
  181. nrf_twi_int_mask_t int_mask);
  182. /**
  183. * @brief Function for enabling the TWI peripheral.
  184. *
  185. * @param[in] p_twi TWI instance.
  186. */
  187. __STATIC_INLINE void nrf_twi_enable(NRF_TWI_Type * p_twi);
  188. /**
  189. * @brief Function for disabling the TWI peripheral.
  190. *
  191. * @param[in] p_twi TWI instance.
  192. */
  193. __STATIC_INLINE void nrf_twi_disable(NRF_TWI_Type * p_twi);
  194. /**
  195. * @brief Function for configuring TWI pins.
  196. *
  197. *
  198. * @param[in] p_twi TWI instance.
  199. * @param[in] scl_pin SCL pin number.
  200. * @param[in] sda_pin SDA pin number.
  201. */
  202. __STATIC_INLINE void nrf_twi_pins_set(NRF_TWI_Type * p_twi,
  203. uint32_t scl_pin,
  204. uint32_t sda_pin);
  205. /**
  206. * @brief Function for setting the TWI master clock frequency.
  207. *
  208. * @param[in] p_twi TWI instance.
  209. * @param[in] frequency TWI frequency.
  210. */
  211. __STATIC_INLINE void nrf_twi_frequency_set(NRF_TWI_Type * p_twi,
  212. nrf_twi_frequency_t frequency);
  213. /**
  214. * @brief Function for checking the TWI error source.
  215. *
  216. * The error flags are cleared after reading.
  217. *
  218. * @param[in] p_twi TWI instance.
  219. *
  220. * @return Mask with error source flags.
  221. */
  222. __STATIC_INLINE uint32_t nrf_twi_errorsrc_get_and_clear(NRF_TWI_Type * p_twi);
  223. /**
  224. * @brief Function for setting the address to be used in TWI transfers.
  225. *
  226. * @param[in] p_twi TWI instance.
  227. * @param[in] address Address to be used in transfers.
  228. */
  229. __STATIC_INLINE void nrf_twi_address_set(NRF_TWI_Type * p_twi, uint8_t address);
  230. /**
  231. * @brief Function for reading data received by TWI.
  232. *
  233. * @param[in] p_twi TWI instance.
  234. *
  235. * @return Received data.
  236. */
  237. __STATIC_INLINE uint8_t nrf_twi_rxd_get(NRF_TWI_Type * p_twi);
  238. /**
  239. * @brief Function for writing data to be transmitted by TWI.
  240. *
  241. * @param[in] p_twi TWI instance.
  242. * @param[in] data Data to be transmitted.
  243. */
  244. __STATIC_INLINE void nrf_twi_txd_set(NRF_TWI_Type * p_twi, uint8_t data);
  245. __STATIC_INLINE void nrf_twi_shorts_set(NRF_TWI_Type * p_twi,
  246. uint32_t shorts_mask);
  247. /**
  248. * @}
  249. */
  250. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  251. __STATIC_INLINE void nrf_twi_task_trigger(NRF_TWI_Type * p_twi,
  252. nrf_twi_task_t task)
  253. {
  254. *((volatile uint32_t *)((uint8_t *)p_twi + (uint32_t)task)) = 0x1UL;
  255. }
  256. __STATIC_INLINE uint32_t * nrf_twi_task_address_get(NRF_TWI_Type * p_twi,
  257. nrf_twi_task_t task)
  258. {
  259. return (uint32_t *)((uint8_t *)p_twi + (uint32_t)task);
  260. }
  261. __STATIC_INLINE void nrf_twi_event_clear(NRF_TWI_Type * p_twi,
  262. nrf_twi_event_t event)
  263. {
  264. *((volatile uint32_t *)((uint8_t *)p_twi + (uint32_t)event)) = 0x0UL;
  265. }
  266. __STATIC_INLINE bool nrf_twi_event_check(NRF_TWI_Type * p_twi,
  267. nrf_twi_event_t event)
  268. {
  269. return (bool)*(volatile uint32_t *)((uint8_t *)p_twi + (uint32_t)event);
  270. }
  271. __STATIC_INLINE uint32_t * nrf_twi_event_address_get(NRF_TWI_Type * p_twi,
  272. nrf_twi_event_t event)
  273. {
  274. return (uint32_t *)((uint8_t *)p_twi + (uint32_t)event);
  275. }
  276. __STATIC_INLINE void nrf_twi_shorts_enable(NRF_TWI_Type * p_twi,
  277. uint32_t shorts_mask)
  278. {
  279. p_twi->SHORTS |= shorts_mask;
  280. }
  281. __STATIC_INLINE void nrf_twi_shorts_disable(NRF_TWI_Type * p_twi,
  282. uint32_t shorts_mask)
  283. {
  284. p_twi->SHORTS &= ~(shorts_mask);
  285. }
  286. __STATIC_INLINE void nrf_twi_int_enable(NRF_TWI_Type * p_twi,
  287. uint32_t int_mask)
  288. {
  289. p_twi->INTENSET = int_mask;
  290. }
  291. __STATIC_INLINE void nrf_twi_int_disable(NRF_TWI_Type * p_twi,
  292. uint32_t int_mask)
  293. {
  294. p_twi->INTENCLR = int_mask;
  295. }
  296. __STATIC_INLINE bool nrf_twi_int_enable_check(NRF_TWI_Type * p_twi,
  297. nrf_twi_int_mask_t int_mask)
  298. {
  299. return (bool)(p_twi->INTENSET & int_mask);
  300. }
  301. __STATIC_INLINE void nrf_twi_enable(NRF_TWI_Type * p_twi)
  302. {
  303. p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
  304. }
  305. __STATIC_INLINE void nrf_twi_disable(NRF_TWI_Type * p_twi)
  306. {
  307. p_twi->ENABLE = (TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos);
  308. }
  309. __STATIC_INLINE void nrf_twi_pins_set(NRF_TWI_Type * p_twi,
  310. uint32_t scl_pin,
  311. uint32_t sda_pin)
  312. {
  313. p_twi->PSELSCL = scl_pin;
  314. p_twi->PSELSDA = sda_pin;
  315. }
  316. __STATIC_INLINE void nrf_twi_frequency_set(NRF_TWI_Type * p_twi,
  317. nrf_twi_frequency_t frequency)
  318. {
  319. p_twi->FREQUENCY = frequency;
  320. }
  321. __STATIC_INLINE uint32_t nrf_twi_errorsrc_get_and_clear(NRF_TWI_Type * p_twi)
  322. {
  323. uint32_t error_source = p_twi->ERRORSRC;
  324. // [error flags are cleared by writing '1' on their position]
  325. p_twi->ERRORSRC = error_source;
  326. return error_source;
  327. }
  328. __STATIC_INLINE void nrf_twi_address_set(NRF_TWI_Type * p_twi, uint8_t address)
  329. {
  330. p_twi->ADDRESS = address;
  331. }
  332. __STATIC_INLINE uint8_t nrf_twi_rxd_get(NRF_TWI_Type * p_twi)
  333. {
  334. return (uint8_t)p_twi->RXD;
  335. }
  336. __STATIC_INLINE void nrf_twi_txd_set(NRF_TWI_Type * p_twi, uint8_t data)
  337. {
  338. p_twi->TXD = data;
  339. }
  340. __STATIC_INLINE void nrf_twi_shorts_set(NRF_TWI_Type * p_twi,
  341. uint32_t shorts_mask)
  342. {
  343. p_twi->SHORTS = shorts_mask;
  344. }
  345. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  346. #endif // NRF_TWI_H__