register configuration for ADC-1
Posted: Tue Jun 19, 2018 11:10 am
Hi
Currently I'm trying to configure the ADC1 peripheral of the esp32. I notice that the most recent reference manual is not very consistent regarding ADC.
According to the documentation, I think I have set the registers correctly, but I get strange results.
For the configuration I also looked at the ESP-IDF framework, but there, some registers are configured which are not described in the reference manual at all (such as for example)
Maybe someone could verify my configuration ?
This is what I have so far:
set bit_width:
set attenuation
setup gpio / RTC-IO MUX - for channel 0 (gpio 36)
read ADC value
Currently I'm trying to configure the ADC1 peripheral of the esp32. I notice that the most recent reference manual is not very consistent regarding ADC.
According to the documentation, I think I have set the registers correctly, but I get strange results.
For the configuration I also looked at the ESP-IDF framework, but there, some registers are configured which are not described in the reference manual at all (such as
Code: Select all
SENS.sar_slave_addr1.meas_status
Maybe someone could verify my configuration ?
This is what I have so far:
set bit_width:
Code: Select all
SENS_SAR_START_FORCE_REG &= ~(SENS_SAR1_BIT_WIDTH_MASK);
SENS_SAR_START_FORCE_REG |= (bit_width << SENS_SAR1_BIT_WIDTH);
SENS_SAR_READ_CTRL_REG &= ~(SENS_SAR1_SAMPLE_BIT_MASK);
SENS_SAR_READ_CTRL_REG |= (bit_width << SENS_SAR1_SAMPLE_BIT);
Code: Select all
SENS_SAR_ATTEN1_REG &= ~(SENS_SAR_ATTEN1_MASK(channel*2));
SENS_SAR_ATTEN1_REG |= (attenuation << (channel*2));
Code: Select all
uint32_t rtc_gpio_num = 0;
// rtc_gpio_init (gpio 36)
RTCIO_SENSOR_PADS_REG |= (uint32_t)(1<<RTCIO_SENSOR_SENSE1_MUX_SEL); // gpio connected to analog RTC
RTCIO_SENSOR_PADS_REG &= ~(uint32_t)RTCIO_SENSOR_SENSE1_FUN_SEL_MASK;// always function 0
// rtc_gpio_output_disable
RTCIO_RTC_GPIO_ENABLE_W1TS_REG &= ~((uint32_t)(1 << (rtc_gpio_num + RTCIO_RTC_GPIO_ENABLE_W1TS)));
RTCIO_RTC_GPIO_ENABLE_W1TC_REG |= (uint32_t)(1 << (rtc_gpio_num + RTCIO_RTC_GPIO_ENABLE_W1TC));
// rtc_gpio_input_disable
RTCIO_SENSOR_PADS_REG &= ~(uint32_t)(1 << RTCIO_SENSOR_SENSE1_FUN_IE);
Code: Select all
uint16_t adc1_read_raw(adc1_channel_t channel)
{
// disable HALL sensor
RTCIO_HALL_SENS_REG &= ~((uint32_t)(1<<RTCIO_HALL_XPD_HALL));
// set ADC-CTRL-RTC controller
SENS_SAR_MEAS_START1_REG |= (uint32_t)(1<<SENS_MEAS1_START_FORCE);
SENS_SAR_MEAS_START1_REG |= (uint32_t)(1<<SENS_SAR1_EN_PAD_FORCE);
SENS_SAR_READ_CTRL_REG &= ~((uint32_t)(1<<SENS_SAR1_DIG_FORCE));
SENS_SAR_TOUCH_CTRL1_REG |= (uint32_t)(1<<SENS_XPD_HALL_FORCE);
SENS_SAR_TOUCH_CTRL1_REG |= (uint32_t)(1<<SENS_HALL_PHASE_FORCE);
// set channel in SAR_MEAS_START1
SENS_SAR_MEAS_START1_REG &= ~(SENS_SAR1_EN_PAD_MASK);
SENS_SAR_MEAS_START1_REG |= (uint32_t)(1<<(SENS_SAR1_EN_PAD + channel));
// start
SENS_SAR_MEAS_START1_REG &= ~(uint32_t)(1<<SENS_MEAS1_START_SAR);
SENS_SAR_MEAS_START1_REG |= (uint32_t)(1<<SENS_MEAS1_START_SAR);
// wait until done
while ( (SENS_SAR_MEAS_START1_REG & (uint32_t)(1 << SENS_MEAS1_DONE_SAR)) == 0);
// return value
return SENS_SAR_MEAS_START1_REG & SENS_MEAS1_DATA_SAR_MASK;
}