adc: add pm_lock to adc driver on esp32c3

This commit is contained in:
Armando 2021-03-10 12:01:00 +08:00
parent 271b84f5b6
commit 1509264f2e
3 changed files with 31 additions and 6 deletions

View File

@ -19,6 +19,7 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "esp_intr_alloc.h" #include "esp_intr_alloc.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_pm.h"
#include "sys/lock.h" #include "sys/lock.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
@ -96,6 +97,7 @@ typedef struct adc_digi_context_t {
adc_atten_t adc1_atten; //Attenuation for ADC1. On this chip each ADC can only support one attenuation. adc_atten_t adc1_atten; //Attenuation for ADC1. On this chip each ADC can only support one attenuation.
adc_atten_t adc2_atten; //Attenuation for ADC2. On this chip each ADC can only support one attenuation. adc_atten_t adc2_atten; //Attenuation for ADC2. On this chip each ADC can only support one attenuation.
adc_digi_config_t digi_controller_config; //Digital Controller Configuration adc_digi_config_t digi_controller_config; //Digital Controller Configuration
esp_pm_lock_handle_t pm_lock; //For power management
} adc_digi_context_t; } adc_digi_context_t;
static adc_digi_context_t *s_adc_digi_ctx = NULL; static adc_digi_context_t *s_adc_digi_ctx = NULL;
@ -179,6 +181,13 @@ esp_err_t adc_digi_initialize(const adc_digi_init_config_t *init_config)
goto cleanup; goto cleanup;
} }
#if CONFIG_PM_ENABLE
ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &s_adc_digi_ctx->pm_lock);
if (ret != ESP_OK) {
goto cleanup;
}
#endif //CONFIG_PM_ENABLE
//init gpio pins //init gpio pins
if (init_config->adc1_chan_mask) { if (init_config->adc1_chan_mask) {
ret = adc_digi_gpio_init(ADC_NUM_1, init_config->adc1_chan_mask); ret = adc_digi_gpio_init(ADC_NUM_1, init_config->adc1_chan_mask);
@ -295,6 +304,11 @@ esp_err_t adc_digi_start(void)
} }
ADC_DIGI_LOCK_ACQUIRE(); ADC_DIGI_LOCK_ACQUIRE();
#if CONFIG_PM_ENABLE
// Lock APB frequency while ADC driver is in use
esp_pm_lock_acquire(s_adc_digi_ctx->pm_lock);
#endif
adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT(); adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
if (s_adc_digi_ctx->use_adc1) { if (s_adc_digi_ctx->use_adc1) {
uint32_t cal_val = adc_get_calibration_offset(ADC_NUM_1, ADC_CHANNEL_MAX, s_adc_digi_ctx->adc1_atten); uint32_t cal_val = adc_get_calibration_offset(ADC_NUM_1, ADC_CHANNEL_MAX, s_adc_digi_ctx->adc1_atten);
@ -337,6 +351,11 @@ esp_err_t adc_digi_stop(void)
//stop DMA //stop DMA
adc_hal_digi_rxdma_stop(&s_adc_digi_ctx->hal); adc_hal_digi_rxdma_stop(&s_adc_digi_ctx->hal);
adc_hal_digi_deinit(); adc_hal_digi_deinit();
#if CONFIG_PM_ENABLE
if (s_adc_digi_ctx->pm_lock) {
esp_pm_lock_release(s_adc_digi_ctx->pm_lock);
}
#endif //CONFIG_PM_ENABLE
ADC_DIGI_LOCK_RELEASE(); ADC_DIGI_LOCK_RELEASE();
//When using SARADC2 module, this task needs to be protected from WIFI //When using SARADC2 module, this task needs to be protected from WIFI
@ -395,6 +414,12 @@ esp_err_t adc_digi_deinitialize(void)
s_adc_digi_ctx->ringbuf_hdl = NULL; s_adc_digi_ctx->ringbuf_hdl = NULL;
} }
#if CONFIG_PM_ENABLE
if (s_adc_digi_ctx->pm_lock) {
esp_pm_lock_delete(s_adc_digi_ctx->pm_lock);
}
#endif //CONFIG_PM_ENABLE
free(s_adc_digi_ctx->rx_dma_buf); free(s_adc_digi_ctx->rx_dma_buf);
free(s_adc_digi_ctx->hal.rx_desc); free(s_adc_digi_ctx->hal.rx_desc);
free(s_adc_digi_ctx->digi_controller_config.adc_pattern); free(s_adc_digi_ctx->digi_controller_config.adc_pattern);

View File

@ -495,7 +495,7 @@ esp_err_t adc_digi_controller_config(const adc_digi_config_t *config);
/** /**
* @brief Initialize the Digital ADC. * @brief Initialize the Digital ADC.
* *
* @param init_config Pointer to Digital ADC initilisation config. Refer to ``adc_digi_init_config_t``. * @param init_config Pointer to Digital ADC initilization config. Refer to ``adc_digi_init_config_t``.
* *
* @return * @return
* - ESP_ERR_INVALID_ARG If the combination of arguments is invalid. * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid.

View File

@ -393,8 +393,8 @@ esp_err_t adc_hal_convert(adc_ll_num_t adc_n, int channel, int *out_raw)
if ((int)adc_ll_rtc_analysis_raw_data(adc_n, (uint16_t)(*out_raw))) { if ((int)adc_ll_rtc_analysis_raw_data(adc_n, (uint16_t)(*out_raw))) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} else { }
return ESP_OK; return ESP_OK;
} }
}
#endif //#if !CONFIG_IDF_TARGET_ESP32C3 #endif //#if !CONFIG_IDF_TARGET_ESP32C3