esp_app_trace_util.h 4.6 KB

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