8000 Fix always zero AnalogIn on nordic by dhalbert · Pull Request #9114 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Fix always zero AnalogIn on nordic #9114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2024
Merged
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
11 changes: 7 additions & 4 deletions ports/nrf/common-hal/analogio/AnalogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// Something else might have used the ADC in a different way,
// so we completely re-initialize it.

nrf_saadc_value_t value = -1;
nrf_saadc_value_t value = 0;

const nrf_saadc_channel_config_t config = {
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
Expand Down Expand Up @@ -120,9 +120,12 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {

nrf_saadc_disable(NRF_SAADC);

if (value < 0) {
value = 0;
}
// Adding the "asm volatile" memory fence here or anywhere after the declaration of `value`
// fixes an issue with gcc13 which causes `value` to always be zero.
// Compiling with gcc10 or gcc12 is fine.
// It can also be fixed by declaring `value` to be static.
// I think I'd like to declare `value` as volatile, but that causes type errors.
asm volatile ("" : : : "memory");

// Stretch 14-bit ADC reading to 16-bit range
return (value << 2) | (value >> 12);
Expand Down
0