Skip to content

Commit

Permalink
Merge pull request #9114 from dhalbert/fix-nordic-analogin
Browse files Browse the repository at this point in the history
Fix always zero AnalogIn on nordic
  • Loading branch information
tannewt authored Apr 1, 2024
2 parents 495394b + 2f6f882 commit 0ef433b
Showing 1 changed file with 7 additions and 4 deletions.
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 comments on commit 0ef433b

Please sign in to comment.