Skip to content

Commit

Permalink
Merge branch 'feature/adc_continuous_new_data_replace_old_data' into …
Browse files Browse the repository at this point in the history
…'master'

adc: added a flag to replace internal pool data with newest data, when pool is full

Closes IDF-7395

See merge request espressif/esp-idf!23744
  • Loading branch information
Icarus113 committed Jun 1, 2023
2 parents 9340696 + 56c6695 commit 1033322
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
22 changes: 21 additions & 1 deletion components/esp_adc/adc_continuous.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ esp_err_t adc_continuous_new_handle(const adc_continuous_handle_cfg_t *hdl_confi
}

//ringbuffer storage/struct buffer
adc_ctx->ringbuf_size = hdl_config->max_store_buf_size;
adc_ctx->ringbuf_storage = heap_caps_calloc(1, hdl_config->max_store_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
adc_ctx->ringbuf_struct = heap_caps_calloc(1, sizeof(StaticRingbuffer_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (!adc_ctx->ringbuf_storage || !adc_ctx->ringbuf_struct) {
Expand Down Expand Up @@ -233,6 +234,7 @@ esp_err_t adc_continuous_new_handle(const adc_continuous_handle_cfg_t *hdl_confi
};
adc_hal_dma_ctx_config(&adc_ctx->hal, &config);

adc_ctx->flags.flush_pool = hdl_config->flags.flush_pool;
adc_ctx->fsm = ADC_FSM_INIT;
*ret_handle = adc_ctx;

Expand Down Expand Up @@ -312,7 +314,25 @@ static IRAM_ATTR bool s_adc_dma_intr(adc_continuous_ctx_t *adc_digi_ctx)
}

if (ret == pdFALSE) {
//ringbuffer overflow
if (adc_digi_ctx->flags.flush_pool) {
size_t actual_size = 0;
uint8_t *old_data = xRingbufferReceiveUpToFromISR(adc_digi_ctx->ringbuf_hdl, &actual_size, adc_digi_ctx->ringbuf_size);
/**
* Replace by ringbuffer reset API when this API is ready.
* Now we do mannual reset.
* For old_data == NULL condition (equals to the future ringbuffer reset fail condition), we don't care this time data,
* as this only happens when the ringbuffer size is small, new data will be filled in soon.
*/
if (old_data) {
vRingbufferReturnItemFromISR(adc_digi_ctx->ringbuf_hdl, old_data, &taskAwoken);
xRingbufferSendFromISR(adc_digi_ctx->ringbuf_hdl, finished_buffer, finished_size, &taskAwoken);
if (taskAwoken == pdTRUE) {
need_yield |= true;
}
}
}

//ringbuffer overflow happens before
if (adc_digi_ctx->cbs.on_pool_ovf) {
adc_continuous_evt_data_t edata = {};
if (adc_digi_ctx->cbs.on_pool_ovf(adc_digi_ctx, &edata, adc_digi_ctx->user_data)) {
Expand Down
4 changes: 4 additions & 0 deletions components/esp_adc/adc_continuous_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct adc_continuous_ctx_t {
RingbufHandle_t ringbuf_hdl; //RX ringbuffer handler
void* ringbuf_storage; //Ringbuffer storage buffer
void* ringbuf_struct; //Ringbuffer structure buffer
size_t ringbuf_size; //Ringbuffer size
intptr_t rx_eof_desc_addr; //eof descriptor address of RX channel
adc_fsm_t fsm; //ADC continuous mode driver internal states
bool use_adc1; //1: ADC unit1 will be used; 0: ADC unit1 won't be used.
Expand All @@ -94,6 +95,9 @@ struct adc_continuous_ctx_t {
adc_continuous_evt_cbs_t cbs; //Callbacks
void *user_data; //User context
esp_pm_lock_handle_t pm_lock; //For power management
struct {
uint32_t flush_pool: 1; //Flush the internal pool when the pool is full. With this flag, the `on_pool_ovf` event will not happen.
} flags;
#if SOC_ADC_DIG_IIR_FILTER_SUPPORTED
adc_iir_filter_t *iir_filter[SOC_ADC_DIGI_IIR_FILTER_NUM]; //ADC IIR filter context
#endif
Expand Down
3 changes: 3 additions & 0 deletions components/esp_adc/include/esp_adc/adc_continuous.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ typedef struct adc_continuous_ctx_t *adc_continuous_handle_t;
typedef struct {
uint32_t max_store_buf_size; ///< Max length of the conversion Results that driver can store, in bytes.
uint32_t conv_frame_size; ///< Conversion frame size, in bytes. This should be in multiples of `SOC_ADC_DIGI_DATA_BYTES_PER_CONV`.
struct {
uint32_t flush_pool: 1; ///< Flush the internal pool when the pool is full.
} flags; ///< Driver flags
} adc_continuous_handle_cfg_t;

/**
Expand Down
56 changes: 55 additions & 1 deletion components/esp_adc/test_apps/adc/main/test_adc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,62 @@ TEST_CASE("ADC continuous big conv_frame_size test", "[adc_continuous]")
free(result);
}


#define ADC_FLUSH_TEST_SIZE 64

TEST_CASE("ADC continuous flush internal pool", "[adc_continuous][mannual][ignore]")
{
adc_continuous_handle_t handle = NULL;
adc_continuous_handle_cfg_t adc_config = {
.max_store_buf_size = ADC_FLUSH_TEST_SIZE,
.conv_frame_size = ADC_FLUSH_TEST_SIZE,
.flags.flush_pool = true,
};
TEST_ESP_OK(adc_continuous_new_handle(&adc_config, &handle));

adc_continuous_config_t dig_cfg = {
.sample_freq_hz = 50 * 1000,
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
.format = ADC_DRIVER_TEST_OUTPUT_TYPE,
};
adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
adc_pattern[0].atten = ADC_ATTEN_DB_11;
adc_pattern[0].channel = ADC1_TEST_CHAN0;
adc_pattern[0].unit = ADC_UNIT_1;
adc_pattern[0].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
dig_cfg.adc_pattern = adc_pattern;
dig_cfg.pattern_num = 1;
TEST_ESP_OK(adc_continuous_config(handle, &dig_cfg));

uint8_t result[ADC_FLUSH_TEST_SIZE] = {0};
uint32_t ret_num = 0;
TEST_ESP_OK(adc_continuous_start(handle));

while (1) {
TEST_ESP_OK(adc_continuous_read(handle, result, ADC_FLUSH_TEST_SIZE, &ret_num, ADC_MAX_DELAY));
printf("ret_num: 0d%"PRIu32" bytes\n", ret_num);
TEST_ASSERT(ret_num == ADC_FLUSH_TEST_SIZE);

for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) {
adc_digi_output_data_t *p = (void*)&result[i];
#if (SOC_ADC_DIGI_RESULT_BYTES == 2)
printf("ADC1, Channel: %d, Value: %d\n", p->type1.channel, p->type1.data);
#else
printf("ADC1, Channel: %d, Value: %d\n", p->type2.channel, p->type2.data);
#endif
}
/**
* With this delay, check the read out data to be the newest data
*/
vTaskDelay(2000);
}

TEST_ESP_OK(adc_continuous_stop(handle));
TEST_ESP_OK(adc_continuous_deinit(handle));
}

#if SOC_ADC_DIG_IIR_FILTER_SUPPORTED
TEST_CASE("ADC filter exhausted allocation", "[adc_oneshot]")
TEST_CASE("ADC filter exhausted allocation", "[adc_continuous]")
{
adc_continuous_handle_t handle = NULL;
adc_continuous_handle_cfg_t adc_config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static bool IRAM_ATTR NOINLINE_ATTR s_conv_done_cb(adc_continuous_handle_t handl
return false;
}

TEST_CASE("ADC continuous work with ISR and Flash", "[adc_oneshot]")
TEST_CASE("ADC continuous work with ISR and Flash", "[adc_continuous]")
{
adc_continuous_handle_t handle = NULL;
adc_continuous_handle_cfg_t adc_config = {
Expand Down
1 change: 1 addition & 0 deletions docs/en/api-reference/peripherals/adc_continuous.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ To create an ADC continuous mode driver handle, set up the required configuratio

- :cpp:member:`adc_continuous_handle_cfg_t::max_store_buf_size` set the maximum size (in bytes) of the pool that the driver saves ADC conversion result into. If this pool is full, new conversion results will be lost.
- :cpp:member:`adc_continuous_handle_cfg_t::conv_frame_size` set the size of the ADC conversion frame, in bytes.
- :cpp:member:`adc_continuous_handle_cfg_t::flags` set the flags that can change the driver behaviour.


After setting up above configurations for the ADC, call :cpp:func:`adc_continuous_new_handle` with the prepared :cpp:type:`adc_continuous_handle_cfg_t`. This function may fail due to various errors such as invalid argumemts, insufficient memory, etc.
Expand Down

0 comments on commit 1033322

Please sign in to comment.