Skip to content

Commit 453c82c

Browse files
committed
nrf/common-hal/analogio/AnalogIn.c: fix apparent gcc13 issue
1 parent b858170 commit 453c82c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ 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+
// Adding the "asm volatile" memory fence here or many places after this declaration
80+
// fixes an issue with gcc13 which causes `value` to always be zero.
81+
// Compiling with gcc10 or gcc12 is fine.
82+
// It can also be fixed by declaring `value` to be static.
83+
// I think I'd like to declare this as volatile, but that causes type errors.
84+
nrf_saadc_value_t value = 0;
85+
asm volatile ("" : : : "memory");
8086

8187
const nrf_saadc_channel_config_t config = {
8288
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
@@ -120,10 +126,6 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
120126

121127
nrf_saadc_disable(NRF_SAADC);
122128

123-
if (value < 0) {
124-
value = 0;
125-
}
126-
127129
// Stretch 14-bit ADC reading to 16-bit range
128130
return (value << 2) | (value >> 12);
129131
}

0 commit comments

Comments
 (0)