nrfx_twim.h 19 KB

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