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;
}
Thanks