nrf_twim.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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_TWIM_H__
  13. #define NRF_TWIM_H__
  14. /**
  15. * @defgroup nrf_twim_hal TWIM HAL
  16. * @{
  17. * @ingroup nrf_twi_master
  18. *
  19. * @brief Hardware access layer for managing the TWIM peripheral.
  20. */
  21. #include <stdint.h>
  22. #include <stddef.h>
  23. #include <stdbool.h>
  24. #include "nrf.h"
  25. /**
  26. * @brief TWIM tasks.
  27. */
  28. typedef enum
  29. {
  30. /*lint -save -e30*/
  31. NRF_TWIM_TASK_STARTRX = offsetof(NRF_TWIM_Type, TASKS_STARTRX), ///< Start TWI receive sequence.
  32. NRF_TWIM_TASK_STARTTX = offsetof(NRF_TWIM_Type, TASKS_STARTTX), ///< Start TWI transmit sequence.
  33. NRF_TWIM_TASK_STOP = offsetof(NRF_TWIM_Type, TASKS_STOP), ///< Stop TWI transaction.
  34. NRF_TWIM_TASK_SUSPEND = offsetof(NRF_TWIM_Type, TASKS_SUSPEND), ///< Suspend TWI transaction.
  35. NRF_TWIM_TASK_RESUME = offsetof(NRF_TWIM_Type, TASKS_RESUME) ///< Resume TWI transaction.
  36. /*lint -restore*/
  37. } nrf_twim_task_t;
  38. /**
  39. * @brief TWIM events.
  40. */
  41. typedef enum
  42. {
  43. /*lint -save -e30*/
  44. NRF_TWIM_EVENT_STOPPED = offsetof(NRF_TWIM_Type, EVENTS_STOPPED), ///< TWI stopped.
  45. NRF_TWIM_EVENT_ERROR = offsetof(NRF_TWIM_Type, EVENTS_ERROR), ///< TWI error.
  46. NRF_TWIM_EVENT_SUSPENDED = 0x148, ///< TWI suspended.
  47. NRF_TWIM_EVENT_RXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_RXSTARTED), ///< Receive sequence started.
  48. NRF_TWIM_EVENT_TXSTARTED = offsetof(NRF_TWIM_Type, EVENTS_TXSTARTED), ///< Transmit sequence started.
  49. NRF_TWIM_EVENT_LASTRX = offsetof(NRF_TWIM_Type, EVENTS_LASTRX), ///< Byte boundary, starting to receive the last byte.
  50. NRF_TWIM_EVENT_LASTTX = offsetof(NRF_TWIM_Type, EVENTS_LASTTX) ///< Byte boundary, starting to transmit the last byte.
  51. /*lint -restore*/
  52. } nrf_twim_event_t;
  53. /**
  54. * @brief TWIM shortcuts.
  55. */
  56. typedef enum
  57. {
  58. NRF_TWIM_SHORT_LASTTX_STARTRX_MASK = TWIM_SHORTS_LASTTX_STARTRX_Msk, ///< Shortcut between LASTTX event and STARTRX task.
  59. NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK = TWIM_SHORTS_LASTTX_SUSPEND_Msk, ///< Shortcut between LASTTX event and SUSPEND task.
  60. NRF_TWIM_SHORT_LASTTX_STOP_MASK = TWIM_SHORTS_LASTTX_STOP_Msk, ///< Shortcut between LASTTX event and STOP task.
  61. NRF_TWIM_SHORT_LASTRX_STARTTX_MASK = TWIM_SHORTS_LASTRX_STARTTX_Msk, ///< Shortcut between LASTRX event and STARTTX task.
  62. NRF_TWIM_SHORT_LASTRX_STOP_MASK = TWIM_SHORTS_LASTRX_STOP_Msk ///< Shortcut between LASTRX event and STOP task.
  63. } nrf_twim_short_mask_t;
  64. /**
  65. * @brief TWIM interrupts.
  66. */
  67. typedef enum
  68. {
  69. NRF_TWIM_INT_STOPPED_MASK = TWIM_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  70. NRF_TWIM_INT_ERROR_MASK = TWIM_INTENSET_ERROR_Msk, ///< Interrupt on ERROR event.
  71. NRF_TWIM_INT_SUSPENDED_MASK = (1 << 18), ///< Interrupt on SUSPENDED event.
  72. NRF_TWIM_INT_RXSTARTED_MASK = TWIM_INTENSET_RXSTARTED_Msk, ///< Interrupt on RXSTARTED event.
  73. NRF_TWIM_INT_TXSTARTED_MASK = TWIM_INTENSET_TXSTARTED_Msk, ///< Interrupt on TXSTARTED event.
  74. NRF_TWIM_INT_LASTRX_MASK = TWIM_INTENSET_LASTRX_Msk, ///< Interrupt on LASTRX event.
  75. NRF_TWIM_INT_LASTTX_MASK = TWIM_INTENSET_LASTTX_Msk ///< Interrupt on LASTTX event.
  76. } nrf_twim_int_mask_t;
  77. /**
  78. * @brief TWIM master clock frequency.
  79. */
  80. typedef enum
  81. {
  82. NRF_TWIM_FREQ_100K = TWIM_FREQUENCY_FREQUENCY_K100, ///< 100 kbps.
  83. NRF_TWIM_FREQ_250K = TWIM_FREQUENCY_FREQUENCY_K250, ///< 250 kbps.
  84. NRF_TWIM_FREQ_400K = TWIM_FREQUENCY_FREQUENCY_K400 ///< 400 kbps.
  85. } nrf_twim_frequency_t;
  86. /**
  87. * @brief TWIM error source.
  88. */
  89. typedef enum
  90. {
  91. NRF_TWIM_ERROR_ADDRESS_NACK = TWIM_ERRORSRC_ANACK_Msk, ///< NACK received after sending the address.
  92. NRF_TWIM_ERROR_DATA_NACK = TWIM_ERRORSRC_DNACK_Msk ///< NACK received after sending a data byte.
  93. } nrf_twim_error_t;
  94. /**
  95. * @brief Function for activating a specific TWIM task.
  96. *
  97. * @param[in] p_twim TWIM instance.
  98. * @param[in] task Task to activate.
  99. */
  100. __STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_twim,
  101. nrf_twim_task_t task);
  102. /**
  103. * @brief Function for getting the address of a specific TWIM task register.
  104. *
  105. * @param[in] p_twim TWIM instance.
  106. * @param[in] task Requested task.
  107. *
  108. * @return Address of the specified task register.
  109. */
  110. __STATIC_INLINE uint32_t * nrf_twim_task_address_get(NRF_TWIM_Type * p_twim,
  111. nrf_twim_task_t task);
  112. /**
  113. * @brief Function for clearing a specific TWIM event.
  114. *
  115. * @param[in] p_twim TWIM instance.
  116. * @param[in] event Event to clear.
  117. */
  118. __STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type * p_twim,
  119. nrf_twim_event_t event);
  120. /**
  121. * @brief Function for checking the state of a specific TWIM event.
  122. *
  123. * @param[in] p_twim TWIM instance.
  124. * @param[in] event Event to check.
  125. *
  126. * @retval true If the event is set.
  127. * @retval false If the event is not set.
  128. */
  129. __STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type * p_twim,
  130. nrf_twim_event_t event);
  131. /**
  132. * @brief Function for getting the address of a specific TWIM event register.
  133. *
  134. * @param[in] p_twim TWIM instance.
  135. * @param[in] event Requested event.
  136. *
  137. * @return Address of the specified event register.
  138. */
  139. __STATIC_INLINE uint32_t * nrf_twim_event_address_get(NRF_TWIM_Type * p_twim,
  140. nrf_twim_event_t event);
  141. /**
  142. * @brief Function for enabling specified shortcuts.
  143. *
  144. * @param[in] p_twim TWIM instance.
  145. * @param[in] shorts_mask Shortcuts to enable.
  146. */
  147. __STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_twim,
  148. uint32_t shorts_mask);
  149. /**
  150. * @brief Function for disabling specified shortcuts.
  151. *
  152. * @param[in] p_twim TWIM instance.
  153. * @param[in] shorts_mask Shortcuts to disable.
  154. */
  155. __STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_twim,
  156. uint32_t shorts_mask);
  157. /**
  158. * @brief Function for enabling specified interrupts.
  159. *
  160. * @param[in] p_twim TWIM instance.
  161. * @param[in] int_mask Interrupts to enable.
  162. */
  163. __STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_twim,
  164. uint32_t int_mask);
  165. /**
  166. * @brief Function for disabling specified interrupts.
  167. *
  168. * @param[in] p_twim TWIM instance.
  169. * @param[in] int_mask Interrupts to disable.
  170. */
  171. __STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_twim,
  172. uint32_t int_mask);
  173. /**
  174. * @brief Function for checking the state of a given interrupt.
  175. *
  176. * @param[in] p_twim TWIM instance.
  177. * @param[in] int_mask Interrupt to check.
  178. *
  179. * @retval true If the interrupt is enabled.
  180. * @retval false If the interrupt is not enabled.
  181. */
  182. __STATIC_INLINE bool nrf_twim_int_enable_check(NRF_TWIM_Type * p_twim,
  183. nrf_twim_int_mask_t int_mask);
  184. /**
  185. * @brief Function for enabling the TWIM peripheral.
  186. *
  187. * @param[in] p_twim TWIM instance.
  188. */
  189. __STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_twim);
  190. /**
  191. * @brief Function for disabling the TWIM peripheral.
  192. *
  193. * @param[in] p_twim TWIM instance.
  194. */
  195. __STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_twim);
  196. /**
  197. * @brief Function for configuring TWI pins.
  198. *
  199. *
  200. * @param[in] p_twim TWIM instance.
  201. * @param[in] scl_pin SCL pin number.
  202. * @param[in] sda_pin SDA pin number.
  203. */
  204. __STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_twim,
  205. uint32_t scl_pin,
  206. uint32_t sda_pin);
  207. /**
  208. * @brief Function for setting the TWI master clock frequency.
  209. *
  210. * @param[in] p_twim TWIM instance.
  211. * @param[in] frequency TWI frequency.
  212. */
  213. __STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type * p_twim,
  214. nrf_twim_frequency_t frequency);
  215. /**
  216. * @brief Function for checking the TWI error source.
  217. *
  218. * The error flags are cleared after reading.
  219. *
  220. * @param[in] p_twim TWIM instance.
  221. *
  222. * @return Mask with error source flags.
  223. */
  224. __STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_twim);
  225. /**
  226. * @brief Function for setting the address to be used in TWI transfers.
  227. *
  228. * @param[in] p_twim TWIM instance.
  229. * @param[in] address Address to be used in transfers.
  230. */
  231. __STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_twim,
  232. uint8_t address);
  233. /**
  234. * @brief Function for setting the transmit buffer.
  235. *
  236. * @param[in] p_twim TWIM instance.
  237. * @param[in] p_buffer Pointer to the buffer with data to send.
  238. * @param[in] length Maximum number of data bytes to transmit.
  239. */
  240. __STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_twim,
  241. uint8_t const * p_buffer,
  242. uint8_t length);
  243. /**
  244. * @brief Function for setting the receive buffer.
  245. *
  246. * @param[in] p_twim TWIM instance.
  247. * @param[in] p_buffer Pointer to the buffer for received data.
  248. * @param[in] length Maximum number of data bytes to receive.
  249. */
  250. __STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_twim,
  251. uint8_t * p_buffer,
  252. uint8_t length);
  253. __STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_twim,
  254. uint32_t shorts_mask);
  255. __STATIC_INLINE uint32_t nrf_twim_txd_amount_get(NRF_TWIM_Type * p_twim);
  256. __STATIC_INLINE uint32_t nrf_twim_rxd_amount_get(NRF_TWIM_Type * p_twim);
  257. /**
  258. * @brief Function for enabling the TX list feature.
  259. *
  260. * @param[in] p_twim TWIM instance.
  261. */
  262. __STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_twim);
  263. /**
  264. * @brief Function for disabling the TX list feature.
  265. *
  266. * @param[in] p_twim TWIM instance.
  267. */
  268. __STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_twim);
  269. /**
  270. * @brief Function for enabling the RX list feature.
  271. *
  272. * @param[in] p_twim TWIM instance.
  273. */
  274. __STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_twim);
  275. /**
  276. * @brief Function for disabling the RX list feature.
  277. *
  278. * @param[in] p_twim TWIM instance.
  279. */
  280. __STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_twim);
  281. /**
  282. * @}
  283. */
  284. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  285. __STATIC_INLINE void nrf_twim_task_trigger(NRF_TWIM_Type * p_twim,
  286. nrf_twim_task_t task)
  287. {
  288. *((volatile uint32_t *)((uint8_t *)p_twim + (uint32_t)task)) = 0x1UL;
  289. }
  290. __STATIC_INLINE uint32_t * nrf_twim_task_address_get(NRF_TWIM_Type * p_twim,
  291. nrf_twim_task_t task)
  292. {
  293. return (uint32_t *)((uint8_t *)p_twim + (uint32_t)task);
  294. }
  295. __STATIC_INLINE void nrf_twim_event_clear(NRF_TWIM_Type * p_twim,
  296. nrf_twim_event_t event)
  297. {
  298. *((volatile uint32_t *)((uint8_t *)p_twim + (uint32_t)event)) = 0x0UL;
  299. }
  300. __STATIC_INLINE bool nrf_twim_event_check(NRF_TWIM_Type * p_twim,
  301. nrf_twim_event_t event)
  302. {
  303. return (bool)*(volatile uint32_t *)((uint8_t *)p_twim + (uint32_t)event);
  304. }
  305. __STATIC_INLINE uint32_t * nrf_twim_event_address_get(NRF_TWIM_Type * p_twim,
  306. nrf_twim_event_t event)
  307. {
  308. return (uint32_t *)((uint8_t *)p_twim + (uint32_t)event);
  309. }
  310. __STATIC_INLINE void nrf_twim_shorts_enable(NRF_TWIM_Type * p_twim,
  311. uint32_t shorts_mask)
  312. {
  313. p_twim->SHORTS |= shorts_mask;
  314. }
  315. __STATIC_INLINE void nrf_twim_shorts_disable(NRF_TWIM_Type * p_twim,
  316. uint32_t shorts_mask)
  317. {
  318. p_twim->SHORTS &= ~(shorts_mask);
  319. }
  320. __STATIC_INLINE void nrf_twim_int_enable(NRF_TWIM_Type * p_twim,
  321. uint32_t int_mask)
  322. {
  323. p_twim->INTENSET = int_mask;
  324. }
  325. __STATIC_INLINE void nrf_twim_int_disable(NRF_TWIM_Type * p_twim,
  326. uint32_t int_mask)
  327. {
  328. p_twim->INTENCLR = int_mask;
  329. }
  330. __STATIC_INLINE bool nrf_twim_int_enable_check(NRF_TWIM_Type * p_twim,
  331. nrf_twim_int_mask_t int_mask)
  332. {
  333. return (bool)(p_twim->INTENSET & int_mask);
  334. }
  335. __STATIC_INLINE void nrf_twim_enable(NRF_TWIM_Type * p_twim)
  336. {
  337. p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
  338. }
  339. __STATIC_INLINE void nrf_twim_disable(NRF_TWIM_Type * p_twim)
  340. {
  341. p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
  342. }
  343. __STATIC_INLINE void nrf_twim_pins_set(NRF_TWIM_Type * p_twim,
  344. uint32_t scl_pin,
  345. uint32_t sda_pin)
  346. {
  347. p_twim->PSEL.SCL = scl_pin;
  348. p_twim->PSEL.SDA = sda_pin;
  349. }
  350. __STATIC_INLINE void nrf_twim_frequency_set(NRF_TWIM_Type * p_twim,
  351. nrf_twim_frequency_t frequency)
  352. {
  353. p_twim->FREQUENCY = frequency;
  354. }
  355. __STATIC_INLINE uint32_t nrf_twim_errorsrc_get_and_clear(NRF_TWIM_Type * p_twim)
  356. {
  357. uint32_t error_source = p_twim->ERRORSRC;
  358. // [error flags are cleared by writing '1' on their position]
  359. p_twim->ERRORSRC = error_source;
  360. return error_source;
  361. }
  362. __STATIC_INLINE void nrf_twim_address_set(NRF_TWIM_Type * p_twim,
  363. uint8_t address)
  364. {
  365. p_twim->ADDRESS = address;
  366. }
  367. __STATIC_INLINE void nrf_twim_tx_buffer_set(NRF_TWIM_Type * p_twim,
  368. uint8_t const * p_buffer,
  369. uint8_t length)
  370. {
  371. p_twim->TXD.PTR = (uint32_t)p_buffer;
  372. p_twim->TXD.MAXCNT = length;
  373. }
  374. __STATIC_INLINE void nrf_twim_rx_buffer_set(NRF_TWIM_Type * p_twim,
  375. uint8_t * p_buffer,
  376. uint8_t length)
  377. {
  378. p_twim->RXD.PTR = (uint32_t)p_buffer;
  379. p_twim->RXD.MAXCNT = length;
  380. }
  381. __STATIC_INLINE void nrf_twim_shorts_set(NRF_TWIM_Type * p_twim,
  382. uint32_t shorts_mask)
  383. {
  384. p_twim->SHORTS = shorts_mask;
  385. }
  386. __STATIC_INLINE uint32_t nrf_twim_txd_amount_get(NRF_TWIM_Type * p_twim)
  387. {
  388. return p_twim->TXD.AMOUNT;
  389. }
  390. __STATIC_INLINE uint32_t nrf_twim_rxd_amount_get(NRF_TWIM_Type * p_twim)
  391. {
  392. return p_twim->RXD.AMOUNT;
  393. }
  394. __STATIC_INLINE void nrf_twim_tx_list_enable(NRF_TWIM_Type * p_twim)
  395. {
  396. p_twim->TXD.LIST = 1;
  397. }
  398. __STATIC_INLINE void nrf_twim_tx_list_disable(NRF_TWIM_Type * p_twim)
  399. {
  400. p_twim->TXD.LIST = 0;
  401. }
  402. __STATIC_INLINE void nrf_twim_rx_list_enable(NRF_TWIM_Type * p_twim)
  403. {
  404. p_twim->RXD.LIST = 1;
  405. }
  406. __STATIC_INLINE void nrf_twim_rx_list_disable(NRF_TWIM_Type * p_twim)
  407. {
  408. p_twim->RXD.LIST = 0;
  409. }
  410. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  411. #endif // NRF_TWIM_H__