nrf_twis.h 20 KB

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