nrfx_twis.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_TWIS_H__
  41. #define NRFX_TWIS_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_twis.h>
  44. #include <hal/nrf_gpio.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_twis TWIS driver
  50. * @{
  51. * @ingroup nrf_twis
  52. * @brief Two Wire Slave interface (TWIS) peripheral driver.
  53. */
  54. /**
  55. * @brief TWIS driver instance data structure.
  56. */
  57. typedef struct
  58. {
  59. NRF_TWIS_Type * p_reg; ///< Pointer to a structure with TWIS registers.
  60. uint8_t drv_inst_idx; ///< Driver instance index.
  61. } nrfx_twis_t;
  62. enum {
  63. #if NRFX_CHECK(NRFX_TWIS0_ENABLED)
  64. NRFX_TWIS0_INST_IDX,
  65. #endif
  66. #if NRFX_CHECK(NRFX_TWIS1_ENABLED)
  67. NRFX_TWIS1_INST_IDX,
  68. #endif
  69. #if NRFX_CHECK(NRFX_TWIS2_ENABLED)
  70. NRFX_TWIS2_INST_IDX,
  71. #endif
  72. #if NRFX_CHECK(NRFX_TWIS3_ENABLED)
  73. NRFX_TWIS3_INST_IDX,
  74. #endif
  75. NRFX_TWIS_ENABLED_COUNT
  76. };
  77. /**
  78. * @brief Macro for creating a TWIS driver instance.
  79. */
  80. #define NRFX_TWIS_INSTANCE(id) \
  81. { \
  82. .p_reg = NRFX_CONCAT_2(NRF_TWIS, id), \
  83. .drv_inst_idx = NRFX_CONCAT_3(NRFX_TWIS, id, _INST_IDX), \
  84. }
  85. /**
  86. * @brief Event callback function event definitions.
  87. */
  88. typedef enum
  89. {
  90. NRFX_TWIS_EVT_READ_REQ, ///< Read request detected.
  91. /**< If there is no buffer prepared, buf_req flag in the even will be set.
  92. Call then @ref nrfx_twis_tx_prepare to give parameters for buffer.
  93. */
  94. NRFX_TWIS_EVT_READ_DONE, ///< Read request has finished - free any data.
  95. NRFX_TWIS_EVT_READ_ERROR, ///< Read request finished with error.
  96. NRFX_TWIS_EVT_WRITE_REQ, ///< Write request detected.
  97. /**< If there is no buffer prepared, buf_req flag in the even will be set.
  98. Call then @ref nrfx_twis_rx_prepare to give parameters for buffer.
  99. */
  100. NRFX_TWIS_EVT_WRITE_DONE, ///< Write request has finished - process data.
  101. NRFX_TWIS_EVT_WRITE_ERROR, ///< Write request finished with error.
  102. NRFX_TWIS_EVT_GENERAL_ERROR ///< Error that happens not inside WRITE or READ transaction.
  103. } nrfx_twis_evt_type_t;
  104. /**
  105. * @brief Possible error sources.
  106. *
  107. * This is flag enum - values from this enum can be connected using logical or operator.
  108. * @note
  109. * We could use directly @ref nrf_twis_error_t. Error type enum is redefined here because
  110. * of possible future extension (eg. supporting timeouts and synchronous mode).
  111. */
  112. typedef enum
  113. {
  114. NRFX_TWIS_ERROR_OVERFLOW = NRF_TWIS_ERROR_OVERFLOW, /**< RX buffer overflow detected, and prevented. */
  115. NRFX_TWIS_ERROR_DATA_NACK = NRF_TWIS_ERROR_DATA_NACK, /**< NACK sent after receiving a data byte. */
  116. NRFX_TWIS_ERROR_OVERREAD = NRF_TWIS_ERROR_OVERREAD, /**< TX buffer over-read detected, and prevented. */
  117. NRFX_TWIS_ERROR_UNEXPECTED_EVENT = 1 << 8 /**< Unexpected event detected by state machine. */
  118. } nrfx_twis_error_t;
  119. /**
  120. * @brief TWIS driver event structure.
  121. */
  122. typedef struct
  123. {
  124. nrfx_twis_evt_type_t type; ///< Event type.
  125. union
  126. {
  127. bool buf_req; ///< Flag for @ref NRFX_TWIS_EVT_READ_REQ and @ref NRFX_TWIS_EVT_WRITE_REQ.
  128. /**< Information if transmission buffer requires to be prepared. */
  129. uint32_t tx_amount; ///< Data for @ref NRFX_TWIS_EVT_READ_DONE.
  130. uint32_t rx_amount; ///< Data for @ref NRFX_TWIS_EVT_WRITE_DONE.
  131. uint32_t error; ///< Data for @ref NRFX_TWIS_EVT_GENERAL_ERROR.
  132. } data;
  133. } nrfx_twis_evt_t;
  134. /**
  135. * @brief TWI slave event callback function type.
  136. *
  137. * @param[in] p_event Event information structure.
  138. */
  139. typedef void (*nrfx_twis_event_handler_t)(nrfx_twis_evt_t const * p_event);
  140. /**
  141. * @brief Structure for TWIS configuration.
  142. */
  143. typedef struct
  144. {
  145. uint32_t addr[2]; //!< Set addresses that this slave should respond. Set 0 to disable.
  146. uint32_t scl; //!< SCL pin number.
  147. uint32_t sda; //!< SDA pin number.
  148. nrf_gpio_pin_pull_t scl_pull; //!< SCL pin pull.
  149. nrf_gpio_pin_pull_t sda_pull; //!< SDA pin pull.
  150. uint8_t interrupt_priority; //!< The priority of interrupt for the module to set.
  151. } nrfx_twis_config_t;
  152. /**
  153. * @brief Generate default configuration for TWIS driver instance.
  154. */
  155. #define NRFX_TWIS_DEFAULT_CONFIG \
  156. { \
  157. .addr = { NRFX_TWIS_DEFAULT_CONFIG_ADDR0, \
  158. NRFX_TWIS_DEFAULT_CONFIG_ADDR1 }, \
  159. .scl = 31, \
  160. .scl_pull = (nrf_gpio_pin_pull_t)NRFX_TWIS_DEFAULT_CONFIG_SCL_PULL, \
  161. .sda = 31, \
  162. .sda_pull = (nrf_gpio_pin_pull_t)NRFX_TWIS_DEFAULT_CONFIG_SDA_PULL, \
  163. .interrupt_priority = NRFX_TWIS_DEFAULT_CONFIG_IRQ_PRIORITY \
  164. }
  165. /**
  166. * @brief Function for initializing the TWIS driver instance.
  167. *
  168. * Function initializes and enables TWIS driver.
  169. * @attention After driver initialization enable it by @ref nrfx_twis_enable.
  170. *
  171. * @param[in] p_instance Pointer to the driver instance structure.
  172. * @attention @em p_instance has to be global object.
  173. * It would be used by interrupts so make it sure that object
  174. * would not be destroyed when function is leaving.
  175. * @param[in] p_config Pointer to the structure with initial configuration.
  176. * @param[in] event_handler Event handler provided by the user.
  177. *
  178. * @retval NRFX_SUCCESS If initialization was successful.
  179. * @retval NRFX_ERROR_INVALID_STATE If the driver is already initialized.
  180. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  181. * instance ID is already in use. This is
  182. * possible only if NRFX_PRS_ENABLED
  183. * is set to a value other than zero.
  184. */
  185. nrfx_err_t nrfx_twis_init(nrfx_twis_t const * p_instance,
  186. nrfx_twis_config_t const * p_config,
  187. nrfx_twis_event_handler_t event_handler);
  188. /**
  189. * @brief Function for uninitializing the TWIS driver instance.
  190. *
  191. * Function initializes the peripheral and resets all registers to default values.
  192. *
  193. * @param[in] p_instance Pointer to the driver instance structure.
  194. * @note
  195. * It is safe to call nrfx_twis_uninit even before initialization.
  196. * Actually @ref nrfx_twis_init function calls this function to
  197. * make sure that TWIS state is known.
  198. * @note
  199. * If TWIS driver was in uninitialized state before calling this function,
  200. * selected pins would not be reset to default configuration.
  201. */
  202. void nrfx_twis_uninit(nrfx_twis_t const * p_instance);
  203. /**
  204. * @brief Enable TWIS instance.
  205. *
  206. * This function enables TWIS instance.
  207. * Function defined if there is needs for dynamically enabling and disabling the peripheral.
  208. * Use @ref nrfx_twis_enable and @ref nrfx_twis_disable functions.
  209. * They do not change any configuration registers.
  210. *
  211. * @param p_instance Pointer to the driver instance structure.
  212. */
  213. void nrfx_twis_enable(nrfx_twis_t const * p_instance);
  214. /**
  215. * @brief Disable TWIS instance.
  216. *
  217. * Disabling TWIS instance gives possibility to turn off the TWIS while
  218. * holding configuration done by @ref nrfx_twis_init.
  219. *
  220. * @param p_instance Pointer to the driver instance structure.
  221. */
  222. void nrfx_twis_disable(nrfx_twis_t const * p_instance);
  223. /**
  224. * @brief Get and clear last error flags.
  225. *
  226. * Function gets information about errors.
  227. * This is also the only possibility to exit from error substate of the internal state machine.
  228. *
  229. * @param[in] p_instance Pointer to the driver instance structure.
  230. * @return Error flags defined in @ref nrfx_twis_error_t.
  231. * @attention
  232. * This function clears error state and flags.
  233. */
  234. uint32_t nrfx_twis_error_get_and_clear(nrfx_twis_t const * p_instance);
  235. /**
  236. * @brief Prepare data for sending.
  237. *
  238. * This function should be used in response for @ref NRFX_TWIS_EVT_READ_REQ event.
  239. *
  240. * @note Peripherals using EasyDMA (including TWIS) require the transfer buffers
  241. * to be placed in the Data RAM region. If this condition is not met,
  242. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  243. *
  244. * @param[in] p_instance Pointer to the driver instance structure.
  245. * @param[in] p_buf Transmission buffer.
  246. * @attention Transmission buffer has to be placed in RAM.
  247. * @param size Maximum number of bytes that master may read from buffer given.
  248. *
  249. * @retval NRFX_SUCCESS Preparation finished properly.
  250. * @retval NRFX_ERROR_INVALID_ADDR Given @em p_buf is not placed inside the RAM.
  251. * @retval NRFX_ERROR_INVALID_LENGTH Wrong value in @em size parameter.
  252. * @retval NRFX_ERROR_INVALID_STATE Module not initialized or not enabled.
  253. */
  254. nrfx_err_t nrfx_twis_tx_prepare(nrfx_twis_t const * p_instance,
  255. void const * p_buf,
  256. size_t size);
  257. /**
  258. * @brief Get number of transmitted bytes.
  259. *
  260. * Function returns number of bytes sent.
  261. * This function may be called after @ref NRFX_TWIS_EVT_READ_DONE or @ref NRFX_TWIS_EVT_READ_ERROR events.
  262. *
  263. * @param[in] p_instance Pointer to the driver instance structure.
  264. *
  265. * @return Number of bytes sent.
  266. */
  267. __STATIC_INLINE size_t nrfx_twis_tx_amount(nrfx_twis_t const * p_instance);
  268. /**
  269. * @brief Prepare data for receiving
  270. *
  271. * This function should be used in response for @ref NRFX_TWIS_EVT_WRITE_REQ event.
  272. *
  273. * @note Peripherals using EasyDMA (including TWIS) require the transfer buffers
  274. * to be placed in the Data RAM region. If this condition is not met,
  275. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  276. *
  277. * @param[in] p_instance Pointer to the driver instance structure.
  278. * @param[in] p_buf Buffer that would be filled with received data.
  279. * @attention Receiving buffer has to be placed in RAM.
  280. * @param size Size of the buffer (maximum amount of data to receive).
  281. *
  282. * @retval NRFX_SUCCESS Preparation finished properly.
  283. * @retval NRFX_ERROR_INVALID_ADDR Given @em p_buf is not placed inside the RAM.
  284. * @retval NRFX_ERROR_INVALID_LENGTH Wrong value in @em size parameter.
  285. * @retval NRFX_ERROR_INVALID_STATE Module not initialized or not enabled.
  286. */
  287. nrfx_err_t nrfx_twis_rx_prepare(nrfx_twis_t const * p_instance,
  288. void * p_buf,
  289. size_t size);
  290. /**
  291. * @brief Get number of received bytes.
  292. *
  293. * Function returns number of bytes received.
  294. * This function may be called after @ref NRFX_TWIS_EVT_WRITE_DONE or @ref NRFX_TWIS_EVT_WRITE_ERROR events.
  295. *
  296. * @param[in] p_instance Pointer to the driver instance structure.
  297. *
  298. * @return Number of bytes received.
  299. */
  300. __STATIC_INLINE size_t nrfx_twis_rx_amount(nrfx_twis_t const * p_instance);
  301. /**
  302. * @brief Function checks if driver is busy right now.
  303. *
  304. * Actual driver substate is tested.
  305. * If driver is in any other state than IDLE or ERROR this function returns true.
  306. *
  307. * @param[in] p_instance Pointer to the driver instance structure.
  308. *
  309. * @retval true Driver is in state other than ERROR or IDLE.
  310. * @retval false There is no transmission pending.
  311. */
  312. bool nrfx_twis_is_busy(nrfx_twis_t const * p_instance);
  313. /**
  314. * @brief Function checks if driver is waiting for tx buffer.
  315. *
  316. * If this function returns true, it means that driver is stalled expecting
  317. * of the @ref nrfx_twis_tx_prepare function call.
  318. *
  319. * @param[in] p_instance Pointer to the driver instance structure.
  320. *
  321. * @retval true Driver waits for @ref nrfx_twis_tx_prepare.
  322. * @retval false Driver is not in the state where it waits for preparing tx buffer.
  323. */
  324. bool nrfx_twis_is_waiting_tx_buff(nrfx_twis_t const * p_instance);
  325. /**
  326. * @brief Function checks if driver is waiting for rx buffer.
  327. *
  328. * If this function returns true, it means that driver is staled expecting
  329. * of the @ref nrfx_twis_rx_prepare function call.
  330. *
  331. * @param[in] p_instance Pointer to the driver instance structure.
  332. *
  333. * @retval true Driver waits for @ref nrfx_twis_rx_prepare.
  334. * @retval false Driver is not in the state where it waits for preparing rx buffer.
  335. */
  336. bool nrfx_twis_is_waiting_rx_buff(nrfx_twis_t const * p_instance);
  337. /**
  338. * @brief Check if driver is sending data.
  339. *
  340. * If this function returns true, it means that there is ongoing output transmission.
  341. *
  342. * @param[in] p_instance Pointer to the driver instance structure.
  343. *
  344. * @retval true There is ongoing output transmission.
  345. * @retval false Driver is in other state.
  346. */
  347. bool nrfx_twis_is_pending_tx(nrfx_twis_t const * p_instance);
  348. /**
  349. * @brief Check if driver is receiving data.
  350. *
  351. * If this function returns true, it means that there is ongoing input transmission.
  352. *
  353. * @param[in] p_instance Pointer to the driver instance structure.
  354. *
  355. * @retval true There is ongoing input transmission.
  356. * @retval false Driver is in other state.
  357. */
  358. bool nrfx_twis_is_pending_rx(nrfx_twis_t const * p_instance);
  359. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  360. __STATIC_INLINE size_t nrfx_twis_tx_amount(nrfx_twis_t const * p_instance)
  361. {
  362. return nrf_twis_tx_amount_get(p_instance->p_reg);
  363. }
  364. __STATIC_INLINE size_t nrfx_twis_rx_amount(nrfx_twis_t const * p_instance)
  365. {
  366. return nrf_twis_rx_amount_get(p_instance->p_reg);
  367. }
  368. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  369. void nrfx_twis_0_irq_handler(void);
  370. void nrfx_twis_1_irq_handler(void);
  371. void nrfx_twis_2_irq_handler(void);
  372. void nrfx_twis_3_irq_handler(void);
  373. /** @} */
  374. #ifdef __cplusplus
  375. }
  376. #endif
  377. #endif // NRFX_TWIS_H__