app_trace.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. // Hot It Works
  15. // ************
  16. // 1. Components Overview
  17. // ======================
  18. // Xtensa has useful feature: TRAX debug module. It allows recording program execution flow at run-time without disturbing CPU.
  19. // Exectution flow data are written to configurable Trace RAM block. Besides accessing Trace RAM itself TRAX module also allows to read/write
  20. // trace memory via its registers by means of JTAG, APB or ERI transactions.
  21. // ESP32 has two Xtensa cores with separate TRAX modules on them and provides two special memory regions to be used as trace memory.
  22. // Chip allows muxing access to those trace memory blocks in such a way that while one block is accessed by CPUs another one can be accessed by host
  23. // by means of reading/writing TRAX registers via JTAG. Blocks muxing is configurable at run-time and allows switching trace memory blocks between
  24. // accessors in round-robin fashion so they can read/write separate memory blocks without disturbing each other.
  25. // This module implements application tracing feature based on above mechanisms. It allows to transfer arbitrary user data to/from
  26. // host via JTAG with minimal impact on system performance. This module is implied to be used in the following tracing scheme.
  27. // ------>------ ----- (host components) -----
  28. // | | | |
  29. // ------------------- ----------------------- ----------------------- ---------------- ------ --------- -----------------
  30. // |trace data source|-->|target tracing module|<--->|TRAX_MEM0 | TRAX_MEM1|---->|TRAX_DATA_REGS|<-->|JTAG|<--->|OpenOCD|-->|trace data sink|
  31. // ------------------- ----------------------- ----------------------- ---------------- ------ --------- -----------------
  32. // | | | |
  33. // | ------<------ ---------------- |
  34. // |<------------------------------------------->|TRAX_CTRL_REGS|<---->|
  35. // ----------------
  36. // In general tracing goes in the following way. User application requests tracing module to send some data by calling esp_apptrace_buffer_get(),
  37. // module allocates necessary buffer in current input trace block. Then user fills received buffer with data and calls esp_apptrace_buffer_put().
  38. // When current input trace block is filled with app data it is exposed to host and the second block becomes input one and buffer filling restarts.
  39. // While target application fills one TRAX block host reads another one via JTAG.
  40. // This module also allows communication in the opposite direction: from host to target. As it was said ESP32 and host can access different TRAX blocks
  41. // simultaneously, so while target writes trace data to one block host can write its own data (e.g. tracing commands) to another one then when
  42. // blocks are switched host receives trace data and target receives data written by host application. Target user application can read host data
  43. // by calling esp_apptrace_read() API.
  44. // To control buffer switching and for other communication purposes this implementation uses some TRAX registers. It is safe since HW TRAX tracing
  45. // can not be used along with application tracing feature so these registers are freely readable/writeable via JTAG from host and via ERI from ESP32 cores.
  46. // Overhead of this implementation on target CPU is produced only by allocating/managing buffers and copying of data.
  47. // On the host side special OpenOCD command must be used to read trace data.
  48. // 2. TRAX Registers layout
  49. // ========================
  50. // This module uses two TRAX HW registers to communicate with host SW (OpenOCD).
  51. // - Control register uses TRAX_DELAYCNT as storage. Only lower 24 bits of TRAX_DELAYCNT are writable. Control register has the following bitfields:
  52. // | 31..XXXXXX..24 | 23 .(host_connect). 23| 22..(block_id)..15 | 14..(block_len)..0 |
  53. // 14..0 bits - actual length of user data in trace memory block. Target updates it every time it fills memory block and exposes it to host.
  54. // Host writes zero to this field when it finishes reading exposed block;
  55. // 21..15 bits - trace memory block transfer ID. Block counter. It can overflow. Updated by target, host should not modify it. Actually can be 2 bits;
  56. // 22 bit - 'host data present' flag. If set to one there is data from host, otherwise - no host data;
  57. // 23 bit - 'host connected' flag. If zero then host is not connected and tracing module works in post-mortem mode, otherwise in streaming mode;
  58. // - Status register uses TRAX_TRIGGERPC as storage. If this register is not zero then current CPU is changing TRAX registers and
  59. // this register holds address of the instruction which application will execute when it finishes with those registers modifications.
  60. // See 'Targets Connection' setion for details.
  61. // 3. Modes of operation
  62. // =====================
  63. // This module supports two modes of operation:
  64. // - Post-mortem mode. This is the default mode. In this mode application tracing module does not check whether host has read all the data from block
  65. // exposed to it and switches block in any case. The mode does not need host interaction for operation and so can be useful when only the latest
  66. // trace data are necessary, e.g. for analyzing crashes. On panic the latest data from current input block are exposed to host and host can read them.
  67. // It can happen that system panic occurs when there are very small amount of data which are not exposed to host yet (e.g. crash just after the
  68. // TRAX block switch). In this case the previous 16KB of collected data will be dropped and host will see the latest, but very small piece of trace.
  69. // It can be insufficient to diagnose the problem. To avoid such situations there is menuconfig option
  70. // CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH
  71. // which controls the threshold for flushing data in case of panic.
  72. // - Streaming mode. Tracing module enters this mode when host connects to target and sets respective bits in control registers (per core).
  73. // In this mode before switching the block tracing module waits for the host to read all the data from the previously exposed block.
  74. // On panic tracing module also waits (timeout is configured via menuconfig via CONFIG_ESP32_APPTRACE_ONPANIC_HOST_FLUSH_TMO) for the host to read all data.
  75. // 4. Communication Protocol
  76. // =========================
  77. // 4.1 Trace Memory Blocks
  78. // -----------------------
  79. // Communication is controlled via special register. Host periodically polls control register on each core to find out if there are any data available.
  80. // When current input memory block is filled it is exposed to host and 'block_len' and 'block_id' fields are updated in the control register.
  81. // Host reads new register value and according to it's value starts reading data from exposed block. Meanwhile target starts filling another trace block.
  82. // When host finishes reading the block it clears 'block_len' field in control register indicating to the target that it is ready to accept the next one.
  83. // If the host has some data to transfer to the target it writes them to trace memory block before clearing 'block_len' field. Then it sets
  84. // 'host_data_present' bit and clears 'block_len' field in control register. Upon every block switch target checks 'host_data_present' bit and if it is set
  85. // reads them to down buffer before writing any trace data to switched TRAX block.
  86. // 4.2 User Data Chunks Level
  87. // --------------------------
  88. // Since trace memory block is shared between user data chunks and data copying is performed on behalf of the API user (in its normal context) in
  89. // multithreading environment it can happen that task/ISR which copies data is preempted by another high prio task/ISR. So it is possible situation
  90. // that task/ISR will fail to complete filling its data chunk before the whole trace block is exposed to the host. To handle such conditions tracing
  91. // module prepends all user data chunks with header which contains allocated buffer size and actual data length within it. OpenOCD command
  92. // which reads application traces reports error when it reads incomplete user data block.
  93. // Data which are transffered from host to target are also prepended with a header. Down channel data header is simple and consists of one two bytes field
  94. // containing length of host data following the header.
  95. // 4.3 Data Buffering
  96. // ------------------
  97. // It takes some time for the host to read TRAX memory block via JTAG. In streaming mode it can happen that target has filled its TRAX block, but host
  98. // has not completed reading of the previous one yet. So in this case time critical tracing calls (which can not be delayed for too long time due to
  99. // the lack of free memory in TRAX block) can be dropped. To avoid such scenarios tracing module implements data buffering. Buffered data will be sent
  100. // to the host later when TRAX block switch occurs. The maximum size of the buffered data is controlled by menuconfig option
  101. // CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX.
  102. // 4.4 Target Connection/Disconnection
  103. // -----------------------------------
  104. // When host is going to start tracing in streaming mode it needs to put both ESP32 cores into initial state when 'host connected' bit is set
  105. // on both cores. To accomplish this host halts both cores and sets this bit in TRAX registers. But target code can be halted in state when it has read control
  106. // register but has not updated its value. To handle such situations target code indicates to the host that it is updating control register by writing
  107. // non-zero value to status register. Actually it writes address of the instruction which it will execute when it finishes with
  108. // the registers update. When target is halted during control register update host sets breakpoint at the address from status register and resumes CPU.
  109. // After target code finishes with register update it is halted on breakpoint, host detects it and safely sets 'host connected' bit. When both cores
  110. // are set up they are resumed. Tracing starts without further intrusion into CPUs work.
  111. // When host is going to stop tracing in streaming mode it needs to disconnect targets. Disconnection process is done using the same algorithm
  112. // as for connecting, but 'host connected' bits are cleared on ESP32 cores.
  113. // 5. Module Access Synchronization
  114. // ================================
  115. // Access to internal module's data is synchronized with custom mutex. Mutex is a wrapper for portMUX_TYPE and uses almost the same sync mechanism as in
  116. // vPortCPUAcquireMutex/vPortCPUReleaseMutex. The mechanism uses S32C1I Xtensa instruction to implement exclusive access to module's data from tasks and
  117. // ISRs running on both cores. Also custom mutex allows specifying timeout for locking operation. Locking routine checks underlaying mutex in cycle until
  118. // it gets its ownership or timeout expires. The differences of application tracing module's mutex implementation from vPortCPUAcquireMutex/vPortCPUReleaseMutex are:
  119. // - Support for timeouts.
  120. // - Local IRQs for CPU which owns the mutex are disabled till the call to unlocking routine. This is made to avoid possible task's prio inversion.
  121. // When low prio task takes mutex and enables local IRQs gets preempted by high prio task which in its turn can try to acquire mutex using infinite timeout.
  122. // So no local task switch occurs when mutex is locked. But this does not apply to tasks on another CPU.
  123. // WARNING: Priority inversion can happen when low prio task works on one CPU and medium and high prio tasks work on another.
  124. // WARNING: Care must be taken when selecting timeout values for trace calls from ISRs. Tracing module does not care about watchdogs when waiting
  125. // on internal locks and for host to complete previous block reading, so if timeout value exceeds watchdog's one it can lead to the system reboot.
  126. // 6. Timeouts
  127. // ===========
  128. // Timeout mechanism is based on xthal_get_ccount() routine and supports timeout values in microseconds.
  129. // There are two situations when task/ISR can be delayed by tracing API call. Timeout mechanism takes into account both conditions:
  130. // - Trace data are locked by another task/ISR. When wating on trace data lock.
  131. // - Current TRAX memory input block is full when working in streaming mode (host is connected). When waiting for host to complete previous block reading.
  132. // When wating for any of above conditions xthal_get_ccount() is called periodically to calculate time elapsed from trace API routine entry. When elapsed
  133. // time exceeds specified timeout value operation is canceled and ESP_ERR_TIMEOUT code is returned.
  134. #include <string.h>
  135. #include <sys/param.h>
  136. #include "soc/soc.h"
  137. #include "soc/dport_reg.h"
  138. #include "eri.h"
  139. #include "trax.h"
  140. #include "soc/timer_periph.h"
  141. #include "freertos/FreeRTOS.h"
  142. #include "esp_app_trace.h"
  143. #if CONFIG_ESP32_APPTRACE_ENABLE
  144. #define ESP_APPTRACE_MAX_VPRINTF_ARGS 256
  145. #define ESP_APPTRACE_HOST_BUF_SIZE 256
  146. #define ESP_APPTRACE_PRINT_LOCK 0
  147. #include "esp_log.h"
  148. const static char *TAG = "esp_apptrace";
  149. #if ESP_APPTRACE_PRINT_LOCK
  150. #define ESP_APPTRACE_LOG( format, ... ) \
  151. do { \
  152. esp_apptrace_log_lock(); \
  153. ets_printf(format, ##__VA_ARGS__); \
  154. esp_apptrace_log_unlock(); \
  155. } while(0)
  156. #else
  157. #define ESP_APPTRACE_LOG( format, ... ) \
  158. do { \
  159. ets_printf(format, ##__VA_ARGS__); \
  160. } while(0)
  161. #endif
  162. #define ESP_APPTRACE_LOG_LEV( _L_, level, format, ... ) \
  163. do { \
  164. if (LOG_LOCAL_LEVEL >= level) { \
  165. ESP_APPTRACE_LOG(LOG_FORMAT(_L_, format), esp_log_early_timestamp(), TAG, ##__VA_ARGS__); \
  166. } \
  167. } while(0)
  168. #define ESP_APPTRACE_LOGE( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_ERROR, format, ##__VA_ARGS__)
  169. #define ESP_APPTRACE_LOGW( format, ... ) ESP_APPTRACE_LOG_LEV(W, ESP_LOG_WARN, format, ##__VA_ARGS__)
  170. #define ESP_APPTRACE_LOGI( format, ... ) ESP_APPTRACE_LOG_LEV(I, ESP_LOG_INFO, format, ##__VA_ARGS__)
  171. #define ESP_APPTRACE_LOGD( format, ... ) ESP_APPTRACE_LOG_LEV(D, ESP_LOG_DEBUG, format, ##__VA_ARGS__)
  172. #define ESP_APPTRACE_LOGV( format, ... ) ESP_APPTRACE_LOG_LEV(V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)
  173. #define ESP_APPTRACE_LOGO( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_NONE, format, ##__VA_ARGS__)
  174. // TODO: move these (and same definitions in trax.c to dport_reg.h)
  175. #define TRACEMEM_MUX_PROBLK0_APPBLK1 0
  176. #define TRACEMEM_MUX_BLK0_ONLY 1
  177. #define TRACEMEM_MUX_BLK1_ONLY 2
  178. #define TRACEMEM_MUX_PROBLK1_APPBLK0 3
  179. // TRAX is disabled, so we use its registers for our own purposes
  180. // | 31..XXXXXX..24 | 23 .(host_connect). 23 | 22 .(host_data). 22| 21..(block_id)..15 | 14..(block_len)..0 |
  181. #define ESP_APPTRACE_TRAX_CTRL_REG ERI_TRAX_DELAYCNT
  182. #define ESP_APPTRACE_TRAX_STAT_REG ERI_TRAX_TRIGGERPC
  183. #define ESP_APPTRACE_TRAX_BLOCK_LEN_MSK 0x7FFFUL
  184. #define ESP_APPTRACE_TRAX_BLOCK_LEN(_l_) ((_l_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  185. #define ESP_APPTRACE_TRAX_BLOCK_LEN_GET(_v_) ((_v_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  186. #define ESP_APPTRACE_TRAX_BLOCK_ID_MSK 0x7FUL
  187. #define ESP_APPTRACE_TRAX_BLOCK_ID(_id_) (((_id_) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK) << 15)
  188. #define ESP_APPTRACE_TRAX_BLOCK_ID_GET(_v_) (((_v_) >> 15) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)
  189. #define ESP_APPTRACE_TRAX_HOST_DATA (1 << 22)
  190. #define ESP_APPTRACE_TRAX_HOST_CONNECT (1 << 23)
  191. #if CONFIG_SYSVIEW_ENABLE
  192. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) (0)
  193. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (_v_)
  194. #else
  195. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) ((_cid_) << 15)
  196. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (~(1 << 15) & (_v_))
  197. #endif
  198. #define ESP_APPTRACE_USR_BLOCK_RAW_SZ(_s_) ((_s_) + sizeof(esp_tracedata_hdr_t))
  199. static volatile uint8_t *s_trax_blocks[] = {
  200. (volatile uint8_t *) 0x3FFFC000,
  201. (volatile uint8_t *) 0x3FFF8000
  202. };
  203. #define ESP_APPTRACE_TRAX_BLOCKS_NUM (sizeof(s_trax_blocks)/sizeof(s_trax_blocks[0]))
  204. #define ESP_APPTRACE_TRAX_INBLOCK_START 0
  205. #define ESP_APPTRACE_TRAX_INBLOCK_MARKER() (s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2])
  206. #define ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(_v_) do {s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2] += (_v_);}while(0)
  207. #define ESP_APPTRACE_TRAX_INBLOCK_GET() (&s_trace_buf.trax.blocks[s_trace_buf.trax.state.in_block % 2])
  208. #define ESP_APPTRACE_TRAX_BLOCK_SIZE (0x4000UL)
  209. #if CONFIG_SYSVIEW_ENABLE
  210. #define ESP_APPTRACE_USR_DATA_LEN_MAX 255UL
  211. #else
  212. #define ESP_APPTRACE_USR_DATA_LEN_MAX (ESP_APPTRACE_TRAX_BLOCK_SIZE - sizeof(esp_tracedata_hdr_t))
  213. #endif
  214. #define ESP_APPTRACE_HW_TRAX 0
  215. #define ESP_APPTRACE_HW_MAX 1
  216. #define ESP_APPTRACE_HW(_i_) (&s_trace_hw[_i_])
  217. /** Trace data header. Every user data chunk is prepended with this header.
  218. * User allocates block with esp_apptrace_buffer_get and then fills it with data,
  219. * in multithreading environment it can happen that tasks gets buffer and then gets interrupted,
  220. * so it is possible that user data are incomplete when TRAX memory block is exposed to the host.
  221. * In this case host SW will see that wr_sz < block_sz and will report error.
  222. */
  223. typedef struct {
  224. #if CONFIG_SYSVIEW_ENABLE
  225. uint8_t block_sz; // size of allocated block for user data
  226. uint8_t wr_sz; // size of actually written data
  227. #else
  228. uint16_t block_sz; // size of allocated block for user data
  229. uint16_t wr_sz; // size of actually written data
  230. #endif
  231. } esp_tracedata_hdr_t;
  232. /** TODO: docs
  233. */
  234. typedef struct {
  235. uint16_t block_sz; // size of allocated block for user data
  236. } esp_hostdata_hdr_t;
  237. /** TRAX HW transport state */
  238. typedef struct {
  239. uint32_t in_block; // input block ID
  240. // TODO: change to uint16_t
  241. uint32_t markers[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // block filling level markers
  242. } esp_apptrace_trax_state_t;
  243. /** memory block parameters */
  244. typedef struct {
  245. uint8_t *start; // start address
  246. uint16_t sz; // size
  247. } esp_apptrace_mem_block_t;
  248. /** TRAX HW transport data */
  249. typedef struct {
  250. volatile esp_apptrace_trax_state_t state; // state
  251. esp_apptrace_mem_block_t blocks[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // memory blocks
  252. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  253. // ring buffer control struct for pending user blocks
  254. esp_apptrace_rb_t rb_pend;
  255. // storage for pending user blocks
  256. uint8_t pending_data[CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX + 1];
  257. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  258. // ring buffer control struct for pending user data chunks sizes,
  259. // every chunk contains whole number of user blocks and fit into TRAX memory block
  260. esp_apptrace_rb_t rb_pend_chunk_sz;
  261. // storage for above ring buffer data
  262. uint16_t pending_chunk_sz[CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX/ESP_APPTRACE_TRAX_BLOCK_SIZE + 2];
  263. // current (accumulated) pending user data chunk size
  264. uint16_t cur_pending_chunk_sz;
  265. #endif
  266. #endif
  267. } esp_apptrace_trax_data_t;
  268. /** tracing module internal data */
  269. typedef struct {
  270. esp_apptrace_lock_t lock; // sync lock
  271. uint8_t inited; // module initialization state flag
  272. // ring buffer control struct for data from host (down buffer)
  273. esp_apptrace_rb_t rb_down;
  274. // storage for above ring buffer data
  275. esp_apptrace_trax_data_t trax; // TRAX HW transport data
  276. } esp_apptrace_buffer_t;
  277. static esp_apptrace_buffer_t s_trace_buf;
  278. #if ESP_APPTRACE_PRINT_LOCK
  279. static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
  280. #endif
  281. typedef struct {
  282. uint8_t *(*get_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  283. esp_err_t (*put_up_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  284. esp_err_t (*flush_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  285. uint8_t *(*get_down_buffer)(uint32_t *, esp_apptrace_tmo_t *);
  286. esp_err_t (*put_down_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  287. bool (*host_is_connected)(void);
  288. esp_err_t (*status_reg_set)(uint32_t val);
  289. esp_err_t (*status_reg_get)(uint32_t *val);
  290. } esp_apptrace_hw_t;
  291. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size);
  292. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo);
  293. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo);
  294. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  295. static bool esp_apptrace_trax_host_is_connected(void);
  296. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo);
  297. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  298. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val);
  299. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val);
  300. static esp_apptrace_hw_t s_trace_hw[ESP_APPTRACE_HW_MAX] = {
  301. {
  302. .get_up_buffer = esp_apptrace_trax_get_buffer,
  303. .put_up_buffer = esp_apptrace_trax_put_buffer,
  304. .flush_up_buffer = esp_apptrace_trax_flush,
  305. .get_down_buffer = esp_apptrace_trax_down_buffer_get,
  306. .put_down_buffer = esp_apptrace_trax_down_buffer_put,
  307. .host_is_connected = esp_apptrace_trax_host_is_connected,
  308. .status_reg_set = esp_apptrace_trax_status_reg_set,
  309. .status_reg_get = esp_apptrace_trax_status_reg_get
  310. }
  311. };
  312. static inline int esp_apptrace_log_lock(void)
  313. {
  314. #if ESP_APPTRACE_PRINT_LOCK
  315. esp_apptrace_tmo_t tmo;
  316. esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
  317. int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
  318. return ret;
  319. #else
  320. return 0;
  321. #endif
  322. }
  323. static inline void esp_apptrace_log_unlock(void)
  324. {
  325. #if ESP_APPTRACE_PRINT_LOCK
  326. esp_apptrace_lock_give(&s_log_lock);
  327. #endif
  328. }
  329. static inline esp_err_t esp_apptrace_lock_initialize(esp_apptrace_lock_t *lock)
  330. {
  331. #if CONFIG_ESP32_APPTRACE_LOCK_ENABLE
  332. esp_apptrace_lock_init(lock);
  333. #endif
  334. return ESP_OK;
  335. }
  336. static inline esp_err_t esp_apptrace_lock_cleanup(void)
  337. {
  338. return ESP_OK;
  339. }
  340. esp_err_t esp_apptrace_lock(esp_apptrace_tmo_t *tmo)
  341. {
  342. #if CONFIG_ESP32_APPTRACE_LOCK_ENABLE
  343. esp_err_t ret = esp_apptrace_lock_take(&s_trace_buf.lock, tmo);
  344. if (ret != ESP_OK) {
  345. return ESP_FAIL;
  346. }
  347. #endif
  348. return ESP_OK;
  349. }
  350. esp_err_t esp_apptrace_unlock(void)
  351. {
  352. esp_err_t ret = ESP_OK;
  353. #if CONFIG_ESP32_APPTRACE_LOCK_ENABLE
  354. ret = esp_apptrace_lock_give(&s_trace_buf.lock);
  355. #endif
  356. return ret;
  357. }
  358. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  359. static void esp_apptrace_trax_init(void)
  360. {
  361. // Stop trace, if any (on the current CPU)
  362. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TRSTP);
  363. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TMEN);
  364. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(ESP_APPTRACE_TRAX_INBLOCK_START));
  365. // this is for OpenOCD to let him know where stub entries vector is resided
  366. // must be read by host before any transfer using TRAX
  367. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0);
  368. ESP_APPTRACE_LOGI("Initialized TRAX on CPU%d", xPortGetCoreID());
  369. }
  370. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  371. // keep the size of buffered data for copying to TRAX mem block.
  372. // Only whole user blocks should be copied from buffer to TRAX block upon the switch
  373. static void esp_apptrace_trax_pend_chunk_sz_update(uint16_t size)
  374. {
  375. ESP_APPTRACE_LOGD("Update chunk enter %d/%d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz, size,
  376. s_trace_buf.trax.rb_pend_chunk_sz.wr, s_trace_buf.trax.rb_pend_chunk_sz.rd, s_trace_buf.trax.rb_pend_chunk_sz.cur_size);
  377. if ((uint32_t)s_trace_buf.trax.cur_pending_chunk_sz + (uint32_t)size <= ESP_APPTRACE_TRAX_BLOCK_SIZE) {
  378. ESP_APPTRACE_LOGD("Update chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  379. s_trace_buf.trax.cur_pending_chunk_sz += size;
  380. } else {
  381. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  382. if (!chunk_sz) {
  383. assert(false && "Failed to alloc pended chunk sz slot!");
  384. } else {
  385. ESP_APPTRACE_LOGD("Update new chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  386. *chunk_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  387. s_trace_buf.trax.cur_pending_chunk_sz = size;
  388. }
  389. }
  390. }
  391. static uint16_t esp_apptrace_trax_pend_chunk_sz_get(void)
  392. {
  393. uint16_t ch_sz;
  394. ESP_APPTRACE_LOGD("Get chunk enter %d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz,
  395. s_trace_buf.trax.rb_pend_chunk_sz.wr, s_trace_buf.trax.rb_pend_chunk_sz.rd, s_trace_buf.trax.rb_pend_chunk_sz.cur_size);
  396. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  397. if (!chunk_sz) {
  398. ch_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  399. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  400. } else {
  401. ch_sz = *chunk_sz;
  402. }
  403. return ch_sz;
  404. }
  405. #endif
  406. // assumed to be protected by caller from multi-core/thread access
  407. static esp_err_t esp_apptrace_trax_block_switch(void)
  408. {
  409. int prev_block_num = s_trace_buf.trax.state.in_block % 2;
  410. int new_block_num = prev_block_num ? (0) : (1);
  411. int res = ESP_OK;
  412. extern uint32_t __esp_apptrace_trax_eri_updated;
  413. // indicate to host that we are about to update.
  414. // this is used only to place CPU into streaming mode at tracing startup
  415. // before starting streaming host can halt us after we read ESP_APPTRACE_TRAX_CTRL_REG and before we updated it
  416. // HACK: in this case host will set breakpoint just after ESP_APPTRACE_TRAX_CTRL_REG update,
  417. // here we set address to set bp at
  418. // enter ERI update critical section
  419. eri_write(ESP_APPTRACE_TRAX_STAT_REG, (uint32_t)&__esp_apptrace_trax_eri_updated);
  420. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  421. uint32_t host_connected = ESP_APPTRACE_TRAX_HOST_CONNECT & ctrl_reg;
  422. if (host_connected) {
  423. uint32_t acked_block = ESP_APPTRACE_TRAX_BLOCK_ID_GET(ctrl_reg);
  424. uint32_t host_to_read = ESP_APPTRACE_TRAX_BLOCK_LEN_GET(ctrl_reg);
  425. if (host_to_read != 0 || acked_block != (s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)) {
  426. ESP_APPTRACE_LOGD("HC[%d]: Can not switch %x %d %x %x/%lx, m %d", xPortGetCoreID(), ctrl_reg, host_to_read, acked_block,
  427. s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK, s_trace_buf.trax.state.in_block,
  428. s_trace_buf.trax.state.markers[prev_block_num]);
  429. res = ESP_ERR_NO_MEM;
  430. goto _on_func_exit;
  431. }
  432. }
  433. s_trace_buf.trax.state.markers[new_block_num] = 0;
  434. // switch to new block
  435. s_trace_buf.trax.state.in_block++;
  436. DPORT_WRITE_PERI_REG(DPORT_TRACEMEM_MUX_MODE_REG, new_block_num ? TRACEMEM_MUX_BLK0_ONLY : TRACEMEM_MUX_BLK1_ONLY);
  437. // handle data from host
  438. esp_hostdata_hdr_t *hdr = (esp_hostdata_hdr_t *)s_trace_buf.trax.blocks[new_block_num].start;
  439. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA && hdr->block_sz > 0) {
  440. // TODO: add support for multiple blocks from host, currently there is no need for that
  441. uint8_t *p = s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.blocks[new_block_num].sz;
  442. ESP_APPTRACE_LOGD("Recvd %d bytes from host [%x %x %x %x %x %x %x %x .. %x %x %x %x %x %x %x %x]", hdr->block_sz,
  443. *(s_trace_buf.trax.blocks[new_block_num].start+0), *(s_trace_buf.trax.blocks[new_block_num].start+1),
  444. *(s_trace_buf.trax.blocks[new_block_num].start+2), *(s_trace_buf.trax.blocks[new_block_num].start+3),
  445. *(s_trace_buf.trax.blocks[new_block_num].start+4), *(s_trace_buf.trax.blocks[new_block_num].start+5),
  446. *(s_trace_buf.trax.blocks[new_block_num].start+6), *(s_trace_buf.trax.blocks[new_block_num].start+7),
  447. *(p-8), *(p-7), *(p-6), *(p-5), *(p-4), *(p-3), *(p-2), *(p-1));
  448. uint32_t sz = esp_apptrace_trax_down_buffer_write_nolock((uint8_t *)(hdr+1), hdr->block_sz);
  449. if (sz != hdr->block_sz) {
  450. ESP_APPTRACE_LOGE("Failed to write %d bytes to down buffer (%d %d)!", hdr->block_sz - sz, hdr->block_sz, sz);
  451. }
  452. hdr->block_sz = 0;
  453. }
  454. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  455. // copy pending data to TRAX block if any
  456. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  457. uint16_t max_chunk_sz = esp_apptrace_trax_pend_chunk_sz_get();
  458. #else
  459. uint16_t max_chunk_sz = s_trace_buf.trax.blocks[new_block_num].sz;
  460. #endif
  461. while (s_trace_buf.trax.state.markers[new_block_num] < max_chunk_sz) {
  462. uint32_t read_sz = esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend);
  463. if (read_sz == 0) {
  464. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  465. /* theres is a bug: esp_apptrace_trax_pend_chunk_sz_get returned wrong value,
  466. it must be greater or equal to one returned by esp_apptrace_rb_read_size_get */
  467. ESP_APPTRACE_LOGE("No pended bytes, must be > 0 and <= %d!", max_chunk_sz);
  468. #endif
  469. break;
  470. }
  471. if (read_sz > max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num]) {
  472. read_sz = max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num];
  473. }
  474. uint8_t *ptr = esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend, read_sz);
  475. if (!ptr) {
  476. assert(false && "Failed to consume pended bytes!!");
  477. break;
  478. }
  479. if (host_connected) {
  480. ESP_APPTRACE_LOGD("Pump %d pend bytes [%x %x %x %x : %x %x %x %x : %x %x %x %x : %x %x...%x %x]",
  481. read_sz, *(ptr+0), *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
  482. *(ptr+5), *(ptr+6), *(ptr+7), *(ptr+8), *(ptr+9), *(ptr+10), *(ptr+11), *(ptr+12), *(ptr+13), *(ptr+read_sz-2), *(ptr+read_sz-1));
  483. }
  484. memcpy(s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.state.markers[new_block_num], ptr, read_sz);
  485. s_trace_buf.trax.state.markers[new_block_num] += read_sz;
  486. }
  487. #endif
  488. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(s_trace_buf.trax.state.in_block) |
  489. host_connected | ESP_APPTRACE_TRAX_BLOCK_LEN(s_trace_buf.trax.state.markers[prev_block_num]));
  490. _on_func_exit:
  491. // exit ERI update critical section
  492. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0x0);
  493. // TODO: currently host sets breakpoint, use break instruction to stop;
  494. // it will allow to use ESP_APPTRACE_TRAX_STAT_REG for other purposes
  495. asm volatile (
  496. " .global __esp_apptrace_trax_eri_updated\n"
  497. "__esp_apptrace_trax_eri_updated:\n"); // host will set bp here to resolve collision at streaming start
  498. return res;
  499. }
  500. static esp_err_t esp_apptrace_trax_block_switch_waitus(esp_apptrace_tmo_t *tmo)
  501. {
  502. int res;
  503. while ((res = esp_apptrace_trax_block_switch()) != ESP_OK) {
  504. res = esp_apptrace_tmo_check(tmo);
  505. if (res != ESP_OK) {
  506. break;
  507. }
  508. }
  509. return res;
  510. }
  511. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo)
  512. {
  513. uint8_t *ptr = NULL;
  514. int res = esp_apptrace_lock(tmo);
  515. if (res != ESP_OK) {
  516. return NULL;
  517. }
  518. while (1) {
  519. uint32_t sz = esp_apptrace_rb_read_size_get(&s_trace_buf.rb_down);
  520. if (sz != 0) {
  521. *size = MIN(*size, sz);
  522. ptr = esp_apptrace_rb_consume(&s_trace_buf.rb_down, *size);
  523. if (!ptr) {
  524. assert(false && "Failed to consume bytes from down buffer!");
  525. }
  526. break;
  527. }
  528. // may need to flush
  529. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  530. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA) {
  531. ESP_APPTRACE_LOGD("force flush");
  532. res = esp_apptrace_trax_block_switch_waitus(tmo);
  533. if (res != ESP_OK) {
  534. ESP_APPTRACE_LOGE("Failed to switch to another block to recv data from host!");
  535. /*do not return error because data can be in down buffer already*/
  536. }
  537. } else {
  538. // check tmo only if there is no data from host
  539. res = esp_apptrace_tmo_check(tmo);
  540. if (res != ESP_OK) {
  541. return NULL;
  542. }
  543. }
  544. }
  545. if (esp_apptrace_unlock() != ESP_OK) {
  546. assert(false && "Failed to unlock apptrace data!");
  547. }
  548. return ptr;
  549. }
  550. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  551. {
  552. /* nothing todo */
  553. return ESP_OK;
  554. }
  555. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size)
  556. {
  557. uint32_t total_sz = 0;
  558. while (total_sz < size) {
  559. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock WRS %d-%d-%d %d", s_trace_buf.rb_down.wr, s_trace_buf.rb_down.rd,
  560. s_trace_buf.rb_down.cur_size, size);
  561. uint32_t wr_sz = esp_apptrace_rb_write_size_get(&s_trace_buf.rb_down);
  562. if (wr_sz == 0) {
  563. break;
  564. }
  565. if (wr_sz > size - total_sz) {
  566. wr_sz = size - total_sz;
  567. }
  568. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d", wr_sz);
  569. uint8_t *ptr = esp_apptrace_rb_produce(&s_trace_buf.rb_down, wr_sz);
  570. if (!ptr) {
  571. assert(false && "Failed to produce bytes to down buffer!");
  572. }
  573. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d to 0x%x from 0x%x", wr_sz, ptr, data + total_sz + wr_sz);
  574. memcpy(ptr, data + total_sz, wr_sz);
  575. total_sz += wr_sz;
  576. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d/%d", wr_sz, total_sz);
  577. }
  578. return total_sz;
  579. }
  580. static inline uint8_t *esp_apptrace_data_header_init(uint8_t *ptr, uint16_t usr_size)
  581. {
  582. // it is safe to use xPortGetCoreID() in macro call because arg is used only once inside it
  583. ((esp_tracedata_hdr_t *)ptr)->block_sz = ESP_APPTRACE_USR_BLOCK_CORE(xPortGetCoreID()) | usr_size;
  584. ((esp_tracedata_hdr_t *)ptr)->wr_sz = 0;
  585. return ptr + sizeof(esp_tracedata_hdr_t);
  586. }
  587. static inline uint8_t *esp_apptrace_trax_wait4buf(uint16_t size, esp_apptrace_tmo_t *tmo, int *pended)
  588. {
  589. uint8_t *ptr = NULL;
  590. int res = esp_apptrace_trax_block_switch_waitus(tmo);
  591. if (res != ESP_OK) {
  592. return NULL;
  593. }
  594. // check if we still have pending data
  595. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  596. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  597. // if after TRAX block switch still have pending data (not all pending data have been pumped to TRAX block)
  598. // alloc new pending buffer
  599. *pended = 1;
  600. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  601. if (!ptr) {
  602. ESP_APPTRACE_LOGE("Failed to alloc pend buf 1: w-r-s %d-%d-%d!", s_trace_buf.trax.rb_pend.wr, s_trace_buf.trax.rb_pend.rd, s_trace_buf.trax.rb_pend.cur_size);
  603. }
  604. } else
  605. #endif
  606. {
  607. // update block pointers
  608. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + size > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  609. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  610. *pended = 1;
  611. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  612. if (ptr == NULL) {
  613. ESP_APPTRACE_LOGE("Failed to alloc pend buf 2: w-r-s %d-%d-%d!", s_trace_buf.trax.rb_pend.wr, s_trace_buf.trax.rb_pend.rd, s_trace_buf.trax.rb_pend.cur_size);
  614. }
  615. #endif
  616. } else {
  617. *pended = 0;
  618. ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  619. }
  620. }
  621. return ptr;
  622. }
  623. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo)
  624. {
  625. uint8_t *buf_ptr = NULL;
  626. if (size > ESP_APPTRACE_USR_DATA_LEN_MAX) {
  627. ESP_APPTRACE_LOGE("Too large user data size %d!", size);
  628. return NULL;
  629. }
  630. int res = esp_apptrace_lock(tmo);
  631. if (res != ESP_OK) {
  632. return NULL;
  633. }
  634. // check for data in the pending buffer
  635. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  636. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  637. // if we have buffered data try to switch TRAX block
  638. esp_apptrace_trax_block_switch();
  639. // if switch was successful, part or all pended data have been copied to TRAX block
  640. }
  641. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  642. // if we have buffered data alloc new pending buffer
  643. ESP_APPTRACE_LOGD("Get %d bytes from PEND buffer", size);
  644. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  645. if (buf_ptr == NULL) {
  646. int pended_buf;
  647. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  648. if (buf_ptr) {
  649. if (pended_buf) {
  650. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  651. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  652. #endif
  653. } else {
  654. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  655. // update cur block marker
  656. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  657. }
  658. }
  659. } else {
  660. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  661. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  662. #endif
  663. }
  664. } else
  665. #endif
  666. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + ESP_APPTRACE_USR_BLOCK_RAW_SZ(size) > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  667. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  668. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from PEND buffer", size);
  669. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  670. if (buf_ptr) {
  671. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  672. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  673. #endif
  674. }
  675. #endif
  676. if (buf_ptr == NULL) {
  677. int pended_buf;
  678. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from pend buffer", size);
  679. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  680. if (buf_ptr) {
  681. if (pended_buf) {
  682. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  683. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  684. #endif
  685. } else {
  686. ESP_APPTRACE_LOGD("Got %d bytes from TRAX buffer", size);
  687. // update cur block marker
  688. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  689. }
  690. }
  691. }
  692. } else {
  693. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  694. // fit to curr TRAX nlock
  695. buf_ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  696. // update cur block marker
  697. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  698. }
  699. if (buf_ptr) {
  700. buf_ptr = esp_apptrace_data_header_init(buf_ptr, size);
  701. }
  702. // now we can safely unlock apptrace to allow other tasks/ISRs to get other buffers and write their data
  703. if (esp_apptrace_unlock() != ESP_OK) {
  704. assert(false && "Failed to unlock apptrace data!");
  705. }
  706. return buf_ptr;
  707. }
  708. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  709. {
  710. int res = ESP_OK;
  711. esp_tracedata_hdr_t *hdr = (esp_tracedata_hdr_t *)(ptr - sizeof(esp_tracedata_hdr_t));
  712. // update written size
  713. hdr->wr_sz = hdr->block_sz;
  714. // TODO: mark block as busy in order not to re-use it for other tracing calls until it is completely written
  715. // TODO: avoid potential situation when all memory is consumed by low prio tasks which can not complete writing due to
  716. // higher prio tasks and the latter can not allocate buffers at all
  717. // this is abnormal situation can be detected on host which will receive only uncompleted buffers
  718. // workaround: use own memcpy which will kick-off dead tracing calls
  719. return res;
  720. }
  721. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo)
  722. {
  723. int res = ESP_OK;
  724. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() < min_sz) {
  725. ESP_APPTRACE_LOGI("Ignore flush request for min %d bytes. Bytes in TRAX block: %d.", min_sz, ESP_APPTRACE_TRAX_INBLOCK_MARKER());
  726. return ESP_OK;
  727. }
  728. // switch TRAX block while size of data is more than min size
  729. while (ESP_APPTRACE_TRAX_INBLOCK_MARKER() > 0) {
  730. ESP_APPTRACE_LOGD("Try to flush %d bytes. Wait until block switch for %u us", ESP_APPTRACE_TRAX_INBLOCK_MARKER(), tmo->tmo);
  731. res = esp_apptrace_trax_block_switch_waitus(tmo);
  732. if (res != ESP_OK) {
  733. ESP_APPTRACE_LOGE("Failed to switch to another block!");
  734. return res;
  735. }
  736. }
  737. return res;
  738. }
  739. static bool esp_apptrace_trax_host_is_connected(void)
  740. {
  741. return eri_read(ESP_APPTRACE_TRAX_CTRL_REG) & ESP_APPTRACE_TRAX_HOST_CONNECT ? true : false;
  742. }
  743. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val)
  744. {
  745. eri_write(ESP_APPTRACE_TRAX_STAT_REG, val);
  746. return ESP_OK;
  747. }
  748. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val)
  749. {
  750. *val = eri_read(ESP_APPTRACE_TRAX_STAT_REG);
  751. return ESP_OK;
  752. }
  753. static esp_err_t esp_apptrace_trax_dest_init(void)
  754. {
  755. for (int i = 0; i < ESP_APPTRACE_TRAX_BLOCKS_NUM; i++) {
  756. s_trace_buf.trax.blocks[i].start = (uint8_t *)s_trax_blocks[i];
  757. s_trace_buf.trax.blocks[i].sz = ESP_APPTRACE_TRAX_BLOCK_SIZE;
  758. s_trace_buf.trax.state.markers[i] = 0;
  759. }
  760. s_trace_buf.trax.state.in_block = ESP_APPTRACE_TRAX_INBLOCK_START;
  761. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  762. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend, s_trace_buf.trax.pending_data,
  763. sizeof(s_trace_buf.trax.pending_data));
  764. #if CONFIG_ESP32_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  765. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  766. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend_chunk_sz, (uint8_t *)s_trace_buf.trax.pending_chunk_sz,
  767. sizeof(s_trace_buf.trax.pending_chunk_sz));
  768. #endif
  769. #endif
  770. DPORT_WRITE_PERI_REG(DPORT_PRO_TRACEMEM_ENA_REG, DPORT_PRO_TRACEMEM_ENA_M);
  771. #if CONFIG_FREERTOS_UNICORE == 0
  772. DPORT_WRITE_PERI_REG(DPORT_APP_TRACEMEM_ENA_REG, DPORT_APP_TRACEMEM_ENA_M);
  773. #endif
  774. // Expose block 1 to host, block 0 is current trace input buffer
  775. DPORT_WRITE_PERI_REG(DPORT_TRACEMEM_MUX_MODE_REG, TRACEMEM_MUX_BLK1_ONLY);
  776. return ESP_OK;
  777. }
  778. #endif
  779. esp_err_t esp_apptrace_init(void)
  780. {
  781. int res;
  782. if (!s_trace_buf.inited) {
  783. memset(&s_trace_buf, 0, sizeof(s_trace_buf));
  784. // disabled by default
  785. esp_apptrace_rb_init(&s_trace_buf.rb_down, NULL, 0);
  786. res = esp_apptrace_lock_initialize(&s_trace_buf.lock);
  787. if (res != ESP_OK) {
  788. ESP_APPTRACE_LOGE("Failed to init log lock (%d)!", res);
  789. return res;
  790. }
  791. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  792. res = esp_apptrace_trax_dest_init();
  793. if (res != ESP_OK) {
  794. ESP_APPTRACE_LOGE("Failed to init TRAX dest data (%d)!", res);
  795. esp_apptrace_lock_cleanup();
  796. return res;
  797. }
  798. #endif
  799. }
  800. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  801. // init TRAX on this CPU
  802. esp_apptrace_trax_init();
  803. #endif
  804. s_trace_buf.inited |= 1 << xPortGetCoreID(); // global and this CPU-specific data are inited
  805. return ESP_OK;
  806. }
  807. void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)
  808. {
  809. esp_apptrace_rb_init(&s_trace_buf.rb_down, buf, size);
  810. }
  811. esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo)
  812. {
  813. int res = ESP_OK;
  814. esp_apptrace_tmo_t tmo;
  815. esp_apptrace_hw_t *hw = NULL;
  816. if (dest == ESP_APPTRACE_DEST_TRAX) {
  817. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  818. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  819. #else
  820. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  821. return ESP_ERR_NOT_SUPPORTED;
  822. #endif
  823. } else {
  824. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  825. return ESP_ERR_NOT_SUPPORTED;
  826. }
  827. if (buf == NULL || size == NULL || *size == 0) {
  828. return ESP_ERR_INVALID_ARG;
  829. }
  830. //TODO: callback system
  831. esp_apptrace_tmo_init(&tmo, user_tmo);
  832. uint32_t act_sz = *size;
  833. *size = 0;
  834. uint8_t * ptr = hw->get_down_buffer(&act_sz, &tmo);
  835. if (ptr && act_sz > 0) {
  836. ESP_APPTRACE_LOGD("Read %d bytes from host", act_sz);
  837. memcpy(buf, ptr, act_sz);
  838. res = hw->put_down_buffer(ptr, &tmo);
  839. *size = act_sz;
  840. } else {
  841. res = ESP_ERR_TIMEOUT;
  842. }
  843. return res;
  844. }
  845. uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo)
  846. {
  847. esp_apptrace_tmo_t tmo;
  848. esp_apptrace_hw_t *hw = NULL;
  849. if (dest == ESP_APPTRACE_DEST_TRAX) {
  850. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  851. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  852. #else
  853. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  854. return NULL;
  855. #endif
  856. } else {
  857. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  858. return NULL;
  859. }
  860. if (size == NULL || *size == 0) {
  861. return NULL;
  862. }
  863. esp_apptrace_tmo_init(&tmo, user_tmo);
  864. return hw->get_down_buffer(size, &tmo);
  865. }
  866. esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  867. {
  868. esp_apptrace_tmo_t tmo;
  869. esp_apptrace_hw_t *hw = NULL;
  870. if (dest == ESP_APPTRACE_DEST_TRAX) {
  871. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  872. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  873. #else
  874. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  875. return ESP_ERR_NOT_SUPPORTED;
  876. #endif
  877. } else {
  878. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  879. return ESP_ERR_NOT_SUPPORTED;
  880. }
  881. if (ptr == NULL) {
  882. return ESP_ERR_INVALID_ARG;
  883. }
  884. esp_apptrace_tmo_init(&tmo, user_tmo);
  885. return hw->put_down_buffer(ptr, &tmo);
  886. }
  887. esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo)
  888. {
  889. uint8_t *ptr = NULL;
  890. esp_apptrace_tmo_t tmo;
  891. esp_apptrace_hw_t *hw = NULL;
  892. if (dest == ESP_APPTRACE_DEST_TRAX) {
  893. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  894. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  895. #else
  896. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  897. return ESP_ERR_NOT_SUPPORTED;
  898. #endif
  899. } else {
  900. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  901. return ESP_ERR_NOT_SUPPORTED;
  902. }
  903. if (data == NULL || size == 0) {
  904. return ESP_ERR_INVALID_ARG;
  905. }
  906. esp_apptrace_tmo_init(&tmo, user_tmo);
  907. ptr = hw->get_up_buffer(size, &tmo);
  908. if (ptr == NULL) {
  909. return ESP_ERR_NO_MEM;
  910. }
  911. // actually can be suspended here by higher prio tasks/ISRs
  912. //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check
  913. memcpy(ptr, data, size);
  914. // now indicate that this buffer is ready to be sent off to host
  915. return hw->put_up_buffer(ptr, &tmo);
  916. }
  917. int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap)
  918. {
  919. uint16_t nargs = 0;
  920. uint8_t *pout, *p = (uint8_t *)fmt;
  921. esp_apptrace_tmo_t tmo;
  922. esp_apptrace_hw_t *hw = NULL;
  923. if (dest == ESP_APPTRACE_DEST_TRAX) {
  924. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  925. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  926. #else
  927. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  928. return ESP_ERR_NOT_SUPPORTED;
  929. #endif
  930. } else {
  931. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  932. return ESP_ERR_NOT_SUPPORTED;
  933. }
  934. if (fmt == NULL) {
  935. return ESP_ERR_INVALID_ARG;
  936. }
  937. esp_apptrace_tmo_init(&tmo, user_tmo);
  938. ESP_APPTRACE_LOGD("fmt %x", fmt);
  939. while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) {
  940. p++;
  941. if (*p != '%' && *p != 0) {
  942. nargs++;
  943. }
  944. }
  945. ESP_APPTRACE_LOGD("nargs = %d", nargs);
  946. if (p) {
  947. ESP_APPTRACE_LOGE("Failed to store all printf args!");
  948. }
  949. pout = hw->get_up_buffer(1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo);
  950. if (pout == NULL) {
  951. ESP_APPTRACE_LOGE("Failed to get buffer!");
  952. return -1;
  953. }
  954. p = pout;
  955. *pout = nargs;
  956. pout++;
  957. *(const char **)pout = fmt;
  958. pout += sizeof(char *);
  959. while (nargs-- > 0) {
  960. uint32_t arg = va_arg(ap, uint32_t);
  961. *(uint32_t *)pout = arg;
  962. pout += sizeof(uint32_t);
  963. ESP_APPTRACE_LOGD("arg %x", arg);
  964. }
  965. int ret = hw->put_up_buffer(p, &tmo);
  966. if (ret != ESP_OK) {
  967. ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret);
  968. return -1;
  969. }
  970. return (pout - p);
  971. }
  972. int esp_apptrace_vprintf(const char *fmt, va_list ap)
  973. {
  974. return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_TRAX, /*ESP_APPTRACE_TMO_INFINITE*/0, fmt, ap);
  975. }
  976. uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo)
  977. {
  978. esp_apptrace_tmo_t tmo;
  979. esp_apptrace_hw_t *hw = NULL;
  980. if (dest == ESP_APPTRACE_DEST_TRAX) {
  981. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  982. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  983. #else
  984. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  985. return NULL;
  986. #endif
  987. } else {
  988. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  989. return NULL;
  990. }
  991. if (size == 0) {
  992. return NULL;
  993. }
  994. esp_apptrace_tmo_init(&tmo, user_tmo);
  995. return hw->get_up_buffer(size, &tmo);
  996. }
  997. esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  998. {
  999. esp_apptrace_tmo_t tmo;
  1000. esp_apptrace_hw_t *hw = NULL;
  1001. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1002. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  1003. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1004. #else
  1005. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1006. return ESP_ERR_NOT_SUPPORTED;
  1007. #endif
  1008. } else {
  1009. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1010. return ESP_ERR_NOT_SUPPORTED;
  1011. }
  1012. if (ptr == NULL) {
  1013. return ESP_ERR_INVALID_ARG;
  1014. }
  1015. esp_apptrace_tmo_init(&tmo, user_tmo);
  1016. return hw->put_up_buffer(ptr, &tmo);
  1017. }
  1018. esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo)
  1019. {
  1020. esp_apptrace_tmo_t tmo;
  1021. esp_apptrace_hw_t *hw = NULL;
  1022. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1023. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  1024. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1025. #else
  1026. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1027. return ESP_ERR_NOT_SUPPORTED;
  1028. #endif
  1029. } else {
  1030. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1031. return ESP_ERR_NOT_SUPPORTED;
  1032. }
  1033. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1034. return hw->flush_up_buffer(min_sz, &tmo);
  1035. }
  1036. esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo)
  1037. {
  1038. int res;
  1039. esp_apptrace_tmo_t tmo;
  1040. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1041. res = esp_apptrace_lock(&tmo);
  1042. if (res != ESP_OK) {
  1043. ESP_APPTRACE_LOGE("Failed to lock apptrace data (%d)!", res);
  1044. return res;
  1045. }
  1046. res = esp_apptrace_flush_nolock(dest, 0, esp_apptrace_tmo_remaining_us(&tmo));
  1047. if (res != ESP_OK) {
  1048. ESP_APPTRACE_LOGE("Failed to flush apptrace data (%d)!", res);
  1049. }
  1050. if (esp_apptrace_unlock() != ESP_OK) {
  1051. assert(false && "Failed to unlock apptrace data!");
  1052. }
  1053. return res;
  1054. }
  1055. bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)
  1056. {
  1057. esp_apptrace_hw_t *hw = NULL;
  1058. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1059. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  1060. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1061. #else
  1062. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1063. return false;
  1064. #endif
  1065. } else {
  1066. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1067. return false;
  1068. }
  1069. return hw->host_is_connected();
  1070. }
  1071. esp_err_t esp_apptrace_status_reg_set(esp_apptrace_dest_t dest, uint32_t val)
  1072. {
  1073. esp_apptrace_hw_t *hw = NULL;
  1074. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1075. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  1076. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1077. #else
  1078. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1079. return ESP_ERR_NOT_SUPPORTED;
  1080. #endif
  1081. } else {
  1082. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1083. return ESP_ERR_NOT_SUPPORTED;
  1084. }
  1085. return hw->status_reg_set(val);
  1086. }
  1087. esp_err_t esp_apptrace_status_reg_get(esp_apptrace_dest_t dest, uint32_t *val)
  1088. {
  1089. esp_apptrace_hw_t *hw = NULL;
  1090. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1091. #if CONFIG_ESP32_APPTRACE_DEST_TRAX
  1092. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1093. #else
  1094. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1095. return ESP_ERR_NOT_SUPPORTED;
  1096. #endif
  1097. } else {
  1098. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1099. return ESP_ERR_NOT_SUPPORTED;
  1100. }
  1101. return hw->status_reg_get(val);
  1102. }
  1103. #endif