app_trace_util.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. //
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_app_trace_util.h"
  10. #include "sdkconfig.h"
  11. ///////////////////////////////////////////////////////////////////////////////
  12. ///////////////////////////////// Locks /////////////////////////////////////
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #if ESP_APPTRACE_PRINT_LOCK
  15. static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
  16. #endif
  17. int esp_apptrace_log_lock(void)
  18. {
  19. #if ESP_APPTRACE_PRINT_LOCK
  20. esp_apptrace_tmo_t tmo;
  21. esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
  22. int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
  23. return ret;
  24. #else
  25. return 0;
  26. #endif
  27. }
  28. void esp_apptrace_log_unlock(void)
  29. {
  30. #if ESP_APPTRACE_PRINT_LOCK
  31. esp_apptrace_lock_give(&s_log_lock);
  32. #endif
  33. }
  34. ///////////////////////////////////////////////////////////////////////////////
  35. ///////////////////////////////// TIMEOUT /////////////////////////////////////
  36. ///////////////////////////////////////////////////////////////////////////////
  37. esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo)
  38. {
  39. if (tmo->tmo != (int64_t)-1) {
  40. tmo->elapsed = esp_timer_get_time() - tmo->start;
  41. if (tmo->elapsed >= tmo->tmo) {
  42. return ESP_ERR_TIMEOUT;
  43. }
  44. }
  45. return ESP_OK;
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////
  48. ///////////////////////////////// LOCK ////////////////////////////////////////
  49. ///////////////////////////////////////////////////////////////////////////////
  50. esp_err_t esp_apptrace_lock_take(esp_apptrace_lock_t *lock, esp_apptrace_tmo_t *tmo)
  51. {
  52. int res;
  53. while (1) {
  54. //Todo: Replace the current locking mechanism and int_state with portTRY_ENTER_CRITICAL() instead.
  55. // do not overwrite lock->int_state before we actually acquired the mux
  56. #if CONFIG_FREERTOS_SMP
  57. unsigned int_state = portDISABLE_INTERRUPTS();
  58. #else
  59. unsigned int_state = portSET_INTERRUPT_MASK_FROM_ISR();
  60. #endif
  61. bool success = spinlock_acquire(&lock->mux, 0);
  62. if (success) {
  63. lock->int_state = int_state;
  64. return ESP_OK;
  65. }
  66. #if CONFIG_FREERTOS_SMP
  67. portRESTORE_INTERRUPTS(int_state);
  68. #else
  69. portCLEAR_INTERRUPT_MASK_FROM_ISR(int_state);
  70. #endif
  71. // we can be preempted from this place till the next call (above) to portSET_INTERRUPT_MASK_FROM_ISR()
  72. res = esp_apptrace_tmo_check(tmo);
  73. if (res != ESP_OK) {
  74. break;
  75. }
  76. }
  77. return res;
  78. }
  79. esp_err_t esp_apptrace_lock_give(esp_apptrace_lock_t *lock)
  80. {
  81. // save lock's irq state value for this CPU
  82. unsigned int_state = lock->int_state;
  83. // after call to the following func we can not be sure that lock->int_state
  84. // is not overwritten by other CPU who has acquired the mux just after we released it. See esp_apptrace_lock_take().
  85. spinlock_release(&lock->mux);
  86. #if CONFIG_FREERTOS_SMP
  87. portRESTORE_INTERRUPTS(int_state);
  88. #else
  89. portCLEAR_INTERRUPT_MASK_FROM_ISR(int_state);
  90. #endif
  91. return ESP_OK;
  92. }
  93. ///////////////////////////////////////////////////////////////////////////////
  94. ////////////////////////////// RING BUFFER ////////////////////////////////////
  95. ///////////////////////////////////////////////////////////////////////////////
  96. uint8_t *esp_apptrace_rb_produce(esp_apptrace_rb_t *rb, uint32_t size)
  97. {
  98. uint8_t *ptr = rb->data + rb->wr;
  99. // check for avalable space
  100. if (rb->rd <= rb->wr) {
  101. // |?R......W??|
  102. if (rb->wr + size >= rb->size) {
  103. if (rb->rd == 0) {
  104. return NULL; // cannot wrap wr
  105. }
  106. if (rb->wr + size == rb->size) {
  107. rb->wr = 0;
  108. } else {
  109. // check if we can wrap wr earlier to get space for requested size
  110. if (size > rb->rd - 1) {
  111. return NULL; // cannot wrap wr
  112. }
  113. // shrink buffer a bit, full size will be restored at rd wrapping
  114. rb->cur_size = rb->wr;
  115. rb->wr = 0;
  116. ptr = rb->data;
  117. if (rb->rd == rb->cur_size) {
  118. rb->rd = 0;
  119. if (rb->cur_size < rb->size) {
  120. rb->cur_size = rb->size;
  121. }
  122. }
  123. rb->wr += size;
  124. }
  125. } else {
  126. rb->wr += size;
  127. }
  128. } else {
  129. // |?W......R??|
  130. if (size > rb->rd - rb->wr - 1) {
  131. return NULL;
  132. }
  133. rb->wr += size;
  134. }
  135. return ptr;
  136. }
  137. uint8_t *esp_apptrace_rb_consume(esp_apptrace_rb_t *rb, uint32_t size)
  138. {
  139. uint8_t *ptr = rb->data + rb->rd;
  140. if (rb->rd <= rb->wr) {
  141. // |?R......W??|
  142. if (rb->rd + size > rb->wr) {
  143. return NULL;
  144. }
  145. rb->rd += size;
  146. } else {
  147. // |?W......R??|
  148. if (rb->rd + size > rb->cur_size) {
  149. return NULL;
  150. } else if (rb->rd + size == rb->cur_size) {
  151. // restore full size usage
  152. if (rb->cur_size < rb->size) {
  153. rb->cur_size = rb->size;
  154. }
  155. rb->rd = 0;
  156. } else {
  157. rb->rd += size;
  158. }
  159. }
  160. return ptr;
  161. }
  162. uint32_t esp_apptrace_rb_read_size_get(esp_apptrace_rb_t *rb)
  163. {
  164. uint32_t size = 0;
  165. if (rb->rd <= rb->wr) {
  166. // |?R......W??|
  167. size = rb->wr - rb->rd;
  168. } else {
  169. // |?W......R??|
  170. size = rb->cur_size - rb->rd;
  171. }
  172. return size;
  173. }
  174. uint32_t esp_apptrace_rb_write_size_get(esp_apptrace_rb_t *rb)
  175. {
  176. uint32_t size = 0;
  177. if (rb->rd <= rb->wr) {
  178. // |?R......W??|
  179. size = rb->size - rb->wr;
  180. if (size && rb->rd == 0) {
  181. size--;
  182. }
  183. } else {
  184. // |?W......R??|
  185. size = rb->rd - rb->wr - 1;
  186. }
  187. return size;
  188. }