8000 Merge pull request #9114 from dhalbert/fix-nordic-analogin · squix78/circuitpython@0ef433b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ef433b

Browse files
authored
10000
Merge pull request adafruit#9114 from dhalbert/fix-nordic-analogin
Fix always zero AnalogIn on nordic
2 parents 495394b + 2f6f882 commit 0ef433b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

ports/nrf/common-hal/analogio/AnalogIn.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
7676
// Something else might have used the ADC in a different way,
7777
// so we completely re-initialize it.
7878

79-
nrf_saadc_value_t value = -1;
79+
nrf_saadc_value_t value = 0;
8080

8181
const nrf_saadc_channel_config_t config = {
8282
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
@@ -120,9 +120,12 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
120120

121121
nrf_saadc_disable(NRF_SAADC);
122122

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

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

0 commit comments

Comments
 (0)
0