What is ADC lock and why it takes so long to read from multiple ADC1 pins?

raitisg
Posts: 1
Joined: Wed May 02, 2018 4:57 pm

What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby raitisg » Wed May 02, 2018 5:05 pm

Why it takes so long to read from multiple ADC pins? It there a way to make it faster?

uint32_t a1 = adc1_get_raw_custom(ADC1_CHANNEL_4);
uint32_t a2 = adc1_get_raw_custom(ADC1_CHANNEL_5);
uint32_t a3 = adc1_get_raw_custom(ADC1_CHANNEL_6);
uint32_t a4 = adc1_get_raw_custom(ADC1_CHANNEL_7);

I got this serial LOG, when using intrnal ADC1

Using BLE, but not WiFi.
D (587967) RTC_MODULE: adc mode takes adc1 lock.
D (587970) RTC_MODULE: returns adc1 lock.
D (587974) RTC_MODULE: adc mode takes adc1 lock.
D (587979) RTC_MODULE: returns adc1 lock.
D (587983) RTC_MODULE: adc mode takes adc1 lock.
D (587988) RTC_MODULE: returns adc1 lock.
D (587992) RTC_MODULE: adc mode takes adc1 lock.
D (587996) RTC_MODULE: returns adc1 lock.
D (588000) RTC_MODULE: adc mode takes adc1 lock.
D (588005) RTC_MODULE: returns adc1 lock.
D (588009) RTC_MODULE: adc mode takes adc1 lock.
D (588014) RTC_MODULE: returns adc1 lock.
D (588018) RTC_MODULE: adc mode takes adc1 lock.
D (588023) RTC_MODULE: returns adc1 lock.
D (588027) RTC_MODULE: adc mode takes adc1 lock.
D (588031) RTC_MODULE: returns adc1 lock.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby kolban » Sun May 06, 2018 1:23 am

I am far from a super scientist in this area ... but here is a thought ... and this may have general applicability and hence if I'm wildly off base, I'd love to be schooled.

Imagine a serial connection at 115200 bits per second. If we imagine a 1 character = 1 byte and to transmit a character over UART we need 9 bits (8 bits of data plus a stop bit). Then at 115200, the maximum number of characters we can transmit is 115200/9 = 12,800 characters per second. Which then becomes 12.8 characters per millisecond. Sounds good.

Now let us think of a log statement such as:

D (587967) RTC_MODULE: adc mode takes adc1 lock.

I count this to be about 48 characters. At 12.8 characters per millisecond, this would then take 3.75 millisecs to write that line. Again, this is pure transmission minimum.

In your log, you are asking a good question ... "Where is my time being spent?" ... however you are asking where are my 4-5 msecs being spent. Is it possible the time you are measuring includes "blocked writes" to the serial subystem and hence when measuring wall-clock times at the 5 msec range your actual reporting technique is skewing the results?

What if you perform your test in a tight loop for 1000 iterations with zero logging and then log the time it takes to perform a 1000 iterations and then calculate the cost for 1 iteration. Do you get the same results as you get just now?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby snahmad75 » Fri Oct 05, 2018 6:08 pm

I am getting this repeat manner

I need to get calculate adc voltage every 5/10 seconds and show on my web page.
when every i calculate it gets this debug logs.

Setup:

Code: Select all

voltage_ = 0;
		//Check TP is burned into eFuse
		if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) != ESP_OK) {
			ACDTRACEF(ES24_ADC_CONV_BASE, "eFuse Two Point : not Supported\n");
			return ES24_ADC_CONV_BASE;
		}
		//Check Vref is burned into eFuse
		if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) != ESP_OK) {
			return ES24_ADC_CONV_BASE;
		}

		//Configure ADC
		if (unit == ADC_UNIT_1) {
			adc1_config_width(ADC_WIDTH_BIT_12);
			adc1_config_channel_atten((adc1_channel_t)channel, atten);
		}
		else {
			adc2_config_channel_atten((adc2_channel_t)channel, atten);
		}

		//Characterize ADC
		__adc_chars = (esp_adc_cal_characteristics_t*)calloc(1, sizeof(esp_adc_cal_characteristics_t));
		esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, __adc_chars);
	

Measure

Code: Select all

//Multisampling
for (int i = 0; i < NO_OF_SAMPLES; i++) {
if (unit == ADC_UNIT_1) {
		adc_reading += adc1_get_raw((adc1_channel_t)channel);
}
else {
int raw;
adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw);
adc_reading += raw;
}
}
adc_reading /= NO_OF_SAMPLES;
//Convert adc_reading to voltage in mV
voltage_ = esp_adc_cal_raw_to_voltage(adc_reading, __adc_chars);
D (29368) RTC_MODULE: returns adc1 lock.
D (29372) RTC_MODULE: adc mode takes adc1 lock.
D (29377) RTC_MODULE: returns adc1 lock.
D (29381) RTC_MODULE: adc mode takes adc1 lock.
D (29385) RTC_MODULE: returns adc1 lock.
D (29389) RTC_MODULE: adc mode takes adc1 lock.
D (29394) RTC_MODULE: returns adc1 lock.
D (29398) RTC_MODULE: adc mode takes adc1 lock.
D (29403) RTC_MODULE: returns adc1 lock.
D (29407) RTC_MODULE: adc mode takes adc1 lock.
D (29411) RTC_MODULE: returns adc1 lock.
D (29415) RTC_MODULE: adc mode takes adc1 lock.
D (29420) RTC_MODULE: returns adc1 lock.
D (29424) RTC_MODULE: adc mode takes adc1 lock.
D (29428) RTC_MODULE: returns adc1 lock.
D (29432) RTC_MODULE: adc mode takes adc1 lock.
D (29437) RTC_MODULE: returns adc1 lock.
D (29441) RTC_MODULE: adc mode takes adc1 lock.
D (29446) RTC_MODULE: returns adc1 lock.
D (29450) RTC_MODULE: adc mode takes adc1 lock.
D (29454) RTC_MODULE: returns adc1 lock.
D (29458) RTC_MODULE: adc mode takes adc1 lock.
D (29463) RTC_MODULE: returns adc1 lock.
D (29467) RTC_MODULE: adc mode takes adc1 lock.
D (29471) RTC_MODULE: returns adc1 lock.

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby snahmad75 » Fri Oct 05, 2018 8:19 pm

Kindly do reply. I are going into production within 1 month time. need to sort this out.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby ESP_Dazz » Tue Oct 09, 2018 3:09 pm

What is the ADC1 lock?
ADC1 lock is a software lock that prevents simultaneous usage of ADC1 between multiple threads. Every time a task calls adc1_get_raw() it must first acquire the lock to ensure it retains exclusive access to ADC1 throughout the conversion process. Likewise, the lock must be released after the conversion process is finished. If the lock is already acquired (i.e. another task is already using ADC1), the task attempting to take the lock will block.

Why does reading ADC1 take so long?
Referring to the log output above, the majority of time is actually spent in outputting the log rather than in the ADC conversion process.
With logs enabled, adc1_get_raw() consumes about 30000 CPU cycles at 160MHz (about 190us). Without the log output, adc1_get_raw() consumes about 6400 CPU cycles at 160MHz (about 40us).

You can suppress the log outputs by changing your log level in menuconfig->component_config->log_output

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby snahmad75 » Tue Oct 09, 2018 8:25 pm

changing your log level in menuconfig->component_config->log_output ???

What log level I should set to.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby ESP_Dazz » Tue Oct 16, 2018 6:08 am

snahmad75 wrote:changing your log level in menuconfig->component_config->log_output ???

What log level I should set to.
The Default Log Verbosity setting works by only compiling logs from the lowest level (Error) up to an including the specified level. For example, selecting Info level will result in allowing Error, Warning, and Info level logs. The ADC lock logs are of debug level (denoted by the 'D'), therefore setting your log level to Info or lower will suppress the ADC lock logs.

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: What is ADC lock and why it takes so long to read from multiple ADC1 pins?

Postby snahmad75 » Tue Oct 16, 2018 8:05 am

ok, good. all working.

Who is online

Users browsing this forum: Bing [Bot] and 131 guests