2021-05-02 22:15:17 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-03-21 23:07:37 -04:00
|
|
|
//
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_app_trace_util.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#include "sdkconfig.h"
|
2020-12-21 12:17:42 -05:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////// Locks /////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if ESP_APPTRACE_PRINT_LOCK
|
|
|
|
static esp_apptrace_lock_t s_log_lock = {.irq_stat = 0, .portmux = portMUX_INITIALIZER_UNLOCKED};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int esp_apptrace_log_lock(void)
|
|
|
|
{
|
|
|
|
#if ESP_APPTRACE_PRINT_LOCK
|
|
|
|
esp_apptrace_tmo_t tmo;
|
|
|
|
esp_apptrace_tmo_init(&tmo, ESP_APPTRACE_TMO_INFINITE);
|
|
|
|
int ret = esp_apptrace_lock_take(&s_log_lock, &tmo);
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return 0;
|
2019-06-05 22:57:29 -04:00
|
|
|
#endif
|
2020-12-21 12:17:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_apptrace_log_unlock(void)
|
|
|
|
{
|
|
|
|
#if ESP_APPTRACE_PRINT_LOCK
|
|
|
|
esp_apptrace_lock_give(&s_log_lock);
|
|
|
|
#endif
|
|
|
|
}
|
2017-03-21 23:07:37 -04:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-07-24 12:57:44 -04:00
|
|
|
///////////////////////////////// TIMEOUT /////////////////////////////////////
|
2017-03-21 23:07:37 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo)
|
|
|
|
{
|
2020-12-21 12:17:42 -05:00
|
|
|
if (tmo->tmo != (int64_t)-1) {
|
|
|
|
tmo->elapsed = esp_timer_get_time() - tmo->start;
|
2017-07-24 12:57:44 -04:00
|
|
|
if (tmo->elapsed >= tmo->tmo) {
|
2017-03-21 23:07:37 -04:00
|
|
|
return ESP_ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-24 12:57:44 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////// LOCK ////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
esp_err_t esp_apptrace_lock_take(esp_apptrace_lock_t *lock, esp_apptrace_tmo_t *tmo)
|
2017-03-21 23:07:37 -04:00
|
|
|
{
|
2022-11-15 02:43:41 -05:00
|
|
|
esp_err_t ret;
|
2017-03-21 23:07:37 -04:00
|
|
|
|
2017-09-02 20:44:21 -04:00
|
|
|
while (1) {
|
2022-11-15 02:43:41 -05:00
|
|
|
// Try enter a critical section (i.e., take the spinlock) with 0 timeout
|
|
|
|
if (portTRY_ENTER_CRITICAL(&(lock->mux), 0) == pdTRUE) {
|
2017-09-02 20:44:21 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2022-11-15 02:43:41 -05:00
|
|
|
// Failed to enter the critical section, so interrupts are still enabled. Check if we have timed out.
|
|
|
|
ret = esp_apptrace_tmo_check(tmo);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
break; // Timed out, exit now
|
2017-09-02 20:44:21 -04:00
|
|
|
}
|
2022-11-15 02:43:41 -05:00
|
|
|
// Haven't timed out, try again
|
2017-03-21 23:07:37 -04:00
|
|
|
}
|
2022-11-15 02:43:41 -05:00
|
|
|
return ret;
|
2017-03-21 23:07:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_apptrace_lock_give(esp_apptrace_lock_t *lock)
|
|
|
|
{
|
2022-11-15 02:43:41 -05:00
|
|
|
portEXIT_CRITICAL(&(lock->mux));
|
2017-07-20 02:34:45 -04:00
|
|
|
return ESP_OK;
|
2017-03-21 23:07:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////// RING BUFFER ////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
uint8_t *esp_apptrace_rb_produce(esp_apptrace_rb_t *rb, uint32_t size)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = rb->data + rb->wr;
|
|
|
|
// check for avalable space
|
|
|
|
if (rb->rd <= rb->wr) {
|
|
|
|
// |?R......W??|
|
|
|
|
if (rb->wr + size >= rb->size) {
|
|
|
|
if (rb->rd == 0) {
|
|
|
|
return NULL; // cannot wrap wr
|
|
|
|
}
|
|
|
|
if (rb->wr + size == rb->size) {
|
|
|
|
rb->wr = 0;
|
|
|
|
} else {
|
|
|
|
// check if we can wrap wr earlier to get space for requested size
|
|
|
|
if (size > rb->rd - 1) {
|
|
|
|
return NULL; // cannot wrap wr
|
|
|
|
}
|
|
|
|
// shrink buffer a bit, full size will be restored at rd wrapping
|
|
|
|
rb->cur_size = rb->wr;
|
|
|
|
rb->wr = 0;
|
|
|
|
ptr = rb->data;
|
|
|
|
if (rb->rd == rb->cur_size) {
|
|
|
|
rb->rd = 0;
|
|
|
|
if (rb->cur_size < rb->size) {
|
|
|
|
rb->cur_size = rb->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->wr += size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rb->wr += size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// |?W......R??|
|
|
|
|
if (size > rb->rd - rb->wr - 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rb->wr += size;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *esp_apptrace_rb_consume(esp_apptrace_rb_t *rb, uint32_t size)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = rb->data + rb->rd;
|
|
|
|
if (rb->rd <= rb->wr) {
|
|
|
|
// |?R......W??|
|
|
|
|
if (rb->rd + size > rb->wr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rb->rd += size;
|
|
|
|
} else {
|
|
|
|
// |?W......R??|
|
|
|
|
if (rb->rd + size > rb->cur_size) {
|
|
|
|
return NULL;
|
|
|
|
} else if (rb->rd + size == rb->cur_size) {
|
|
|
|
// restore full size usage
|
|
|
|
if (rb->cur_size < rb->size) {
|
|
|
|
rb->cur_size = rb->size;
|
|
|
|
}
|
|
|
|
rb->rd = 0;
|
|
|
|
} else {
|
|
|
|
rb->rd += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_apptrace_rb_read_size_get(esp_apptrace_rb_t *rb)
|
|
|
|
{
|
|
|
|
uint32_t size = 0;
|
|
|
|
if (rb->rd <= rb->wr) {
|
|
|
|
// |?R......W??|
|
|
|
|
size = rb->wr - rb->rd;
|
|
|
|
} else {
|
|
|
|
// |?W......R??|
|
|
|
|
size = rb->cur_size - rb->rd;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2017-07-24 12:57:44 -04:00
|
|
|
|
|
|
|
uint32_t esp_apptrace_rb_write_size_get(esp_apptrace_rb_t *rb)
|
|
|
|
{
|
|
|
|
uint32_t size = 0;
|
|
|
|
if (rb->rd <= rb->wr) {
|
|
|
|
// |?R......W??|
|
|
|
|
size = rb->size - rb->wr;
|
|
|
|
if (size && rb->rd == 0) {
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// |?W......R??|
|
|
|
|
size = rb->rd - rb->wr - 1;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|