nrfx_twi.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 NRFX_TWI_H__
  41. #define NRFX_TWI_H__
  42. #include <nrfx.h>
  43. #include <nrfx_twi_twim.h>
  44. #include <hal/nrf_twi.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_twi TWI driver
  50. * @{
  51. * @ingroup nrf_twi
  52. * @brief Two Wire Interface master (TWI) peripheral driver.
  53. */
  54. /**
  55. * @brief Structure for the TWI master driver instance.
  56. */
  57. typedef struct
  58. {
  59. NRF_TWI_Type * p_twi; ///< Pointer to a structure with TWI registers.
  60. uint8_t drv_inst_idx; ///< Index of the driver instance. For internal use only.
  61. } nrfx_twi_t;
  62. /** @brief Macro for creating a TWI master driver instance. */
  63. #define NRFX_TWI_INSTANCE(id) \
  64. { \
  65. .p_twi = NRFX_CONCAT_2(NRF_TWI, id), \
  66. .drv_inst_idx = NRFX_CONCAT_3(NRFX_TWI, id, _INST_IDX), \
  67. }
  68. #ifndef __NRFX_DOXYGEN__
  69. enum {
  70. #if NRFX_CHECK(NRFX_TWI0_ENABLED)
  71. NRFX_TWI0_INST_IDX,
  72. #endif
  73. #if NRFX_CHECK(NRFX_TWI1_ENABLED)
  74. NRFX_TWI1_INST_IDX,
  75. #endif
  76. NRFX_TWI_ENABLED_COUNT
  77. };
  78. #endif
  79. /** @brief Structure for the configuration of the TWI master driver instance. */
  80. typedef struct
  81. {
  82. uint32_t scl; ///< SCL pin number.
  83. uint32_t sda; ///< SDA pin number.
  84. nrf_twi_frequency_t frequency; ///< TWI frequency.
  85. uint8_t interrupt_priority; ///< Interrupt priority.
  86. bool hold_bus_uninit; ///< Hold pull up state on GPIO pins after uninit.
  87. } nrfx_twi_config_t;
  88. /** @brief The default configuration of the TWI master driver instance. */
  89. #define NRFX_TWI_DEFAULT_CONFIG \
  90. { \
  91. .frequency = (nrf_twi_frequency_t)NRFX_TWI_DEFAULT_CONFIG_FREQUENCY, \
  92. .scl = 31, \
  93. .sda = 31, \
  94. .interrupt_priority = NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY, \
  95. .hold_bus_uninit = NRFX_TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT, \
  96. }
  97. /** @brief Flag indicating that the interrupt after each transfer will be suppressed, and the event handler will not be called. */
  98. #define NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER (1UL << 2)
  99. /** @brief Flag indicating that the TX transfer will not end with a stop condition. */
  100. #define NRFX_TWI_FLAG_TX_NO_STOP (1UL << 5)
  101. /** @brief Flag indicating that the transfer will be suspended. */
  102. #define NRFX_TWI_FLAG_SUSPEND (1UL << 6)
  103. /** @brief TWI master driver event types. */
  104. typedef enum
  105. {
  106. NRFX_TWI_EVT_DONE, ///< Transfer completed event.
  107. NRFX_TWI_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
  108. NRFX_TWI_EVT_DATA_NACK, ///< Error event: NACK received after sending a data byte.
  109. NRFX_TWI_EVT_OVERRUN, ///< Error event: The unread data is replaced by new data.
  110. NRFX_TWI_EVT_BUS_ERROR ///< Error event: An unexpected transition occurred on the bus.
  111. } nrfx_twi_evt_type_t;
  112. /** @brief TWI master driver transfer types. */
  113. typedef enum
  114. {
  115. NRFX_TWI_XFER_TX, ///< TX transfer.
  116. NRFX_TWI_XFER_RX, ///< RX transfer.
  117. NRFX_TWI_XFER_TXRX, ///< TX transfer followed by RX transfer with repeated start.
  118. NRFX_TWI_XFER_TXTX ///< TX transfer followed by TX transfer with repeated start.
  119. } nrfx_twi_xfer_type_t;
  120. /** @brief Structure for a TWI transfer descriptor. */
  121. typedef struct
  122. {
  123. nrfx_twi_xfer_type_t type; ///< Type of transfer.
  124. uint8_t address; ///< Slave address.
  125. size_t primary_length; ///< Number of bytes transferred.
  126. size_t secondary_length; ///< Number of bytes transferred.
  127. uint8_t * p_primary_buf; ///< Pointer to transferred data.
  128. uint8_t * p_secondary_buf; ///< Pointer to transferred data.
  129. } nrfx_twi_xfer_desc_t;
  130. /** @brief Macro for setting the TX transfer descriptor. */
  131. #define NRFX_TWI_XFER_DESC_TX(addr, p_data, length) \
  132. { \
  133. .type = NRFX_TWI_XFER_TX, \
  134. .address = (addr), \
  135. .primary_length = (length), \
  136. .secondary_length = 0, \
  137. .p_primary_buf = (p_data), \
  138. .p_secondary_buf = NULL, \
  139. }
  140. /** @brief Macro for setting the RX transfer descriptor. */
  141. #define NRFX_TWI_XFER_DESC_RX(addr, p_data, length) \
  142. { \
  143. .type = NRFX_TWI_XFER_RX, \
  144. .address = (addr), \
  145. .primary_length = (length), \
  146. .secondary_length = 0, \
  147. .p_primary_buf = (p_data), \
  148. .p_secondary_buf = NULL, \
  149. }
  150. /** @brief Macro for setting the TX-RX transfer descriptor. */
  151. #define NRFX_TWI_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len) \
  152. { \
  153. .type = NRFX_TWI_XFER_TXRX, \
  154. .address = (addr), \
  155. .primary_length = (tx_len), \
  156. .secondary_length = (rx_len), \
  157. .p_primary_buf = (p_tx), \
  158. .p_secondary_buf = (p_rx), \
  159. }
  160. /** @brief Macro for setting the TX-TX transfer descriptor. */
  161. #define NRFX_TWI_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
  162. { \
  163. .type = NRFX_TWI_XFER_TXTX, \
  164. .address = (addr), \
  165. .primary_length = (tx_len), \
  166. .secondary_length = (tx_len2), \
  167. .p_primary_buf = (p_tx), \
  168. .p_secondary_buf = (p_tx2), \
  169. }
  170. /** @brief Structure for a TWI event. */
  171. typedef struct
  172. {
  173. nrfx_twi_evt_type_t type; ///< Event type.
  174. nrfx_twi_xfer_desc_t xfer_desc; ///< Transfer details.
  175. } nrfx_twi_evt_t;
  176. /** @brief TWI event handler prototype. */
  177. typedef void (* nrfx_twi_evt_handler_t)(nrfx_twi_evt_t const * p_event,
  178. void * p_context);
  179. /**
  180. * @brief Function for initializing the TWI driver instance.
  181. *
  182. * @param[in] p_instance Pointer to the driver instance structure.
  183. * @param[in] p_config Pointer to the structure with the initial configuration.
  184. * @param[in] event_handler Event handler provided by the user. If NULL, blocking mode is enabled.
  185. * @param[in] p_context Context passed to event handler.
  186. *
  187. * @retval NRFX_SUCCESS Initialization is successful.
  188. * @retval NRFX_ERROR_INVALID_STATE The driver is in invalid state.
  189. * @retval NRFX_ERROR_BUSY Some other peripheral with the same
  190. * instance ID is already in use. This is
  191. * possible only if @ref nrfx_prs module
  192. * is enabled.
  193. */
  194. nrfx_err_t nrfx_twi_init(nrfx_twi_t const * p_instance,
  195. nrfx_twi_config_t const * p_config,
  196. nrfx_twi_evt_handler_t event_handler,
  197. void * p_context);
  198. /**
  199. * @brief Function for uninitializing the TWI instance.
  200. *
  201. * @param[in] p_instance Pointer to the driver instance structure.
  202. */
  203. void nrfx_twi_uninit(nrfx_twi_t const * p_instance);
  204. /**
  205. * @brief Function for enabling the TWI instance.
  206. *
  207. * @param[in] p_instance Pointer to the driver instance structure.
  208. */
  209. void nrfx_twi_enable(nrfx_twi_t const * p_instance);
  210. /**
  211. * @brief Function for disabling the TWI instance.
  212. *
  213. * @param[in] p_instance Pointer to the driver instance structure.
  214. */
  215. void nrfx_twi_disable(nrfx_twi_t const * p_instance);
  216. /**
  217. * @brief Function for sending data to a TWI slave.
  218. *
  219. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  220. * the function returns the error code @ref NRFX_ERROR_BUSY.
  221. *
  222. * @note This function is deprecated. Use @ref nrfx_twi_xfer instead.
  223. *
  224. * @param[in] p_instance Pointer to the driver instance structure.
  225. * @param[in] address Address of a specific slave device (only 7 LSB).
  226. * @param[in] p_data Pointer to a transmit buffer.
  227. * @param[in] length Number of bytes to send.
  228. * @param[in] no_stop If set, the stop condition is not generated on the bus
  229. * after the transfer has completed successfully (allowing
  230. * for a repeated start in the next transfer).
  231. *
  232. * @retval NRFX_SUCCESS The procedure is successful.
  233. * @retval NRFX_ERROR_BUSY The driver is not ready for a new transfer.
  234. * @retval NRFX_ERROR_INTERNAL An error is detected by hardware.
  235. * @retval NRFX_ERROR_INVALID_STATE RX transaction is suspended on bus.
  236. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK Negative acknowledgement (NACK) is received after sending
  237. * the address in polling mode.
  238. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK Negative acknowledgement (NACK) is received after sending
  239. * a data byte in polling mode.
  240. */
  241. nrfx_err_t nrfx_twi_tx(nrfx_twi_t const * p_instance,
  242. uint8_t address,
  243. uint8_t const * p_data,
  244. size_t length,
  245. bool no_stop);
  246. /**
  247. * @brief Function for reading data from a TWI slave.
  248. *
  249. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  250. * the function returns the error code @ref NRFX_ERROR_BUSY.
  251. *
  252. * @note This function is deprecated. Use @ref nrfx_twi_xfer instead.
  253. *
  254. * @param[in] p_instance Pointer to the driver instance structure.
  255. * @param[in] address Address of a specific slave device (only 7 LSB).
  256. * @param[in] p_data Pointer to a receive buffer.
  257. * @param[in] length Number of bytes to be received.
  258. *
  259. * @retval NRFX_SUCCESS The procedure is successful.
  260. * @retval NRFX_ERROR_BUSY The driver is not ready for a new transfer.
  261. * @retval NRFX_ERROR_INTERNAL An error is detected by hardware.
  262. * @retval NRFX_ERROR_INVALID_STATE TX transaction is suspended on bus.
  263. * @retval NRFX_ERROR_DRV_TWI_ERR_OVERRUN The unread data is replaced by new data.
  264. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK Negative acknowledgement (NACK) is received after sending
  265. * the address in polling mode.
  266. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK Negative acknowledgement (NACK) is received after sending
  267. * a data byte in polling mode.
  268. */
  269. nrfx_err_t nrfx_twi_rx(nrfx_twi_t const * p_instance,
  270. uint8_t address,
  271. uint8_t * p_data,
  272. size_t length);
  273. /**
  274. * @brief Function for performing a TWI transfer.
  275. *
  276. * The following transfer types can be configured (@ref nrfx_twi_xfer_desc_t::type):
  277. * - @ref NRFX_TWI_XFER_TXRX - Write operation followed by a read operation (without STOP condition in between).
  278. * - @ref NRFX_TWI_XFER_TXTX - Write operation followed by a write operation (without STOP condition in between).
  279. * - @ref NRFX_TWI_XFER_TX - Write operation (with or without STOP condition).
  280. * - @ref NRFX_TWI_XFER_RX - Read operation (with STOP condition).
  281. *
  282. * @note TX-RX and TX-TX transfers are supported only in non-blocking mode.
  283. *
  284. * Additional options are provided using the flags parameter:
  285. * - @ref NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER - No user event handler after transfer completion. In most cases, this also means no interrupt at the end of the transfer.
  286. * - @ref NRFX_TWI_FLAG_TX_NO_STOP - No stop condition after TX transfer.
  287. * - @ref NRFX_TWI_FLAG_SUSPEND - Transfer will be suspended. This allows for combining multiple transfers into one transaction.
  288. * Only transactions with the same direction can be combined. To finish the transaction, call the function without this flag.
  289. *
  290. * @note
  291. * Some flag combinations are invalid:
  292. * - @ref NRFX_TWI_FLAG_TX_NO_STOP with @ref nrfx_twi_xfer_desc_t::type different than @ref NRFX_TWI_XFER_TX
  293. *
  294. * @param[in] p_instance Pointer to the driver instance structure.
  295. * @param[in] p_xfer_desc Pointer to the transfer descriptor.
  296. * @param[in] flags Transfer options (0 for default settings).
  297. *
  298. * @retval NRFX_SUCCESS The procedure is successful.
  299. * @retval NRFX_ERROR_BUSY The driver is not ready for a new transfer.
  300. * @retval NRFX_ERROR_NOT_SUPPORTED The provided parameters are not supported.
  301. * @retval NRFX_ERROR_INTERNAL An unexpected transition occurred on the bus.
  302. * @retval NRFX_ERROR_INVALID_STATE Other direction of transaction is suspended on the bus.
  303. * @retval NRFX_ERROR_DRV_TWI_ERR_OVERRUN The unread data is replaced by new data (TXRX and RX)
  304. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK Negative acknowledgement (NACK) is received after sending
  305. * the address in polling mode.
  306. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK Negative acknowledgement (NACK) is received after sending
  307. * a data byte in polling mode.
  308. */
  309. nrfx_err_t nrfx_twi_xfer(nrfx_twi_t const * p_instance,
  310. nrfx_twi_xfer_desc_t const * p_xfer_desc,
  311. uint32_t flags);
  312. /**
  313. * @brief Function for checking the TWI driver state.
  314. *
  315. * @param[in] p_instance TWI instance.
  316. *
  317. * @retval true The TWI driver is currently busy performing a transfer.
  318. * @retval false The TWI driver is ready for a new transfer.
  319. */
  320. bool nrfx_twi_is_busy(nrfx_twi_t const * p_instance);
  321. /**
  322. * @brief Function for getting the transferred data count.
  323. *
  324. * @param[in] p_instance Pointer to the driver instance structure.
  325. *
  326. * @return Data count.
  327. */
  328. size_t nrfx_twi_data_count_get(nrfx_twi_t const * const p_instance);
  329. /**
  330. * @brief Function for returning the address of a STOPPED TWI event.
  331. *
  332. * A STOPPED event can be used to detect the end of a transfer if the @ref NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER
  333. * option is used.
  334. *
  335. * @param[in] p_instance Pointer to the driver instance structure.
  336. *
  337. * @return STOPPED event address.
  338. */
  339. uint32_t nrfx_twi_stopped_event_get(nrfx_twi_t const * p_instance);
  340. /**
  341. * @brief Function for recovering the bus.
  342. *
  343. * This function checks if the bus is not stuck because of a slave holding the SDA line in the low state,
  344. * and if needed it performs required number of pulses on the SCL line to make the slave release the SDA line.
  345. * Finally, the function generates a STOP condition on the bus to put it into a known state.
  346. *
  347. * @note This function can be used only if the TWI driver is uninitialized.
  348. *
  349. * @param[in] scl_pin SCL pin number.
  350. * @param[in] sda_pin SDA pin number.
  351. *
  352. * @retval NRFX_SUCCESS Bus recovery was successful.
  353. * @retval NRFX_ERROR_INTERNAL Bus recovery failed.
  354. */
  355. __STATIC_INLINE nrfx_err_t nrfx_twi_bus_recover(uint32_t scl_pin, uint32_t sda_pin);
  356. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  357. __STATIC_INLINE nrfx_err_t nrfx_twi_bus_recover(uint32_t scl_pin, uint32_t sda_pin)
  358. {
  359. return nrfx_twi_twim_bus_recover(scl_pin, sda_pin);
  360. }
  361. #endif
  362. /** @} */
  363. void nrfx_twi_0_irq_handler(void);
  364. void nrfx_twi_1_irq_handler(void);
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif // NRFX_TWI_H__