nrf_twis.h 22 KB

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