esp_app_trace_util.h 5.3 KB

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