From e5af121553f166081f12bb63f187b79751cb5a9a Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 9 May 2023 15:04:45 +0200 Subject: [PATCH] nrf: refactor ADC code a little bit * Initialize the ADC in Configure() (instead of in Get()). * Do not set all channels to "not connected" - that's already the reset value. * Don't disable the ADC after use. It's not necessary to disable (current consumption appears to remain the same whether enabled or disabled). --- src/machine/machine_nrf52xxx.go | 36 ++++++++++++++------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/machine/machine_nrf52xxx.go b/src/machine/machine_nrf52xxx.go index 259f0503..e0d9d152 100644 --- a/src/machine/machine_nrf52xxx.go +++ b/src/machine/machine_nrf52xxx.go @@ -19,7 +19,21 @@ func InitADC() { // Configure configures an ADC pin to be able to read analog data. func (a ADC) Configure(ADCConfig) { - return // no pin specific setup on nrf52 machine. + // Enable ADC. + // The ADC does not consume a noticeable amount of current simply by being + // enabled. + nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos) + + // Configure ADC. + nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit) + + // Configure channel 0, which is the only channel we use. + nrf.SAADC.CH[0].CONFIG.Set(nrf.SAADC_CH_CONFIG_RESP_Bypass<