nrf_twi_mngr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_TWI_MNGR)
  42. #include "nrf_twi_mngr.h"
  43. #include "nrf_assert.h"
  44. #include "app_util_platform.h"
  45. typedef volatile struct
  46. {
  47. bool transaction_in_progress;
  48. uint8_t transaction_result;
  49. } nrf_twi_mngr_cb_data_t;
  50. static ret_code_t start_transfer(nrf_twi_mngr_t const * p_nrf_twi_mngr)
  51. {
  52. ASSERT(p_nrf_twi_mngr != NULL);
  53. // Pointer for cleaner code.
  54. nrf_twi_mngr_cb_t * p_cb = p_nrf_twi_mngr->p_nrf_twi_mngr_cb;
  55. // [use a local variable to avoid using two volatile variables in one
  56. // expression]
  57. uint8_t current_transfer_idx = p_cb->current_transfer_idx;
  58. nrf_twi_mngr_transfer_t const * p_transfer =
  59. &p_cb->p_current_transaction->p_transfers[current_transfer_idx];
  60. uint8_t address = NRF_TWI_MNGR_OP_ADDRESS(p_transfer->operation);
  61. nrf_drv_twi_xfer_desc_t xfer_desc;
  62. uint32_t flags;
  63. xfer_desc.address = address;
  64. xfer_desc.p_primary_buf = p_transfer->p_data;
  65. xfer_desc.primary_length = p_transfer->length;
  66. /* If it is possible try to bind two transfers together. They can be combined if:
  67. * - there is no stop condition after current transfer.
  68. * - current transfer is TX.
  69. * - there is at least one more transfer in the transaction.
  70. * - address of next transfer is the same as current transfer.
  71. */
  72. if ((p_transfer->flags & NRF_TWI_MNGR_NO_STOP) &&
  73. !NRF_TWI_MNGR_IS_READ_OP(p_transfer->operation) &&
  74. // Adding 1 to check if next transfer is from the same transaction.
  75. ((current_transfer_idx + 1) < p_cb->p_current_transaction->number_of_transfers) &&
  76. (NRF_TWI_MNGR_OP_ADDRESS(p_transfer->operation) ==
  77. NRF_TWI_MNGR_OP_ADDRESS(p_cb->p_current_transaction->
  78. p_transfers[current_transfer_idx + 1].operation)))
  79. {
  80. nrf_twi_mngr_transfer_t const * p_second_transfer =
  81. &p_cb->p_current_transaction->p_transfers[current_transfer_idx + 1];
  82. xfer_desc.p_secondary_buf = p_second_transfer->p_data;
  83. xfer_desc.secondary_length = p_second_transfer->length;
  84. xfer_desc.type = NRF_TWI_MNGR_IS_READ_OP(p_second_transfer->operation) ?
  85. NRF_DRV_TWI_XFER_TXRX : NRF_DRV_TWI_XFER_TXTX;
  86. flags = (p_second_transfer->flags & NRF_TWI_MNGR_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0;
  87. p_cb->current_transfer_idx++;
  88. }
  89. else
  90. {
  91. xfer_desc.type = NRF_TWI_MNGR_IS_READ_OP(p_transfer->operation) ? NRF_DRV_TWI_XFER_RX :
  92. NRF_DRV_TWI_XFER_TX;
  93. xfer_desc.p_secondary_buf = NULL;
  94. xfer_desc.secondary_length = 0;
  95. flags = (p_transfer->flags & NRF_TWI_MNGR_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0;
  96. }
  97. return nrf_drv_twi_xfer(&p_nrf_twi_mngr->twi, &xfer_desc, flags);
  98. }
  99. static void transaction_end_signal(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  100. ret_code_t result)
  101. {
  102. ASSERT(p_nrf_twi_mngr != NULL);
  103. if (p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction->callback)
  104. {
  105. // [use a local variable to avoid using two volatile variables in one
  106. // expression]
  107. void * p_user_data = p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction->p_user_data;
  108. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction->callback(result, p_user_data);
  109. }
  110. }
  111. static void twi_event_handler(nrf_drv_twi_evt_t const * p_event,
  112. void * p_context);
  113. // This function starts pending transaction if there is no current one or
  114. // when 'switch_transaction' parameter is set to true. It is important to
  115. // switch to new transaction without setting 'p_nrf_twi_mngr->p_current_transaction'
  116. // to NULL in between, since this pointer is used to check idle status - see
  117. // 'nrf_twi_mngr_is_idle()'.
  118. static void start_pending_transaction(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  119. bool switch_transaction)
  120. {
  121. ASSERT(p_nrf_twi_mngr != NULL);
  122. // Pointer for cleaner code.
  123. nrf_twi_mngr_cb_t * p_cb = p_nrf_twi_mngr->p_nrf_twi_mngr_cb;
  124. for (;;)
  125. {
  126. bool start_transaction = false;
  127. CRITICAL_REGION_ENTER();
  128. if (switch_transaction || nrf_twi_mngr_is_idle(p_nrf_twi_mngr))
  129. {
  130. if (nrf_queue_pop(p_nrf_twi_mngr->p_queue, (void *)(&p_cb->p_current_transaction))
  131. == NRF_SUCCESS)
  132. {
  133. start_transaction = true;
  134. }
  135. else
  136. {
  137. p_cb->p_current_transaction = NULL;
  138. }
  139. }
  140. CRITICAL_REGION_EXIT();
  141. if (!start_transaction)
  142. {
  143. return;
  144. }
  145. else
  146. {
  147. ret_code_t result;
  148. nrf_drv_twi_config_t const * p_instance_cfg =
  149. p_cb->p_current_transaction->p_required_twi_cfg == NULL ?
  150. &p_cb->default_configuration :
  151. p_cb->p_current_transaction->p_required_twi_cfg;
  152. if (memcmp(p_cb->p_current_configuration, p_instance_cfg, sizeof(*p_instance_cfg)) != 0)
  153. {
  154. ret_code_t err_code;
  155. nrf_drv_twi_uninit(&p_nrf_twi_mngr->twi);
  156. err_code = nrf_drv_twi_init(&p_nrf_twi_mngr->twi,
  157. p_instance_cfg,
  158. twi_event_handler,
  159. (void *)p_nrf_twi_mngr);
  160. ASSERT(err_code == NRF_SUCCESS);
  161. nrf_drv_twi_enable(&p_nrf_twi_mngr->twi);
  162. UNUSED_VARIABLE(err_code);
  163. p_cb->p_current_configuration = p_instance_cfg;
  164. }
  165. // Try to start first transfer for this new transaction.
  166. p_cb->current_transfer_idx = 0;
  167. result = start_transfer(p_nrf_twi_mngr);
  168. // If transaction started successfully there is nothing more to do here now.
  169. if (result == NRF_SUCCESS)
  170. {
  171. return;
  172. }
  173. // Transfer failed to start - notify user that this transaction
  174. // cannot be started and try with next one (in next iteration of
  175. // the loop).
  176. transaction_end_signal(p_nrf_twi_mngr, result);
  177. switch_transaction = true;
  178. }
  179. }
  180. }
  181. static void twi_event_handler(nrf_drv_twi_evt_t const * p_event,
  182. void * p_context)
  183. {
  184. ASSERT(p_event != NULL);
  185. nrf_twi_mngr_t * p_nrf_twi_mngr = (nrf_twi_mngr_t *)p_context;
  186. ret_code_t result;
  187. // This callback should be called only during transaction.
  188. ASSERT(p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction != NULL);
  189. if (p_event->type == NRF_DRV_TWI_EVT_DONE)
  190. {
  191. result = NRF_SUCCESS;
  192. // Transfer finished successfully. If there is another one to be
  193. // performed in the current transaction, start it now.
  194. // [use a local variable to avoid using two volatile variables in one
  195. // expression]
  196. uint8_t current_transfer_idx = p_nrf_twi_mngr->p_nrf_twi_mngr_cb->current_transfer_idx;
  197. ++current_transfer_idx;
  198. if (current_transfer_idx <
  199. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction->number_of_transfers)
  200. {
  201. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->current_transfer_idx = current_transfer_idx;
  202. result = start_transfer(p_nrf_twi_mngr);
  203. if (result == NRF_SUCCESS)
  204. {
  205. // The current transaction goes on and we've successfully
  206. // started its next transfer -> there is nothing more to do.
  207. return;
  208. }
  209. // [if the next transfer could not be started due to some error
  210. // we finish the transaction with this error code as the result]
  211. }
  212. }
  213. else
  214. {
  215. result = NRF_ERROR_INTERNAL;
  216. }
  217. // The current transaction has been completed or interrupted by some error.
  218. // Notify the user and start next one (if there is any).
  219. transaction_end_signal(p_nrf_twi_mngr, result);
  220. // [we switch transactions here ('p_nrf_twi_mngr->p_current_transaction' is set
  221. // to NULL only if there is nothing more to do) in order to not generate
  222. // spurious idle status (even for a moment)]
  223. start_pending_transaction(p_nrf_twi_mngr, true);
  224. }
  225. ret_code_t nrf_twi_mngr_init(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  226. nrf_drv_twi_config_t const * p_default_twi_config)
  227. {
  228. ASSERT(p_nrf_twi_mngr != NULL);
  229. ASSERT(p_nrf_twi_mngr->p_queue != NULL);
  230. ASSERT(p_nrf_twi_mngr->p_queue->size > 0);
  231. ASSERT(p_default_twi_config != NULL);
  232. ret_code_t err_code;
  233. err_code = nrf_drv_twi_init(&p_nrf_twi_mngr->twi,
  234. p_default_twi_config,
  235. twi_event_handler,
  236. (void *)p_nrf_twi_mngr);
  237. VERIFY_SUCCESS(err_code);
  238. nrf_drv_twi_enable(&p_nrf_twi_mngr->twi);
  239. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction = NULL;
  240. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->default_configuration = *p_default_twi_config;
  241. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_configuration =
  242. &p_nrf_twi_mngr->p_nrf_twi_mngr_cb->default_configuration;
  243. return NRF_SUCCESS;
  244. }
  245. void nrf_twi_mngr_uninit(nrf_twi_mngr_t const * p_nrf_twi_mngr)
  246. {
  247. ASSERT(p_nrf_twi_mngr != NULL);
  248. nrf_drv_twi_uninit(&p_nrf_twi_mngr->twi);
  249. p_nrf_twi_mngr->p_nrf_twi_mngr_cb->p_current_transaction = NULL;
  250. }
  251. ret_code_t nrf_twi_mngr_schedule(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  252. nrf_twi_mngr_transaction_t const * p_transaction)
  253. {
  254. ASSERT(p_nrf_twi_mngr != NULL);
  255. ASSERT(p_transaction != NULL);
  256. ASSERT(p_transaction->p_transfers != NULL);
  257. ASSERT(p_transaction->number_of_transfers != 0);
  258. ret_code_t result = NRF_SUCCESS;
  259. result = nrf_queue_push(p_nrf_twi_mngr->p_queue, (void *)(&p_transaction));
  260. if (result == NRF_SUCCESS)
  261. {
  262. // New transaction has been successfully added to queue,
  263. // so if we are currently idle it's time to start the job.
  264. start_pending_transaction(p_nrf_twi_mngr, false);
  265. }
  266. return result;
  267. }
  268. static void internal_transaction_cb(ret_code_t result, void * p_user_data)
  269. {
  270. nrf_twi_mngr_cb_data_t *p_cb_data = (nrf_twi_mngr_cb_data_t *)p_user_data;
  271. p_cb_data->transaction_result = result;
  272. p_cb_data->transaction_in_progress = false;
  273. }
  274. ret_code_t nrf_twi_mngr_perform(nrf_twi_mngr_t const * p_nrf_twi_mngr,
  275. nrf_drv_twi_config_t const * p_config,
  276. nrf_twi_mngr_transfer_t const * p_transfers,
  277. uint8_t number_of_transfers,
  278. void (* user_function)(void))
  279. {
  280. ASSERT(p_nrf_twi_mngr != NULL);
  281. ASSERT(p_transfers != NULL);
  282. ASSERT(number_of_transfers != 0);
  283. nrf_twi_mngr_cb_data_t cb_data =
  284. {
  285. .transaction_in_progress = true
  286. };
  287. nrf_twi_mngr_transaction_t internal_transaction =
  288. {
  289. .callback = internal_transaction_cb,
  290. .p_user_data = (void *)&cb_data,
  291. .p_transfers = p_transfers,
  292. .number_of_transfers = number_of_transfers,
  293. .p_required_twi_cfg = p_config
  294. };
  295. ret_code_t result = nrf_twi_mngr_schedule(p_nrf_twi_mngr, &internal_transaction);
  296. VERIFY_SUCCESS(result);
  297. while (cb_data.transaction_in_progress)
  298. {
  299. if (user_function)
  300. {
  301. user_function();
  302. }
  303. }
  304. return cb_data.transaction_result;
  305. }
  306. #endif //NRF_MODULE_ENABLED(NRF_TWI_MNGR)