Skip to content

Commit 54b66d9

Browse files
authored
Merge pull request #3052 from dhalbert/samd51-voltage-check-delay
delay 1ms on SAMD51 when reading processor voltage
2 parents f1a497e + 1687c8b commit 54b66d9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

ports/atmel-samd/common-hal/microcontroller/Processor.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* POSSIBILITY OF SUCH DAMAGE.
6262
*/
6363

64+
#include "py/mphal.h"
6465
#include "common-hal/microcontroller/Processor.h"
6566

6667
#include "samd/adc.h"
@@ -286,11 +287,15 @@ float common_hal_mcu_processor_get_voltage(void) {
286287

287288
#ifdef SAMD51
288289
hri_supc_set_VREF_SEL_bf(SUPC, SUPC_VREF_SEL_1V0_Val);
289-
// ONDEMAND must be clear, and VREFOE must be set, or else the ADC conversion will not complete.
290-
// See https://community.atmel.com/forum/samd51-using-intref-adc-voltage-reference
291-
hri_supc_clear_VREF_ONDEMAND_bit(SUPC);
292290
hri_supc_set_VREF_VREFOE_bit(SUPC);
291+
293292
adc_sync_set_reference(&adc, ADC_REFCTRL_REFSEL_INTREF_Val);
293+
294+
// On some processor samples, the ADC will hang trying to read the voltage. A simple
295+
// delay after setting the SUPC bits seems to fix things. This appears to be due to VREFOE
296+
// startup time. There is no synchronization bit to check.
297+
// See https://community.atmel.com/forum/samd51-using-intref-adc-voltage-reference
298+
mp_hal_delay_ms(1);
294299
#endif
295300

296301
adc_sync_set_resolution(&adc, ADC_CTRLB_RESSEL_12BIT_Val);

0 commit comments

Comments
 (0)