nrfx_twis.h 15 KB

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