nrf_drv_twi.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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_DRV_TWI_H__
  41. #define NRF_DRV_TWI_H__
  42. #include <nrfx.h>
  43. #ifdef TWIM_PRESENT
  44. #include <nrfx_twim.h>
  45. #else
  46. // Compilers (at least the smart ones) will remove the TWIM related code
  47. // (blocks starting with "if (NRF_DRV_TWI_USE_TWIM)") when it is not used,
  48. // but to perform the compilation they need the following definitions.
  49. #define nrfx_twim_init(...) 0
  50. #define nrfx_twim_uninit(...)
  51. #define nrfx_twim_enable(...)
  52. #define nrfx_twim_disable(...)
  53. #define nrfx_twim_tx(...) 0
  54. #define nrfx_twim_rx(...) 0
  55. #define nrfx_twim_is_busy(...) 0
  56. #define nrfx_twim_start_task_get(...) 0
  57. #define nrfx_twim_stopped_event_get(...) 0
  58. #endif
  59. #ifdef TWI_PRESENT
  60. #include <nrfx_twi.h>
  61. #else
  62. // Compilers (at least the smart ones) will remove the TWI related code
  63. // (blocks starting with "if (NRF_DRV_TWI_USE_TWI)") when it is not used,
  64. // but to perform the compilation they need the following definitions.
  65. #define nrfx_twi_init(...) 0
  66. #define nrfx_twi_uninit(...)
  67. #define nrfx_twi_enable(...)
  68. #define nrfx_twi_disable(...)
  69. #define nrfx_twi_tx(...) 0
  70. #define nrfx_twi_rx(...) 0
  71. #define nrfx_twi_is_busy(...) 0
  72. #define nrfx_twi_data_count_get(...) 0
  73. #define nrfx_twi_stopped_event_get(...) 0
  74. // This part is for old modules that use directly TWI HAL definitions
  75. // (to make them compilable for chips that have only TWIM).
  76. #define NRF_TWI_ERROR_ADDRESS_NACK NRF_TWIM_ERROR_ADDRESS_NACK
  77. #define NRF_TWI_ERROR_DATA_NACK NRF_TWIM_ERROR_DATA_NACK
  78. #define NRF_TWI_FREQ_100K NRF_TWIM_FREQ_100K
  79. #define NRF_TWI_FREQ_250K NRF_TWIM_FREQ_250K
  80. #define NRF_TWI_FREQ_400K NRF_TWIM_FREQ_400K
  81. #endif
  82. #ifdef __cplusplus
  83. extern "C" {
  84. #endif
  85. /**
  86. * @defgroup nrf_drv_twi TWI driver - legacy layer
  87. * @{
  88. * @ingroup nrf_twi
  89. * @brief Layer providing compatibility with the former API.
  90. */
  91. /**
  92. * @brief Structure for the TWI master driver instance.
  93. */
  94. typedef struct
  95. {
  96. uint8_t inst_idx;
  97. union
  98. {
  99. #ifdef TWIM_PRESENT
  100. nrfx_twim_t twim;
  101. #endif
  102. #ifdef TWI_PRESENT
  103. nrfx_twi_t twi;
  104. #endif
  105. } u;
  106. bool use_easy_dma;
  107. } nrf_drv_twi_t;
  108. /**
  109. * @brief Macro for creating a TWI master driver instance.
  110. */
  111. #define NRF_DRV_TWI_INSTANCE(id) NRF_DRV_TWI_INSTANCE_(id)
  112. #define NRF_DRV_TWI_INSTANCE_(id) NRF_DRV_TWI_INSTANCE_ ## id
  113. #if NRFX_CHECK(NRFX_TWIM0_ENABLED)
  114. #define NRF_DRV_TWI_INSTANCE_0 \
  115. { 0, { .twim = NRFX_TWIM_INSTANCE(0) }, true }
  116. #elif NRFX_CHECK(NRFX_TWI0_ENABLED)
  117. #define NRF_DRV_TWI_INSTANCE_0 \
  118. { 0, { .twi = NRFX_TWI_INSTANCE(0) }, false }
  119. #endif
  120. #if NRFX_CHECK(NRFX_TWIM1_ENABLED)
  121. #define NRF_DRV_TWI_INSTANCE_1 \
  122. { 1, { .twim = NRFX_TWIM_INSTANCE(1) }, true }
  123. #elif NRFX_CHECK(NRFX_TWI1_ENABLED)
  124. #define NRF_DRV_TWI_INSTANCE_1 \
  125. { 1, { .twi = NRFX_TWI_INSTANCE(1) }, false }
  126. #endif
  127. /**
  128. * @brief TWI master clock frequency.
  129. */
  130. typedef enum
  131. {
  132. NRF_DRV_TWI_FREQ_100K = NRF_TWI_FREQ_100K , ///< 100 kbps.
  133. NRF_DRV_TWI_FREQ_250K = NRF_TWI_FREQ_250K , ///< 250 kbps.
  134. NRF_DRV_TWI_FREQ_400K = NRF_TWI_FREQ_400K ///< 400 kbps.
  135. } nrf_drv_twi_frequency_t;
  136. /**
  137. * @brief Structure for the TWI master driver instance configuration.
  138. */
  139. typedef struct
  140. {
  141. uint32_t scl; ///< SCL pin number.
  142. uint32_t sda; ///< SDA pin number.
  143. nrf_drv_twi_frequency_t frequency; ///< TWI frequency.
  144. uint8_t interrupt_priority; ///< Interrupt priority.
  145. bool clear_bus_init; ///< Clear bus during init.
  146. bool hold_bus_uninit; ///< Hold pull up state on gpio pins after uninit.
  147. } nrf_drv_twi_config_t;
  148. /**
  149. * @brief TWI master driver instance default configuration.
  150. */
  151. #define NRF_DRV_TWI_DEFAULT_CONFIG \
  152. { \
  153. .frequency = (nrf_drv_twi_frequency_t)TWI_DEFAULT_CONFIG_FREQUENCY, \
  154. .scl = 31, \
  155. .sda = 31, \
  156. .interrupt_priority = TWI_DEFAULT_CONFIG_IRQ_PRIORITY, \
  157. .clear_bus_init = TWI_DEFAULT_CONFIG_CLR_BUS_INIT, \
  158. .hold_bus_uninit = TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT, \
  159. }
  160. #define NRF_DRV_TWI_FLAG_TX_POSTINC (1UL << 0) /**< TX buffer address incremented after transfer. */
  161. #define NRF_DRV_TWI_FLAG_RX_POSTINC (1UL << 1) /**< RX buffer address incremented after transfer. */
  162. #define NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
  163. #define NRF_DRV_TWI_FLAG_HOLD_XFER (1UL << 3) /**< Set up the transfer but do not start it. */
  164. #define NRF_DRV_TWI_FLAG_REPEATED_XFER (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
  165. #define NRF_DRV_TWI_FLAG_TX_NO_STOP (1UL << 5) /**< Flag indicating that the TX transfer will not end with a stop condition. */
  166. /**
  167. * @brief TWI master driver event types.
  168. */
  169. typedef enum
  170. {
  171. NRF_DRV_TWI_EVT_DONE, ///< Transfer completed event.
  172. NRF_DRV_TWI_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
  173. NRF_DRV_TWI_EVT_DATA_NACK ///< Error event: NACK received after sending a data byte.
  174. } nrf_drv_twi_evt_type_t;
  175. /**
  176. * @brief TWI master driver transfer types.
  177. */
  178. typedef enum
  179. {
  180. NRF_DRV_TWI_XFER_TX, ///< TX transfer.
  181. NRF_DRV_TWI_XFER_RX, ///< RX transfer.
  182. NRF_DRV_TWI_XFER_TXRX, ///< TX transfer followed by RX transfer with repeated start.
  183. NRF_DRV_TWI_XFER_TXTX ///< TX transfer followed by TX transfer with repeated start.
  184. } nrf_drv_twi_xfer_type_t;
  185. /**
  186. * @brief Structure for a TWI transfer descriptor.
  187. */
  188. typedef struct
  189. {
  190. nrf_drv_twi_xfer_type_t type; ///< Type of transfer.
  191. uint8_t address; ///< Slave address.
  192. uint8_t primary_length; ///< Number of bytes transferred.
  193. uint8_t secondary_length; ///< Number of bytes transferred.
  194. uint8_t * p_primary_buf; ///< Pointer to transferred data.
  195. uint8_t * p_secondary_buf; ///< Pointer to transferred data.
  196. } nrf_drv_twi_xfer_desc_t;
  197. /**@brief Macro for setting the TX transfer descriptor. */
  198. #define NRF_DRV_TWI_XFER_DESC_TX(addr, p_data, length) \
  199. { \
  200. .type = NRF_DRV_TWI_XFER_TX, \
  201. .address = addr, \
  202. .primary_length = length, \
  203. .p_primary_buf = p_data, \
  204. }
  205. /**@brief Macro for setting the RX transfer descriptor. */
  206. #define NRF_DRV_TWI_XFER_DESC_RX(addr, p_data, length) \
  207. { \
  208. .type = NRF_DRV_TWI_XFER_RX, \
  209. .address = addr, \
  210. .primary_length = length, \
  211. .p_primary_buf = p_data, \
  212. }
  213. /**@brief Macro for setting the TXRX transfer descriptor. */
  214. #define NRF_DRV_TWI_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len) \
  215. { \
  216. .type = NRF_DRV_TWI_XFER_TXRX, \
  217. .address = addr, \
  218. .primary_length = tx_len, \
  219. .secondary_length = rx_len, \
  220. .p_primary_buf = p_tx, \
  221. .p_secondary_buf = p_rx, \
  222. }
  223. /**@brief Macro for setting the TXTX transfer descriptor. */
  224. #define NRF_DRV_TWI_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
  225. { \
  226. .type = NRF_DRV_TWI_XFER_TXTX, \
  227. .address = addr, \
  228. .primary_length = tx_len, \
  229. .secondary_length = tx_len2, \
  230. .p_primary_buf = p_tx, \
  231. .p_secondary_buf = p_tx2, \
  232. }
  233. /**
  234. * @brief Structure for a TWI event.
  235. */
  236. typedef struct
  237. {
  238. nrf_drv_twi_evt_type_t type; ///< Event type.
  239. nrf_drv_twi_xfer_desc_t xfer_desc; ///< Transfer details.
  240. } nrf_drv_twi_evt_t;
  241. /**
  242. * @brief TWI event handler prototype.
  243. */
  244. typedef void (* nrf_drv_twi_evt_handler_t)(nrf_drv_twi_evt_t const * p_event,
  245. void * p_context);
  246. /**
  247. * @brief Function for initializing the TWI driver instance.
  248. *
  249. * @param[in] p_instance Pointer to the driver instance structure.
  250. * @param[in] p_config Initial configuration.
  251. * @param[in] event_handler Event handler provided by the user. If NULL, blocking mode is enabled.
  252. * @param[in] p_context Context passed to event handler.
  253. *
  254. * @retval NRF_SUCCESS If initialization was successful.
  255. * @retval NRF_ERROR_INVALID_STATE If the driver is in invalid state.
  256. * @retval NRF_ERROR_BUSY If some other peripheral with the same
  257. * instance ID is already in use. This is
  258. * possible only if PERIPHERAL_RESOURCE_SHARING_ENABLED
  259. * is set to a value other than zero.
  260. */
  261. ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const * p_instance,
  262. nrf_drv_twi_config_t const * p_config,
  263. nrf_drv_twi_evt_handler_t event_handler,
  264. void * p_context);
  265. /**
  266. * @brief Function for uninitializing the TWI instance.
  267. *
  268. * @param[in] p_instance Pointer to the driver instance structure.
  269. */
  270. __STATIC_INLINE
  271. void nrf_drv_twi_uninit(nrf_drv_twi_t const * p_instance);
  272. /**
  273. * @brief Function for enabling the TWI instance.
  274. *
  275. * @param[in] p_instance Pointer to the driver instance structure.
  276. */
  277. __STATIC_INLINE
  278. void nrf_drv_twi_enable(nrf_drv_twi_t const * p_instance);
  279. /**
  280. * @brief Function for disabling the TWI instance.
  281. *
  282. * @param[in] p_instance Pointer to the driver instance structure.
  283. */
  284. __STATIC_INLINE
  285. void nrf_drv_twi_disable(nrf_drv_twi_t const * p_instance);
  286. /**
  287. * @brief Function for sending data to a TWI slave.
  288. *
  289. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  290. * the function returns the error code @ref NRF_ERROR_BUSY.
  291. *
  292. * @param[in] p_instance Pointer to the driver instance structure.
  293. * @param[in] address Address of a specific slave device (only 7 LSB).
  294. * @param[in] p_data Pointer to a transmit buffer.
  295. * @param[in] length Number of bytes to send.
  296. * @param[in] no_stop If set, the stop condition is not generated on the bus
  297. * after the transfer has completed successfully (allowing
  298. * for a repeated start in the next transfer).
  299. *
  300. * @retval NRF_SUCCESS If the procedure was successful.
  301. * @retval NRF_ERROR_BUSY If the driver is not ready for a new transfer.
  302. * @retval NRF_ERROR_INTERNAL If an error was detected by hardware.
  303. * @retval NRF_ERROR_INVALID_ADDR If the EasyDMA is used and memory adress in not in RAM.
  304. * @retval NRF_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address in polling mode.
  305. * @retval NRF_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte in polling mode.
  306. */
  307. __STATIC_INLINE
  308. ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
  309. uint8_t address,
  310. uint8_t const * p_data,
  311. uint8_t length,
  312. bool no_stop);
  313. /**
  314. * @brief Function for reading data from a TWI slave.
  315. *
  316. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  317. * the function returns the error code @ref NRF_ERROR_BUSY.
  318. *
  319. * @param[in] p_instance Pointer to the driver instance structure.
  320. * @param[in] address Address of a specific slave device (only 7 LSB).
  321. * @param[in] p_data Pointer to a receive buffer.
  322. * @param[in] length Number of bytes to be received.
  323. *
  324. * @retval NRF_SUCCESS If the procedure was successful.
  325. * @retval NRF_ERROR_BUSY If the driver is not ready for a new transfer.
  326. * @retval NRF_ERROR_INTERNAL If an error was detected by hardware.
  327. * @retval NRF_ERROR_DRV_TWI_ERR_OVERRUN If the unread data was replaced by new data
  328. * @retval NRF_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address in polling mode.
  329. * @retval NRF_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte in polling mode.
  330. */
  331. __STATIC_INLINE
  332. ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,
  333. uint8_t address,
  334. uint8_t * p_data,
  335. uint8_t length);
  336. /**
  337. * @brief Function for preparing a TWI transfer.
  338. *
  339. * The following transfer types can be configured (@ref nrf_drv_twi_xfer_desc_t::type):
  340. * - @ref NRF_DRV_TWI_XFER_TXRX<span></span>: Write operation followed by a read operation (without STOP condition in between).
  341. * - @ref NRF_DRV_TWI_XFER_TXTX<span></span>: Write operation followed by a write operation (without STOP condition in between).
  342. * - @ref NRF_DRV_TWI_XFER_TX<span></span>: Write operation (with or without STOP condition).
  343. * - @ref NRF_DRV_TWI_XFER_RX<span></span>: Read operation (with STOP condition).
  344. *
  345. * Additional options are provided using the flags parameter:
  346. * - @ref NRF_DRV_TWI_FLAG_TX_POSTINC and @ref NRF_DRV_TWI_FLAG_RX_POSTINC<span></span>: Post-incrementation of buffer addresses. Supported only by TWIM.
  347. * - @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER<span></span>: No user event handler after transfer completion. In most cases, this also means no interrupt at the end of the transfer.
  348. * - @ref NRF_DRV_TWI_FLAG_HOLD_XFER<span></span>: Driver is not starting the transfer. Use this flag if the transfer is triggered externally by PPI. Supported only by TWIM.
  349. * Use @ref nrf_drv_twi_start_task_get to get the address of the start task.
  350. * - @ref NRF_DRV_TWI_FLAG_REPEATED_XFER<span></span>: Prepare for repeated transfers. You can set up a number of transfers that will be triggered externally (for example by PPI).
  351. * An example is a TXRX transfer with the options @ref NRF_DRV_TWI_FLAG_RX_POSTINC, @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER, and @ref NRF_DRV_TWI_FLAG_REPEATED_XFER.
  352. * After the transfer is set up, a set of transfers can be triggered by PPI that will read, for example, the same register of an
  353. * external component and put it into a RAM buffer without any interrupts. @ref nrf_drv_twi_stopped_event_get can be used to get the
  354. * address of the STOPPED event, which can be used to count the number of transfers. If @ref NRF_DRV_TWI_FLAG_REPEATED_XFER is used,
  355. * the driver does not set the driver instance into busy state, so you must ensure that the next transfers are set up
  356. * when TWIM is not active. Supported only by TWIM.
  357. * - @ref NRF_DRV_TWI_FLAG_TX_NO_STOP<span></span>: No stop condition after TX transfer.
  358. *
  359. * @note
  360. * Some flag combinations are invalid:
  361. * - @ref NRF_DRV_TWI_FLAG_TX_NO_STOP with @ref nrf_drv_twi_xfer_desc_t::type different than @ref NRF_DRV_TWI_XFER_TX
  362. * - @ref NRF_DRV_TWI_FLAG_REPEATED_XFER with @ref nrf_drv_twi_xfer_desc_t::type set to @ref NRF_DRV_TWI_XFER_TXTX
  363. *
  364. * If @ref nrf_drv_twi_xfer_desc_t::type is set to @ref NRF_DRV_TWI_XFER_TX and the @ref NRF_DRV_TWI_FLAG_TX_NO_STOP and @ref NRF_DRV_TWI_FLAG_REPEATED_XFER
  365. * flags are set, two tasks must be used to trigger a transfer: TASKS_RESUME followed by TASKS_STARTTX. If no stop condition is generated,
  366. * TWIM is in SUSPENDED state. Therefore, it must be resumed before the transfer can be started.
  367. *
  368. * @note
  369. * This function should be used only if the instance is configured to work in non-blocking mode. If the function is used in blocking mode, the driver asserts.
  370. * @note If you are using this function with TWI, the only supported flag is @ref NRF_DRV_TWI_FLAG_TX_NO_STOP. All other flags require TWIM.
  371. *
  372. * @param[in] p_instance Pointer to the driver instance structure.
  373. * @param[in] p_xfer_desc Pointer to the transfer descriptor.
  374. * @param[in] flags Transfer options (0 for default settings).
  375. *
  376. * @retval NRF_SUCCESS If the procedure was successful.
  377. * @retval NRF_ERROR_BUSY If the driver is not ready for a new transfer.
  378. * @retval NRF_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  379. * @retval NRF_ERROR_INTERNAL If an error was detected by hardware.
  380. * @retval NRF_ERROR_INVALID_ADDR If the EasyDMA is used and memory adress in not in RAM
  381. * @retval NRF_ERROR_DRV_TWI_ERR_OVERRUN If the unread data was replaced by new data (TXRX and RX)
  382. * @retval NRF_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address.
  383. * @retval NRF_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte.
  384. */
  385. __STATIC_INLINE
  386. ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t const * p_instance,
  387. nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
  388. uint32_t flags);
  389. /**
  390. * @brief Function for checking the TWI driver state.
  391. *
  392. * @param[in] p_instance TWI instance.
  393. *
  394. * @retval true If the TWI driver is currently busy performing a transfer.
  395. * @retval false If the TWI driver is ready for a new transfer.
  396. */
  397. __STATIC_INLINE
  398. bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance);
  399. /**
  400. * @brief Function for getting the transferred data count.
  401. *
  402. * This function provides valid results only in legacy mode.
  403. *
  404. * @param[in] p_instance Pointer to the driver instance structure.
  405. *
  406. * @return Data count.
  407. */
  408. __STATIC_INLINE
  409. uint32_t nrf_drv_twi_data_count_get(nrf_drv_twi_t const * const p_instance);
  410. /**
  411. * @brief Function for returning the address of a TWI/TWIM start task.
  412. *
  413. * This function should be used if @ref nrf_drv_twi_xfer was called with the flag @ref NRF_DRV_TWI_FLAG_HOLD_XFER.
  414. * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
  415. *
  416. * @param[in] p_instance Pointer to the driver instance structure.
  417. * @param[in] xfer_type Transfer type used in the last call of the @ref nrf_drv_twi_xfer function.
  418. *
  419. * @return Start task address (TX or RX) depending on the value of xfer_type.
  420. */
  421. __STATIC_INLINE
  422. uint32_t nrf_drv_twi_start_task_get(nrf_drv_twi_t const * p_instance, nrf_drv_twi_xfer_type_t xfer_type);
  423. /**
  424. * @brief Function for returning the address of a STOPPED TWI/TWIM event.
  425. *
  426. * A STOPPED event can be used to detect the end of a transfer if the @ref NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER
  427. * option is used.
  428. *
  429. * @param[in] p_instance Pointer to the driver instance structure.
  430. *
  431. * @return STOPPED event address.
  432. */
  433. __STATIC_INLINE
  434. uint32_t nrf_drv_twi_stopped_event_get(nrf_drv_twi_t const * p_instance);
  435. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  436. #if defined(TWI_PRESENT) && !defined(TWIM_PRESENT)
  437. #define NRF_DRV_TWI_WITH_TWI
  438. #elif !defined(TWI_PRESENT) && defined(TWIM_PRESENT)
  439. #define NRF_DRV_TWI_WITH_TWIM
  440. #else
  441. #if (NRFX_CHECK(TWI0_ENABLED) && NRFX_CHECK(TWI0_USE_EASY_DMA)) || \
  442. (NRFX_CHECK(TWI1_ENABLED) && NRFX_CHECK(TWI1_USE_EASY_DMA))
  443. #define NRF_DRV_TWI_WITH_TWIM
  444. #endif
  445. #if (NRFX_CHECK(TWI0_ENABLED) && !NRFX_CHECK(TWI0_USE_EASY_DMA)) || \
  446. (NRFX_CHECK(TWI1_ENABLED) && !NRFX_CHECK(TWI1_USE_EASY_DMA))
  447. #define NRF_DRV_TWI_WITH_TWI
  448. #endif
  449. #endif
  450. #if defined(NRF_DRV_TWI_WITH_TWIM) && defined(NRF_DRV_TWI_WITH_TWI)
  451. #define NRF_DRV_TWI_USE_TWIM (p_instance->use_easy_dma)
  452. #elif defined(NRF_DRV_TWI_WITH_TWIM)
  453. #define NRF_DRV_TWI_USE_TWIM true
  454. #else
  455. #define NRF_DRV_TWI_USE_TWIM false
  456. #endif
  457. #define NRF_DRV_TWI_USE_TWI (!NRF_DRV_TWI_USE_TWIM)
  458. __STATIC_INLINE
  459. void nrf_drv_twi_uninit(nrf_drv_twi_t const * p_instance)
  460. {
  461. if (NRF_DRV_TWI_USE_TWIM)
  462. {
  463. nrfx_twim_uninit(&p_instance->u.twim);
  464. }
  465. else if (NRF_DRV_TWI_USE_TWI)
  466. {
  467. nrfx_twi_uninit(&p_instance->u.twi);
  468. }
  469. }
  470. __STATIC_INLINE
  471. void nrf_drv_twi_enable(nrf_drv_twi_t const * p_instance)
  472. {
  473. if (NRF_DRV_TWI_USE_TWIM)
  474. {
  475. nrfx_twim_enable(&p_instance->u.twim);
  476. }
  477. else if (NRF_DRV_TWI_USE_TWI)
  478. {
  479. nrfx_twi_enable(&p_instance->u.twi);
  480. }
  481. }
  482. __STATIC_INLINE
  483. void nrf_drv_twi_disable(nrf_drv_twi_t const * p_instance)
  484. {
  485. if (NRF_DRV_TWI_USE_TWIM)
  486. {
  487. nrfx_twim_disable(&p_instance->u.twim);
  488. }
  489. else if (NRF_DRV_TWI_USE_TWI)
  490. {
  491. nrfx_twi_disable(&p_instance->u.twi);
  492. }
  493. }
  494. __STATIC_INLINE
  495. ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
  496. uint8_t address,
  497. uint8_t const * p_data,
  498. uint8_t length,
  499. bool no_stop)
  500. {
  501. ret_code_t result = 0;
  502. if (NRF_DRV_TWI_USE_TWIM)
  503. {
  504. result = nrfx_twim_tx(&p_instance->u.twim,
  505. address, p_data, length, no_stop);
  506. }
  507. else if (NRF_DRV_TWI_USE_TWI)
  508. {
  509. result = nrfx_twi_tx(&p_instance->u.twi,
  510. address, p_data, length, no_stop);
  511. }
  512. return result;
  513. }
  514. __STATIC_INLINE
  515. ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,
  516. uint8_t address,
  517. uint8_t * p_data,
  518. uint8_t length)
  519. {
  520. ret_code_t result = 0;
  521. if (NRF_DRV_TWI_USE_TWIM)
  522. {
  523. result = nrfx_twim_rx(&p_instance->u.twim,
  524. address, p_data, length);
  525. }
  526. else if (NRF_DRV_TWI_USE_TWI)
  527. {
  528. result = nrfx_twi_rx(&p_instance->u.twi,
  529. address, p_data, length);
  530. }
  531. return result;
  532. }
  533. __STATIC_INLINE
  534. ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t const * p_instance,
  535. nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
  536. uint32_t flags)
  537. {
  538. ret_code_t result = 0;
  539. if (NRF_DRV_TWI_USE_TWIM)
  540. {
  541. #ifdef TWIM_PRESENT
  542. nrfx_twim_xfer_desc_t const twim_xfer_desc =
  543. {
  544. .type = (nrfx_twim_xfer_type_t)p_xfer_desc->type,
  545. .address = p_xfer_desc->address,
  546. .primary_length = p_xfer_desc->primary_length,
  547. .secondary_length = p_xfer_desc->secondary_length,
  548. .p_primary_buf = p_xfer_desc->p_primary_buf,
  549. .p_secondary_buf = p_xfer_desc->p_secondary_buf,
  550. };
  551. result = nrfx_twim_xfer(&p_instance->u.twim, &twim_xfer_desc, flags);
  552. #endif
  553. }
  554. else if (NRF_DRV_TWI_USE_TWI)
  555. {
  556. #ifdef TWI_PRESENT
  557. nrfx_twi_xfer_desc_t const twi_xfer_desc =
  558. {
  559. .type = (nrfx_twi_xfer_type_t)p_xfer_desc->type,
  560. .address = p_xfer_desc->address,
  561. .primary_length = p_xfer_desc->primary_length,
  562. .secondary_length = p_xfer_desc->secondary_length,
  563. .p_primary_buf = p_xfer_desc->p_primary_buf,
  564. .p_secondary_buf = p_xfer_desc->p_secondary_buf,
  565. };
  566. result = nrfx_twi_xfer(&p_instance->u.twi, &twi_xfer_desc, flags);
  567. #endif
  568. }
  569. return result;
  570. }
  571. __STATIC_INLINE
  572. bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance)
  573. {
  574. bool result = 0;
  575. if (NRF_DRV_TWI_USE_TWIM)
  576. {
  577. result = nrfx_twim_is_busy(&p_instance->u.twim);
  578. }
  579. else if (NRF_DRV_TWI_USE_TWI)
  580. {
  581. result = nrfx_twi_is_busy(&p_instance->u.twi);
  582. }
  583. return result;
  584. }
  585. __STATIC_INLINE
  586. uint32_t nrf_drv_twi_data_count_get(nrf_drv_twi_t const * const p_instance)
  587. {
  588. uint32_t result = 0;
  589. if (NRF_DRV_TWI_USE_TWIM)
  590. {
  591. NRFX_ASSERT(false); // not supported
  592. result = 0;
  593. }
  594. else if (NRF_DRV_TWI_USE_TWI)
  595. {
  596. result = nrfx_twi_data_count_get(&p_instance->u.twi);
  597. }
  598. return result;
  599. }
  600. __STATIC_INLINE
  601. uint32_t nrf_drv_twi_start_task_get(nrf_drv_twi_t const * p_instance,
  602. nrf_drv_twi_xfer_type_t xfer_type)
  603. {
  604. uint32_t result = 0;
  605. if (NRF_DRV_TWI_USE_TWIM)
  606. {
  607. result = nrfx_twim_start_task_get(&p_instance->u.twim,
  608. (nrfx_twim_xfer_type_t)xfer_type);
  609. }
  610. else if (NRF_DRV_TWI_USE_TWI)
  611. {
  612. NRFX_ASSERT(false); // not supported
  613. result = 0;
  614. }
  615. return result;
  616. }
  617. __STATIC_INLINE
  618. uint32_t nrf_drv_twi_stopped_event_get(nrf_drv_twi_t const * p_instance)
  619. {
  620. uint32_t result = 0;
  621. if (NRF_DRV_TWI_USE_TWIM)
  622. {
  623. result = nrfx_twim_stopped_event_get(&p_instance->u.twim);
  624. }
  625. else if (NRF_DRV_TWI_USE_TWI)
  626. {
  627. result = nrfx_twi_stopped_event_get(&p_instance->u.twi);
  628. }
  629. return result;
  630. }
  631. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  632. /** @} */
  633. #ifdef __cplusplus
  634. }
  635. #endif
  636. #endif // NRF_DRV_TWI_H__