app_trace.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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_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_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_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 "sdkconfig.h"
  137. #include "soc/soc.h"
  138. #include "soc/dport_access.h"
  139. #if CONFIG_IDF_TARGET_ESP32
  140. #include "soc/dport_reg.h"
  141. #elif CONFIG_IDF_TARGET_ESP32S2
  142. #include "soc/sensitive_reg.h"
  143. #endif
  144. #if __XTENSA__
  145. #include "eri.h"
  146. #include "trax.h"
  147. #endif
  148. #include "soc/timer_periph.h"
  149. #include "freertos/FreeRTOS.h"
  150. #include "esp_app_trace.h"
  151. #include "esp_rom_sys.h"
  152. #if CONFIG_APPTRACE_ENABLE
  153. #define ESP_APPTRACE_MAX_VPRINTF_ARGS 256
  154. #define ESP_APPTRACE_HOST_BUF_SIZE 256
  155. #define ESP_APPTRACE_PRINT_LOCK 0
  156. #include "esp_log.h"
  157. const static char *TAG = "esp_apptrace";
  158. #if ESP_APPTRACE_PRINT_LOCK
  159. #define ESP_APPTRACE_LOG( format, ... ) \
  160. do { \
  161. esp_apptrace_log_lock(); \
  162. esp_rom_printf(format, ##__VA_ARGS__); \
  163. esp_apptrace_log_unlock(); \
  164. } while(0)
  165. #else
  166. #define ESP_APPTRACE_LOG( format, ... ) \
  167. do { \
  168. esp_rom_printf(format, ##__VA_ARGS__); \
  169. } while(0)
  170. #endif
  171. #define ESP_APPTRACE_LOG_LEV( _L_, level, format, ... ) \
  172. do { \
  173. if (LOG_LOCAL_LEVEL >= level) { \
  174. ESP_APPTRACE_LOG(LOG_FORMAT(_L_, format), esp_log_early_timestamp(), TAG, ##__VA_ARGS__); \
  175. } \
  176. } while(0)
  177. #define ESP_APPTRACE_LOGE( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_ERROR, format, ##__VA_ARGS__)
  178. #define ESP_APPTRACE_LOGW( format, ... ) ESP_APPTRACE_LOG_LEV(W, ESP_LOG_WARN, format, ##__VA_ARGS__)
  179. #define ESP_APPTRACE_LOGI( format, ... ) ESP_APPTRACE_LOG_LEV(I, ESP_LOG_INFO, format, ##__VA_ARGS__)
  180. #define ESP_APPTRACE_LOGD( format, ... ) ESP_APPTRACE_LOG_LEV(D, ESP_LOG_DEBUG, format, ##__VA_ARGS__)
  181. #define ESP_APPTRACE_LOGV( format, ... ) ESP_APPTRACE_LOG_LEV(V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)
  182. #define ESP_APPTRACE_LOGO( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_NONE, format, ##__VA_ARGS__)
  183. // TODO: move these (and same definitions in trax.c to dport_reg.h)
  184. #if CONFIG_IDF_TARGET_ESP32
  185. #define TRACEMEM_MUX_PROBLK0_APPBLK1 0
  186. #define TRACEMEM_MUX_BLK0_ONLY 1
  187. #define TRACEMEM_MUX_BLK1_ONLY 2
  188. #define TRACEMEM_MUX_PROBLK1_APPBLK0 3
  189. #elif CONFIG_IDF_TARGET_ESP32S2
  190. #define TRACEMEM_MUX_BLK0_NUM 19
  191. #define TRACEMEM_MUX_BLK1_NUM 20
  192. #define TRACEMEM_BLK_NUM2ADDR(_n_) (0x3FFB8000UL + 0x4000UL*((_n_)-4))
  193. #endif
  194. // TRAX is disabled, so we use its registers for our own purposes
  195. // | 31..XXXXXX..24 | 23 .(host_connect). 23 | 22 .(host_data). 22| 21..(block_id)..15 | 14..(block_len)..0 |
  196. #define ESP_APPTRACE_TRAX_CTRL_REG ERI_TRAX_DELAYCNT
  197. #define ESP_APPTRACE_TRAX_STAT_REG ERI_TRAX_TRIGGERPC
  198. #define ESP_APPTRACE_TRAX_BLOCK_LEN_MSK 0x7FFFUL
  199. #define ESP_APPTRACE_TRAX_BLOCK_LEN(_l_) ((_l_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  200. #define ESP_APPTRACE_TRAX_BLOCK_LEN_GET(_v_) ((_v_) & ESP_APPTRACE_TRAX_BLOCK_LEN_MSK)
  201. #define ESP_APPTRACE_TRAX_BLOCK_ID_MSK 0x7FUL
  202. #define ESP_APPTRACE_TRAX_BLOCK_ID(_id_) (((_id_) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK) << 15)
  203. #define ESP_APPTRACE_TRAX_BLOCK_ID_GET(_v_) (((_v_) >> 15) & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)
  204. #define ESP_APPTRACE_TRAX_HOST_DATA (1 << 22)
  205. #define ESP_APPTRACE_TRAX_HOST_CONNECT (1 << 23)
  206. #if CONFIG_SYSVIEW_ENABLE
  207. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) (0)
  208. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (_v_)
  209. #else
  210. #define ESP_APPTRACE_USR_BLOCK_CORE(_cid_) ((_cid_) << 15)
  211. #define ESP_APPTRACE_USR_BLOCK_LEN(_v_) (~(1 << 15) & (_v_))
  212. #endif
  213. #define ESP_APPTRACE_USR_BLOCK_RAW_SZ(_s_) ((_s_) + sizeof(esp_tracedata_hdr_t))
  214. #if CONFIG_IDF_TARGET_ESP32
  215. static volatile uint8_t *s_trax_blocks[] = {
  216. (volatile uint8_t *) 0x3FFFC000,
  217. (volatile uint8_t *) 0x3FFF8000
  218. };
  219. #elif CONFIG_IDF_TARGET_ESP32S2
  220. static volatile uint8_t *s_trax_blocks[] = {
  221. (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK0_NUM),
  222. (volatile uint8_t *)TRACEMEM_BLK_NUM2ADDR(TRACEMEM_MUX_BLK1_NUM)
  223. };
  224. #endif
  225. #define ESP_APPTRACE_TRAX_BLOCKS_NUM (sizeof(s_trax_blocks)/sizeof(s_trax_blocks[0]))
  226. #define ESP_APPTRACE_TRAX_INBLOCK_START 0
  227. #define ESP_APPTRACE_TRAX_INBLOCK_MARKER() (s_trace_buf.trax.state.markers[s_trace_buf.trax.state.in_block % 2])
  228. #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)
  229. #define ESP_APPTRACE_TRAX_INBLOCK_GET() (&s_trace_buf.trax.blocks[s_trace_buf.trax.state.in_block % 2])
  230. #define ESP_APPTRACE_TRAX_BLOCK_SIZE (0x4000UL)
  231. #if CONFIG_SYSVIEW_ENABLE
  232. #define ESP_APPTRACE_USR_DATA_LEN_MAX 255UL
  233. #else
  234. #define ESP_APPTRACE_USR_DATA_LEN_MAX (ESP_APPTRACE_TRAX_BLOCK_SIZE - sizeof(esp_tracedata_hdr_t))
  235. #endif
  236. #define ESP_APPTRACE_HW_TRAX 0
  237. #define ESP_APPTRACE_HW_MAX 1
  238. #define ESP_APPTRACE_HW(_i_) (&s_trace_hw[_i_])
  239. /** Trace data header. Every user data chunk is prepended with this header.
  240. * User allocates block with esp_apptrace_buffer_get and then fills it with data,
  241. * in multithreading environment it can happen that tasks gets buffer and then gets interrupted,
  242. * so it is possible that user data are incomplete when TRAX memory block is exposed to the host.
  243. * In this case host SW will see that wr_sz < block_sz and will report error.
  244. */
  245. typedef struct {
  246. #if CONFIG_SYSVIEW_ENABLE
  247. uint8_t block_sz; // size of allocated block for user data
  248. uint8_t wr_sz; // size of actually written data
  249. #else
  250. uint16_t block_sz; // size of allocated block for user data
  251. uint16_t wr_sz; // size of actually written data
  252. #endif
  253. } esp_tracedata_hdr_t;
  254. /** TODO: docs
  255. */
  256. typedef struct {
  257. uint16_t block_sz; // size of allocated block for user data
  258. } esp_hostdata_hdr_t;
  259. /** TRAX HW transport state */
  260. typedef struct {
  261. uint32_t in_block; // input block ID
  262. // TODO: change to uint16_t
  263. uint32_t markers[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // block filling level markers
  264. } esp_apptrace_trax_state_t;
  265. /** memory block parameters */
  266. typedef struct {
  267. uint8_t *start; // start address
  268. uint16_t sz; // size
  269. } esp_apptrace_mem_block_t;
  270. /** TRAX HW transport data */
  271. typedef struct {
  272. volatile esp_apptrace_trax_state_t state; // state
  273. esp_apptrace_mem_block_t blocks[ESP_APPTRACE_TRAX_BLOCKS_NUM]; // memory blocks
  274. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  275. // ring buffer control struct for pending user blocks
  276. esp_apptrace_rb_t rb_pend;
  277. // storage for pending user blocks
  278. uint8_t pending_data[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX + 1];
  279. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  280. // ring buffer control struct for pending user data chunks sizes,
  281. // every chunk contains whole number of user blocks and fit into TRAX memory block
  282. esp_apptrace_rb_t rb_pend_chunk_sz;
  283. // storage for above ring buffer data
  284. uint16_t pending_chunk_sz[CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX/ESP_APPTRACE_TRAX_BLOCK_SIZE + 2];
  285. // current (accumulated) pending user data chunk size
  286. uint16_t cur_pending_chunk_sz;
  287. #endif
  288. #endif
  289. } esp_apptrace_trax_data_t;
  290. /** tracing module internal data */
  291. typedef struct {
  292. esp_apptrace_lock_t lock; // sync lock
  293. uint8_t inited; // module initialization state flag
  294. // ring buffer control struct for data from host (down buffer)
  295. esp_apptrace_rb_t rb_down;
  296. // storage for above ring buffer data
  297. esp_apptrace_trax_data_t trax; // TRAX HW transport data
  298. } esp_apptrace_buffer_t;
  299. static esp_apptrace_buffer_t s_trace_buf;
  300. #if ESP_APPTRACE_PRINT_LOCK
  301. static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
  302. #endif
  303. typedef struct {
  304. uint8_t *(*get_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  305. esp_err_t (*put_up_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  306. esp_err_t (*flush_up_buffer)(uint32_t, esp_apptrace_tmo_t *);
  307. uint8_t *(*get_down_buffer)(uint32_t *, esp_apptrace_tmo_t *);
  308. esp_err_t (*put_down_buffer)(uint8_t *, esp_apptrace_tmo_t *);
  309. bool (*host_is_connected)(void);
  310. esp_err_t (*status_reg_set)(uint32_t val);
  311. esp_err_t (*status_reg_get)(uint32_t *val);
  312. } esp_apptrace_hw_t;
  313. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size);
  314. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo);
  315. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo);
  316. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  317. static bool esp_apptrace_trax_host_is_connected(void);
  318. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo);
  319. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo);
  320. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val);
  321. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val);
  322. static esp_apptrace_hw_t s_trace_hw[ESP_APPTRACE_HW_MAX] = {
  323. {
  324. .get_up_buffer = esp_apptrace_trax_get_buffer,
  325. .put_up_buffer = esp_apptrace_trax_put_buffer,
  326. .flush_up_buffer = esp_apptrace_trax_flush,
  327. .get_down_buffer = esp_apptrace_trax_down_buffer_get,
  328. .put_down_buffer = esp_apptrace_trax_down_buffer_put,
  329. .host_is_connected = esp_apptrace_trax_host_is_connected,
  330. .status_reg_set = esp_apptrace_trax_status_reg_set,
  331. .status_reg_get = esp_apptrace_trax_status_reg_get
  332. }
  333. };
  334. static inline int esp_apptrace_log_lock(void)
  335. {
  336. #if ESP_APPTRACE_PRINT_LOCK
  337. esp_apptrace_tmo_t tmo;
  338. esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
  339. int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
  340. return ret;
  341. #else
  342. return 0;
  343. #endif
  344. }
  345. static inline void esp_apptrace_log_unlock(void)
  346. {
  347. #if ESP_APPTRACE_PRINT_LOCK
  348. esp_apptrace_lock_give(&s_log_lock);
  349. #endif
  350. }
  351. static inline esp_err_t esp_apptrace_lock_initialize(esp_apptrace_lock_t *lock)
  352. {
  353. #if CONFIG_APPTRACE_LOCK_ENABLE
  354. esp_apptrace_lock_init(lock);
  355. #endif
  356. return ESP_OK;
  357. }
  358. static inline esp_err_t esp_apptrace_lock_cleanup(void)
  359. {
  360. return ESP_OK;
  361. }
  362. esp_err_t esp_apptrace_lock(esp_apptrace_tmo_t *tmo)
  363. {
  364. #if CONFIG_APPTRACE_LOCK_ENABLE
  365. esp_err_t ret = esp_apptrace_lock_take(&s_trace_buf.lock, tmo);
  366. if (ret != ESP_OK) {
  367. return ESP_FAIL;
  368. }
  369. #endif
  370. return ESP_OK;
  371. }
  372. esp_err_t esp_apptrace_unlock(void)
  373. {
  374. esp_err_t ret = ESP_OK;
  375. #if CONFIG_APPTRACE_LOCK_ENABLE
  376. ret = esp_apptrace_lock_give(&s_trace_buf.lock);
  377. #endif
  378. return ret;
  379. }
  380. #if CONFIG_APPTRACE_DEST_TRAX
  381. static inline void esp_apptrace_trax_select_memory_block(int block_num)
  382. {
  383. // select memory block to be exposed to the TRAX module (accessed by host)
  384. #if CONFIG_IDF_TARGET_ESP32
  385. DPORT_WRITE_PERI_REG(DPORT_TRACEMEM_MUX_MODE_REG, block_num ? TRACEMEM_MUX_BLK0_ONLY : TRACEMEM_MUX_BLK1_ONLY);
  386. #elif CONFIG_IDF_TARGET_ESP32S2
  387. DPORT_WRITE_PERI_REG(DPORT_PMS_OCCUPY_3_REG, block_num ? BIT(TRACEMEM_MUX_BLK0_NUM-4) : BIT(TRACEMEM_MUX_BLK1_NUM-4));
  388. #endif
  389. }
  390. static void esp_apptrace_trax_init(void)
  391. {
  392. // Stop trace, if any (on the current CPU)
  393. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TRSTP);
  394. eri_write(ERI_TRAX_TRAXCTRL, TRAXCTRL_TMEN);
  395. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(ESP_APPTRACE_TRAX_INBLOCK_START));
  396. // this is for OpenOCD to let him know where stub entries vector is resided
  397. // must be read by host before any transfer using TRAX
  398. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0);
  399. ESP_APPTRACE_LOGI("Initialized TRAX on CPU%d", xPortGetCoreID());
  400. }
  401. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  402. // keep the size of buffered data for copying to TRAX mem block.
  403. // Only whole user blocks should be copied from buffer to TRAX block upon the switch
  404. static void esp_apptrace_trax_pend_chunk_sz_update(uint16_t size)
  405. {
  406. ESP_APPTRACE_LOGD("Update chunk enter %d/%d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz, size,
  407. 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);
  408. if ((uint32_t)s_trace_buf.trax.cur_pending_chunk_sz + (uint32_t)size <= ESP_APPTRACE_TRAX_BLOCK_SIZE) {
  409. ESP_APPTRACE_LOGD("Update chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  410. s_trace_buf.trax.cur_pending_chunk_sz += size;
  411. } else {
  412. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  413. if (!chunk_sz) {
  414. assert(false && "Failed to alloc pended chunk sz slot!");
  415. } else {
  416. ESP_APPTRACE_LOGD("Update new chunk %d/%d", s_trace_buf.trax.cur_pending_chunk_sz, size);
  417. *chunk_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  418. s_trace_buf.trax.cur_pending_chunk_sz = size;
  419. }
  420. }
  421. }
  422. static uint16_t esp_apptrace_trax_pend_chunk_sz_get(void)
  423. {
  424. uint16_t ch_sz;
  425. ESP_APPTRACE_LOGD("Get chunk enter %d w-r-s %d-%d-%d", s_trace_buf.trax.cur_pending_chunk_sz,
  426. 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);
  427. uint16_t *chunk_sz = (uint16_t *)esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend_chunk_sz, sizeof(uint16_t));
  428. if (!chunk_sz) {
  429. ch_sz = s_trace_buf.trax.cur_pending_chunk_sz;
  430. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  431. } else {
  432. ch_sz = *chunk_sz;
  433. }
  434. return ch_sz;
  435. }
  436. #endif
  437. // assumed to be protected by caller from multi-core/thread access
  438. static __attribute__((noinline)) esp_err_t esp_apptrace_trax_block_switch(void)
  439. {
  440. int prev_block_num = s_trace_buf.trax.state.in_block % 2;
  441. int new_block_num = prev_block_num ? (0) : (1);
  442. int res = ESP_OK;
  443. extern uint32_t __esp_apptrace_trax_eri_updated;
  444. // indicate to host that we are about to update.
  445. // this is used only to place CPU into streaming mode at tracing startup
  446. // before starting streaming host can halt us after we read ESP_APPTRACE_TRAX_CTRL_REG and before we updated it
  447. // HACK: in this case host will set breakpoint just after ESP_APPTRACE_TRAX_CTRL_REG update,
  448. // here we set address to set bp at
  449. // enter ERI update critical section
  450. eri_write(ESP_APPTRACE_TRAX_STAT_REG, (uint32_t)&__esp_apptrace_trax_eri_updated);
  451. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  452. uint32_t host_connected = ESP_APPTRACE_TRAX_HOST_CONNECT & ctrl_reg;
  453. if (host_connected) {
  454. uint32_t acked_block = ESP_APPTRACE_TRAX_BLOCK_ID_GET(ctrl_reg);
  455. uint32_t host_to_read = ESP_APPTRACE_TRAX_BLOCK_LEN_GET(ctrl_reg);
  456. if (host_to_read != 0 || acked_block != (s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK)) {
  457. ESP_APPTRACE_LOGD("HC[%d]: Can not switch %x %d %x %x/%lx, m %d", xPortGetCoreID(), ctrl_reg, host_to_read, acked_block,
  458. s_trace_buf.trax.state.in_block & ESP_APPTRACE_TRAX_BLOCK_ID_MSK, s_trace_buf.trax.state.in_block,
  459. s_trace_buf.trax.state.markers[prev_block_num]);
  460. res = ESP_ERR_NO_MEM;
  461. goto _on_func_exit;
  462. }
  463. }
  464. s_trace_buf.trax.state.markers[new_block_num] = 0;
  465. // switch to new block
  466. s_trace_buf.trax.state.in_block++;
  467. esp_apptrace_trax_select_memory_block(new_block_num);
  468. // handle data from host
  469. esp_hostdata_hdr_t *hdr = (esp_hostdata_hdr_t *)s_trace_buf.trax.blocks[new_block_num].start;
  470. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA && hdr->block_sz > 0) {
  471. // TODO: add support for multiple blocks from host, currently there is no need for that
  472. uint8_t *p = s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.blocks[new_block_num].sz;
  473. 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,
  474. *(s_trace_buf.trax.blocks[new_block_num].start+0), *(s_trace_buf.trax.blocks[new_block_num].start+1),
  475. *(s_trace_buf.trax.blocks[new_block_num].start+2), *(s_trace_buf.trax.blocks[new_block_num].start+3),
  476. *(s_trace_buf.trax.blocks[new_block_num].start+4), *(s_trace_buf.trax.blocks[new_block_num].start+5),
  477. *(s_trace_buf.trax.blocks[new_block_num].start+6), *(s_trace_buf.trax.blocks[new_block_num].start+7),
  478. *(p-8), *(p-7), *(p-6), *(p-5), *(p-4), *(p-3), *(p-2), *(p-1));
  479. uint32_t sz = esp_apptrace_trax_down_buffer_write_nolock((uint8_t *)(hdr+1), hdr->block_sz);
  480. if (sz != hdr->block_sz) {
  481. ESP_APPTRACE_LOGE("Failed to write %d bytes to down buffer (%d %d)!", hdr->block_sz - sz, hdr->block_sz, sz);
  482. }
  483. hdr->block_sz = 0;
  484. }
  485. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  486. // copy pending data to TRAX block if any
  487. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  488. uint16_t max_chunk_sz = esp_apptrace_trax_pend_chunk_sz_get();
  489. #else
  490. uint16_t max_chunk_sz = s_trace_buf.trax.blocks[new_block_num].sz;
  491. #endif
  492. while (s_trace_buf.trax.state.markers[new_block_num] < max_chunk_sz) {
  493. uint32_t read_sz = esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend);
  494. if (read_sz == 0) {
  495. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  496. /* theres is a bug: esp_apptrace_trax_pend_chunk_sz_get returned wrong value,
  497. it must be greater or equal to one returned by esp_apptrace_rb_read_size_get */
  498. ESP_APPTRACE_LOGE("No pended bytes, must be > 0 and <= %d!", max_chunk_sz);
  499. #endif
  500. break;
  501. }
  502. if (read_sz > max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num]) {
  503. read_sz = max_chunk_sz - s_trace_buf.trax.state.markers[new_block_num];
  504. }
  505. uint8_t *ptr = esp_apptrace_rb_consume(&s_trace_buf.trax.rb_pend, read_sz);
  506. if (!ptr) {
  507. assert(false && "Failed to consume pended bytes!!");
  508. break;
  509. }
  510. if (host_connected) {
  511. ESP_APPTRACE_LOGD("Pump %d pend bytes [%x %x %x %x : %x %x %x %x : %x %x %x %x : %x %x...%x %x]",
  512. read_sz, *(ptr+0), *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
  513. *(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));
  514. }
  515. memcpy(s_trace_buf.trax.blocks[new_block_num].start + s_trace_buf.trax.state.markers[new_block_num], ptr, read_sz);
  516. s_trace_buf.trax.state.markers[new_block_num] += read_sz;
  517. }
  518. #endif
  519. eri_write(ESP_APPTRACE_TRAX_CTRL_REG, ESP_APPTRACE_TRAX_BLOCK_ID(s_trace_buf.trax.state.in_block) |
  520. host_connected | ESP_APPTRACE_TRAX_BLOCK_LEN(s_trace_buf.trax.state.markers[prev_block_num]));
  521. _on_func_exit:
  522. // exit ERI update critical section
  523. eri_write(ESP_APPTRACE_TRAX_STAT_REG, 0x0);
  524. // TODO: currently host sets breakpoint, use break instruction to stop;
  525. // it will allow to use ESP_APPTRACE_TRAX_STAT_REG for other purposes
  526. asm volatile (
  527. " .global __esp_apptrace_trax_eri_updated\n"
  528. "__esp_apptrace_trax_eri_updated:\n"); // host will set bp here to resolve collision at streaming start
  529. return res;
  530. }
  531. static esp_err_t esp_apptrace_trax_block_switch_waitus(esp_apptrace_tmo_t *tmo)
  532. {
  533. int res;
  534. while ((res = esp_apptrace_trax_block_switch()) != ESP_OK) {
  535. res = esp_apptrace_tmo_check(tmo);
  536. if (res != ESP_OK) {
  537. break;
  538. }
  539. }
  540. return res;
  541. }
  542. static uint8_t *esp_apptrace_trax_down_buffer_get(uint32_t *size, esp_apptrace_tmo_t *tmo)
  543. {
  544. uint8_t *ptr = NULL;
  545. int res = esp_apptrace_lock(tmo);
  546. if (res != ESP_OK) {
  547. return NULL;
  548. }
  549. while (1) {
  550. uint32_t sz = esp_apptrace_rb_read_size_get(&s_trace_buf.rb_down);
  551. if (sz != 0) {
  552. *size = MIN(*size, sz);
  553. ptr = esp_apptrace_rb_consume(&s_trace_buf.rb_down, *size);
  554. if (!ptr) {
  555. assert(false && "Failed to consume bytes from down buffer!");
  556. }
  557. break;
  558. }
  559. // may need to flush
  560. uint32_t ctrl_reg = eri_read(ESP_APPTRACE_TRAX_CTRL_REG);
  561. if (ctrl_reg & ESP_APPTRACE_TRAX_HOST_DATA) {
  562. ESP_APPTRACE_LOGD("force flush");
  563. res = esp_apptrace_trax_block_switch_waitus(tmo);
  564. if (res != ESP_OK) {
  565. ESP_APPTRACE_LOGE("Failed to switch to another block to recv data from host!");
  566. /*do not return error because data can be in down buffer already*/
  567. }
  568. } else {
  569. // check tmo only if there is no data from host
  570. res = esp_apptrace_tmo_check(tmo);
  571. if (res != ESP_OK) {
  572. return NULL;
  573. }
  574. }
  575. }
  576. if (esp_apptrace_unlock() != ESP_OK) {
  577. assert(false && "Failed to unlock apptrace data!");
  578. }
  579. return ptr;
  580. }
  581. static esp_err_t esp_apptrace_trax_down_buffer_put(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  582. {
  583. /* nothing todo */
  584. return ESP_OK;
  585. }
  586. static uint32_t esp_apptrace_trax_down_buffer_write_nolock(uint8_t *data, uint32_t size)
  587. {
  588. uint32_t total_sz = 0;
  589. while (total_sz < size) {
  590. 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,
  591. s_trace_buf.rb_down.cur_size, size);
  592. uint32_t wr_sz = esp_apptrace_rb_write_size_get(&s_trace_buf.rb_down);
  593. if (wr_sz == 0) {
  594. break;
  595. }
  596. if (wr_sz > size - total_sz) {
  597. wr_sz = size - total_sz;
  598. }
  599. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d", wr_sz);
  600. uint8_t *ptr = esp_apptrace_rb_produce(&s_trace_buf.rb_down, wr_sz);
  601. if (!ptr) {
  602. assert(false && "Failed to produce bytes to down buffer!");
  603. }
  604. 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);
  605. memcpy(ptr, data + total_sz, wr_sz);
  606. total_sz += wr_sz;
  607. ESP_APPTRACE_LOGD("esp_apptrace_trax_down_buffer_write_nolock wr %d/%d", wr_sz, total_sz);
  608. }
  609. return total_sz;
  610. }
  611. static inline uint8_t *esp_apptrace_data_header_init(uint8_t *ptr, uint16_t usr_size)
  612. {
  613. // it is safe to use xPortGetCoreID() in macro call because arg is used only once inside it
  614. ((esp_tracedata_hdr_t *)ptr)->block_sz = ESP_APPTRACE_USR_BLOCK_CORE(xPortGetCoreID()) | usr_size;
  615. ((esp_tracedata_hdr_t *)ptr)->wr_sz = 0;
  616. return ptr + sizeof(esp_tracedata_hdr_t);
  617. }
  618. static inline uint8_t *esp_apptrace_trax_wait4buf(uint16_t size, esp_apptrace_tmo_t *tmo, int *pended)
  619. {
  620. uint8_t *ptr = NULL;
  621. int res = esp_apptrace_trax_block_switch_waitus(tmo);
  622. if (res != ESP_OK) {
  623. return NULL;
  624. }
  625. // check if we still have pending data
  626. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  627. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  628. // if after TRAX block switch still have pending data (not all pending data have been pumped to TRAX block)
  629. // alloc new pending buffer
  630. *pended = 1;
  631. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  632. if (!ptr) {
  633. 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);
  634. }
  635. } else
  636. #endif
  637. {
  638. // update block pointers
  639. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + size > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  640. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  641. *pended = 1;
  642. ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, size);
  643. if (ptr == NULL) {
  644. 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);
  645. }
  646. #endif
  647. } else {
  648. *pended = 0;
  649. ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  650. }
  651. }
  652. return ptr;
  653. }
  654. static uint8_t *esp_apptrace_trax_get_buffer(uint32_t size, esp_apptrace_tmo_t *tmo)
  655. {
  656. uint8_t *buf_ptr = NULL;
  657. if (size > ESP_APPTRACE_USR_DATA_LEN_MAX) {
  658. ESP_APPTRACE_LOGE("Too large user data size %d!", size);
  659. return NULL;
  660. }
  661. int res = esp_apptrace_lock(tmo);
  662. if (res != ESP_OK) {
  663. return NULL;
  664. }
  665. // check for data in the pending buffer
  666. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  667. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  668. // if we have buffered data try to switch TRAX block
  669. esp_apptrace_trax_block_switch();
  670. // if switch was successful, part or all pended data have been copied to TRAX block
  671. }
  672. if (esp_apptrace_rb_read_size_get(&s_trace_buf.trax.rb_pend) > 0) {
  673. // if we have buffered data alloc new pending buffer
  674. ESP_APPTRACE_LOGD("Get %d bytes from PEND buffer", size);
  675. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  676. if (buf_ptr == NULL) {
  677. int pended_buf;
  678. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  679. if (buf_ptr) {
  680. if (pended_buf) {
  681. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  682. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  683. #endif
  684. } else {
  685. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  686. // update cur block marker
  687. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  688. }
  689. }
  690. } else {
  691. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  692. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  693. #endif
  694. }
  695. } else
  696. #endif
  697. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() + ESP_APPTRACE_USR_BLOCK_RAW_SZ(size) > ESP_APPTRACE_TRAX_INBLOCK_GET()->sz) {
  698. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  699. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from PEND buffer", size);
  700. buf_ptr = esp_apptrace_rb_produce(&s_trace_buf.trax.rb_pend, ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  701. if (buf_ptr) {
  702. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  703. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  704. #endif
  705. }
  706. #endif
  707. if (buf_ptr == NULL) {
  708. int pended_buf;
  709. ESP_APPTRACE_LOGD("TRAX full. Get %d bytes from pend buffer", size);
  710. buf_ptr = esp_apptrace_trax_wait4buf(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size), tmo, &pended_buf);
  711. if (buf_ptr) {
  712. if (pended_buf) {
  713. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  714. esp_apptrace_trax_pend_chunk_sz_update(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  715. #endif
  716. } else {
  717. ESP_APPTRACE_LOGD("Got %d bytes from TRAX buffer", size);
  718. // update cur block marker
  719. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  720. }
  721. }
  722. }
  723. } else {
  724. ESP_APPTRACE_LOGD("Get %d bytes from TRAX buffer", size);
  725. // fit to curr TRAX nlock
  726. buf_ptr = ESP_APPTRACE_TRAX_INBLOCK_GET()->start + ESP_APPTRACE_TRAX_INBLOCK_MARKER();
  727. // update cur block marker
  728. ESP_APPTRACE_TRAX_INBLOCK_MARKER_UPD(ESP_APPTRACE_USR_BLOCK_RAW_SZ(size));
  729. }
  730. if (buf_ptr) {
  731. buf_ptr = esp_apptrace_data_header_init(buf_ptr, size);
  732. }
  733. // now we can safely unlock apptrace to allow other tasks/ISRs to get other buffers and write their data
  734. if (esp_apptrace_unlock() != ESP_OK) {
  735. assert(false && "Failed to unlock apptrace data!");
  736. }
  737. return buf_ptr;
  738. }
  739. static esp_err_t esp_apptrace_trax_put_buffer(uint8_t *ptr, esp_apptrace_tmo_t *tmo)
  740. {
  741. int res = ESP_OK;
  742. esp_tracedata_hdr_t *hdr = (esp_tracedata_hdr_t *)(ptr - sizeof(esp_tracedata_hdr_t));
  743. // update written size
  744. hdr->wr_sz = hdr->block_sz;
  745. // TODO: mark block as busy in order not to re-use it for other tracing calls until it is completely written
  746. // TODO: avoid potential situation when all memory is consumed by low prio tasks which can not complete writing due to
  747. // higher prio tasks and the latter can not allocate buffers at all
  748. // this is abnormal situation can be detected on host which will receive only uncompleted buffers
  749. // workaround: use own memcpy which will kick-off dead tracing calls
  750. return res;
  751. }
  752. static esp_err_t esp_apptrace_trax_flush(uint32_t min_sz, esp_apptrace_tmo_t *tmo)
  753. {
  754. int res = ESP_OK;
  755. if (ESP_APPTRACE_TRAX_INBLOCK_MARKER() < min_sz) {
  756. ESP_APPTRACE_LOGI("Ignore flush request for min %d bytes. Bytes in TRAX block: %d.", min_sz, ESP_APPTRACE_TRAX_INBLOCK_MARKER());
  757. return ESP_OK;
  758. }
  759. // switch TRAX block while size of data is more than min size
  760. while (ESP_APPTRACE_TRAX_INBLOCK_MARKER() > 0) {
  761. ESP_APPTRACE_LOGD("Try to flush %d bytes. Wait until block switch for %u us", ESP_APPTRACE_TRAX_INBLOCK_MARKER(), tmo->tmo);
  762. res = esp_apptrace_trax_block_switch_waitus(tmo);
  763. if (res != ESP_OK) {
  764. ESP_APPTRACE_LOGE("Failed to switch to another block!");
  765. return res;
  766. }
  767. }
  768. return res;
  769. }
  770. static bool esp_apptrace_trax_host_is_connected(void)
  771. {
  772. return eri_read(ESP_APPTRACE_TRAX_CTRL_REG) & ESP_APPTRACE_TRAX_HOST_CONNECT ? true : false;
  773. }
  774. static esp_err_t esp_apptrace_trax_status_reg_set(uint32_t val)
  775. {
  776. eri_write(ESP_APPTRACE_TRAX_STAT_REG, val);
  777. return ESP_OK;
  778. }
  779. static esp_err_t esp_apptrace_trax_status_reg_get(uint32_t *val)
  780. {
  781. *val = eri_read(ESP_APPTRACE_TRAX_STAT_REG);
  782. return ESP_OK;
  783. }
  784. static esp_err_t esp_apptrace_trax_dest_init(void)
  785. {
  786. for (size_t i = 0; i < ESP_APPTRACE_TRAX_BLOCKS_NUM; i++) {
  787. s_trace_buf.trax.blocks[i].start = (uint8_t *)s_trax_blocks[i];
  788. s_trace_buf.trax.blocks[i].sz = ESP_APPTRACE_TRAX_BLOCK_SIZE;
  789. s_trace_buf.trax.state.markers[i] = 0;
  790. }
  791. s_trace_buf.trax.state.in_block = ESP_APPTRACE_TRAX_INBLOCK_START;
  792. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > 0
  793. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend, s_trace_buf.trax.pending_data,
  794. sizeof(s_trace_buf.trax.pending_data));
  795. #if CONFIG_APPTRACE_PENDING_DATA_SIZE_MAX > ESP_APPTRACE_TRAX_BLOCK_SIZE
  796. s_trace_buf.trax.cur_pending_chunk_sz = 0;
  797. esp_apptrace_rb_init(&s_trace_buf.trax.rb_pend_chunk_sz, (uint8_t *)s_trace_buf.trax.pending_chunk_sz,
  798. sizeof(s_trace_buf.trax.pending_chunk_sz));
  799. #endif
  800. #endif
  801. #if CONFIG_IDF_TARGET_ESP32
  802. DPORT_WRITE_PERI_REG(DPORT_PRO_TRACEMEM_ENA_REG, DPORT_PRO_TRACEMEM_ENA_M);
  803. #if CONFIG_FREERTOS_UNICORE == 0
  804. DPORT_WRITE_PERI_REG(DPORT_APP_TRACEMEM_ENA_REG, DPORT_APP_TRACEMEM_ENA_M);
  805. #endif
  806. #endif
  807. esp_apptrace_trax_select_memory_block(0);
  808. return ESP_OK;
  809. }
  810. #endif
  811. esp_err_t esp_apptrace_init(void)
  812. {
  813. int res;
  814. if (!s_trace_buf.inited) {
  815. memset(&s_trace_buf, 0, sizeof(s_trace_buf));
  816. // disabled by default
  817. esp_apptrace_rb_init(&s_trace_buf.rb_down, NULL, 0);
  818. res = esp_apptrace_lock_initialize(&s_trace_buf.lock);
  819. if (res != ESP_OK) {
  820. ESP_APPTRACE_LOGE("Failed to init log lock (%d)!", res);
  821. return res;
  822. }
  823. #if CONFIG_APPTRACE_DEST_TRAX
  824. res = esp_apptrace_trax_dest_init();
  825. if (res != ESP_OK) {
  826. ESP_APPTRACE_LOGE("Failed to init TRAX dest data (%d)!", res);
  827. esp_apptrace_lock_cleanup();
  828. return res;
  829. }
  830. #endif
  831. }
  832. #if CONFIG_APPTRACE_DEST_TRAX
  833. // init TRAX on this CPU
  834. esp_apptrace_trax_init();
  835. #endif
  836. s_trace_buf.inited |= 1 << xPortGetCoreID(); // global and this CPU-specific data are inited
  837. return ESP_OK;
  838. }
  839. void esp_apptrace_down_buffer_config(uint8_t *buf, uint32_t size)
  840. {
  841. esp_apptrace_rb_init(&s_trace_buf.rb_down, buf, size);
  842. }
  843. esp_err_t esp_apptrace_read(esp_apptrace_dest_t dest, void *buf, uint32_t *size, uint32_t user_tmo)
  844. {
  845. int res = ESP_OK;
  846. esp_apptrace_tmo_t tmo;
  847. esp_apptrace_hw_t *hw = NULL;
  848. if (dest == ESP_APPTRACE_DEST_TRAX) {
  849. #if CONFIG_APPTRACE_DEST_TRAX
  850. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  851. #else
  852. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  853. return ESP_ERR_NOT_SUPPORTED;
  854. #endif
  855. } else {
  856. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  857. return ESP_ERR_NOT_SUPPORTED;
  858. }
  859. if (buf == NULL || size == NULL || *size == 0) {
  860. return ESP_ERR_INVALID_ARG;
  861. }
  862. //TODO: callback system
  863. esp_apptrace_tmo_init(&tmo, user_tmo);
  864. uint32_t act_sz = *size;
  865. *size = 0;
  866. uint8_t * ptr = hw->get_down_buffer(&act_sz, &tmo);
  867. if (ptr && act_sz > 0) {
  868. ESP_APPTRACE_LOGD("Read %d bytes from host", act_sz);
  869. memcpy(buf, ptr, act_sz);
  870. res = hw->put_down_buffer(ptr, &tmo);
  871. *size = act_sz;
  872. } else {
  873. res = ESP_ERR_TIMEOUT;
  874. }
  875. return res;
  876. }
  877. uint8_t *esp_apptrace_down_buffer_get(esp_apptrace_dest_t dest, uint32_t *size, uint32_t user_tmo)
  878. {
  879. esp_apptrace_tmo_t tmo;
  880. esp_apptrace_hw_t *hw = NULL;
  881. if (dest == ESP_APPTRACE_DEST_TRAX) {
  882. #if CONFIG_APPTRACE_DEST_TRAX
  883. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  884. #else
  885. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  886. return NULL;
  887. #endif
  888. } else {
  889. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  890. return NULL;
  891. }
  892. if (size == NULL || *size == 0) {
  893. return NULL;
  894. }
  895. esp_apptrace_tmo_init(&tmo, user_tmo);
  896. return hw->get_down_buffer(size, &tmo);
  897. }
  898. esp_err_t esp_apptrace_down_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  899. {
  900. esp_apptrace_tmo_t tmo;
  901. esp_apptrace_hw_t *hw = NULL;
  902. if (dest == ESP_APPTRACE_DEST_TRAX) {
  903. #if CONFIG_APPTRACE_DEST_TRAX
  904. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  905. #else
  906. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  907. return ESP_ERR_NOT_SUPPORTED;
  908. #endif
  909. } else {
  910. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  911. return ESP_ERR_NOT_SUPPORTED;
  912. }
  913. if (ptr == NULL) {
  914. return ESP_ERR_INVALID_ARG;
  915. }
  916. esp_apptrace_tmo_init(&tmo, user_tmo);
  917. return hw->put_down_buffer(ptr, &tmo);
  918. }
  919. esp_err_t esp_apptrace_write(esp_apptrace_dest_t dest, const void *data, uint32_t size, uint32_t user_tmo)
  920. {
  921. uint8_t *ptr = NULL;
  922. esp_apptrace_tmo_t tmo;
  923. esp_apptrace_hw_t *hw = NULL;
  924. if (dest == ESP_APPTRACE_DEST_TRAX) {
  925. #if CONFIG_APPTRACE_DEST_TRAX
  926. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  927. #else
  928. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  929. return ESP_ERR_NOT_SUPPORTED;
  930. #endif
  931. } else {
  932. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  933. return ESP_ERR_NOT_SUPPORTED;
  934. }
  935. if (data == NULL || size == 0) {
  936. return ESP_ERR_INVALID_ARG;
  937. }
  938. esp_apptrace_tmo_init(&tmo, user_tmo);
  939. ptr = hw->get_up_buffer(size, &tmo);
  940. if (ptr == NULL) {
  941. return ESP_ERR_NO_MEM;
  942. }
  943. // actually can be suspended here by higher prio tasks/ISRs
  944. //TODO: use own memcpy with dead trace calls kick-off algo and tmo expiration check
  945. memcpy(ptr, data, size);
  946. // now indicate that this buffer is ready to be sent off to host
  947. return hw->put_up_buffer(ptr, &tmo);
  948. }
  949. int esp_apptrace_vprintf_to(esp_apptrace_dest_t dest, uint32_t user_tmo, const char *fmt, va_list ap)
  950. {
  951. uint16_t nargs = 0;
  952. uint8_t *pout, *p = (uint8_t *)fmt;
  953. esp_apptrace_tmo_t tmo;
  954. esp_apptrace_hw_t *hw = NULL;
  955. if (dest == ESP_APPTRACE_DEST_TRAX) {
  956. #if CONFIG_APPTRACE_DEST_TRAX
  957. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  958. #else
  959. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  960. return ESP_ERR_NOT_SUPPORTED;
  961. #endif
  962. } else {
  963. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  964. return ESP_ERR_NOT_SUPPORTED;
  965. }
  966. if (fmt == NULL) {
  967. return ESP_ERR_INVALID_ARG;
  968. }
  969. esp_apptrace_tmo_init(&tmo, user_tmo);
  970. ESP_APPTRACE_LOGD("fmt %x", fmt);
  971. while ((p = (uint8_t *)strchr((char *)p, '%')) && nargs < ESP_APPTRACE_MAX_VPRINTF_ARGS) {
  972. p++;
  973. if (*p != '%' && *p != 0) {
  974. nargs++;
  975. }
  976. }
  977. ESP_APPTRACE_LOGD("nargs = %d", nargs);
  978. if (p) {
  979. ESP_APPTRACE_LOGE("Failed to store all printf args!");
  980. }
  981. pout = hw->get_up_buffer(1 + sizeof(char *) + nargs * sizeof(uint32_t), &tmo);
  982. if (pout == NULL) {
  983. ESP_APPTRACE_LOGE("Failed to get buffer!");
  984. return -1;
  985. }
  986. p = pout;
  987. *pout = nargs;
  988. pout++;
  989. *(const char **)pout = fmt;
  990. pout += sizeof(char *);
  991. while (nargs-- > 0) {
  992. uint32_t arg = va_arg(ap, uint32_t);
  993. *(uint32_t *)pout = arg;
  994. pout += sizeof(uint32_t);
  995. ESP_APPTRACE_LOGD("arg %x", arg);
  996. }
  997. int ret = hw->put_up_buffer(p, &tmo);
  998. if (ret != ESP_OK) {
  999. ESP_APPTRACE_LOGE("Failed to put printf buf (%d)!", ret);
  1000. return -1;
  1001. }
  1002. return (pout - p);
  1003. }
  1004. int esp_apptrace_vprintf(const char *fmt, va_list ap)
  1005. {
  1006. return esp_apptrace_vprintf_to(ESP_APPTRACE_DEST_TRAX, /*ESP_APPTRACE_TMO_INFINITE*/0, fmt, ap);
  1007. }
  1008. uint8_t *esp_apptrace_buffer_get(esp_apptrace_dest_t dest, uint32_t size, uint32_t user_tmo)
  1009. {
  1010. esp_apptrace_tmo_t tmo;
  1011. esp_apptrace_hw_t *hw = NULL;
  1012. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1013. #if CONFIG_APPTRACE_DEST_TRAX
  1014. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1015. #else
  1016. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1017. return NULL;
  1018. #endif
  1019. } else {
  1020. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1021. return NULL;
  1022. }
  1023. if (size == 0) {
  1024. return NULL;
  1025. }
  1026. esp_apptrace_tmo_init(&tmo, user_tmo);
  1027. return hw->get_up_buffer(size, &tmo);
  1028. }
  1029. esp_err_t esp_apptrace_buffer_put(esp_apptrace_dest_t dest, uint8_t *ptr, uint32_t user_tmo)
  1030. {
  1031. esp_apptrace_tmo_t tmo;
  1032. esp_apptrace_hw_t *hw = NULL;
  1033. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1034. #if CONFIG_APPTRACE_DEST_TRAX
  1035. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1036. #else
  1037. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1038. return ESP_ERR_NOT_SUPPORTED;
  1039. #endif
  1040. } else {
  1041. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1042. return ESP_ERR_NOT_SUPPORTED;
  1043. }
  1044. if (ptr == NULL) {
  1045. return ESP_ERR_INVALID_ARG;
  1046. }
  1047. esp_apptrace_tmo_init(&tmo, user_tmo);
  1048. return hw->put_up_buffer(ptr, &tmo);
  1049. }
  1050. esp_err_t esp_apptrace_flush_nolock(esp_apptrace_dest_t dest, uint32_t min_sz, uint32_t usr_tmo)
  1051. {
  1052. esp_apptrace_tmo_t tmo;
  1053. esp_apptrace_hw_t *hw = NULL;
  1054. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1055. #if CONFIG_APPTRACE_DEST_TRAX
  1056. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1057. #else
  1058. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1059. return ESP_ERR_NOT_SUPPORTED;
  1060. #endif
  1061. } else {
  1062. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1063. return ESP_ERR_NOT_SUPPORTED;
  1064. }
  1065. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1066. return hw->flush_up_buffer(min_sz, &tmo);
  1067. }
  1068. esp_err_t esp_apptrace_flush(esp_apptrace_dest_t dest, uint32_t usr_tmo)
  1069. {
  1070. int res;
  1071. esp_apptrace_tmo_t tmo;
  1072. esp_apptrace_tmo_init(&tmo, usr_tmo);
  1073. res = esp_apptrace_lock(&tmo);
  1074. if (res != ESP_OK) {
  1075. ESP_APPTRACE_LOGE("Failed to lock apptrace data (%d)!", res);
  1076. return res;
  1077. }
  1078. res = esp_apptrace_flush_nolock(dest, 0, esp_apptrace_tmo_remaining_us(&tmo));
  1079. if (res != ESP_OK) {
  1080. ESP_APPTRACE_LOGE("Failed to flush apptrace data (%d)!", res);
  1081. }
  1082. if (esp_apptrace_unlock() != ESP_OK) {
  1083. assert(false && "Failed to unlock apptrace data!");
  1084. }
  1085. return res;
  1086. }
  1087. bool esp_apptrace_host_is_connected(esp_apptrace_dest_t dest)
  1088. {
  1089. esp_apptrace_hw_t *hw = NULL;
  1090. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1091. #if CONFIG_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 false;
  1096. #endif
  1097. } else {
  1098. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1099. return false;
  1100. }
  1101. return hw->host_is_connected();
  1102. }
  1103. esp_err_t esp_apptrace_status_reg_set(esp_apptrace_dest_t dest, uint32_t val)
  1104. {
  1105. esp_apptrace_hw_t *hw = NULL;
  1106. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1107. #if CONFIG_APPTRACE_DEST_TRAX
  1108. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1109. #else
  1110. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1111. return ESP_ERR_NOT_SUPPORTED;
  1112. #endif
  1113. } else {
  1114. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1115. return ESP_ERR_NOT_SUPPORTED;
  1116. }
  1117. return hw->status_reg_set(val);
  1118. }
  1119. esp_err_t esp_apptrace_status_reg_get(esp_apptrace_dest_t dest, uint32_t *val)
  1120. {
  1121. esp_apptrace_hw_t *hw = NULL;
  1122. if (dest == ESP_APPTRACE_DEST_TRAX) {
  1123. #if CONFIG_APPTRACE_DEST_TRAX
  1124. hw = ESP_APPTRACE_HW(ESP_APPTRACE_HW_TRAX);
  1125. #else
  1126. ESP_APPTRACE_LOGE("Application tracing via TRAX is disabled in menuconfig!");
  1127. return ESP_ERR_NOT_SUPPORTED;
  1128. #endif
  1129. } else {
  1130. ESP_APPTRACE_LOGE("Trace destinations other then TRAX are not supported yet!");
  1131. return ESP_ERR_NOT_SUPPORTED;
  1132. }
  1133. return hw->status_reg_get(val);
  1134. }
  1135. #endif