Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions megaavr/cores/dxcore/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/


#include "avr/io.h"
#include "wiring_private.h"
#include "pins_arduino.h"
#include "Arduino.h"
Expand Down Expand Up @@ -390,12 +391,8 @@ inline __attribute__((always_inline)) void check_valid_resolution(uint8_t res) {

/* Ex Version*/
void analogReference(uint8_t mode) {
check_valid_analog_ref(mode);
if (mode > 7)
mode &= 7;
if (mode != 1 && mode != 3) {
ADC0.CTRLC = mode;
}
check_valid_analog_ref(mode); // does all the checking
ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | (mode & (ADC_REFSEL_gm));
}
/* Ex Version*/
inline uint8_t getAnalogReference() {
Expand Down Expand Up @@ -699,16 +696,8 @@ inline __attribute__((always_inline)) void check_valid_resolution(uint8_t res) {
return _analog_options & 0x0F;
}
void analogReference(uint8_t mode) {
check_valid_analog_ref(mode);
#if defined(STRICT_ERROR_CHECKING)
if (mode > 7) return;
#else
mode &= 7;
#endif
if (mode != 1 && mode != 3) {
ADC0.CTRLC = mode;
}
// Uh? Is that it? That was, ah, a tiny bit simpler.
check_valid_analog_ref(mode); // does all the checking
ADC0.CTRLC = (ADC0.CTRLC & ~(ADC_REFSEL_gm)) | (mode & (ADC_REFSEL_gm));
}
inline uint8_t getAnalogReference() {
uint8_t r = ADC0.CTRLC & ADC_REFSEL_gm;
Expand Down Expand Up @@ -950,10 +939,8 @@ inline __attribute__((always_inline)) void check_valid_resolution(uint8_t res) {
######## ## ## ## ######## ######## ## ######## ######## */

void analogReference(uint8_t mode) {
check_valid_analog_ref(mode);
if (mode < 7 && mode != 4) {
VREF.ADC0REF = (VREF.ADC0REF & ~(VREF_REFSEL_gm))|(mode);
}
check_valid_analog_ref(mode); // does all the checking
VREF.ADC0REF = (VREF.ADC0REF & ~(VREF_REFSEL_gm))|(mode & (VREF_REFSEL_gm));
Comment on lines +942 to +943

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems check_valid_analog_ref() checks only at compile time.
Isn't it better to keep runtime checking? E.g.:

Suggested change
check_valid_analog_ref(mode); // does all the checking
VREF.ADC0REF = (VREF.ADC0REF & ~(VREF_REFSEL_gm))|(mode & (VREF_REFSEL_gm));
check_valid_analog_ref(mode);
mode &= VREF_REFSEL_gm;
if (mode < 7 && mode != 4) {
VREF.ADC0REF = (VREF.ADC0REF & ~(VREF_REFSEL_gm))|(mode);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, runtime checking is a good thing - but only if you can return an error code, which you can not do here.
The runtime checking is done partially by masking the mode value with VREF_REFSEL_gm, I think that is good enough. BTW, the hard part in debugging this error was to understand why the function did not modify the register, only the source code could tell.

}

int16_t analogRead(uint8_t pin) {
Expand Down
Loading