nrf_twim.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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_TWIM_H__
  41. #define NRF_TWIM_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_twim_hal TWIM HAL
  48. * @{
  49. * @ingroup nrf_twim
  50. * @brief Hardware access layer for managing the TWIM peripheral.
  51. */
  52. /** @brief TWIM tasks. */
  53. typedef enum
  54. {
  55. NRF_TWIM_TASK_STARTRX = offsetof(NRF_TWIM_Type, TASKS_STARTRX), ///< Start TWI receive sequence.
  56. NRF_TWIM_TASK_STARTTX = offsetof(NRF_TWIM_Type, TASKS_STARTTX), ///< Start TWI transmit sequence.
  57. NRF_TWIM_TASK_STOP = offsetof(NRF_TWIM_Type, TASKS_STOP), ///< Stop TWI transaction.
  58. NRF_TWIM_TASK_SUSPEND = offsetof(NRF_TWIM_Type, TASKS_SUSPEND), ///< Suspend TWI transaction.
  59. NRF_TWIM_TASK_RESUME = offsetof(NRF_TWIM_Type, TASKS_RESUME) ///< Resume TWI transaction.
  60. } nrf_twim_task_t;
  61. /** @brief TWIM events. */
  62. typedef enum
  63. {
  64. NRF_TWIM_EVENT_STOPPED = offsetof(NRF_TWIM_Type, EVENTS_STOPPED), ///< TWI stopped.
  65. NRF_TWIM_EVENT_ERROR = offsetof(NRF_TWIM_Type, EVENTS_ERROR), ///< TWI error.
  66. NRF_TWIM_EVENT_SUSPENDED = 0x148, ///< TWI suspended.
  67. NRF_TWIM_EVENT_RXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_RXSTARTED), ///< Receive sequence started.
  68. NRF_TWIM_EVENT_TXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_TXSTARTED), ///< Transmit sequence started.
  69. NRF_TWIM_EVENT_LASTRX = offsetof(NRF_TWIM_Type, EVENTS_LASTRX), ///< Byte boundary, starting to receive the last byte.
  70. NRF_TWIM_EVENT_LASTTX = offsetof(NRF_TWIM_Type, EVENTS_LASTTX) ///< Byte boundary, starting to transmit the last byte.
  71. } nrf_twim_event_t;
  72. /** @brief TWIM shortcuts. */
  73. typedef enum
  74. {
  75. NRF_TWIM_SHORT_LASTTX_STARTRX_MASK = TWIM_SHORTS_LASTTX_STARTRX_Msk, ///< Shortcut between LASTTX event and STARTRX task.
  76. NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK = TWIM_SHORTS_LASTTX_SUSPEND_Msk, ///< Shortcut between LASTTX event and SUSPEND task.
  77. NRF_TWIM_SHORT_LASTTX_STOP_MASK = TWIM_SHORTS_LASTTX_STOP_Msk, ///< Shortcut between LASTTX event and STOP task.
  78. NRF_TWIM_SHORT_LASTRX_STARTTX_MASK = TWIM_SHORTS_LASTRX_STARTTX_Msk, ///< Shortcut between LASTRX event and STARTTX task.
  79. NRF_TWIM_SHORT_LASTRX_STOP_MASK = TWIM_SHORTS_LASTRX_STOP_Msk, ///< Shortcut between LASTRX event and STOP task.
  80. NRF_TWIM_ALL_SHORTS_MASK = TWIM_SHORTS_LASTTX_STARTRX_Msk |
  81. TWIM_SHORTS_LASTTX_SUSPEND_Msk |
  82. TWIM_SHORTS_LASTTX_STOP_Msk |
  83. TWIM_SHORTS_LASTRX_STARTTX_Msk |
  84. TWIM_SHORTS_LASTRX_STOP_Msk ///< All TWIM shortcuts.
  85. } nrf_twim_short_mask_t;
  86. /** @brief TWIM interrupts. */
  87. typedef enum
  88. {
  89. NRF_TWIM_INT_STOPPED_MASK = TWIM_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  90. NRF_TWIM_INT_ERROR_MASK = TWIM_INTENSET_ERROR_Msk, ///< Interrupt on ERROR event.
  91. NRF_TWIM_INT_SUSPENDED_MASK = TWIM_INTENSET_SUSPENDED_Msk, ///< Interrupt on SUSPENDED event.
  92. NRF_TWIM_INT_RXSTARTED_MASK = TWIM_INTENSET_RXSTARTED_Msk, ///< Interrupt on RXSTARTED event.
  93. NRF_TWIM_INT_TXSTARTED_MASK = TWIM_INTENSET_TXSTARTED_Msk, ///< Interrupt on TXSTARTED event.
  94. NRF_TWIM_INT_LASTRX_MASK = TWIM_INTENSET_LASTRX_Msk, ///< Interrupt on LASTRX event.
  95. NRF_TWIM_INT_LASTTX_MASK = TWIM_INTENSET_LASTTX_Msk, ///< Interrupt on LASTTX event.
  96. NRF_TWIM_ALL_INTS_MASK = TWIM_INTENSET_STOPPED_Msk |
  97. TWIM_INTENSET_ERROR_Msk |
  98. TWIM_INTENSET_SUSPENDED_Msk |
  99. TWIM_INTENSET_RXSTARTED_Msk |
  100. TWIM_INTENSET_TXSTARTED_Msk |
  101. TWIM_INTENSET_LASTRX_Msk |
  102. TWIM_INTENSET_LASTTX_Msk ///< All TWIM interrupts.
  103. } nrf_twim_int_mask_t;
  104. /** @brief TWIM master clock frequency. */
  105. typedef enum
  106. {
  107. NRF_TWIM_FREQ_100K = TWIM_FREQUENCY_FREQUENCY_K100, ///< 100 kbps.
  108. NRF_TWIM_FREQ_250K = TWIM_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  109. NRF_TWIM_FREQ_400K = TWIM_FREQUENCY_FREQUENCY_K400 ///< 400 kbps.
  110. } nrf_twim_frequency_t;
  111. /** @brief TWIM error source. */
  112. typedef enum
  113. {
  114. NRF_TWIM_ERROR_ADDRESS_NACK = TWIM_ERRORSRC_ANACK_Msk, ///< NACK received after sending the address.
  115. NRF_TWIM_ERROR_DATA_NACK = TWIM_ERRORSRC_DNACK_Msk, ///< NACK received after sending a data byte.
  116. NRF_TWIM_ERROR_OVERRUN = TWIM_ERRORSRC_OVERRUN_Msk ///< Overrun error.
  117. /**< A new byte was received before the previous byte was
  118. * handled by peripheral. (previous data is lost). */
  119. } nrf_twim_error_t;
  120. /**
  121. * @brief Function for activating the specified TWIM task.
  122. *
  123. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  124. * @param[in] task Task to be activated.
  125. */
  126. __STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_reg,
  127. nrf_twim_task_t task);
  128. /**
  129. * @brief Function for getting the address of the specified TWIM task register.
  130. *
  131. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  132. * @param[in] task The specified task.
  133. *
  134. * @return Address of the specified task register.
  135. */
  136. __STATIC_INLINE uint32_t * nrf_twim_task_address_get(NRF_TWIM_Type * p_reg,
  137. nrf_twim_task_t task);
  138. /**
  139. * @brief Function for clearing the specified TWIM event.
  140. *
  141. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  142. * @param[in] event Event to clear.
  143. */
  144. __STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type * p_reg,
  145. nrf_twim_event_t event);
  146. /**
  147. * @brief Function for retrieving the state of the TWIM event.
  148. *
  149. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  150. * @param[in] event Event to be checked.
  151. *
  152. * @retval true The event has been generated.
  153. * @retval false The event has not been generated.
  154. */
  155. __STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type * p_reg,
  156. nrf_twim_event_t event);
  157. /**
  158. * @brief Function for getting the address of the specified TWIM event register.
  159. *
  160. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  161. * @param[in] event The specified event.
  162. *
  163. * @return Address of the specified event register.
  164. */
  165. __STATIC_INLINE uint32_t * nrf_twim_event_address_get(NRF_TWIM_Type * p_reg,
  166. nrf_twim_event_t event);
  167. /**
  168. * @brief Function for enabling the specified shortcuts.
  169. *
  170. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  171. * @param[in] mask Shortcuts to be enabled.
  172. */
  173. __STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_reg,
  174. uint32_t mask);
  175. /**
  176. * @brief Function for disabling the specified shortcuts.
  177. *
  178. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  179. * @param[in] mask Shortcuts to be disabled.
  180. */
  181. __STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_reg,
  182. uint32_t mask);
  183. /**
  184. * @brief Function for enabling the specified interrupts.
  185. *
  186. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  187. * @param[in] mask Mask of interrupts to be enabled.
  188. */
  189. __STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_reg,
  190. uint32_t mask);
  191. /**
  192. * @brief Function for disabling the specified interrupts.
  193. *
  194. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  195. * @param[in] mask Mask of interrupts to be disabled.
  196. */
  197. __STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_reg,
  198. uint32_t mask);
  199. /**
  200. * @brief Function for checking the state of a given interrupt.
  201. *
  202. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  203. * @param[in] twim_int Interrupt to be checked.
  204. *
  205. * @retval true The interrupt is enabled.
  206. * @retval false The interrupt is not enabled.
  207. */
  208. __STATIC_INLINE bool nrf_twim_int_enable_check(NRF_TWIM_Type * p_reg,
  209. nrf_twim_int_mask_t twim_int);
  210. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  211. /**
  212. * @brief Function for setting the subscribe configuration for a given
  213. * TWIM task.
  214. *
  215. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  216. * @param[in] task Task for which to set the configuration.
  217. * @param[in] channel Channel through which to subscribe events.
  218. */
  219. __STATIC_INLINE void nrf_twim_subscribe_set(NRF_TWIM_Type * p_reg,
  220. nrf_twim_task_t task,
  221. uint8_t channel);
  222. /**
  223. * @brief Function for clearing the subscribe configuration for a given
  224. * TWIM task.
  225. *
  226. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  227. * @param[in] task Task for which to clear the configuration.
  228. */
  229. __STATIC_INLINE void nrf_twim_subscribe_clear(NRF_TWIM_Type * p_reg,
  230. nrf_twim_task_t task);
  231. /**
  232. * @brief Function for setting the publish configuration for a given
  233. * TWIM event.
  234. *
  235. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  236. * @param[in] event Event for which to set the configuration.
  237. * @param[in] channel Channel through which to publish the event.
  238. */
  239. __STATIC_INLINE void nrf_twim_publish_set(NRF_TWIM_Type * p_reg,
  240. nrf_twim_event_t event,
  241. uint8_t channel);
  242. /**
  243. * @brief Function for clearing the publish configuration for a given
  244. * TWIM event.
  245. *
  246. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  247. * @param[in] event Event for which to clear the configuration.
  248. */
  249. __STATIC_INLINE void nrf_twim_publish_clear(NRF_TWIM_Type * p_reg,
  250. nrf_twim_event_t event);
  251. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  252. /**
  253. * @brief Function for enabling the TWIM peripheral.
  254. *
  255. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  256. */
  257. __STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_reg);
  258. /**
  259. * @brief Function for disabling the TWIM peripheral.
  260. *
  261. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  262. */
  263. __STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_reg);
  264. /**
  265. * @brief Function for configuring TWI pins.
  266. *
  267. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  268. * @param[in] scl_pin SCL pin number.
  269. * @param[in] sda_pin SDA pin number.
  270. */
  271. __STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_reg,
  272. uint32_t scl_pin,
  273. uint32_t sda_pin);
  274. /**
  275. * @brief Function for setting the TWI master clock frequency.
  276. *
  277. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  278. * @param[in] frequency TWI frequency.
  279. */
  280. __STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type * p_reg,
  281. nrf_twim_frequency_t frequency);
  282. /**
  283. * @brief Function for checking the TWI error source.
  284. *
  285. * The error flags are cleared after reading.
  286. *
  287. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  288. *
  289. * @return Mask with error source flags.
  290. */
  291. __STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_reg);
  292. /**
  293. * @brief Function for setting the address to be used in TWI transfers.
  294. *
  295. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  296. * @param[in] address Address to be used in transfers.
  297. */
  298. __STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_reg,
  299. uint8_t address);
  300. /**
  301. * @brief Function for setting the transmit buffer.
  302. *
  303. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  304. * @param[in] p_buffer Pointer to the buffer with data to send.
  305. * @param[in] length Maximum number of data bytes to transmit.
  306. */
  307. __STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_reg,
  308. uint8_t const * p_buffer,
  309. size_t length);
  310. /**
  311. * @brief Function for setting the receive buffer.
  312. *
  313. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  314. * @param[in] p_buffer Pointer to the buffer for received data.
  315. * @param[in] length Maximum number of data bytes to receive.
  316. */
  317. __STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_reg,
  318. uint8_t * p_buffer,
  319. size_t length);
  320. /**
  321. * @brief Function for setting the specified shortcuts.
  322. *
  323. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  324. * @param[in] mask Shortcuts to be set.
  325. */
  326. __STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_reg,
  327. uint32_t mask);
  328. /**
  329. * @brief Function for getting the shortcut setting.
  330. *
  331. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  332. *
  333. * @return Current shortcut configuration.
  334. */
  335. __STATIC_INLINE uint32_t nrf_twim_shorts_get(NRF_TWIM_Type const * p_reg);
  336. /**
  337. * @brief Function for getting the amount of transmitted bytes.
  338. *
  339. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  340. *
  341. * @return Amount of transmitted bytes.
  342. */
  343. __STATIC_INLINE size_t nrf_twim_txd_amount_get(NRF_TWIM_Type * p_reg);
  344. /**
  345. * @brief Function for getting the amount of received bytes.
  346. *
  347. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  348. *
  349. * @return Amount of received bytes.
  350. */
  351. __STATIC_INLINE size_t nrf_twim_rxd_amount_get(NRF_TWIM_Type * p_reg);
  352. /**
  353. * @brief Function for enabling the TX list feature.
  354. *
  355. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  356. */
  357. __STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_reg);
  358. /**
  359. * @brief Function for disabling the TX list feature.
  360. *
  361. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  362. */
  363. __STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_reg);
  364. /**
  365. * @brief Function for enabling the RX list feature.
  366. *
  367. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  368. */
  369. __STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_reg);
  370. /**
  371. * @brief Function for disabling the RX list feature.
  372. *
  373. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  374. */
  375. __STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_reg);
  376. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  377. __STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_reg,
  378. nrf_twim_task_t task)
  379. {
  380. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  381. }
  382. __STATIC_INLINE uint32_t * nrf_twim_task_address_get(NRF_TWIM_Type * p_reg,
  383. nrf_twim_task_t task)
  384. {
  385. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)task);
  386. }
  387. __STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type * p_reg,
  388. nrf_twim_event_t event)
  389. {
  390. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  391. #if __CORTEX_M == 0x04
  392. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  393. (void)dummy;
  394. #endif
  395. }
  396. __STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type * p_reg,
  397. nrf_twim_event_t event)
  398. {
  399. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  400. }
  401. __STATIC_INLINE uint32_t * nrf_twim_event_address_get(NRF_TWIM_Type * p_reg,
  402. nrf_twim_event_t event)
  403. {
  404. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  405. }
  406. __STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_reg,
  407. uint32_t mask)
  408. {
  409. p_reg->SHORTS |= mask;
  410. }
  411. __STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_reg,
  412. uint32_t mask)
  413. {
  414. p_reg->SHORTS &= ~(mask);
  415. }
  416. __STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_reg,
  417. uint32_t mask)
  418. {
  419. p_reg->INTENSET = mask;
  420. }
  421. __STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_reg,
  422. uint32_t mask)
  423. {
  424. p_reg->INTENCLR = mask;
  425. }
  426. __STATIC_INLINE bool nrf_twim_int_enable_check(NRF_TWIM_Type * p_reg,
  427. nrf_twim_int_mask_t twim_int)
  428. {
  429. return (bool)(p_reg->INTENSET & twim_int);
  430. }
  431. #if defined(DPPI_PRESENT)
  432. __STATIC_INLINE void nrf_twim_subscribe_set(NRF_TWIM_Type * p_reg,
  433. nrf_twim_task_t task,
  434. uint8_t channel)
  435. {
  436. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  437. ((uint32_t)channel | TWIM_SUBSCRIBE_STARTRX_EN_Msk);
  438. }
  439. __STATIC_INLINE void nrf_twim_subscribe_clear(NRF_TWIM_Type * p_reg,
  440. nrf_twim_task_t task)
  441. {
  442. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  443. }
  444. __STATIC_INLINE void nrf_twim_publish_set(NRF_TWIM_Type * p_reg,
  445. nrf_twim_event_t event,
  446. uint8_t channel)
  447. {
  448. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  449. ((uint32_t)channel | TWIM_PUBLISH_STOPPED_EN_Msk);
  450. }
  451. __STATIC_INLINE void nrf_twim_publish_clear(NRF_TWIM_Type * p_reg,
  452. nrf_twim_event_t event)
  453. {
  454. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  455. }
  456. #endif // defined(DPPI_PRESENT)
  457. __STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_reg)
  458. {
  459. p_reg->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
  460. }
  461. __STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_reg)
  462. {
  463. p_reg->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
  464. }
  465. __STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_reg,
  466. uint32_t scl_pin,
  467. uint32_t sda_pin)
  468. {
  469. p_reg->PSEL.SCL = scl_pin;
  470. p_reg->PSEL.SDA = sda_pin;
  471. }
  472. __STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type * p_reg,
  473. nrf_twim_frequency_t frequency)
  474. {
  475. p_reg->FREQUENCY = frequency;
  476. }
  477. __STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_reg)
  478. {
  479. uint32_t error_source = p_reg->ERRORSRC;
  480. // [error flags are cleared by writing '1' on their position]
  481. p_reg->ERRORSRC = error_source;
  482. return error_source;
  483. }
  484. __STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_reg,
  485. uint8_t address)
  486. {
  487. p_reg->ADDRESS = address;
  488. }
  489. __STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_reg,
  490. uint8_t const * p_buffer,
  491. size_t length)
  492. {
  493. p_reg->TXD.PTR = (uint32_t)p_buffer;
  494. p_reg->TXD.MAXCNT = length;
  495. }
  496. __STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_reg,
  497. uint8_t * p_buffer,
  498. size_t length)
  499. {
  500. p_reg->RXD.PTR = (uint32_t)p_buffer;
  501. p_reg->RXD.MAXCNT = length;
  502. }
  503. __STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_reg,
  504. uint32_t mask)
  505. {
  506. p_reg->SHORTS = mask;
  507. }
  508. __STATIC_INLINE size_t nrf_twim_txd_amount_get(NRF_TWIM_Type * p_reg)
  509. {
  510. return p_reg->TXD.AMOUNT;
  511. }
  512. __STATIC_INLINE uint32_t nrf_twim_shorts_get(NRF_TWIM_Type const * p_reg)
  513. {
  514. return p_reg->SHORTS;
  515. }
  516. __STATIC_INLINE size_t nrf_twim_rxd_amount_get(NRF_TWIM_Type * p_reg)
  517. {
  518. return p_reg->RXD.AMOUNT;
  519. }
  520. __STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_reg)
  521. {
  522. p_reg->TXD.LIST = 1;
  523. }
  524. __STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_reg)
  525. {
  526. p_reg->TXD.LIST = 0;
  527. }
  528. __STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_reg)
  529. {
  530. p_reg->RXD.LIST = 1;
  531. }
  532. __STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_reg)
  533. {
  534. p_reg->RXD.LIST = 0;
  535. }
  536. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  537. /** @} */
  538. #ifdef __cplusplus
  539. }
  540. #endif
  541. #endif // NRF_TWIM_H__