app_twi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "app_twi.h"
  13. #include "nrf_assert.h"
  14. #include "app_util_platform.h"
  15. #include "sdk_common.h"
  16. // Increase specified queue index and when it goes outside the queue move it
  17. // on the beginning of the queue.
  18. #define INCREASE_IDX(idx, p_queue) \
  19. do { \
  20. ++idx; \
  21. p_queue->idx = (idx > p_queue->size) ? 0 : idx; \
  22. } while (0)
  23. static bool queue_put(app_twi_queue_t * p_queue,
  24. app_twi_transaction_t const * p_transaction)
  25. {
  26. // [use a local variable to avoid using two volatile variables in one
  27. // expression]
  28. uint8_t write_idx = p_queue->write_idx;
  29. // If the queue is already full, we cannot put any more elements into it.
  30. if ((write_idx == p_queue->size && p_queue->read_idx == 0) ||
  31. write_idx == p_queue->read_idx-1)
  32. {
  33. return false;
  34. }
  35. // Write the new element on the position specified by the write index.
  36. p_queue->p_buffer[write_idx] = p_transaction;
  37. // Increase the write index and when it goes outside the queue move it
  38. // on the beginning.
  39. INCREASE_IDX(write_idx, p_queue);
  40. return true;
  41. }
  42. static app_twi_transaction_t const * queue_get(app_twi_queue_t * p_queue)
  43. {
  44. // [use a local variable to avoid using two volatile variables in one
  45. // expression]
  46. uint8_t read_idx = p_queue->read_idx;
  47. // If the queue is empty, we cannot return any more elements from it.
  48. if (read_idx == p_queue->write_idx)
  49. {
  50. return NULL;
  51. }
  52. // Read the element from the position specified by the read index.
  53. app_twi_transaction_t const * p_transaction = p_queue->p_buffer[read_idx];
  54. // Increase the read index and when it goes outside the queue move it
  55. // on the beginning.
  56. INCREASE_IDX(read_idx, p_queue);
  57. return p_transaction;
  58. }
  59. static ret_code_t start_transfer(app_twi_t * p_app_twi)
  60. {
  61. ASSERT(p_app_twi != NULL);
  62. // [use a local variable to avoid using two volatile variables in one
  63. // expression]
  64. uint8_t current_transfer_idx = p_app_twi->current_transfer_idx;
  65. app_twi_transfer_t const * p_transfer =
  66. &p_app_twi->p_current_transaction->p_transfers[current_transfer_idx];
  67. uint8_t address = APP_TWI_OP_ADDRESS(p_transfer->operation);
  68. nrf_drv_twi_xfer_desc_t xfer_desc;
  69. uint32_t flags;
  70. xfer_desc.address = address;
  71. xfer_desc.p_primary_buf = p_transfer->p_data;
  72. xfer_desc.primary_length = p_transfer->length;
  73. /* If it is possible try to bind two transfers together. They can be combined if:
  74. * - there is no stop condition after current transfer.
  75. * - current transfer is TX.
  76. * - there is at least one more transfer in the transaction.
  77. * - address of next trnasfer is the same as current transfer.
  78. */
  79. if ((p_transfer->flags & APP_TWI_NO_STOP) &&
  80. !APP_TWI_IS_READ_OP(p_transfer->operation) &&
  81. ((current_transfer_idx+1) < p_app_twi->p_current_transaction->number_of_transfers) &&
  82. APP_TWI_OP_ADDRESS(p_transfer->operation) ==
  83. APP_TWI_OP_ADDRESS(p_app_twi->p_current_transaction->p_transfers[current_transfer_idx+1].operation)
  84. )
  85. {
  86. app_twi_transfer_t const * p_second_transfer =
  87. &p_app_twi->p_current_transaction->p_transfers[current_transfer_idx+1];
  88. xfer_desc.p_secondary_buf = p_second_transfer->p_data;
  89. xfer_desc.secondary_length = p_second_transfer->length;
  90. xfer_desc.type = APP_TWI_IS_READ_OP(p_second_transfer->operation) ? NRF_DRV_TWI_XFER_TXRX :
  91. NRF_DRV_TWI_XFER_TXTX;
  92. flags = (p_second_transfer->flags & APP_TWI_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0;
  93. p_app_twi->current_transfer_idx++;
  94. }
  95. else
  96. {
  97. xfer_desc.type = APP_TWI_IS_READ_OP(p_transfer->operation) ? NRF_DRV_TWI_XFER_RX :
  98. NRF_DRV_TWI_XFER_TX;
  99. xfer_desc.p_secondary_buf = NULL;
  100. xfer_desc.secondary_length = 0;
  101. flags = (p_transfer->flags & APP_TWI_NO_STOP) ? NRF_DRV_TWI_FLAG_TX_NO_STOP : 0;
  102. }
  103. return nrf_drv_twi_xfer(&p_app_twi->twi, &xfer_desc, flags);
  104. }
  105. static void signal_end_of_transaction(app_twi_t const * p_app_twi,
  106. ret_code_t result)
  107. {
  108. ASSERT(p_app_twi != NULL);
  109. if (p_app_twi->p_current_transaction->callback)
  110. {
  111. // [use a local variable to avoid using two volatile variables in one
  112. // expression]
  113. void * p_user_data = p_app_twi->p_current_transaction->p_user_data;
  114. p_app_twi->p_current_transaction->callback(result, p_user_data);
  115. }
  116. }
  117. // This function starts pending transaction if there is no current one or
  118. // when 'switch_transaction' parameter is set to true. It is important to
  119. // switch to new transaction without setting 'p_app_twi->p_current_transaction'
  120. // to NULL in between, since this pointer is used to check idle status - see
  121. // 'app_twi_is_idle()'.
  122. static void start_pending_transaction(app_twi_t * p_app_twi,
  123. bool switch_transaction)
  124. {
  125. ASSERT(p_app_twi != NULL);
  126. for (;;)
  127. {
  128. bool start_transaction = false;
  129. CRITICAL_REGION_ENTER();
  130. if (switch_transaction || app_twi_is_idle(p_app_twi))
  131. {
  132. p_app_twi->p_current_transaction = queue_get(&p_app_twi->queue);
  133. if (p_app_twi->p_current_transaction != NULL)
  134. {
  135. start_transaction = true;
  136. }
  137. }
  138. CRITICAL_REGION_EXIT();
  139. if (!start_transaction)
  140. {
  141. return;
  142. }
  143. else
  144. {
  145. ret_code_t result;
  146. // Try to start first transfer for this new transaction.
  147. p_app_twi->current_transfer_idx = 0;
  148. result = start_transfer(p_app_twi);
  149. // If it started successfully there is nothing more to do here now.
  150. if (result == NRF_SUCCESS)
  151. {
  152. return;
  153. }
  154. // Transfer failed to start - notify user that this transaction
  155. // cannot be started and try with next one (in next iteration of
  156. // the loop).
  157. signal_end_of_transaction(p_app_twi, result);
  158. switch_transaction = true;
  159. }
  160. }
  161. }
  162. static void twi_event_handler(nrf_drv_twi_evt_t const * p_event,
  163. void * p_context)
  164. {
  165. ASSERT(p_event != NULL);
  166. app_twi_t * p_app_twi = (app_twi_t *)p_context;
  167. ret_code_t result;
  168. // This callback should be called only during transaction.
  169. ASSERT(p_app_twi->p_current_transaction != NULL);
  170. if (p_event->type == NRF_DRV_TWI_EVT_DONE)
  171. {
  172. result = NRF_SUCCESS;
  173. // Transfer finished successfully. If there is another one to be
  174. // performed in the current transaction, start it now.
  175. // [use a local variable to avoid using two volatile variables in one
  176. // expression]
  177. uint8_t current_transfer_idx = p_app_twi->current_transfer_idx;
  178. ++current_transfer_idx;
  179. if (current_transfer_idx <
  180. p_app_twi->p_current_transaction->number_of_transfers)
  181. {
  182. p_app_twi->current_transfer_idx = current_transfer_idx;
  183. result = start_transfer(p_app_twi);
  184. if (result == NRF_SUCCESS)
  185. {
  186. // The current transaction goes on and we've successfully
  187. // started its next transfer -> there is nothing more to do.
  188. return;
  189. }
  190. // [if the next transfer could not be started due to some error
  191. // we finish the transaction with this error code as the result]
  192. }
  193. }
  194. else
  195. {
  196. result = NRF_ERROR_INTERNAL;
  197. }
  198. // The current transaction has been completed or interrupted by some error.
  199. // Notify the user and start next one (if there is any).
  200. signal_end_of_transaction(p_app_twi, result);
  201. // [we switch transactions here ('p_app_twi->p_current_transaction' is set
  202. // to NULL only if there is nothing more to do) in order to not generate
  203. // spurious idle status (even for a moment)]
  204. start_pending_transaction(p_app_twi, true);
  205. }
  206. ret_code_t app_twi_init(app_twi_t * p_app_twi,
  207. nrf_drv_twi_config_t const * p_twi_config,
  208. uint8_t queue_size,
  209. app_twi_transaction_t const * * p_queue_buffer)
  210. {
  211. ASSERT(p_app_twi != NULL);
  212. ASSERT(queue_size != 0);
  213. ASSERT(p_queue_buffer != NULL);
  214. ret_code_t err_code;
  215. err_code = nrf_drv_twi_init(&p_app_twi->twi,
  216. p_twi_config,
  217. twi_event_handler,
  218. p_app_twi);
  219. VERIFY_SUCCESS(err_code);
  220. nrf_drv_twi_enable(&p_app_twi->twi);
  221. p_app_twi->queue.p_buffer = p_queue_buffer;
  222. p_app_twi->queue.size = queue_size;
  223. p_app_twi->queue.read_idx = 0;
  224. p_app_twi->queue.write_idx = 0;
  225. p_app_twi->internal_transaction_in_progress = false;
  226. p_app_twi->p_current_transaction = NULL;
  227. return NRF_SUCCESS;
  228. }
  229. void app_twi_uninit(app_twi_t * p_app_twi)
  230. {
  231. ASSERT(p_app_twi != NULL);
  232. nrf_drv_twi_uninit(&(p_app_twi->twi));
  233. p_app_twi->p_current_transaction = NULL;
  234. }
  235. ret_code_t app_twi_schedule(app_twi_t * p_app_twi,
  236. app_twi_transaction_t const * p_transaction)
  237. {
  238. ASSERT(p_app_twi != NULL);
  239. ASSERT(p_transaction != NULL);
  240. ASSERT(p_transaction->p_transfers != NULL);
  241. ASSERT(p_transaction->number_of_transfers != 0);
  242. ret_code_t result = NRF_SUCCESS;
  243. CRITICAL_REGION_ENTER();
  244. if (!queue_put(&p_app_twi->queue, p_transaction))
  245. {
  246. result = NRF_ERROR_BUSY;
  247. }
  248. CRITICAL_REGION_EXIT();
  249. if (result == NRF_SUCCESS)
  250. {
  251. // New transaction has been successfully added to queue,
  252. // so if we are currently idle it's time to start the job.
  253. start_pending_transaction(p_app_twi, false);
  254. }
  255. return result;
  256. }
  257. static void internal_transaction_cb(ret_code_t result, void * p_user_data)
  258. {
  259. app_twi_t * p_app_twi = (app_twi_t *)p_user_data;
  260. p_app_twi->internal_transaction_result = result;
  261. p_app_twi->internal_transaction_in_progress = false;
  262. }
  263. ret_code_t app_twi_perform(app_twi_t * p_app_twi,
  264. app_twi_transfer_t const * p_transfers,
  265. uint8_t number_of_transfers,
  266. void (* user_function)(void))
  267. {
  268. ASSERT(p_app_twi != NULL);
  269. ASSERT(p_transfers != NULL);
  270. ASSERT(number_of_transfers != 0);
  271. bool busy = false;
  272. CRITICAL_REGION_ENTER();
  273. if (p_app_twi->internal_transaction_in_progress)
  274. {
  275. busy = true;
  276. }
  277. else
  278. {
  279. p_app_twi->internal_transaction_in_progress = true;
  280. }
  281. CRITICAL_REGION_EXIT();
  282. if (busy)
  283. {
  284. return NRF_ERROR_BUSY;
  285. }
  286. else
  287. {
  288. app_twi_transaction_t internal_transaction =
  289. {
  290. .callback = internal_transaction_cb,
  291. .p_user_data = p_app_twi,
  292. .p_transfers = p_transfers,
  293. .number_of_transfers = number_of_transfers,
  294. };
  295. ret_code_t result = app_twi_schedule(p_app_twi, &internal_transaction);
  296. VERIFY_SUCCESS(result);
  297. while (p_app_twi->internal_transaction_in_progress)
  298. {
  299. if (user_function)
  300. {
  301. user_function();
  302. }
  303. }
  304. return p_app_twi->internal_transaction_result;
  305. }
  306. }