nrfx_twim.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * Copyright (c) 2015 - 2019, 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_TWIM_H__
  41. #define NRFX_TWIM_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_twim.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_twim TWIM driver
  49. * @{
  50. * @ingroup nrf_twim
  51. * @brief TWIM peripheral driver.
  52. */
  53. /**
  54. * @brief Structure for the TWI master driver instance.
  55. */
  56. typedef struct
  57. {
  58. NRF_TWIM_Type * p_twim; ///< Pointer to a structure with TWIM registers.
  59. uint8_t drv_inst_idx; ///< Driver instance index.
  60. } nrfx_twim_t;
  61. /**
  62. * @brief Macro for creating a TWI master driver instance.
  63. */
  64. #define NRFX_TWIM_INSTANCE(id) \
  65. { \
  66. .p_twim = NRFX_CONCAT_2(NRF_TWIM, id), \
  67. .drv_inst_idx = NRFX_CONCAT_3(NRFX_TWIM, id, _INST_IDX), \
  68. }
  69. enum {
  70. #if NRFX_CHECK(NRFX_TWIM0_ENABLED)
  71. NRFX_TWIM0_INST_IDX,
  72. #endif
  73. #if NRFX_CHECK(NRFX_TWIM1_ENABLED)
  74. NRFX_TWIM1_INST_IDX,
  75. #endif
  76. #if NRFX_CHECK(NRFX_TWIM2_ENABLED)
  77. NRFX_TWIM2_INST_IDX,
  78. #endif
  79. #if NRFX_CHECK(NRFX_TWIM3_ENABLED)
  80. NRFX_TWIM3_INST_IDX,
  81. #endif
  82. NRFX_TWIM_ENABLED_COUNT
  83. };
  84. /**
  85. * @brief Structure for the TWI master driver instance configuration.
  86. */
  87. typedef struct
  88. {
  89. uint32_t scl; ///< SCL pin number.
  90. uint32_t sda; ///< SDA pin number.
  91. nrf_twim_frequency_t frequency; ///< TWIM frequency.
  92. uint8_t interrupt_priority; ///< Interrupt priority.
  93. bool hold_bus_uninit; ///< Hold pull up state on gpio pins after uninit.
  94. } nrfx_twim_config_t;
  95. /**
  96. * @brief TWI master driver instance default configuration.
  97. */
  98. #define NRFX_TWIM_DEFAULT_CONFIG \
  99. { \
  100. .frequency = (nrf_twim_frequency_t)NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY, \
  101. .scl = 31, \
  102. .sda = 31, \
  103. .interrupt_priority = NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY, \
  104. .hold_bus_uninit = NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT, \
  105. }
  106. #define NRFX_TWIM_FLAG_TX_POSTINC (1UL << 0) /**< TX buffer address incremented after transfer. */
  107. #define NRFX_TWIM_FLAG_RX_POSTINC (1UL << 1) /**< RX buffer address incremented after transfer. */
  108. #define NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
  109. #define NRFX_TWIM_FLAG_HOLD_XFER (1UL << 3) /**< Set up the transfer but do not start it. */
  110. #define NRFX_TWIM_FLAG_REPEATED_XFER (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
  111. #define NRFX_TWIM_FLAG_TX_NO_STOP (1UL << 5) /**< Flag indicating that the TX transfer will not end with a stop condition. */
  112. /**
  113. * @brief TWI master driver event types.
  114. */
  115. typedef enum
  116. {
  117. NRFX_TWIM_EVT_DONE, ///< Transfer completed event.
  118. NRFX_TWIM_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
  119. NRFX_TWIM_EVT_DATA_NACK ///< Error event: NACK received after sending a data byte.
  120. } nrfx_twim_evt_type_t;
  121. /**
  122. * @brief TWI master driver transfer types.
  123. */
  124. typedef enum
  125. {
  126. NRFX_TWIM_XFER_TX, ///< TX transfer.
  127. NRFX_TWIM_XFER_RX, ///< RX transfer.
  128. NRFX_TWIM_XFER_TXRX, ///< TX transfer followed by RX transfer with repeated start.
  129. NRFX_TWIM_XFER_TXTX ///< TX transfer followed by TX transfer with repeated start.
  130. } nrfx_twim_xfer_type_t;
  131. /**
  132. * @brief Structure for a TWI transfer descriptor.
  133. */
  134. typedef struct
  135. {
  136. nrfx_twim_xfer_type_t type; ///< Type of transfer.
  137. uint8_t address; ///< Slave address.
  138. size_t primary_length; ///< Number of bytes transferred.
  139. size_t secondary_length; ///< Number of bytes transferred.
  140. uint8_t * p_primary_buf; ///< Pointer to transferred data.
  141. uint8_t * p_secondary_buf; ///< Pointer to transferred data.
  142. } nrfx_twim_xfer_desc_t;
  143. /**@brief Macro for setting the TX transfer descriptor. */
  144. #define NRFX_TWIM_XFER_DESC_TX(addr, p_data, length) \
  145. { \
  146. .type = NRFX_TWIM_XFER_TX, \
  147. .address = addr, \
  148. .primary_length = length, \
  149. .p_primary_buf = p_data, \
  150. }
  151. /**@brief Macro for setting the RX transfer descriptor. */
  152. #define NRFX_TWIM_XFER_DESC_RX(addr, p_data, length) \
  153. { \
  154. .type = NRFX_TWIM_XFER_RX, \
  155. .address = addr, \
  156. .primary_length = length, \
  157. .p_primary_buf = p_data, \
  158. }
  159. /**@brief Macro for setting the TXRX transfer descriptor. */
  160. #define NRFX_TWIM_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len) \
  161. { \
  162. .type = NRFX_TWIM_XFER_TXRX, \
  163. .address = addr, \
  164. .primary_length = tx_len, \
  165. .secondary_length = rx_len, \
  166. .p_primary_buf = p_tx, \
  167. .p_secondary_buf = p_rx, \
  168. }
  169. /**@brief Macro for setting the TXTX transfer descriptor. */
  170. #define NRFX_TWIM_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2) \
  171. { \
  172. .type = NRFX_TWIM_XFER_TXTX, \
  173. .address = addr, \
  174. .primary_length = tx_len, \
  175. .secondary_length = tx_len2, \
  176. .p_primary_buf = p_tx, \
  177. .p_secondary_buf = p_tx2, \
  178. }
  179. /**
  180. * @brief Structure for a TWI event.
  181. */
  182. typedef struct
  183. {
  184. nrfx_twim_evt_type_t type; ///< Event type.
  185. nrfx_twim_xfer_desc_t xfer_desc; ///< Transfer details.
  186. } nrfx_twim_evt_t;
  187. /**
  188. * @brief TWI event handler prototype.
  189. */
  190. typedef void (* nrfx_twim_evt_handler_t)(nrfx_twim_evt_t const * p_event,
  191. void * p_context);
  192. /**
  193. * @brief Function for initializing the TWI driver instance.
  194. *
  195. * @param[in] p_instance Pointer to the driver instance structure.
  196. * @param[in] p_config Pointer to the structure with initial configuration.
  197. * @param[in] event_handler Event handler provided by the user. If NULL, blocking mode is enabled.
  198. * @param[in] p_context Context passed to event handler.
  199. *
  200. * @retval NRFX_SUCCESS If initialization was successful.
  201. * @retval NRFX_ERROR_INVALID_STATE If the driver is in invalid state.
  202. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  203. * instance ID is already in use. This is
  204. * possible only if @ref nrfx_prs module
  205. * is enabled.
  206. */
  207. nrfx_err_t nrfx_twim_init(nrfx_twim_t const * p_instance,
  208. nrfx_twim_config_t const * p_config,
  209. nrfx_twim_evt_handler_t event_handler,
  210. void * p_context);
  211. /**
  212. * @brief Function for uninitializing the TWI instance.
  213. *
  214. * @param[in] p_instance Pointer to the driver instance structure.
  215. */
  216. void nrfx_twim_uninit(nrfx_twim_t const * p_instance);
  217. /**
  218. * @brief Function for enabling the TWI instance.
  219. *
  220. * @param[in] p_instance Pointer to the driver instance structure.
  221. */
  222. void nrfx_twim_enable(nrfx_twim_t const * p_instance);
  223. /**
  224. * @brief Function for disabling the TWI instance.
  225. *
  226. * @param[in] p_instance Pointer to the driver instance structure.
  227. */
  228. void nrfx_twim_disable(nrfx_twim_t const * p_instance);
  229. /**
  230. * @brief Function for sending data to a TWI slave.
  231. *
  232. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  233. * the function returns the error code @ref NRFX_ERROR_BUSY.
  234. *
  235. * @note Peripherals using EasyDMA (including TWIM) require the transfer buffers
  236. * to be placed in the Data RAM region. If this condition is not met,
  237. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  238. *
  239. * @param[in] p_instance Pointer to the driver instance structure.
  240. * @param[in] address Address of a specific slave device (only 7 LSB).
  241. * @param[in] p_data Pointer to a transmit buffer.
  242. * @param[in] length Number of bytes to send. Maximum possible length is
  243. * dependent on the used SoC (see the MAXCNT register
  244. * description in the Product Specification). The driver
  245. * checks it with assertion.
  246. * @param[in] no_stop If set, the stop condition is not generated on the bus
  247. * after the transfer has completed successfully (allowing
  248. * for a repeated start in the next transfer).
  249. *
  250. * @retval NRFX_SUCCESS If the procedure was successful.
  251. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  252. * @retval NRFX_ERROR_INTERNAL If an error was detected by hardware.
  253. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffer is not placed in the Data RAM region.
  254. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address in polling mode.
  255. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte in polling mode.
  256. */
  257. nrfx_err_t nrfx_twim_tx(nrfx_twim_t const * p_instance,
  258. uint8_t address,
  259. uint8_t const * p_data,
  260. size_t length,
  261. bool no_stop);
  262. /**
  263. * @brief Function for reading data from a TWI slave.
  264. *
  265. * The transmission will be stopped when an error occurs. If a transfer is ongoing,
  266. * the function returns the error code @ref NRFX_ERROR_BUSY.
  267. *
  268. * @param[in] p_instance Pointer to the driver instance structure.
  269. * @param[in] address Address of a specific slave device (only 7 LSB).
  270. * @param[in] p_data Pointer to a receive buffer.
  271. * @param[in] length Number of bytes to be received. Maximum possible length
  272. * is dependent on the used SoC (see the MAXCNT register
  273. * description in the Product Specification). The driver
  274. * checks it with assertion.
  275. *
  276. * @retval NRFX_SUCCESS If the procedure was successful.
  277. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  278. * @retval NRFX_ERROR_INTERNAL If an error was detected by hardware.
  279. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address in polling mode.
  280. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte in polling mode.
  281. */
  282. nrfx_err_t nrfx_twim_rx(nrfx_twim_t const * p_instance,
  283. uint8_t address,
  284. uint8_t * p_data,
  285. size_t length);
  286. /**
  287. * @brief Function for preparing a TWI transfer.
  288. *
  289. * The following transfer types can be configured (@ref nrfx_twim_xfer_desc_t::type):
  290. * - @ref NRFX_TWIM_XFER_TXRX<span></span>: Write operation followed by a read operation (without STOP condition in between).
  291. * - @ref NRFX_TWIM_XFER_TXTX<span></span>: Write operation followed by a write operation (without STOP condition in between).
  292. * - @ref NRFX_TWIM_XFER_TX<span></span>: Write operation (with or without STOP condition).
  293. * - @ref NRFX_TWIM_XFER_RX<span></span>: Read operation (with STOP condition).
  294. *
  295. * @note TXRX and TXTX transfers are supported only in non-blocking mode.
  296. *
  297. * Additional options are provided using the flags parameter:
  298. * - @ref NRFX_TWIM_FLAG_TX_POSTINC and @ref NRFX_TWIM_FLAG_RX_POSTINC<span></span>: Post-incrementation of buffer addresses. Supported only by TWIM.
  299. * - @ref NRFX_TWIM_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.
  300. * - @ref NRFX_TWIM_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.
  301. * Use @ref nrfx_twim_start_task_get to get the address of the start task.
  302. * - @ref NRFX_TWIM_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).
  303. * An example is a TXRX transfer with the options @ref NRFX_TWIM_FLAG_RX_POSTINC, @ref NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER, and @ref NRFX_TWIM_FLAG_REPEATED_XFER.
  304. * 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
  305. * external component and put it into a RAM buffer without any interrupts. @ref nrfx_twim_stopped_event_get can be used to get the
  306. * address of the STOPPED event, which can be used to count the number of transfers. If @ref NRFX_TWIM_FLAG_REPEATED_XFER is used,
  307. * the driver does not set the driver instance into busy state, so you must ensure that the next transfers are set up
  308. * when TWIM is not active. Supported only by TWIM.
  309. * - @ref NRFX_TWIM_FLAG_TX_NO_STOP<span></span>: No stop condition after TX transfer.
  310. *
  311. * @note
  312. * Some flag combinations are invalid:
  313. * - @ref NRFX_TWIM_FLAG_TX_NO_STOP with @ref nrfx_twim_xfer_desc_t::type different than @ref NRFX_TWIM_XFER_TX
  314. * - @ref NRFX_TWIM_FLAG_REPEATED_XFER with @ref nrfx_twim_xfer_desc_t::type set to @ref NRFX_TWIM_XFER_TXTX
  315. *
  316. * If @ref nrfx_twim_xfer_desc_t::type is set to @ref NRFX_TWIM_XFER_TX and the @ref NRFX_TWIM_FLAG_TX_NO_STOP and @ref NRFX_TWIM_FLAG_REPEATED_XFER
  317. * flags are set, two tasks must be used to trigger a transfer: TASKS_RESUME followed by TASKS_STARTTX. If no stop condition is generated,
  318. * TWIM is in SUSPENDED state. Therefore, it must be resumed before the transfer can be started.
  319. *
  320. * @note Peripherals using EasyDMA (including TWIM) require the transfer buffers
  321. * to be placed in the Data RAM region. If this condition is not met,
  322. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  323. *
  324. * @param[in] p_instance Pointer to the driver instance structure.
  325. * @param[in] p_xfer_desc Pointer to the transfer descriptor.
  326. * @param[in] flags Transfer options (0 for default settings).
  327. *
  328. * @retval NRFX_SUCCESS If the procedure was successful.
  329. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  330. * @retval NRFX_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  331. * @retval NRFX_ERROR_INTERNAL If an error was detected by hardware.
  332. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data RAM region.
  333. * @retval NRFX_ERROR_DRV_TWI_ERR_ANACK If NACK received after sending the address.
  334. * @retval NRFX_ERROR_DRV_TWI_ERR_DNACK If NACK received after sending a data byte.
  335. */
  336. nrfx_err_t nrfx_twim_xfer(nrfx_twim_t const * p_instance,
  337. nrfx_twim_xfer_desc_t const * p_xfer_desc,
  338. uint32_t flags);
  339. /**
  340. * @brief Function for checking the TWI driver state.
  341. *
  342. * @param[in] p_instance TWI instance.
  343. *
  344. * @retval true If the TWI driver is currently busy performing a transfer.
  345. * @retval false If the TWI driver is ready for a new transfer.
  346. */
  347. bool nrfx_twim_is_busy(nrfx_twim_t const * p_instance);
  348. /**
  349. * @brief Function for returning the address of a TWIM start task.
  350. *
  351. * This function should be used if @ref nrfx_twim_xfer was called with the flag @ref NRFX_TWIM_FLAG_HOLD_XFER.
  352. * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
  353. *
  354. * @param[in] p_instance Pointer to the driver instance structure.
  355. * @param[in] xfer_type Transfer type used in the last call of the @ref nrfx_twim_xfer function.
  356. *
  357. * @return Start task address (TX or RX) depending on the value of xfer_type.
  358. */
  359. uint32_t nrfx_twim_start_task_get(nrfx_twim_t const * p_instance, nrfx_twim_xfer_type_t xfer_type);
  360. /**
  361. * @brief Function for returning the address of a STOPPED TWIM event.
  362. *
  363. * A STOPPED event can be used to detect the end of a transfer if the @ref NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER
  364. * option is used.
  365. *
  366. * @param[in] p_instance Pointer to the driver instance structure.
  367. *
  368. * @return STOPPED event address.
  369. */
  370. uint32_t nrfx_twim_stopped_event_get(nrfx_twim_t const * p_instance);
  371. void nrfx_twim_0_irq_handler(void);
  372. void nrfx_twim_1_irq_handler(void);
  373. void nrfx_twim_2_irq_handler(void);
  374. void nrfx_twim_3_irq_handler(void);
  375. /** @} */
  376. #ifdef __cplusplus
  377. }
  378. #endif
  379. #endif // NRFX_TWIM_H__