Skip to content

fix analogReference() - #643

Open
wke67 wants to merge 1 commit into
SpenceKonde:masterfrom
wke67:analogRead
Open

fix analogReference()#643
wke67 wants to merge 1 commit into
SpenceKonde:masterfrom
wke67:analogRead

Conversation

@wke67

@wke67 wke67 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

this fixes analogReference(), issues #639 #626.

analogReference() did not reflect the last changes to INTERNAL2V5 etc. defines. Checking the mode bits is already done by check_valid_analog_ref(), no need to check twice!

…NAL.. defines. Checking of mode bits is already done by check_valid_analog_ref(), no need to check twice.
Comment on lines +942 to +943
check_valid_analog_ref(mode); // does all the checking
VREF.ADC0REF = (VREF.ADC0REF & ~(VREF_REFSEL_gm))|(mode & (VREF_REFSEL_gm));

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants