app_trace_util.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. //
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "esp_app_trace_util.h"
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/clk.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/clk.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/clk.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C3
  25. #include "esp32c3/clk.h"
  26. #endif
  27. ///////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////// TIMEOUT /////////////////////////////////////
  29. ///////////////////////////////////////////////////////////////////////////////
  30. #define ESP_APPTRACE_CPUTICKS2US(_t_, _cpu_freq_) ((_t_)/(_cpu_freq_/1000000))
  31. #define ESP_APPTRACE_US2CPUTICKS(_t_, _cpu_freq_) ((_t_)*(_cpu_freq_/1000000))
  32. esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo)
  33. {
  34. int cpu_freq = esp_clk_cpu_freq();
  35. if (tmo->tmo != ESP_APPTRACE_TMO_INFINITE) {
  36. unsigned cur = portGET_RUN_TIME_COUNTER_VALUE();
  37. if (tmo->start <= cur) {
  38. tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(cur - tmo->start, cpu_freq);
  39. } else {
  40. tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(0xFFFFFFFF - tmo->start + cur, cpu_freq);
  41. }
  42. if (tmo->elapsed >= tmo->tmo) {
  43. return ESP_ERR_TIMEOUT;
  44. }
  45. }
  46. return ESP_OK;
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////
  49. ///////////////////////////////// LOCK ////////////////////////////////////////
  50. ///////////////////////////////////////////////////////////////////////////////
  51. esp_err_t esp_apptrace_lock_take(esp_apptrace_lock_t *lock, esp_apptrace_tmo_t *tmo)
  52. {
  53. int res;
  54. while (1) {
  55. // do not overwrite lock->int_state before we actually acquired the mux
  56. unsigned int_state = portENTER_CRITICAL_NESTED();
  57. // FIXME: if mux is busy it is not good idea to loop during the whole tmo with disabled IRQs.
  58. // So we check mux state using zero tmo, restore IRQs and let others tasks/IRQs to run on this CPU
  59. // while we are doing our own tmo check.
  60. #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
  61. bool success = vPortCPUAcquireMutexTimeout(&lock->mux, 0, __FUNCTION__, __LINE__);
  62. #else
  63. bool success = vPortCPUAcquireMutexTimeout(&lock->mux, 0);
  64. #endif
  65. if (success) {
  66. lock->int_state = int_state;
  67. return ESP_OK;
  68. }
  69. portEXIT_CRITICAL_NESTED(int_state);
  70. // we can be preempted from this place till the next call (above) to portENTER_CRITICAL_NESTED()
  71. res = esp_apptrace_tmo_check(tmo);
  72. if (res != ESP_OK) {
  73. break;
  74. }
  75. }
  76. return res;
  77. }
  78. esp_err_t esp_apptrace_lock_give(esp_apptrace_lock_t *lock)
  79. {
  80. // save lock's irq state value for this CPU
  81. unsigned int_state = lock->int_state;
  82. // after call to the following func we can not be sure that lock->int_state
  83. // is not overwritten by other CPU who has acquired the mux just after we released it. See esp_apptrace_lock_take().
  84. #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
  85. vPortCPUReleaseMutex(&lock->mux, __FUNCTION__, __LINE__);
  86. #else
  87. vPortCPUReleaseMutex(&lock->mux);
  88. #endif
  89. portEXIT_CRITICAL_NESTED(int_state);
  90. return ESP_OK;
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////
  93. ////////////////////////////// RING BUFFER ////////////////////////////////////
  94. ///////////////////////////////////////////////////////////////////////////////
  95. uint8_t *esp_apptrace_rb_produce(esp_apptrace_rb_t *rb, uint32_t size)
  96. {
  97. uint8_t *ptr = rb->data + rb->wr;
  98. // check for avalable space
  99. if (rb->rd <= rb->wr) {
  100. // |?R......W??|
  101. if (rb->wr + size >= rb->size) {
  102. if (rb->rd == 0) {
  103. return NULL; // cannot wrap wr
  104. }
  105. if (rb->wr + size == rb->size) {
  106. rb->wr = 0;
  107. } else {
  108. // check if we can wrap wr earlier to get space for requested size
  109. if (size > rb->rd - 1) {
  110. return NULL; // cannot wrap wr
  111. }
  112. // shrink buffer a bit, full size will be restored at rd wrapping
  113. rb->cur_size = rb->wr;
  114. rb->wr = 0;
  115. ptr = rb->data;
  116. if (rb->rd == rb->cur_size) {
  117. rb->rd = 0;
  118. if (rb->cur_size < rb->size) {
  119. rb->cur_size = rb->size;
  120. }
  121. }
  122. rb->wr += size;
  123. }
  124. } else {
  125. rb->wr += size;
  126. }
  127. } else {
  128. // |?W......R??|
  129. if (size > rb->rd - rb->wr - 1) {
  130. return NULL;
  131. }
  132. rb->wr += size;
  133. }
  134. return ptr;
  135. }
  136. uint8_t *esp_apptrace_rb_consume(esp_apptrace_rb_t *rb, uint32_t size)
  137. {
  138. uint8_t *ptr = rb->data + rb->rd;
  139. if (rb->rd <= rb->wr) {
  140. // |?R......W??|
  141. if (rb->rd + size > rb->wr) {
  142. return NULL;
  143. }
  144. rb->rd += size;
  145. } else {
  146. // |?W......R??|
  147. if (rb->rd + size > rb->cur_size) {
  148. return NULL;
  149. } else if (rb->rd + size == rb->cur_size) {
  150. // restore full size usage
  151. if (rb->cur_size < rb->size) {
  152. rb->cur_size = rb->size;
  153. }
  154. rb->rd = 0;
  155. } else {
  156. rb->rd += size;
  157. }
  158. }
  159. return ptr;
  160. }
  161. uint32_t esp_apptrace_rb_read_size_get(esp_apptrace_rb_t *rb)
  162. {
  163. uint32_t size = 0;
  164. if (rb->rd <= rb->wr) {
  165. // |?R......W??|
  166. size = rb->wr - rb->rd;
  167. } else {
  168. // |?W......R??|
  169. size = rb->cur_size - rb->rd;
  170. }
  171. return size;
  172. }
  173. uint32_t esp_apptrace_rb_write_size_get(esp_apptrace_rb_t *rb)
  174. {
  175. uint32_t size = 0;
  176. if (rb->rd <= rb->wr) {
  177. // |?R......W??|
  178. size = rb->size - rb->wr;
  179. if (size && rb->rd == 0) {
  180. size--;
  181. }
  182. } else {
  183. // |?W......R??|
  184. size = rb->rd - rb->wr - 1;
  185. }
  186. return size;
  187. }