Merge pull request #9161 from dhalbert/fix-nordic-analogin

nrf: fix regression in AnalogIn, fix problem with reuse as digital pin
This commit is contained in:
Dan Halbert 2024-04-10 09:37:30 -04:00 committed by GitHub
commit 25dd72dd2c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -127,6 +127,14 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// I think I'd like to declare `value` as volatile, but that causes type errors.
asm volatile ("" : : : "memory");
// Disconnect ADC from pin.
nrf_saadc_channel_input_set(NRF_SAADC, CHANNEL_NO, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
// value is signed and might be (slightly) < 0, even on single-ended conversions, so force to 0.
if (value < 0) {
value = 0;
}
// Stretch 14-bit ADC reading to 16-bit range
return (value << 2) | (value >> 12);
}