nrf_twis.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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_TWIS_H__
  41. #define NRF_TWIS_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_twis_hal TWIS HAL
  48. * @{
  49. * @ingroup nrf_twis
  50. * @brief Hardware access layer for managing the Two Wire Interface Slave with EasyDMA
  51. * (TWIS) peripheral.
  52. */
  53. /** @brief TWIS tasks. */
  54. typedef enum
  55. {
  56. NRF_TWIS_TASK_STOP = offsetof(NRF_TWIS_Type, TASKS_STOP), /**< Stop TWIS transaction. */
  57. NRF_TWIS_TASK_SUSPEND = offsetof(NRF_TWIS_Type, TASKS_SUSPEND), /**< Suspend TWIS transaction. */
  58. NRF_TWIS_TASK_RESUME = offsetof(NRF_TWIS_Type, TASKS_RESUME), /**< Resume TWIS transaction. */
  59. NRF_TWIS_TASK_PREPARERX = offsetof(NRF_TWIS_Type, TASKS_PREPARERX), /**< Prepare the TWIS slave to respond to a write command. */
  60. NRF_TWIS_TASK_PREPARETX = offsetof(NRF_TWIS_Type, TASKS_PREPARETX) /**< Prepare the TWIS slave to respond to a read command. */
  61. } nrf_twis_task_t;
  62. /** @brief TWIS events. */
  63. typedef enum
  64. {
  65. NRF_TWIS_EVENT_STOPPED = offsetof(NRF_TWIS_Type, EVENTS_STOPPED), /**< TWIS stopped. */
  66. NRF_TWIS_EVENT_ERROR = offsetof(NRF_TWIS_Type, EVENTS_ERROR), /**< TWIS error. */
  67. NRF_TWIS_EVENT_RXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_RXSTARTED), /**< Receive sequence started. */
  68. NRF_TWIS_EVENT_TXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_TXSTARTED), /**< Transmit sequence started. */
  69. NRF_TWIS_EVENT_WRITE = offsetof(NRF_TWIS_Type, EVENTS_WRITE), /**< Write command received. */
  70. NRF_TWIS_EVENT_READ = offsetof(NRF_TWIS_Type, EVENTS_READ) /**< Read command received. */
  71. } nrf_twis_event_t;
  72. /** @brief TWIS shortcuts. */
  73. typedef enum
  74. {
  75. NRF_TWIS_SHORT_WRITE_SUSPEND_MASK = TWIS_SHORTS_WRITE_SUSPEND_Msk, /**< Shortcut between WRITE event and SUSPEND task. */
  76. NRF_TWIS_SHORT_READ_SUSPEND_MASK = TWIS_SHORTS_READ_SUSPEND_Msk, /**< Shortcut between READ event and SUSPEND task. */
  77. } nrf_twis_short_mask_t;
  78. /** @brief TWIS interrupts. */
  79. typedef enum
  80. {
  81. NRF_TWIS_INT_STOPPED_MASK = TWIS_INTEN_STOPPED_Msk, /**< Interrupt on STOPPED event. */
  82. NRF_TWIS_INT_ERROR_MASK = TWIS_INTEN_ERROR_Msk, /**< Interrupt on ERROR event. */
  83. NRF_TWIS_INT_RXSTARTED_MASK = TWIS_INTEN_RXSTARTED_Msk, /**< Interrupt on RXSTARTED event. */
  84. NRF_TWIS_INT_TXSTARTED_MASK = TWIS_INTEN_TXSTARTED_Msk, /**< Interrupt on TXSTARTED event. */
  85. NRF_TWIS_INT_WRITE_MASK = TWIS_INTEN_WRITE_Msk, /**< Interrupt on WRITE event. */
  86. NRF_TWIS_INT_READ_MASK = TWIS_INTEN_READ_Msk, /**< Interrupt on READ event. */
  87. } nrf_twis_int_mask_t;
  88. /** @brief TWIS error source. */
  89. typedef enum
  90. {
  91. NRF_TWIS_ERROR_OVERFLOW = TWIS_ERRORSRC_OVERFLOW_Msk, /**< RX buffer overflow detected, and prevented. */
  92. NRF_TWIS_ERROR_DATA_NACK = TWIS_ERRORSRC_DNACK_Msk, /**< NACK sent after receiving a data byte. */
  93. NRF_TWIS_ERROR_OVERREAD = TWIS_ERRORSRC_OVERREAD_Msk /**< TX buffer over-read detected, and prevented. */
  94. } nrf_twis_error_t;
  95. /** @brief TWIS address matching configuration. */
  96. typedef enum
  97. {
  98. NRF_TWIS_CONFIG_ADDRESS0_MASK = TWIS_CONFIG_ADDRESS0_Msk, /**< Enable or disable address matching on ADDRESS[0]. */
  99. NRF_TWIS_CONFIG_ADDRESS1_MASK = TWIS_CONFIG_ADDRESS1_Msk, /**< Enable or disable address matching on ADDRESS[1]. */
  100. NRF_TWIS_CONFIG_ADDRESS01_MASK = TWIS_CONFIG_ADDRESS0_Msk | TWIS_CONFIG_ADDRESS1_Msk /**< Enable both address matching. */
  101. } nrf_twis_config_addr_mask_t;
  102. /**
  103. * @brief Variable type to hold the amount of data for EasyDMA.
  104. *
  105. * Variable of the minimum size that can hold the amount of data to transfer.
  106. *
  107. * @note Defined to make it simple to change if EasyDMA is updated to support more data in
  108. * the future devices.
  109. */
  110. typedef uint8_t nrf_twis_amount_t;
  111. /**
  112. * @brief Smallest variable type to hold the TWI address.
  113. *
  114. * Variable of the minimum size that can hold a single TWI address.
  115. *
  116. * @note Defined to make it simple to change if the new TWI supports for example
  117. * 10 bit addressing mode.
  118. */
  119. typedef uint8_t nrf_twis_address_t;
  120. /**
  121. * @brief Function for activating the specified TWIS 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_twis_task_trigger(NRF_TWIS_Type * p_reg, nrf_twis_task_t task);
  127. /**
  128. * @brief Function for returning the address of the specified TWIS task register.
  129. *
  130. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  131. * @param[in] task The specified task.
  132. *
  133. * @return Task address.
  134. */
  135. __STATIC_INLINE uint32_t nrf_twis_task_address_get(NRF_TWIS_Type const * p_reg,
  136. nrf_twis_task_t task);
  137. /**
  138. * @brief Function for clearing the specified event.
  139. *
  140. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  141. * @param[in] event The specified event.
  142. */
  143. __STATIC_INLINE void nrf_twis_event_clear(NRF_TWIS_Type * p_reg,
  144. nrf_twis_event_t event);
  145. /**
  146. * @brief Function for retrieving the state of the TWIS event.
  147. *
  148. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  149. * @param[in] event Event to be checked.
  150. *
  151. * @retval true The event has been generated.
  152. * @retval false The event has not been generated.
  153. */
  154. __STATIC_INLINE bool nrf_twis_event_check(NRF_TWIS_Type const * p_reg,
  155. nrf_twis_event_t event);
  156. /**
  157. * @brief Function for getting and clearing the state of the specified event.
  158. *
  159. * This function checks the state of the event and clears it.
  160. *
  161. * @param[in,out] p_reg Pointer to the structure of registers of the peripheral.
  162. * @param[in] event Event.
  163. *
  164. * @retval true The event was set.
  165. * @retval false The event was not set.
  166. */
  167. __STATIC_INLINE bool nrf_twis_event_get_and_clear(NRF_TWIS_Type * p_reg,
  168. nrf_twis_event_t event);
  169. /**
  170. * @brief Function for returning the address of the specified TWIS event register.
  171. *
  172. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  173. * @param[in] event Event.
  174. *
  175. * @return Address.
  176. */
  177. __STATIC_INLINE uint32_t nrf_twis_event_address_get(NRF_TWIS_Type const * p_reg,
  178. nrf_twis_event_t event);
  179. /**
  180. * @brief Function for setting a shortcut.
  181. *
  182. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  183. * @param[in] mask Mask of shortcuts to be enabled.
  184. */
  185. __STATIC_INLINE void nrf_twis_shorts_enable(NRF_TWIS_Type * p_reg, uint32_t mask);
  186. /**
  187. * @brief Function for clearing shortcuts.
  188. *
  189. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  190. * @param[in] mask Mask of shortcuts to be disabled.
  191. */
  192. __STATIC_INLINE void nrf_twis_shorts_disable(NRF_TWIS_Type * p_reg, uint32_t mask);
  193. /**
  194. * @brief Function for getting the shorts mask.
  195. *
  196. * Function returns shorts register.
  197. *
  198. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  199. *
  200. * @return Flags of currently enabled shortcuts
  201. */
  202. __STATIC_INLINE uint32_t nrf_twis_shorts_get(NRF_TWIS_Type const * p_reg);
  203. /**
  204. * @brief Function for enabling the specified interrupts.
  205. *
  206. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  207. * @param[in] mask Mask of interrupts to be enabled.
  208. */
  209. __STATIC_INLINE void nrf_twis_int_enable(NRF_TWIS_Type * p_reg, uint32_t mask);
  210. /**
  211. * @brief Function for retrieving the state of the specified interrupts.
  212. *
  213. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  214. * @param[in] mask Mask of interrupts to be checked.
  215. *
  216. * @retval true Any of the specified interrupts is enabled.
  217. * @retval false None of the specified interrupts is enabled.
  218. */
  219. __STATIC_INLINE bool nrf_twis_int_enable_check(NRF_TWIS_Type const * p_reg, uint32_t mask);
  220. /**
  221. * @brief Function for disabling the specified interrupts.
  222. *
  223. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  224. * @param[in] mask Mask of interrupts to be disabled.
  225. */
  226. __STATIC_INLINE void nrf_twis_int_disable(NRF_TWIS_Type * p_reg, uint32_t mask);
  227. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  228. /**
  229. * @brief Function for setting the subscribe configuration for a given
  230. * TWIS task.
  231. *
  232. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  233. * @param[in] task Task for which to set the configuration.
  234. * @param[in] channel Channel through which to subscribe events.
  235. */
  236. __STATIC_INLINE void nrf_twis_subscribe_set(NRF_TWIS_Type * p_reg,
  237. nrf_twis_task_t task,
  238. uint8_t channel);
  239. /**
  240. * @brief Function for clearing the subscribe configuration for a given
  241. * TWIS task.
  242. *
  243. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  244. * @param[in] task Task for which to clear the configuration.
  245. */
  246. __STATIC_INLINE void nrf_twis_subscribe_clear(NRF_TWIS_Type * p_reg,
  247. nrf_twis_task_t task);
  248. /**
  249. * @brief Function for setting the publish configuration for a given
  250. * TWIS event.
  251. *
  252. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  253. * @param[in] event Event for which to set the configuration.
  254. * @param[in] channel Channel through which to publish the event.
  255. */
  256. __STATIC_INLINE void nrf_twis_publish_set(NRF_TWIS_Type * p_reg,
  257. nrf_twis_event_t event,
  258. uint8_t channel);
  259. /**
  260. * @brief Function for clearing the publish configuration for a given
  261. * TWIS event.
  262. *
  263. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  264. * @param[in] event Event for which to clear the configuration.
  265. */
  266. __STATIC_INLINE void nrf_twis_publish_clear(NRF_TWIS_Type * p_reg,
  267. nrf_twis_event_t event);
  268. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  269. /**
  270. * @brief Function for retrieving and clearing the TWIS error source.
  271. *
  272. * @attention Error sources are cleared after read.
  273. *
  274. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  275. *
  276. * @return Error source mask with values from @ref nrf_twis_error_t.
  277. */
  278. __STATIC_INLINE uint32_t nrf_twis_error_source_get_and_clear(NRF_TWIS_Type * const p_reg);
  279. /**
  280. * @brief Function for getting information about which of the addresses matched.
  281. *
  282. * Function returns index in the address table
  283. * that points to the address that already matched.
  284. *
  285. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  286. *
  287. * @return Index of matched address.
  288. */
  289. __STATIC_INLINE uint_fast8_t nrf_twis_match_get(NRF_TWIS_Type const * p_reg);
  290. /**
  291. * @brief Function for enabling TWIS.
  292. *
  293. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  294. */
  295. __STATIC_INLINE void nrf_twis_enable(NRF_TWIS_Type * p_reg);
  296. /**
  297. * @brief Function for disabling TWIS.
  298. *
  299. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  300. */
  301. __STATIC_INLINE void nrf_twis_disable(NRF_TWIS_Type * p_reg);
  302. /**
  303. * @brief Function for configuring TWIS pins.
  304. *
  305. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  306. * @param[in] scl SCL pin number.
  307. * @param[in] sda SDA pin number.
  308. */
  309. __STATIC_INLINE void nrf_twis_pins_set(NRF_TWIS_Type * p_reg, uint32_t scl, uint32_t sda);
  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_buf Pointer to the buffer for received data.
  315. * @param[in] length Maximum number of data bytes to receive.
  316. */
  317. __STATIC_INLINE void nrf_twis_rx_buffer_set(NRF_TWIS_Type * p_reg,
  318. uint8_t * p_buf,
  319. size_t length);
  320. /**
  321. * @brief Function that prepares TWIS for receiving
  322. *
  323. * This function sets receive buffer and then sets NRF_TWIS_TASK_PREPARERX task.
  324. *
  325. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  326. * @param[in] p_buf Pointer to the buffer for received data.
  327. * @param[in] length Maximum number of data bytes to receive.
  328. */
  329. __STATIC_INLINE void nrf_twis_rx_prepare(NRF_TWIS_Type * p_reg,
  330. uint8_t * p_buf,
  331. size_t length);
  332. /**
  333. * @brief Function for getting number of bytes received in the last transaction.
  334. *
  335. * @param[in] p_reg TWIS instance.
  336. *
  337. * @return Amount of bytes received.
  338. * */
  339. __STATIC_INLINE size_t nrf_twis_rx_amount_get(NRF_TWIS_Type const * p_reg);
  340. /**
  341. * @brief Function for setting the transmit buffer.
  342. *
  343. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  344. * @param[in] p_buf Pointer to the buffer with data to send.
  345. * @param[in] length Maximum number of data bytes to transmit.
  346. */
  347. __STATIC_INLINE void nrf_twis_tx_buffer_set(NRF_TWIS_Type * p_reg,
  348. uint8_t const * p_buf,
  349. size_t length);
  350. /**
  351. * @brief Function for preparing TWIS for transmitting.
  352. *
  353. * This function sets transmit buffer and then sets NRF_TWIS_TASK_PREPARETX task.
  354. *
  355. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  356. * @param[in] p_buf Pointer to the buffer with data to send.
  357. * @param[in] length Maximum number of data bytes to transmit.
  358. */
  359. __STATIC_INLINE void nrf_twis_tx_prepare(NRF_TWIS_Type * p_reg,
  360. uint8_t const * p_buf,
  361. size_t length);
  362. /**
  363. * @brief Function for getting the number of bytes transmitted in the last transaction.
  364. *
  365. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  366. *
  367. * @return Amount of bytes transmitted.
  368. */
  369. __STATIC_INLINE size_t nrf_twis_tx_amount_get(NRF_TWIS_Type const * p_reg);
  370. /**
  371. * @brief Function for setting the slave address.
  372. *
  373. * Function sets the selected address for this TWI interface.
  374. *
  375. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  376. * @param[in] n Index of address to be set.
  377. * @param[in] addr Addres to be set.
  378. *
  379. * @sa nrf_twis_config_address_set
  380. * @sa nrf_twis_config_address_get
  381. */
  382. __STATIC_INLINE void nrf_twis_address_set(NRF_TWIS_Type * p_reg,
  383. uint_fast8_t n,
  384. nrf_twis_address_t addr);
  385. /**
  386. * @brief Function for retrieving configured slave address.
  387. *
  388. * Function gets the selected address for this TWI interface.
  389. *
  390. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  391. * @param[in] n Index of address to get.
  392. *
  393. * @return Configured slave address.
  394. */
  395. __STATIC_INLINE nrf_twis_address_t nrf_twis_address_get(NRF_TWIS_Type const * p_reg,
  396. uint_fast8_t n);
  397. /**
  398. * @brief Function for setting the device address configuration.
  399. *
  400. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  401. * @param[in] addr_mask Mask of address indexes of what device should answer to.
  402. *
  403. * @sa nrf_twis_address_set
  404. */
  405. __STATIC_INLINE void nrf_twis_config_address_set(NRF_TWIS_Type * p_reg,
  406. nrf_twis_config_addr_mask_t addr_mask);
  407. /**
  408. * @brief Function for retrieving the device address configuration.
  409. *
  410. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  411. *
  412. * @return Mask of address indexes of what device should answer to.
  413. */
  414. __STATIC_INLINE nrf_twis_config_addr_mask_t nrf_twis_config_address_get(
  415. NRF_TWIS_Type const * p_reg);
  416. /**
  417. * @brief Function for setting the over-read character.
  418. *
  419. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  420. * @param[in] orc Over-read character. Character clocked out in case of
  421. * over-read of the TXD buffer.
  422. */
  423. __STATIC_INLINE void nrf_twis_orc_set(NRF_TWIS_Type * p_reg,
  424. uint8_t orc);
  425. /**
  426. * @brief Function for setting the over-read character.
  427. *
  428. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  429. *
  430. * @return Over-read character configured for selected instance.
  431. */
  432. __STATIC_INLINE uint8_t nrf_twis_orc_get(NRF_TWIS_Type const * p_reg);
  433. /** @} */ /* End of nrf_twis_hal */
  434. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  435. /* ------------------------------------------------------------------------------------------------
  436. * Internal functions
  437. */
  438. /**
  439. * @internal
  440. * @brief Internal function for getting task or event register address.
  441. *
  442. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  443. * @param[in] offset Offset of the register from the beginning of the instance.
  444. *
  445. * @attention Offset must be modulo 4 value. In other case, hardware fault can occur.
  446. * @return Pointer to the register.
  447. */
  448. __STATIC_INLINE volatile uint32_t* nrf_twis_getRegPtr(NRF_TWIS_Type const * p_reg, uint32_t offset)
  449. {
  450. return (volatile uint32_t*)((uint8_t *)p_reg + (uint32_t)offset);
  451. }
  452. /**
  453. * @internal
  454. * @brief Internal function for getting task/event register address - constant version.
  455. *
  456. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  457. * @param[in] offset Offset of the register from the beginning of the instance.
  458. *
  459. * @attention Offset must be modulo 4 value. In other case, hardware fault can occur.
  460. * @return Pointer to the register.
  461. */
  462. __STATIC_INLINE volatile const uint32_t* nrf_twis_getRegPtr_c(NRF_TWIS_Type const * p_reg,
  463. uint32_t offset)
  464. {
  465. return (volatile const uint32_t*)((uint8_t *)p_reg + (uint32_t)offset);
  466. }
  467. /* ------------------------------------------------------------------------------------------------
  468. * Interface functions definitions
  469. */
  470. __STATIC_INLINE void nrf_twis_task_trigger(NRF_TWIS_Type * p_reg, nrf_twis_task_t task)
  471. {
  472. *(nrf_twis_getRegPtr(p_reg, (uint32_t)task)) = 1UL;
  473. }
  474. __STATIC_INLINE uint32_t nrf_twis_task_address_get(NRF_TWIS_Type const * p_reg,
  475. nrf_twis_task_t task)
  476. {
  477. return (uint32_t)nrf_twis_getRegPtr_c(p_reg, (uint32_t)task);
  478. }
  479. __STATIC_INLINE void nrf_twis_event_clear(NRF_TWIS_Type * p_reg, nrf_twis_event_t event)
  480. {
  481. *(nrf_twis_getRegPtr(p_reg, (uint32_t)event)) = 0UL;
  482. #if __CORTEX_M == 0x04
  483. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  484. (void)dummy;
  485. #endif
  486. }
  487. __STATIC_INLINE bool nrf_twis_event_check(NRF_TWIS_Type const * p_reg, nrf_twis_event_t event)
  488. {
  489. return (bool)*nrf_twis_getRegPtr_c(p_reg, (uint32_t)event);
  490. }
  491. __STATIC_INLINE bool nrf_twis_event_get_and_clear(NRF_TWIS_Type * p_reg, nrf_twis_event_t event)
  492. {
  493. bool ret = nrf_twis_event_check(p_reg, event);
  494. if (ret)
  495. {
  496. nrf_twis_event_clear(p_reg, event);
  497. }
  498. return ret;
  499. }
  500. __STATIC_INLINE uint32_t nrf_twis_event_address_get(NRF_TWIS_Type const * p_reg,
  501. nrf_twis_event_t event)
  502. {
  503. return (uint32_t)nrf_twis_getRegPtr_c(p_reg, (uint32_t)event);
  504. }
  505. __STATIC_INLINE void nrf_twis_shorts_enable(NRF_TWIS_Type * p_reg, uint32_t mask)
  506. {
  507. p_reg->SHORTS |= mask;
  508. }
  509. __STATIC_INLINE void nrf_twis_shorts_disable(NRF_TWIS_Type * p_reg, uint32_t mask)
  510. {
  511. if (~0U == mask)
  512. {
  513. /* Optimized version for "disable all" */
  514. p_reg->SHORTS = 0;
  515. }
  516. else
  517. {
  518. p_reg->SHORTS &= ~mask;
  519. }
  520. }
  521. __STATIC_INLINE uint32_t nrf_twis_shorts_get(NRF_TWIS_Type const * p_reg)
  522. {
  523. return p_reg->SHORTS;
  524. }
  525. __STATIC_INLINE void nrf_twis_int_enable(NRF_TWIS_Type * p_reg, uint32_t mask)
  526. {
  527. p_reg->INTENSET = mask;
  528. }
  529. __STATIC_INLINE bool nrf_twis_int_enable_check(NRF_TWIS_Type const * p_reg, uint32_t mask)
  530. {
  531. return (bool)(p_reg->INTENSET & mask);
  532. }
  533. __STATIC_INLINE void nrf_twis_int_disable(NRF_TWIS_Type * const p_reg, uint32_t mask)
  534. {
  535. p_reg->INTENCLR = mask;
  536. }
  537. #if defined(DPPI_PRESENT)
  538. __STATIC_INLINE void nrf_twis_subscribe_set(NRF_TWIS_Type * p_reg,
  539. nrf_twis_task_t task,
  540. uint8_t channel)
  541. {
  542. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  543. ((uint32_t)channel | TWIS_SUBSCRIBE_STOP_EN_Msk);
  544. }
  545. __STATIC_INLINE void nrf_twis_subscribe_clear(NRF_TWIS_Type * p_reg,
  546. nrf_twis_task_t task)
  547. {
  548. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  549. }
  550. __STATIC_INLINE void nrf_twis_publish_set(NRF_TWIS_Type * p_reg,
  551. nrf_twis_event_t event,
  552. uint8_t channel)
  553. {
  554. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  555. ((uint32_t)channel | TWIS_PUBLISH_STOPPED_EN_Msk);
  556. }
  557. __STATIC_INLINE void nrf_twis_publish_clear(NRF_TWIS_Type * p_reg,
  558. nrf_twis_event_t event)
  559. {
  560. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  561. }
  562. #endif // defined(DPPI_PRESENT)
  563. __STATIC_INLINE uint32_t nrf_twis_error_source_get_and_clear(NRF_TWIS_Type * p_reg)
  564. {
  565. uint32_t ret = p_reg->ERRORSRC;
  566. p_reg->ERRORSRC = ret;
  567. return ret;
  568. }
  569. __STATIC_INLINE uint_fast8_t nrf_twis_match_get(NRF_TWIS_Type const * p_reg)
  570. {
  571. return (uint_fast8_t)p_reg->MATCH;
  572. }
  573. __STATIC_INLINE void nrf_twis_enable(NRF_TWIS_Type * p_reg)
  574. {
  575. p_reg->ENABLE = (TWIS_ENABLE_ENABLE_Enabled << TWIS_ENABLE_ENABLE_Pos);
  576. }
  577. __STATIC_INLINE void nrf_twis_disable(NRF_TWIS_Type * p_reg)
  578. {
  579. p_reg->ENABLE = (TWIS_ENABLE_ENABLE_Disabled << TWIS_ENABLE_ENABLE_Pos);
  580. }
  581. __STATIC_INLINE void nrf_twis_pins_set(NRF_TWIS_Type * p_reg, uint32_t scl, uint32_t sda)
  582. {
  583. p_reg->PSEL.SCL = scl;
  584. p_reg->PSEL.SDA = sda;
  585. }
  586. __STATIC_INLINE void nrf_twis_rx_buffer_set(NRF_TWIS_Type * p_reg,
  587. uint8_t * p_buf,
  588. size_t length)
  589. {
  590. p_reg->RXD.PTR = (uint32_t)p_buf;
  591. p_reg->RXD.MAXCNT = length;
  592. }
  593. __STATIC_INLINE void nrf_twis_rx_prepare(NRF_TWIS_Type * p_reg,
  594. uint8_t * p_buf,
  595. size_t length)
  596. {
  597. nrf_twis_rx_buffer_set(p_reg, p_buf, length);
  598. nrf_twis_task_trigger(p_reg, NRF_TWIS_TASK_PREPARERX);
  599. }
  600. __STATIC_INLINE size_t nrf_twis_rx_amount_get(NRF_TWIS_Type const * p_reg)
  601. {
  602. return p_reg->RXD.AMOUNT;
  603. }
  604. __STATIC_INLINE void nrf_twis_tx_buffer_set(NRF_TWIS_Type * p_reg,
  605. uint8_t const * p_buf,
  606. size_t length)
  607. {
  608. p_reg->TXD.PTR = (uint32_t)p_buf;
  609. p_reg->TXD.MAXCNT = length;
  610. }
  611. __STATIC_INLINE void nrf_twis_tx_prepare(NRF_TWIS_Type * p_reg,
  612. uint8_t const * p_buf,
  613. size_t length)
  614. {
  615. nrf_twis_tx_buffer_set(p_reg, p_buf, length);
  616. nrf_twis_task_trigger(p_reg, NRF_TWIS_TASK_PREPARETX);
  617. }
  618. __STATIC_INLINE size_t nrf_twis_tx_amount_get(NRF_TWIS_Type const * p_reg)
  619. {
  620. return p_reg->TXD.AMOUNT;
  621. }
  622. __STATIC_INLINE void nrf_twis_address_set(NRF_TWIS_Type * p_reg,
  623. uint_fast8_t n,
  624. nrf_twis_address_t addr)
  625. {
  626. p_reg->ADDRESS[n] = addr;
  627. }
  628. __STATIC_INLINE nrf_twis_address_t nrf_twis_address_get(NRF_TWIS_Type const * p_reg, uint_fast8_t n)
  629. {
  630. return (nrf_twis_address_t)p_reg->ADDRESS[n];
  631. }
  632. __STATIC_INLINE void nrf_twis_config_address_set(NRF_TWIS_Type * p_reg,
  633. nrf_twis_config_addr_mask_t addr_mask)
  634. {
  635. /* This is the only configuration in TWIS - just write it without masking */
  636. p_reg->CONFIG = addr_mask;
  637. }
  638. __STATIC_INLINE nrf_twis_config_addr_mask_t nrf_twis_config_address_get(NRF_TWIS_Type const * p_reg)
  639. {
  640. return (nrf_twis_config_addr_mask_t)(p_reg->CONFIG & TWIS_ADDRESS_ADDRESS_Msk);
  641. }
  642. __STATIC_INLINE void nrf_twis_orc_set(NRF_TWIS_Type * p_reg, uint8_t orc)
  643. {
  644. p_reg->ORC = orc;
  645. }
  646. __STATIC_INLINE uint8_t nrf_twis_orc_get(NRF_TWIS_Type const * p_reg)
  647. {
  648. return (uint8_t)p_reg->ORC;
  649. }
  650. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  651. #ifdef __cplusplus
  652. }
  653. #endif
  654. #endif /* NRF_TWIS_H__ */