nrf_twi_mngr.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Copyright (c) 2015 - 2020, 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 NRF_TWI_MNGR_H__
  41. #define NRF_TWI_MNGR_H__
  42. #include <stdint.h>
  43. #include "nrf_drv_twi.h"
  44. #include "sdk_errors.h"
  45. #include "nrf_queue.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @defgroup nrf_twi_mngr TWI transaction manager
  51. * @{
  52. * @ingroup app_common
  53. *
  54. * @brief Module for scheduling TWI transactions.
  55. */
  56. //If TWIM is present buffers can only be in RAM
  57. /*lint -save -e491*/
  58. /**
  59. * @brief Macro checking if buffers should be stored in RAM.
  60. */
  61. #ifndef NRF_TWI_MNGR_BUFFERS_IN_RAM
  62. #define NRF_TWI_MNGR_BUFFERS_IN_RAM defined(TWIM_PRESENT)
  63. #endif
  64. /**
  65. * @brief Modifier used in array declaration for TWI Manager.
  66. *
  67. * @note For TWI peripheral array can be const, for TWIM array has to be located in RAM.
  68. */
  69. #if NRF_TWI_MNGR_BUFFERS_IN_RAM
  70. #define NRF_TWI_MNGR_BUFFER_LOC_IND
  71. #else
  72. #define NRF_TWI_MNGR_BUFFER_LOC_IND const
  73. #endif
  74. /*lint -restore*/
  75. /**
  76. * @brief Flag indicating that a given transfer should not be ended
  77. * with a stop condition.
  78. *
  79. * Use this flag when a stop condition is undesirable between two transfers,
  80. * for example, when the first transfer is a write that sets an address in the slave
  81. * device and the second one is a read that fetches certain data using this
  82. * address. In this case, the second transfer should follow directly after the
  83. * first transfer, with a repeated start condition instead of a stop and then
  84. * a new start condition.
  85. */
  86. #define NRF_TWI_MNGR_NO_STOP 0x01
  87. /**
  88. * @brief Macro for creating a write transfer.
  89. *
  90. * @param[in] address Slave address.
  91. * @param[in] p_data Pointer to the data to be sent.
  92. * @param[in] length Number of bytes to transfer.
  93. * @param[in] flags Transfer flags (see @ref NRF_TWI_MNGR_NO_STOP).
  94. */
  95. #define NRF_TWI_MNGR_WRITE(address, p_data, length, flags) \
  96. NRF_TWI_MNGR_TRANSFER(NRF_TWI_MNGR_WRITE_OP(address), p_data, length, flags)
  97. /**
  98. * @brief Macro for creating a read transfer.
  99. *
  100. * @param address Slave address.
  101. * @param[in] p_data Pointer to the buffer where received data should be placed.
  102. * @param length Number of bytes to transfer.
  103. * @param flags Transfer flags (see @ref NRF_TWI_MNGR_NO_STOP).
  104. */
  105. #define NRF_TWI_MNGR_READ(address, p_data, length, flags) \
  106. NRF_TWI_MNGR_TRANSFER(NRF_TWI_MNGR_READ_OP(address), p_data, length, flags)
  107. /**
  108. * @brief Helper macro, should not be used directly.
  109. */
  110. #define NRF_TWI_MNGR_TRANSFER(_operation, _p_data, _length, _flags) \
  111. { \
  112. .p_data = (uint8_t *)(_p_data), \
  113. .length = _length, \
  114. .operation = _operation, \
  115. .flags = _flags \
  116. }
  117. /**
  118. * @brief Helper macro, should not be used directly.
  119. */
  120. #define NRF_TWI_MNGR_WRITE_OP(address) (((address) << 1) | 0)
  121. /**
  122. * @brief Helper macro, should not be used directly.
  123. */
  124. #define NRF_TWI_MNGR_READ_OP(address) (((address) << 1) | 1)
  125. /**
  126. * @brief Helper macro, should not be used directly.
  127. */
  128. #define NRF_TWI_MNGR_IS_READ_OP(operation) ((operation) & 1)
  129. /**
  130. * @brief Helper macro, should not be used directly.
  131. */
  132. #define NRF_TWI_MNGR_OP_ADDRESS(operation) ((operation) >> 1)
  133. /**
  134. * @brief TWI transaction callback prototype.
  135. *
  136. * @param result Result of operation (NRF_SUCCESS on success,
  137. * otherwise a relevant error code).
  138. * @param[in] p_user_data Pointer to user data defined in transaction
  139. * descriptor.
  140. */
  141. typedef void (* nrf_twi_mngr_callback_t)(ret_code_t result, void * p_user_data);
  142. /**
  143. * @brief TWI transfer descriptor.
  144. */
  145. typedef struct {
  146. uint8_t * p_data; ///< Pointer to the buffer holding the data.
  147. uint8_t length; ///< Number of bytes to transfer.
  148. uint8_t operation; ///< Device address combined with transfer direction.
  149. uint8_t flags; ///< Transfer flags (see @ref NRF_TWI_MNGR_NO_STOP).
  150. } nrf_twi_mngr_transfer_t;
  151. /**
  152. * @brief TWI transaction descriptor.
  153. */
  154. typedef struct {
  155. nrf_twi_mngr_callback_t callback;
  156. ///< User-specified function to be called after the transaction is finished.
  157. void * p_user_data;
  158. ///< Pointer to user data to be passed to the callback.
  159. nrf_twi_mngr_transfer_t const * p_transfers;
  160. ///< Pointer to the array of transfers that make up the transaction.
  161. uint8_t number_of_transfers;
  162. ///< Number of transfers that make up the transaction.
  163. nrf_drv_twi_config_t const * p_required_twi_cfg;
  164. ///< Pointer to instance hardware configuration.
  165. } nrf_twi_mngr_transaction_t;
  166. /**
  167. * @brief TWI instance control block.
  168. */
  169. typedef struct {
  170. nrf_twi_mngr_transaction_t const * volatile p_current_transaction;
  171. ///< Currently realized transaction.
  172. nrf_drv_twi_config_t default_configuration;
  173. ///< Default hardware configuration.
  174. nrf_drv_twi_config_t const * p_current_configuration;
  175. ///< Pointer to current hardware configuration.
  176. uint8_t volatile current_transfer_idx;
  177. ///< Index of currently performed transfer (within current transaction).
  178. } nrf_twi_mngr_cb_t;
  179. /**
  180. * @brief TWI transaction manager instance.
  181. */
  182. typedef struct {
  183. nrf_twi_mngr_cb_t * p_nrf_twi_mngr_cb;
  184. ///< Control block of instance.
  185. nrf_queue_t const * p_queue;
  186. ///< Transaction queue.
  187. nrf_drv_twi_t twi;
  188. ///< Pointer to TWI master driver instance.
  189. } nrf_twi_mngr_t;
  190. /**
  191. * @brief Macro that simplifies defining a TWI transaction manager
  192. * instance.
  193. *
  194. * This macro allocates a static buffer for the transaction queue.
  195. * Therefore, it should be used in only one place in the code for a given
  196. * instance.
  197. *
  198. * @note The queue size is the maximum number of pending transactions
  199. * not counting the one that is currently realized. This means that
  200. * for an empty queue with size of, for example, 4 elements, it is
  201. * possible to schedule up to 5 transactions.
  202. *
  203. * @param[in] _nrf_twi_mngr_name Name of instance to be created.
  204. * @param[in] _queue_size Size of the transaction queue (maximum number
  205. * of pending transactions).
  206. * @param[in] _twi_idx Index of hardware TWI instance to be used.
  207. */
  208. #define NRF_TWI_MNGR_DEF(_nrf_twi_mngr_name, _queue_size, _twi_idx) \
  209. NRF_QUEUE_DEF(nrf_twi_mngr_transaction_t const *, \
  210. _nrf_twi_mngr_name##_queue, \
  211. (_queue_size), \
  212. NRF_QUEUE_MODE_NO_OVERFLOW); \
  213. static nrf_twi_mngr_cb_t CONCAT_2(_nrf_twi_mngr_name, _cb); \
  214. static const nrf_twi_mngr_t _nrf_twi_mngr_name = \
  215. { \
  216. .p_nrf_twi_mngr_cb = &CONCAT_2(_nrf_twi_mngr_name, _cb), \
  217. .p_queue = &_nrf_twi_mngr_name##_queue, \
  218. .twi = NRF_DRV_TWI_INSTANCE(_twi_idx) \
  219. }
  220. /**
  221. * @brief Function for initializing a TWI transaction manager instance.
  222. *
  223. * @param[in] p_nrf_twi_mngr Pointer to the instance to be initialized.
  224. * @param[in] p_default_twi_config Pointer to the TWI master driver configuration. This configuration
  225. * will be used whenever the scheduled transaction will have
  226. * p_twi_configuration set to NULL value.
  227. *
  228. * @return Values returned by the @ref nrf_drv_twi_init function.
  229. */
  230. ret_code_t nrf_twi_mngr_init(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  231. nrf_drv_twi_config_t const * p_default_twi_config);
  232. /**
  233. * @brief Function for uninitializing a TWI transaction manager instance.
  234. *
  235. * @param[in] p_nrf_twi_mngr Pointer to the instance to be uninitialized.
  236. */
  237. void nrf_twi_mngr_uninit(nrf_twi_mngr_t const * p_nrf_twi_mngr);
  238. /**
  239. * @brief Function for scheduling a TWI transaction.
  240. *
  241. * The transaction is enqueued and started as soon as the TWI bus is
  242. * available, thus when all previously scheduled transactions have been
  243. * finished (possibly immediately).
  244. *
  245. * @note If @ref nrf_twi_mngr_transaction_t::p_required_twi_cfg
  246. * is set to a non-NULL value the module will compare it with
  247. * @ref nrf_twi_mngr_cb_t::p_current_configuration and reinitialize hardware
  248. * TWI instance with new parameters if any differences are found.
  249. * If @ref nrf_twi_mngr_transaction_t::p_required_twi_cfg is set to NULL then
  250. * it will treat it as it would be set to @ref nrf_twi_mngr_cb_t::default_configuration.
  251. *
  252. * @param[in] p_nrf_twi_mngr Pointer to the TWI transaction manager instance.
  253. * @param[in] p_transaction Pointer to the descriptor of the transaction to be
  254. * scheduled.
  255. *
  256. * @retval NRF_SUCCESS If the transaction has been successfully scheduled.
  257. * @retval NRF_ERROR_NO_MEM If the queue is full (Only if queue in
  258. * @ref NRF_QUEUE_MODE_NO_OVERFLOW).
  259. */
  260. ret_code_t nrf_twi_mngr_schedule(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  261. nrf_twi_mngr_transaction_t const * p_transaction);
  262. /**
  263. * @brief Function for scheduling a transaction and waiting until it is finished.
  264. *
  265. * This function schedules a transaction that consists of one or more transfers
  266. * and waits until it is finished.
  267. *
  268. * @param[in] p_nrf_twi_mngr Pointer to the TWI transaction manager instance.
  269. * @param[in] p_config Required TWI configuration.
  270. * @param[in] p_transfers Pointer to an array of transfers to be performed.
  271. * @param number_of_transfers Number of transfers to be performed.
  272. * @param user_function User-specified function to be called while
  273. * waiting. NULL if such functionality
  274. * is not needed.
  275. *
  276. * @retval NRF_SUCCESS If the transfers have been successfully realized.
  277. * @retval NRF_ERROR_BUSY If some transfers are already being performed.
  278. * @retval - Other error codes mean that the transaction has ended
  279. * with the error that is specified in the error code.
  280. */
  281. ret_code_t nrf_twi_mngr_perform(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  282. nrf_drv_twi_config_t const * p_config,
  283. nrf_twi_mngr_transfer_t const * p_transfers,
  284. uint8_t number_of_transfers,
  285. void (* user_function)(void));
  286. /**
  287. * @brief Function for getting the current state of a TWI transaction manager
  288. * instance.
  289. *
  290. * @param[in] p_nrf_twi_mngr Pointer to the TWI transaction manager instance.
  291. *
  292. * @retval true If all scheduled transactions have been finished.
  293. * @retval false Otherwise.
  294. */
  295. __STATIC_INLINE bool nrf_twi_mngr_is_idle(nrf_twi_mngr_t const * p_nrf_twi_mngr);
  296. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  297. __STATIC_INLINE bool nrf_twi_mngr_is_idle(nrf_twi_mngr_t const * p_nrf_twi_mngr)
  298. {
  299. return (p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction == NULL);
  300. }
  301. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  302. /**
  303. *@}
  304. **/
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. #endif // NRF_TWI_MNGR_H__