nrf_twi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_TWI_H__
  41. #define NRF_TWI_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_twi_hal TWI HAL
  48. * @{
  49. * @ingroup nrf_twi
  50. * @brief Hardware access layer for managing the TWI peripheral.
  51. */
  52. /** @brief TWI tasks. */
  53. typedef enum
  54. {
  55. NRF_TWI_TASK_STARTRX = offsetof(NRF_TWI_Type, TASKS_STARTRX), ///< Start TWI receive sequence.
  56. NRF_TWI_TASK_STARTTX = offsetof(NRF_TWI_Type, TASKS_STARTTX), ///< Start TWI transmit sequence.
  57. NRF_TWI_TASK_STOP = offsetof(NRF_TWI_Type, TASKS_STOP), ///< Stop TWI transaction.
  58. NRF_TWI_TASK_SUSPEND = offsetof(NRF_TWI_Type, TASKS_SUSPEND), ///< Suspend TWI transaction.
  59. NRF_TWI_TASK_RESUME = offsetof(NRF_TWI_Type, TASKS_RESUME) ///< Resume TWI transaction.
  60. } nrf_twi_task_t;
  61. /** @brief TWI events. */
  62. typedef enum
  63. {
  64. NRF_TWI_EVENT_STOPPED = offsetof(NRF_TWI_Type, EVENTS_STOPPED), ///< TWI stopped.
  65. NRF_TWI_EVENT_RXDREADY = offsetof(NRF_TWI_Type, EVENTS_RXDREADY), ///< TWI RXD byte received.
  66. NRF_TWI_EVENT_TXDSENT = offsetof(NRF_TWI_Type, EVENTS_TXDSENT), ///< TWI TXD byte sent.
  67. NRF_TWI_EVENT_ERROR = offsetof(NRF_TWI_Type, EVENTS_ERROR), ///< TWI error.
  68. NRF_TWI_EVENT_BB = offsetof(NRF_TWI_Type, EVENTS_BB), ///< TWI byte boundary, generated before each byte that is sent or received.
  69. NRF_TWI_EVENT_SUSPENDED = offsetof(NRF_TWI_Type, EVENTS_SUSPENDED) ///< TWI entered the suspended state.
  70. } nrf_twi_event_t;
  71. /** @brief TWI shortcuts. */
  72. typedef enum
  73. {
  74. NRF_TWI_SHORT_BB_SUSPEND_MASK = TWI_SHORTS_BB_SUSPEND_Msk, ///< Shortcut between BB event and SUSPEND task.
  75. NRF_TWI_SHORT_BB_STOP_MASK = TWI_SHORTS_BB_STOP_Msk, ///< Shortcut between BB event and STOP task.
  76. NRF_TWI_ALL_SHORTS_MASK = TWI_SHORTS_BB_SUSPEND_Msk |
  77. TWI_SHORTS_BB_STOP_Msk ///< All TWI shortcuts.
  78. } nrf_twi_short_mask_t;
  79. /** @brief TWI interrupts. */
  80. typedef enum
  81. {
  82. NRF_TWI_INT_STOPPED_MASK = TWI_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  83. NRF_TWI_INT_RXDREADY_MASK = TWI_INTENSET_RXDREADY_Msk, ///< Interrupt on RXDREADY event.
  84. NRF_TWI_INT_TXDSENT_MASK = TWI_INTENSET_TXDSENT_Msk, ///< Interrupt on TXDSENT event.
  85. NRF_TWI_INT_ERROR_MASK = TWI_INTENSET_ERROR_Msk, ///< Interrupt on ERROR event.
  86. NRF_TWI_INT_BB_MASK = TWI_INTENSET_BB_Msk, ///< Interrupt on BB event.
  87. NRF_TWI_INT_SUSPENDED_MASK = TWI_INTENSET_SUSPENDED_Msk, ///< Interrupt on SUSPENDED event.
  88. NRF_TWI_ALL_INTS_MASK = TWI_INTENSET_STOPPED_Msk |
  89. TWI_INTENSET_RXDREADY_Msk |
  90. TWI_INTENSET_TXDSENT_Msk |
  91. TWI_INTENSET_ERROR_Msk |
  92. TWI_INTENSET_BB_Msk |
  93. TWI_INTENSET_SUSPENDED_Msk ///< All TWI interrupts.
  94. } nrf_twi_int_mask_t;
  95. /** @brief TWI error source. */
  96. typedef enum
  97. {
  98. NRF_TWI_ERROR_ADDRESS_NACK = TWI_ERRORSRC_ANACK_Msk, ///< NACK received after sending the address.
  99. NRF_TWI_ERROR_DATA_NACK = TWI_ERRORSRC_DNACK_Msk, ///< NACK received after sending a data byte.
  100. NRF_TWI_ERROR_OVERRUN = TWI_ERRORSRC_OVERRUN_Msk ///< Overrun error.
  101. /**< A new byte was received before the previous byte was read
  102. * from the RXD register (previous data is lost). */
  103. } nrf_twi_error_t;
  104. /** @brief TWI master clock frequency. */
  105. typedef enum
  106. {
  107. NRF_TWI_FREQ_100K = TWI_FREQUENCY_FREQUENCY_K100, ///< 100 kbps.
  108. NRF_TWI_FREQ_250K = TWI_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  109. NRF_TWI_FREQ_400K = TWI_FREQUENCY_FREQUENCY_K400 ///< 400 kbps.
  110. } nrf_twi_frequency_t;
  111. /**
  112. * @brief Function for activating the specified TWI task.
  113. *
  114. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  115. * @param[in] task Task to be activated.
  116. */
  117. __STATIC_INLINE void nrf_twi_task_trigger(NRF_TWI_Type * p_reg,
  118. nrf_twi_task_t task);
  119. /**
  120. * @brief Function for getting the address of the specified TWI task register.
  121. *
  122. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  123. * @param[in] task The specified task.
  124. *
  125. * @return Address of the specified task register.
  126. */
  127. __STATIC_INLINE uint32_t * nrf_twi_task_address_get(NRF_TWI_Type * p_reg,
  128. nrf_twi_task_t task);
  129. /**
  130. * @brief Function for clearing the specified TWI event.
  131. *
  132. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  133. * @param[in] event Event to clear.
  134. */
  135. __STATIC_INLINE void nrf_twi_event_clear(NRF_TWI_Type * p_reg,
  136. nrf_twi_event_t event);
  137. /**
  138. * @brief Function for retrieving the state of the TWI event.
  139. *
  140. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  141. * @param[in] event Event to be checked.
  142. *
  143. * @retval true The event has been generated.
  144. * @retval false The event has not been generated.
  145. */
  146. __STATIC_INLINE bool nrf_twi_event_check(NRF_TWI_Type * p_reg,
  147. nrf_twi_event_t event);
  148. /**
  149. * @brief Function for getting the address of the specified TWI event register.
  150. *
  151. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  152. * @param[in] event The specified event.
  153. *
  154. * @return Address of the specified event register.
  155. */
  156. __STATIC_INLINE uint32_t * nrf_twi_event_address_get(NRF_TWI_Type * p_reg,
  157. nrf_twi_event_t event);
  158. /**
  159. * @brief Function for enabling the specified shortcuts.
  160. *
  161. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  162. * @param[in] mask Shortcuts to be enabled.
  163. */
  164. __STATIC_INLINE void nrf_twi_shorts_enable(NRF_TWI_Type * p_reg,
  165. uint32_t mask);
  166. /**
  167. * @brief Function for disabling the specified shortcuts.
  168. *
  169. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  170. * @param[in] mask Shortcuts to be disabled.
  171. */
  172. __STATIC_INLINE void nrf_twi_shorts_disable(NRF_TWI_Type * p_reg,
  173. uint32_t mask);
  174. /**
  175. * @brief Function for enabling the specified interrupts.
  176. *
  177. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  178. * @param[in] mask Mask of interrupts to be enabled.
  179. */
  180. __STATIC_INLINE void nrf_twi_int_enable(NRF_TWI_Type * p_reg,
  181. uint32_t mask);
  182. /**
  183. * @brief Function for disabling the specified interrupts.
  184. *
  185. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  186. * @param[in] mask Mask of interrupts to be disabled.
  187. */
  188. __STATIC_INLINE void nrf_twi_int_disable(NRF_TWI_Type * p_reg,
  189. uint32_t mask);
  190. /**
  191. * @brief Function for retrieving the state of a given interrupt.
  192. *
  193. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  194. * @param[in] twi_int Interrupt to be checked.
  195. *
  196. * @retval true The interrupt is enabled.
  197. * @retval false The interrupt is not enabled.
  198. */
  199. __STATIC_INLINE bool nrf_twi_int_enable_check(NRF_TWI_Type * p_reg,
  200. nrf_twi_int_mask_t twi_int);
  201. /**
  202. * @brief Function for enabling the TWI peripheral.
  203. *
  204. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  205. */
  206. __STATIC_INLINE void nrf_twi_enable(NRF_TWI_Type * p_reg);
  207. /**
  208. * @brief Function for disabling the TWI peripheral.
  209. *
  210. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  211. */
  212. __STATIC_INLINE void nrf_twi_disable(NRF_TWI_Type * p_reg);
  213. /**
  214. * @brief Function for configuring TWI pins.
  215. *
  216. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  217. * @param[in] scl_pin SCL pin number.
  218. * @param[in] sda_pin SDA pin number.
  219. */
  220. __STATIC_INLINE void nrf_twi_pins_set(NRF_TWI_Type * p_reg,
  221. uint32_t scl_pin,
  222. uint32_t sda_pin);
  223. /**
  224. * @brief Function for retrieving the SCL pin number.
  225. *
  226. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  227. *
  228. * @return SCL pin number.
  229. */
  230. __STATIC_INLINE uint32_t nrf_twi_scl_pin_get(NRF_TWI_Type * p_reg);
  231. /**
  232. * @brief Function for retrieving the SDA pin number.
  233. *
  234. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  235. *
  236. * @return SDA pin number.
  237. */
  238. __STATIC_INLINE uint32_t nrf_twi_sda_pin_get(NRF_TWI_Type * p_reg);
  239. /**
  240. * @brief Function for setting the TWI master clock frequency.
  241. *
  242. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  243. * @param[in] frequency TWI frequency.
  244. */
  245. __STATIC_INLINE void nrf_twi_frequency_set(NRF_TWI_Type * p_reg,
  246. nrf_twi_frequency_t frequency);
  247. /**
  248. * @brief Function for checking the TWI error source.
  249. *
  250. * The error flags are cleared after reading.
  251. *
  252. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  253. *
  254. * @return Mask with error source flags.
  255. */
  256. __STATIC_INLINE uint32_t nrf_twi_errorsrc_get_and_clear(NRF_TWI_Type * p_reg);
  257. /**
  258. * @brief Function for setting the address to be used in TWI transfers.
  259. *
  260. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  261. * @param[in] address Address to be used in transfers.
  262. */
  263. __STATIC_INLINE void nrf_twi_address_set(NRF_TWI_Type * p_reg, uint8_t address);
  264. /**
  265. * @brief Function for reading data received by TWI.
  266. *
  267. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  268. *
  269. * @return Received data.
  270. */
  271. __STATIC_INLINE uint8_t nrf_twi_rxd_get(NRF_TWI_Type * p_reg);
  272. /**
  273. * @brief Function for writing data to be transmitted by TWI.
  274. *
  275. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  276. * @param[in] data Data to be transmitted.
  277. */
  278. __STATIC_INLINE void nrf_twi_txd_set(NRF_TWI_Type * p_reg, uint8_t data);
  279. /**
  280. * @brief Function for setting the specified shortcuts.
  281. *
  282. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  283. * @param[in] mask Shortcuts to be set.
  284. */
  285. __STATIC_INLINE void nrf_twi_shorts_set(NRF_TWI_Type * p_reg,
  286. uint32_t mask);
  287. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  288. __STATIC_INLINE void nrf_twi_task_trigger(NRF_TWI_Type * p_reg,
  289. nrf_twi_task_t task)
  290. {
  291. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  292. }
  293. __STATIC_INLINE uint32_t * nrf_twi_task_address_get(NRF_TWI_Type * p_reg,
  294. nrf_twi_task_t task)
  295. {
  296. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)task);
  297. }
  298. __STATIC_INLINE void nrf_twi_event_clear(NRF_TWI_Type * p_reg,
  299. nrf_twi_event_t event)
  300. {
  301. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  302. #if __CORTEX_M == 0x04
  303. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  304. (void)dummy;
  305. #endif
  306. }
  307. __STATIC_INLINE bool nrf_twi_event_check(NRF_TWI_Type * p_reg,
  308. nrf_twi_event_t event)
  309. {
  310. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  311. }
  312. __STATIC_INLINE uint32_t * nrf_twi_event_address_get(NRF_TWI_Type * p_reg,
  313. nrf_twi_event_t event)
  314. {
  315. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  316. }
  317. __STATIC_INLINE void nrf_twi_shorts_enable(NRF_TWI_Type * p_reg,
  318. uint32_t mask)
  319. {
  320. p_reg->SHORTS |= mask;
  321. }
  322. __STATIC_INLINE void nrf_twi_shorts_disable(NRF_TWI_Type * p_reg,
  323. uint32_t mask)
  324. {
  325. p_reg->SHORTS &= ~(mask);
  326. }
  327. __STATIC_INLINE void nrf_twi_int_enable(NRF_TWI_Type * p_reg,
  328. uint32_t mask)
  329. {
  330. p_reg->INTENSET = mask;
  331. }
  332. __STATIC_INLINE void nrf_twi_int_disable(NRF_TWI_Type * p_reg,
  333. uint32_t mask)
  334. {
  335. p_reg->INTENCLR = mask;
  336. }
  337. __STATIC_INLINE bool nrf_twi_int_enable_check(NRF_TWI_Type * p_reg,
  338. nrf_twi_int_mask_t twi_int)
  339. {
  340. return (bool)(p_reg->INTENSET & twi_int);
  341. }
  342. __STATIC_INLINE void nrf_twi_enable(NRF_TWI_Type * p_reg)
  343. {
  344. p_reg->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
  345. }
  346. __STATIC_INLINE void nrf_twi_disable(NRF_TWI_Type * p_reg)
  347. {
  348. p_reg->ENABLE = (TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos);
  349. }
  350. __STATIC_INLINE void nrf_twi_pins_set(NRF_TWI_Type * p_reg,
  351. uint32_t scl_pin,
  352. uint32_t sda_pin)
  353. {
  354. #if defined(TWI_PSEL_SCL_CONNECT_Pos)
  355. p_reg->PSEL.SCL = scl_pin;
  356. #else
  357. p_reg->PSELSCL = scl_pin;
  358. #endif
  359. #if defined(TWI_PSEL_SDA_CONNECT_Pos)
  360. p_reg->PSEL.SDA = sda_pin;
  361. #else
  362. p_reg->PSELSDA = sda_pin;
  363. #endif
  364. }
  365. __STATIC_INLINE uint32_t nrf_twi_scl_pin_get(NRF_TWI_Type * p_reg)
  366. {
  367. #if defined(TWI_PSEL_SCL_CONNECT_Pos)
  368. return p_reg->PSEL.SCL;
  369. #else
  370. return p_reg->PSELSCL;
  371. #endif
  372. }
  373. __STATIC_INLINE uint32_t nrf_twi_sda_pin_get(NRF_TWI_Type * p_reg)
  374. {
  375. #if defined(TWI_PSEL_SDA_CONNECT_Pos)
  376. return p_reg->PSEL.SDA;
  377. #else
  378. return p_reg->PSELSDA;
  379. #endif
  380. }
  381. __STATIC_INLINE void nrf_twi_frequency_set(NRF_TWI_Type * p_reg,
  382. nrf_twi_frequency_t frequency)
  383. {
  384. p_reg->FREQUENCY = frequency;
  385. }
  386. __STATIC_INLINE uint32_t nrf_twi_errorsrc_get_and_clear(NRF_TWI_Type * p_reg)
  387. {
  388. uint32_t error_source = p_reg->ERRORSRC;
  389. // [error flags are cleared by writing '1' on their position]
  390. p_reg->ERRORSRC = error_source;
  391. return error_source;
  392. }
  393. __STATIC_INLINE void nrf_twi_address_set(NRF_TWI_Type * p_reg, uint8_t address)
  394. {
  395. p_reg->ADDRESS = address;
  396. }
  397. __STATIC_INLINE uint8_t nrf_twi_rxd_get(NRF_TWI_Type * p_reg)
  398. {
  399. return (uint8_t)p_reg->RXD;
  400. }
  401. __STATIC_INLINE void nrf_twi_txd_set(NRF_TWI_Type * p_reg, uint8_t data)
  402. {
  403. p_reg->TXD = data;
  404. }
  405. __STATIC_INLINE void nrf_twi_shorts_set(NRF_TWI_Type * p_reg,
  406. uint32_t mask)
  407. {
  408. p_reg->SHORTS = mask;
  409. }
  410. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  411. /** @} */
  412. #ifdef __cplusplus
  413. }
  414. #endif
  415. #endif // NRF_TWI_H__