Hello all
In order to trigger ADC conversions, I manually configured everything needed to do an ADC conversion, and this works fine, until I try to use an ADC interrupt to read the conversion result.
I prepared a " Minimal, Reproducible Example" (attached) and included a few code snippets from it inline.
This works fine (code from setup() - I don't use loop() ): I start a conversion, wait until ready, read the result and print it to the serial monitor.
// configure generic clock, port, ADC, ...
configureADC();
// do an ADC reading without use of interrupts, to verify it's working
REG_ADC_SWTRIG = ADC_SWTRIG_START;
while ( (ADC->INTFLAG.bit.RESRDY == 0) || (ADC->STATUS.bit.SYNCBUSY == 1) );
Serial.println( REG_ADC_RESULT ); // this resets the 'conversion ready' flag as well
BUT when I enable interrupts and start a next conversion, not waiting for the conversion result but enabling an interrupt handler to be called when the conversion is ready, the system hangs.
This is the remainder of the code, in setup() :
// enable interrupts for ADC
NVIC_EnableIRQ( ADC_IRQn );
REG_ADC_INTENSET = ADC_INTENSET_RESRDY; // enable interrupt on conversion ready
// trigger a second ADC reading (should trigger interrupt handler)
REG_ADC_SWTRIG = ADC_SWTRIG_START;
// show that program flow passes here
Serial.println( "I'm still alive" );
The interrupt handler:
void ADC_handler() {
digitalWrite( 13, HIGH ); // show that program flow passes here
uint16_t result = REG_ADC_RESULT; // this also resets interrupt flag
REG_ADC_INTFLAG = ADC_INTFLAG_RESRDY; // reset interrupt flag (in fact not needed if result register read)
}
The first thing the interrupt handler should do is switching on the built in led, which doesn't happen. This tells me that this handler is never called.
But program execution does not continue either, because also the text "I'm still alive" is never printed.
If I don't enable interrupts OR I don't start the second ADC conversion, the system nicely prints the "I'm still alive" message.
Despite days of trying, and in-depth study of the SAMD21datasheet, I do not succeed in making this work, although I seem to understand what needs to be done to make this work.
So, what am I missing ?
Just to be clear (you got that of course), I'm not using the analogRead(...) function at all.
I would be grateful if anyone knowing more than I do, could help me out here.
Many thanks in advance
test adc.ino (4.7 KB)