Browse Source

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).
pull/3718/head
Ayke van Laethem 2 years ago
committed by Ron Evans
parent
commit
e5af121553
  1. 36
      src/machine/machine_nrf52xxx.go

36
src/machine/machine_nrf52xxx.go

@ -19,7 +19,21 @@ func InitADC() {
// Configure configures an ADC pin to be able to read analog data. // Configure configures an ADC pin to be able to read analog data.
func (a ADC) Configure(ADCConfig) { 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<<nrf.SAADC_CH_CONFIG_RESP_Pos |
nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESN_Pos |
nrf.SAADC_CH_CONFIG_GAIN_Gain1_5<<nrf.SAADC_CH_CONFIG_GAIN_Pos |
nrf.SAADC_CH_CONFIG_REFSEL_Internal<<nrf.SAADC_CH_CONFIG_REFSEL_Pos |
nrf.SAADC_CH_CONFIG_TACQ_3us<<nrf.SAADC_CH_CONFIG_TACQ_Pos |
nrf.SAADC_CH_CONFIG_MODE_SE<<nrf.SAADC_CH_CONFIG_MODE_Pos)
} }
// Get returns the current value of a ADC pin in the range 0..0xffff. // Get returns the current value of a ADC pin in the range 0..0xffff.
@ -56,23 +70,6 @@ func (a ADC) Get() uint16 {
return 0 return 0
} }
nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit)
// Enable ADC.
nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos)
for i := 0; i < 8; i++ {
nrf.SAADC.CH[i].PSELN.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
nrf.SAADC.CH[i].PSELP.Set(nrf.SAADC_CH_PSELP_PSELP_NC)
}
// Configure ADC.
nrf.SAADC.CH[0].CONFIG.Set(((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESP_Pos) & nrf.SAADC_CH_CONFIG_RESP_Msk) |
((nrf.SAADC_CH_CONFIG_RESP_Bypass << nrf.SAADC_CH_CONFIG_RESN_Pos) & nrf.SAADC_CH_CONFIG_RESN_Msk) |
((nrf.SAADC_CH_CONFIG_GAIN_Gain1_5 << nrf.SAADC_CH_CONFIG_GAIN_Pos) & nrf.SAADC_CH_CONFIG_GAIN_Msk) |
((nrf.SAADC_CH_CONFIG_REFSEL_Internal << nrf.SAADC_CH_CONFIG_REFSEL_Pos) & nrf.SAADC_CH_CONFIG_REFSEL_Msk) |
((nrf.SAADC_CH_CONFIG_TACQ_3us << nrf.SAADC_CH_CONFIG_TACQ_Pos) & nrf.SAADC_CH_CONFIG_TACQ_Msk) |
((nrf.SAADC_CH_CONFIG_MODE_SE << nrf.SAADC_CH_CONFIG_MODE_Pos) & nrf.SAADC_CH_CONFIG_MODE_Msk))
// Set pin to read. // Set pin to read.
nrf.SAADC.CH[0].PSELN.Set(pwmPin) nrf.SAADC.CH[0].PSELN.Set(pwmPin)
nrf.SAADC.CH[0].PSELP.Set(pwmPin) nrf.SAADC.CH[0].PSELP.Set(pwmPin)
@ -101,9 +98,6 @@ func (a ADC) Get() uint16 {
} }
nrf.SAADC.EVENTS_STOPPED.Set(0) nrf.SAADC.EVENTS_STOPPED.Set(0)
// Disable the ADC.
nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Disabled << nrf.SAADC_ENABLE_ENABLE_Pos)
value := int16(rawValue.Get()) value := int16(rawValue.Get())
if value < 0 { if value < 0 {
value = 0 value = 0

Loading…
Cancel
Save