ADC2 Implementation

d.cook
Posts: 20
Joined: Tue Dec 06, 2016 2:45 pm

ADC2 Implementation

Postby d.cook » Thu Jan 12, 2017 5:10 pm

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
Daniel Cook
Lead developer @ Mysa

hgijeon
Posts: 2
Joined: Mon Jan 16, 2017 2:43 pm

Re: ADC2 Implementation

Postby hgijeon » Mon Jan 16, 2017 2:52 pm

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.

d.cook
Posts: 20
Joined: Tue Dec 06, 2016 2:45 pm

Re: ADC2 Implementation

Postby d.cook » Tue Jan 17, 2017 9:19 pm

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.
Daniel Cook
Lead developer @ Mysa

Who is online

Users browsing this forum: No registered users and 107 guests