esp_app_trace_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef ESP_APP_TRACE_UTIL_H_
  14. #define ESP_APP_TRACE_UTIL_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "freertos/FreeRTOS.h"
  19. #include "esp_err.h"
  20. /** Infinite waiting timeout */
  21. #define ESP_APPTRACE_TMO_INFINITE ((uint32_t)-1)
  22. /** Structure which holds data necessary for measuring time intervals.
  23. *
  24. * After initialization via esp_apptrace_tmo_init() user needs to call esp_apptrace_tmo_check()
  25. * periodically to check timeout for expiration.
  26. */
  27. typedef struct {
  28. uint32_t start; ///< time interval start (in CPU ticks)
  29. uint32_t tmo; ///< timeout value (in us)
  30. uint32_t elapsed; ///< elapsed time (in us)
  31. } esp_apptrace_tmo_t;
  32. /**
  33. * @brief Initializes timeout structure.
  34. *
  35. * @param tmo Pointer to timeout structure to be initialized.
  36. * @param user_tmo Timeout value (in us). Use ESP_APPTRACE_TMO_INFINITE to wait indefinetly.
  37. */
  38. static inline void esp_apptrace_tmo_init(esp_apptrace_tmo_t *tmo, uint32_t user_tmo)
  39. {
  40. tmo->start = portGET_RUN_TIME_COUNTER_VALUE();
  41. tmo->tmo = user_tmo;
  42. tmo->elapsed = 0;
  43. }
  44. /**
  45. * @brief Checks timeout for expiration.
  46. *
  47. * @param tmo Pointer to timeout structure to be initialized.
  48. *
  49. * @return ESP_OK on success, otherwise \see esp_err_t
  50. */
  51. esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo);
  52. static inline uint32_t esp_apptrace_tmo_remaining_us(esp_apptrace_tmo_t *tmo)
  53. {
  54. return tmo->tmo != ESP_APPTRACE_TMO_INFINITE ? (tmo->elapsed - tmo->tmo) : ESP_APPTRACE_TMO_INFINITE;
  55. }
  56. /** Tracing module synchronization lock */
  57. typedef struct {
  58. portMUX_TYPE mux;
  59. unsigned int_state;
  60. } esp_apptrace_lock_t;
  61. /**
  62. * @brief Initializes lock structure.
  63. *
  64. * @param lock Pointer to lock structure to be initialized.
  65. */
  66. static inline void esp_apptrace_lock_init(esp_apptrace_lock_t *lock)
  67. {
  68. vPortCPUInitializeMutex(&lock->mux);
  69. lock->int_state = 0;
  70. }
  71. /**
  72. * @brief Tries to acquire lock in specified time period.
  73. *
  74. * @param lock Pointer to lock structure.
  75. * @param tmo Pointer to timeout struct.
  76. *
  77. * @return ESP_OK on success, otherwise \see esp_err_t
  78. */
  79. esp_err_t esp_apptrace_lock_take(esp_apptrace_lock_t *lock, esp_apptrace_tmo_t *tmo);
  80. /**
  81. * @brief Releases lock.
  82. *
  83. * @param lock Pointer to lock structure.
  84. *
  85. * @return ESP_OK on success, otherwise \see esp_err_t
  86. */
  87. esp_err_t esp_apptrace_lock_give(esp_apptrace_lock_t *lock);
  88. /** Ring buffer control structure.
  89. *
  90. * @note For purposes of application tracing module if there is no enough space for user data and write pointer can be wrapped
  91. * current ring buffer size can be temporarily shrinked in order to provide buffer with requested size.
  92. */
  93. typedef struct {
  94. uint8_t *data; ///< pointer to data storage
  95. volatile uint32_t size; ///< size of data storage
  96. volatile uint32_t cur_size; ///< current size of data storage
  97. volatile uint32_t rd; ///< read pointer
  98. volatile uint32_t wr; ///< write pointer
  99. } esp_apptrace_rb_t;
  100. /**
  101. * @brief Initializes ring buffer control structure.
  102. *
  103. * @param rb Pointer to ring buffer structure to be initialized.
  104. * @param data Pointer to buffer to be used as ring buffer's data storage.
  105. * @param size Size of buffer to be used as ring buffer's data storage.
  106. */
  107. static inline void esp_apptrace_rb_init(esp_apptrace_rb_t *rb, uint8_t *data, uint32_t size)
  108. {
  109. rb->data = data;
  110. rb->size = rb->cur_size = size;
  111. rb->rd = 0;
  112. rb->wr = 0;
  113. }
  114. /**
  115. * @brief Allocates memory chunk in ring buffer.
  116. *
  117. * @param rb Pointer to ring buffer structure.
  118. * @param size Size of the memory to allocate.
  119. *
  120. * @return Pointer to the allocated memory or NULL in case of failure.
  121. */
  122. uint8_t *esp_apptrace_rb_produce(esp_apptrace_rb_t *rb, uint32_t size);
  123. /**
  124. * @brief Consumes memory chunk in ring buffer.
  125. *
  126. * @param rb Pointer to ring buffer structure.
  127. * @param size Size of the memory to consume.
  128. *
  129. * @return Pointer to consumed memory chunk or NULL in case of failure.
  130. */
  131. uint8_t *esp_apptrace_rb_consume(esp_apptrace_rb_t *rb, uint32_t size);
  132. /**
  133. * @brief Gets size of memory which can consumed with single call to esp_apptrace_rb_consume().
  134. *
  135. * @param rb Pointer to ring buffer structure.
  136. *
  137. * @return Size of memory which can consumed.
  138. *
  139. * @note Due to read pointer wrapping returned size can be less then the total size of available data.
  140. */
  141. uint32_t esp_apptrace_rb_read_size_get(esp_apptrace_rb_t *rb);
  142. /**
  143. * @brief Gets size of memory which can produced with single call to esp_apptrace_rb_produce().
  144. *
  145. * @param rb Pointer to ring buffer structure.
  146. *
  147. * @return Size of memory which can produced.
  148. *
  149. * @note Due to write pointer wrapping returned size can be less then the total size of available data.
  150. */
  151. uint32_t esp_apptrace_rb_write_size_get(esp_apptrace_rb_t *rb);
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif //ESP_APP_TRACE_UTIL_H_