Select one of the symbols to view example projects that use it.
 
Outline
#include "soc/soc.h"
#include "esp_log.h"
#include "esp_cpu.h"
#include "esp_app_trace_port.h"
#include "driver/uart.h"
#include "hal/uart_ll.h"
#include "string.h"
#include "driver/gpio.h"
#define APPTRACE_DEST_UART
#define APP_TRACE_MAX_TX_BUFF_UART
#define APP_TRACE_MAX_TX_MSG_UART
esp_apptrace_uart_data_t
TAG
esp_apptrace_uart_hw_get(int, void **)
...
#define APP_TRACE_UART_STOP_WAIT_TMO
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/app_trace/port/port_uart.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include "soc/soc.h" #include "esp_log.h" #include "esp_cpu.h" #include "esp_app_trace_port.h" #include "driver/uart.h" #include "hal/uart_ll.h" #include "string.h" #include "driver/gpio.h"8 includes #define APPTRACE_DEST_UART (CONFIG_APPTRACE_DEST_UART0 | CONFIG_APPTRACE_DEST_UART1 | CONFIG_APPTRACE_DEST_UART2) #define APP_TRACE_MAX_TX_BUFF_UART CONFIG_APPTRACE_UART_TX_BUFF_SIZE #define APP_TRACE_MAX_TX_MSG_UART CONFIG_APPTRACE_UART_TX_MSG_SIZE /** UART HW transport data */ typedef struct { uint8_t inited; #if CONFIG_APPTRACE_LOCK_ENABLE esp_apptrace_lock_t lock; // sync lock #endif uart_port_t port_num; // TX data ring buffer uint8_t *tx_data_buff; int32_t tx_data_buff_in; int32_t tx_data_buff_out; // TX message buffer uint8_t *tx_msg_buff; uint32_t tx_msg_buff_size; // RX message buffer uint8_t *down_buffer; uint32_t down_buffer_size; // Buffer overflow flags bool message_buff_overflow; bool circular_buff_overflow; }{ ... } esp_apptrace_uart_data_t; #if APPTRACE_DEST_UART static esp_err_t esp_apptrace_uart_init(esp_apptrace_uart_data_t *hw_data); static esp_err_t esp_apptrace_uart_flush(esp_apptrace_uart_data_t *hw_data, esp_apptrace_tmo_t *tmo); static esp_err_t esp_apptrace_uart_flush_nolock(esp_apptrace_uart_data_t *hw_data, uint32_t min_sz, esp_apptrace_tmo_t *tmo); static uint8_t *esp_apptrace_uart_up_buffer_get(esp_apptrace_uart_data_t *hw_data, uint32_t size, esp_apptrace_tmo_t *tmo); static esp_err_t esp_apptrace_uart_up_buffer_put(esp_apptrace_uart_data_t *hw_data, uint8_t *ptr, esp_apptrace_tmo_t *tmo); static void esp_apptrace_uart_down_buffer_config(esp_apptrace_uart_data_t *hw_data, uint8_t *buf, uint32_t size); static uint8_t *esp_apptrace_uart_down_buffer_get(esp_apptrace_uart_data_t *hw_data, uint32_t *size, esp_apptrace_tmo_t *tmo); static esp_err_t esp_apptrace_uart_down_buffer_put(esp_apptrace_uart_data_t *hw_data, uint8_t *ptr, esp_apptrace_tmo_t *tmo); static bool esp_apptrace_uart_host_is_connected(esp_apptrace_uart_data_t *hw_data); /* ... */ #endif // APPTRACE_DEST_UART const static char *TAG = "esp_apptrace_uart"; esp_apptrace_hw_t *esp_apptrace_uart_hw_get(int num, void **data) { ESP_LOGD(TAG,"esp_apptrace_uart_hw_get - %i", num); #if APPTRACE_DEST_UART static esp_apptrace_uart_data_t s_uart_hw_data = { }{...}; static esp_apptrace_hw_t s_uart_hw = { .init = (esp_err_t (*)(void *))esp_apptrace_uart_init, .get_up_buffer = (uint8_t *(*)(void *, uint32_t, esp_apptrace_tmo_t *))esp_apptrace_uart_up_buffer_get, .put_up_buffer = (esp_err_t (*)(void *, uint8_t *, esp_apptrace_tmo_t *))esp_apptrace_uart_up_buffer_put, .flush_up_buffer_nolock = (esp_err_t (*)(void *, uint32_t, esp_apptrace_tmo_t *))esp_apptrace_uart_flush_nolock, .flush_up_buffer = (esp_err_t (*)(void *, esp_apptrace_tmo_t *))esp_apptrace_uart_flush, .down_buffer_config = (void (*)(void *, uint8_t *, uint32_t ))esp_apptrace_uart_down_buffer_config, .get_down_buffer = (uint8_t *(*)(void *, uint32_t *, esp_apptrace_tmo_t *))esp_apptrace_uart_down_buffer_get, .put_down_buffer = (esp_err_t (*)(void *, uint8_t *, esp_apptrace_tmo_t *))esp_apptrace_uart_down_buffer_put, .host_is_connected = (bool (*)(void *))esp_apptrace_uart_host_is_connected, }{...}; s_uart_hw_data.port_num = num; *data = &s_uart_hw_data; return &s_uart_hw;/* ... */ #else return NULL; #endif }{ ... } #if APPTRACE_DEST_UART static esp_err_t esp_apptrace_uart_lock(esp_apptrace_uart_data_t *hw_data, esp_apptrace_tmo_t *tmo) { #if CONFIG_APPTRACE_LOCK_ENABLE esp_err_t ret = esp_apptrace_lock_take(&hw_data->lock, tmo); if (ret != ESP_OK) { return ESP_FAIL; }{...} /* ... */#endif return ESP_OK; }{...} static esp_err_t esp_apptrace_uart_unlock(esp_apptrace_uart_data_t *hw_data) { esp_err_t ret = ESP_OK; #if CONFIG_APPTRACE_LOCK_ENABLE ret = esp_apptrace_lock_give(&hw_data->lock); #endif return ret; }{...} static inline void esp_apptrace_uart_hw_init(void) { ESP_APPTRACE_LOGI("Initialized UART on CPU%d", esp_cpu_get_core_id()); }{...} /*****************************************************************************************/ /***************************** Apptrace HW iface *****************************************/ /*****************************************************************************************/ static esp_err_t esp_apptrace_send_uart_data(esp_apptrace_uart_data_t *hw_data, const char *data, uint32_t size, esp_apptrace_tmo_t *tmo) { esp_err_t res = esp_apptrace_uart_lock(hw_data, tmo); if (res != ESP_OK) { return res; }{...} // We store current out position to handle it without lock volatile int32_t out_position = hw_data->tx_data_buff_out; int len_free = APP_TRACE_MAX_TX_BUFF_UART - (hw_data->tx_data_buff_in - out_position); if (out_position > hw_data->tx_data_buff_in) { len_free = out_position - hw_data->tx_data_buff_in; }{...} int check_len = APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_in; if (size <= len_free) { if ( check_len >= size) { memcpy(&hw_data->tx_data_buff[hw_data->tx_data_buff_in], data, size); hw_data->tx_data_buff_in += size; }{...} else { memcpy(&hw_data->tx_data_buff[hw_data->tx_data_buff_in], data, APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_in); memcpy(&hw_data->tx_data_buff[0], &data[APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_in], size - (APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_in)); hw_data->tx_data_buff_in = size - (APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_in); }{...} if (hw_data->tx_data_buff_in >= APP_TRACE_MAX_TX_BUFF_UART) { hw_data->tx_data_buff_in = 0; }{...} }{...} else { hw_data->circular_buff_overflow = true; }{...} if (esp_apptrace_uart_unlock(hw_data) != ESP_OK) { assert(false && "Failed to unlock apptrace data!"); }{...} return ESP_OK; }{...} static void send_buff_data(esp_apptrace_uart_data_t *hw_data, esp_apptrace_tmo_t *tmo) { if (hw_data->tx_data_buff_in == hw_data->tx_data_buff_out) { return; }{...} // We store current in position to handle it without lock volatile int32_t in_position = hw_data->tx_data_buff_in; if (in_position > hw_data->tx_data_buff_out) { int bytes_sent = uart_write_bytes(hw_data->port_num, &hw_data->tx_data_buff[hw_data->tx_data_buff_out], in_position - hw_data->tx_data_buff_out); hw_data->tx_data_buff_out += bytes_sent; }{...} else { int bytes_sent = uart_write_bytes(hw_data->port_num, &hw_data->tx_data_buff[hw_data->tx_data_buff_out], APP_TRACE_MAX_TX_BUFF_UART - hw_data->tx_data_buff_out); hw_data->tx_data_buff_out += bytes_sent; if (hw_data->tx_data_buff_out >= APP_TRACE_MAX_TX_BUFF_UART) { hw_data->tx_data_buff_out = 0; }{...} }{...} }{...} #define APP_TRACE_UART_STOP_WAIT_TMO 1000000 //us static void esp_apptrace_send_uart_tx_task(void *arg) { esp_apptrace_uart_data_t *hw_data = (esp_apptrace_uart_data_t *)arg; esp_apptrace_tmo_t tmo; esp_apptrace_tmo_init(&tmo, APP_TRACE_UART_STOP_WAIT_TMO); vTaskDelay(10); while (1) { send_buff_data(hw_data, &tmo); vTaskDelay(10); if (hw_data->circular_buff_overflow == true) { hw_data->circular_buff_overflow = false; ESP_LOGE(TAG, "Buffer overflow. Please increase UART baudrate, or increase UART TX ring buffer size in menuconfig."); }{...} if (hw_data->message_buff_overflow == true) { hw_data->message_buff_overflow = false; ESP_LOGE(TAG, "Message size more then message buffer!"); }{...} }{...} }{...} static const int APP_TRACE_UART_RX_BUF_SIZE = 4024; static esp_err_t esp_apptrace_uart_init(esp_apptrace_uart_data_t *hw_data) { int core_id = esp_cpu_get_core_id(); if (core_id == 0) { hw_data->tx_data_buff = (uint8_t *)heap_caps_malloc(APP_TRACE_MAX_TX_BUFF_UART, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); if (hw_data->tx_data_buff == NULL){ return ESP_ERR_NO_MEM; }{...} hw_data->tx_data_buff_in = 0; hw_data->tx_data_buff_out = 0; hw_data->tx_msg_buff = (uint8_t *)heap_caps_malloc(APP_TRACE_MAX_TX_MSG_UART, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); if (hw_data->tx_msg_buff == NULL) { return ESP_ERR_NO_MEM; }{...} hw_data->tx_msg_buff_size = 0; hw_data->down_buffer_size = 0; hw_data->message_buff_overflow = false; hw_data->circular_buff_overflow = false; const uart_config_t uart_config = { .baud_rate = CONFIG_APPTRACE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .source_clk = UART_SCLK_DEFAULT, }{...}; ESP_LOGI(TAG, "UART baud rate: %i", CONFIG_APPTRACE_UART_BAUDRATE); // We won't use a buffer for sending data. esp_err_t err = uart_driver_install(hw_data->port_num, APP_TRACE_UART_RX_BUF_SIZE, APP_TRACE_UART_RX_BUF_SIZE, 0, NULL, 0); assert((err == ESP_OK) && "Not possible to install UART. Please check and change menuconfig parameters!"); err = uart_param_config(hw_data->port_num, &uart_config); assert((err == ESP_OK) && "Not possible to configure UART. Please check and change menuconfig parameters!"); err = uart_set_pin(hw_data->port_num, CONFIG_APPTRACE_UART_TX_GPIO, CONFIG_APPTRACE_UART_RX_GPIO, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); assert((err == ESP_OK) && "Not possible to configure UART RX/TX pins. Please check and change menuconfig parameters!"); int uart_prio = CONFIG_APPTRACE_UART_TASK_PRIO; if (uart_prio >= (configMAX_PRIORITIES-1)) uart_prio = configMAX_PRIORITIES - 1; err = xTaskCreate(esp_apptrace_send_uart_tx_task, "app_trace_uart_tx_task", 2500, hw_data, uart_prio, NULL); assert((err == pdPASS) && "Not possible to configure UART. Not possible to create task!"); #if CONFIG_APPTRACE_LOCK_ENABLE esp_apptrace_lock_init(&hw_data->lock); #endif }{...} // init UART on this CPU esp_apptrace_uart_hw_init(); hw_data->inited |= 1 << core_id; return ESP_OK; }{...} static uint8_t *esp_apptrace_uart_up_buffer_get(esp_apptrace_uart_data_t *hw_data, uint32_t size, esp_apptrace_tmo_t *tmo) { uint8_t *ptr; if (size > APP_TRACE_MAX_TX_MSG_UART) { hw_data->message_buff_overflow = true; return NULL; }{...} if (hw_data->tx_msg_buff_size != 0) { // A previous message was not sent. return NULL; }{...} esp_err_t res = esp_apptrace_uart_lock(hw_data, tmo); if (res != ESP_OK) { return NULL; }{...} ptr = hw_data->tx_msg_buff; hw_data->tx_msg_buff_size = size; // now we can safely unlock apptrace to allow other tasks/ISRs to get other buffers and write their data if (esp_apptrace_uart_unlock(hw_data) != ESP_OK) { assert(false && "Failed to unlock apptrace data!"); }{...} return ptr; }{...} static esp_err_t esp_apptrace_uart_up_buffer_put(esp_apptrace_uart_data_t *hw_data, uint8_t *ptr, esp_apptrace_tmo_t *tmo) { esp_err_t res = esp_apptrace_send_uart_data(hw_data, (const char *)ptr, hw_data->tx_msg_buff_size, tmo); // Clear size to indicate that we've sent data hw_data->tx_msg_buff_size = 0; return res; }{...} static void esp_apptrace_uart_down_buffer_config(esp_apptrace_uart_data_t *hw_data, uint8_t *buf, uint32_t size) { hw_data->down_buffer = (uint8_t *)malloc(size); if (hw_data->down_buffer == NULL){ assert(false && "Failed to allocate apptrace uart down buffer!"); }{...} hw_data->down_buffer_size = size; }{...} static uint8_t *esp_apptrace_uart_down_buffer_get(esp_apptrace_uart_data_t *hw_data, uint32_t *size, esp_apptrace_tmo_t *tmo) { uint8_t *ptr = NULL; if (*size > hw_data->down_buffer_size) { return NULL; }{...} esp_err_t res = esp_apptrace_uart_lock(hw_data, tmo); if (res != ESP_OK) { return NULL; }{...} size_t uart_fifolen = 0; uart_get_buffered_data_len(hw_data->port_num, &uart_fifolen); if (uart_fifolen > 0) { if (*size < uart_fifolen) { uart_fifolen = *size; }{...} *size = uart_fifolen; ptr = hw_data->down_buffer; *size =uart_read_bytes(hw_data->port_num, ptr, uart_fifolen, 0); }{...} if (esp_apptrace_uart_unlock(hw_data) != ESP_OK) { assert(false && "Failed to unlock apptrace data!"); }{...} return ptr; }{...} static esp_err_t esp_apptrace_uart_down_buffer_put(esp_apptrace_uart_data_t *hw_data, uint8_t *ptr, esp_apptrace_tmo_t *tmo) { return ESP_OK; }{...} static bool esp_apptrace_uart_host_is_connected(esp_apptrace_uart_data_t *hw_data) { return hw_data->inited & 1; }{...} static esp_err_t esp_apptrace_uart_flush_nolock(esp_apptrace_uart_data_t *hw_data, uint32_t min_sz, esp_apptrace_tmo_t *tmo) { return ESP_OK; }{...} static esp_err_t esp_apptrace_uart_flush(esp_apptrace_uart_data_t *hw_data, esp_apptrace_tmo_t *tmo) { return ESP_OK; }{...} ... /* ... */#endif // APPTRACE_DEST_UART
Details