esp_app_trace_util.h 5.3 KB

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