Skip to content

Commit e989e72

Browse files
authored
Merge pull request #86 from facchinm/analog_fix_heap_corruption
analogReference: fix heap corruption
2 parents 9f84f25 + 96963d8 commit e989e72

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Diff for: cores/arduino/analog.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,15 @@ void analogReference(uint8_t mode) {
545545
ctrl = ADC_VREF_CONTROL_AVCC0_AVSS0; break;
546546
}
547547

548-
R_ADC_Close(&adc.ctrl);
548+
if(adc.ctrl.opened) {
549+
R_ADC_Close(&adc.ctrl);
550+
}
549551
adc.cfg_extend.adc_vref_control = ctrl;
550552
R_ADC_Open(&adc.ctrl, &adc.cfg);
551553

552-
R_ADC_Close(&adc1.ctrl);
554+
if(adc1.ctrl.opened) {
555+
R_ADC_Close(&adc1.ctrl);
556+
}
553557
adc1.cfg_extend.adc_vref_control = ctrl;
554558
R_ADC_Open(&adc1.ctrl, &adc1.cfg);
555559
}
@@ -638,13 +642,17 @@ void analogReadResolution(int bits) {
638642
break;
639643
}
640644

641-
R_ADC_Close(&adc.ctrl);
645+
if(adc.ctrl.opened) {
646+
R_ADC_Close(&adc.ctrl);
647+
}
642648
auto res = R_ADC_Open(&adc.ctrl, &adc.cfg);
643649
if (res != FSP_SUCCESS) {
644650
adc.cfg.resolution = old_read_resolution;
645651
}
646652

647-
R_ADC_Close(&adc1.ctrl);
653+
if(adc1.ctrl.opened) {
654+
R_ADC_Close(&adc1.ctrl);
655+
}
648656
res = R_ADC_Open(&adc1.ctrl, &adc1.cfg);
649657
if (res != FSP_SUCCESS) {
650658
adc1.cfg.resolution = old1_read_resolution;

0 commit comments

Comments
 (0)