mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-11-03 22:08:28 +00:00 
			
		
		
		
	adc: add pm_lock to adc driver on esp32c3
This commit is contained in:
		@@ -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);
 | 
				
			||||||
@@ -272,7 +281,7 @@ static IRAM_ATTR bool adc_dma_intr(adc_digi_context_t *adc_digi_ctx)
 | 
				
			|||||||
        adc_hal_digi_rxdma_start(&adc_digi_ctx->hal, adc_digi_ctx->rx_dma_buf, adc_digi_ctx->bytes_between_intr);
 | 
					        adc_hal_digi_rxdma_start(&adc_digi_ctx->hal, adc_digi_ctx->rx_dma_buf, adc_digi_ctx->bytes_between_intr);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(taskAwoken == pdTRUE) {
 | 
					    if (taskAwoken == pdTRUE) {
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
@@ -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
 | 
				
			||||||
@@ -390,11 +409,17 @@ esp_err_t adc_digi_deinitialize(void)
 | 
				
			|||||||
        return ESP_ERR_INVALID_STATE;
 | 
					        return ESP_ERR_INVALID_STATE;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(s_adc_digi_ctx->ringbuf_hdl) {
 | 
					    if (s_adc_digi_ctx->ringbuf_hdl) {
 | 
				
			||||||
        vRingbufferDelete(s_adc_digi_ctx->ringbuf_hdl);
 | 
					        vRingbufferDelete(s_adc_digi_ctx->ringbuf_hdl);
 | 
				
			||||||
        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);
 | 
				
			||||||
@@ -527,7 +552,7 @@ esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
 | 
				
			|||||||
    s_adc_digi_ctx->use_adc1 = 0;
 | 
					    s_adc_digi_ctx->use_adc1 = 0;
 | 
				
			||||||
    s_adc_digi_ctx->use_adc2 = 0;
 | 
					    s_adc_digi_ctx->use_adc2 = 0;
 | 
				
			||||||
    for (int i = 0; i < config->adc_pattern_len; i++) {
 | 
					    for (int i = 0; i < config->adc_pattern_len; i++) {
 | 
				
			||||||
        const adc_digi_pattern_table_t* pat = &config->adc_pattern[i];
 | 
					        const adc_digi_pattern_table_t *pat = &config->adc_pattern[i];
 | 
				
			||||||
        if (pat->unit == ADC_NUM_1) {
 | 
					        if (pat->unit == ADC_NUM_1) {
 | 
				
			||||||
            s_adc_digi_ctx->use_adc1 = 1;
 | 
					            s_adc_digi_ctx->use_adc1 = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user