nrfx_twi.h 15 KB

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