Skip to content

Commit fd1e9cf

Browse files
committed
1. Changed attenuation from ADC_ATTEN_DB_0 to ADC_ATTEN_DB_11
2. Scaling the 12-bit values received from DMA engine to 16-bit value
1 parent 3871501 commit fd1e9cf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

ports/espressif/common-hal/analogbufio/BufferedIn.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
#define NUM_SAMPLES_PER_INTERRUPT 256
4747
#define NUM_ADC_CHANNELS 1
4848
#define DMA_BUFFER_SIZE 1024
49-
#define ATTENUATION ADC_ATTEN_DB_0
49+
#define ATTENUATION ADC_ATTEN_DB_11
5050
#define ADC_READ_TIMEOUT_MS 2000
51+
#define ADC_PIN_MAX_VALUE 0xfff
5152

5253
#if defined(CONFIG_IDF_TARGET_ESP32)
5354
#define ADC_RESULT_BYTE 2
@@ -239,6 +240,7 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
239240
uint32_t captured_bytes = 0;
240241
esp_err_t ret;
241242
uint32_t ret_num = 0;
243+
uint32_t adc_reading = 0;
242244
adc_digi_convert_mode_t convert_mode = ADC_CONV_SINGLE_UNIT_2;
243245
adc_digi_output_format_t output_format = ADC_DIGI_OUTPUT_FORMAT_TYPE1;
244246

@@ -264,11 +266,13 @@ uint32_t common_hal_analogbufio_bufferedin_readinto(analogbufio_bufferedin_obj_t
264266
uint16_t *pBuffer = (uint16_t *)(void *)&buffer[captured_bytes];
265267
if (output_format == ADC_DIGI_OUTPUT_FORMAT_TYPE1) {
266268
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
267-
*pBuffer = pResult->type1.data;
269+
adc_reading = pResult->type1.data;
268270
#endif
269271
} else {
270-
*pBuffer = pResult->type2.data;
272+
adc_reading = pResult->type2.data;
271273
}
274+
adc_reading = adc_reading * ((1 << 16) - 1) / ADC_PIN_MAX_VALUE;
275+
*pBuffer = (uint16_t)adc_reading;
272276
captured_bytes += sizeof(uint16_t);
273277
captured_samples++;
274278
} else {

0 commit comments

Comments
 (0)