2021-05-03 02:15:17 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-03-22 03:07:37 +00:00
|
|
|
//
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_app_trace_util.h"
|
2019-06-06 02:57:29 +00:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-03-18 07:46:15 +00:00
|
|
|
#include "esp32/clk.h"
|
2020-01-17 03:47:08 +00:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/clk.h"
|
2020-07-29 05:13:51 +00:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/clk.h"
|
2020-11-26 08:56:13 +00:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/clk.h"
|
2019-06-06 02:57:29 +00:00
|
|
|
#endif
|
2017-03-22 03:07:37 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-07-24 16:57:44 +00:00
|
|
|
///////////////////////////////// TIMEOUT /////////////////////////////////////
|
2017-03-22 03:07:37 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-09-22 15:09:16 +00:00
|
|
|
#define ESP_APPTRACE_CPUTICKS2US(_t_, _cpu_freq_) ((_t_)/(_cpu_freq_/1000000))
|
|
|
|
#define ESP_APPTRACE_US2CPUTICKS(_t_, _cpu_freq_) ((_t_)*(_cpu_freq_/1000000))
|
2017-03-22 03:07:37 +00:00
|
|
|
|
|
|
|
esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo)
|
|
|
|
{
|
2017-09-22 15:09:16 +00:00
|
|
|
int cpu_freq = esp_clk_cpu_freq();
|
2017-07-24 16:57:44 +00:00
|
|
|
if (tmo->tmo != ESP_APPTRACE_TMO_INFINITE) {
|
|
|
|
unsigned cur = portGET_RUN_TIME_COUNTER_VALUE();
|
2017-03-22 03:07:37 +00:00
|
|
|
if (tmo->start <= cur) {
|
2017-09-22 15:09:16 +00:00
|
|
|
tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(cur - tmo->start, cpu_freq);
|
2017-03-22 03:07:37 +00:00
|
|
|
} else {
|
2017-09-22 15:09:16 +00:00
|
|
|
tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(0xFFFFFFFF - tmo->start + cur, cpu_freq);
|
2017-03-22 03:07:37 +00:00
|
|
|
}
|
2017-07-24 16:57:44 +00:00
|
|
|
if (tmo->elapsed >= tmo->tmo) {
|
2017-03-22 03:07:37 +00:00
|
|
|
return ESP_ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:57:44 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////// LOCK ////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
esp_err_t esp_apptrace_lock_take(esp_apptrace_lock_t *lock, esp_apptrace_tmo_t *tmo)
|
2017-03-22 03:07:37 +00:00
|
|
|
{
|
2017-09-03 00:44:21 +00:00
|
|
|
int res;
|
2017-03-22 03:07:37 +00:00
|
|
|
|
2017-09-03 00:44:21 +00:00
|
|
|
while (1) {
|
|
|
|
// do not overwrite lock->int_state before we actually acquired the mux
|
|
|
|
unsigned int_state = portENTER_CRITICAL_NESTED();
|
|
|
|
// FIXME: if mux is busy it is not good idea to loop during the whole tmo with disabled IRQs.
|
|
|
|
// So we check mux state using zero tmo, restore IRQs and let others tasks/IRQs to run on this CPU
|
|
|
|
// while we are doing our own tmo check.
|
2017-10-13 00:33:57 +00:00
|
|
|
#ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
|
|
|
|
bool success = vPortCPUAcquireMutexTimeout(&lock->mux, 0, __FUNCTION__, __LINE__);
|
|
|
|
#else
|
2017-09-03 00:44:21 +00:00
|
|
|
bool success = vPortCPUAcquireMutexTimeout(&lock->mux, 0);
|
2017-10-13 00:33:57 +00:00
|
|
|
#endif
|
2017-09-03 00:44:21 +00:00
|
|
|
if (success) {
|
|
|
|
lock->int_state = int_state;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL_NESTED(int_state);
|
|
|
|
// we can be preempted from this place till the next call (above) to portENTER_CRITICAL_NESTED()
|
|
|
|
res = esp_apptrace_tmo_check(tmo);
|
|
|
|
if (res != ESP_OK) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-22 03:07:37 +00:00
|
|
|
}
|
2017-09-03 00:44:21 +00:00
|
|
|
return res;
|
2017-03-22 03:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_apptrace_lock_give(esp_apptrace_lock_t *lock)
|
|
|
|
{
|
2017-09-03 00:44:21 +00:00
|
|
|
// save lock's irq state value for this CPU
|
|
|
|
unsigned int_state = lock->int_state;
|
|
|
|
// after call to the following func we can not be sure that lock->int_state
|
|
|
|
// is not overwritten by other CPU who has acquired the mux just after we released it. See esp_apptrace_lock_take().
|
2017-10-13 00:33:57 +00:00
|
|
|
#ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
|
|
|
|
vPortCPUReleaseMutex(&lock->mux, __FUNCTION__, __LINE__);
|
|
|
|
#else
|
2017-07-20 06:34:45 +00:00
|
|
|
vPortCPUReleaseMutex(&lock->mux);
|
2017-10-13 00:33:57 +00:00
|
|
|
#endif
|
2017-09-03 00:44:21 +00:00
|
|
|
portEXIT_CRITICAL_NESTED(int_state);
|
2017-07-20 06:34:45 +00:00
|
|
|
return ESP_OK;
|
2017-03-22 03:07:37 +00: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 16:57:44 +00: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;
|
|
|
|
}
|