Page 1 of 1

ADC2 Implementation

Posted: Thu Jan 12, 2017 5:10 pm
by d.cook
I'm trying to write a simple implementation for the ADC2 peripheral - it doesn't seem to be supported by the SDK yet and our electrical design makes it easier to use ADC2 pins rather than ADC1.

I'd had some luck getting something similar working for ADC1 prior to the release of the driver in the SDK, however similar code for ADC2 doesn't seem to work.

EDIT: 'doesn't seem to work' meaning that it always returns zero.

Code: Select all

void adc2_init(bool invert) {
    // Invert bits
    if(invert) {
        REG_SET_BIT(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
    }

    // Clear attenuation bits for all channels (atten = 0db)
    CLEAR_PERI_REG_MASK(SENS_SAR_ATTEN2_REG, SENS_SAR2_ATTEN);
}

uint16_t adc2_get_voltage(const int channel) {
    // Enable ADC2 input
    REG_SET_BIT(RTC_IO_ADC_PAD_REG, RTC_IO_ADC2_FUN_IE);
    // Set ADC2 to function 1 (assuming functions are 0-indexed). See Table 18 in tech. ref.
    REG_SET_BITS(RTC_IO_ADC_PAD_REG, RTC_IO_ADC2_FUN_SEL_M, 0x1 << (RTC_IO_ADC2_FUN_SEL_S));

    // Enable channel
    REG_SET_BIT(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE);
    REG_SET_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_M, 0x1 << (SENS_SAR2_EN_PAD_S + channel));

    // Start ADC conversion
    REG_SET_BIT(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE);
    REG_SET_BIT(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR);
    // Block until complete
    while(!REG_GET_BIT(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR));

    uint16_t out = REG_READ(SENS_SAR_MEAS_START2_REG) & SENS_MEAS2_DATA_SAR_M;

    // Clear start bits
    REG_CLR_BIT(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE);
    REG_CLR_BIT(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR);

    // Clear pin mask
    REG_CLR_BIT(SENS_SAR_MEAS_START2_REG, (SENS_SAR2_EN_PAD_S + channel));

    return out;
}
I believe I have the process correct based on what I can see in the register definition headers. Can anyone familiar with this spot something I've missed?


Thanks

Re: ADC2 Implementation

Posted: Mon Jan 16, 2017 2:52 pm
by hgijeon
I think reading some arduino-core code might be helpful.
if statement of __analogRead() in https://github.com/espressif/arduino-es ... -hal-adc.c
and __analogInit() could solve the problem.

Re: ADC2 Implementation

Posted: Tue Jan 17, 2017 9:19 pm
by d.cook
Thanks! I didn't realize the Arduino libraries had implemented it. I'll take a look at it when I get back to this issue in our code.