aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2023-05-09 16:46:50 +0200
committerRon Evans <[email protected]>2023-05-09 19:51:05 +0200
commitf4c8c37b7b4a618574f8255b7b089b34f44a247e (patch)
treefc34dbe4159e80a262eb02bc36cc6a205bc93433
parent868812717f432349f911bc45b4f7945d942388fa (diff)
downloadtinygo-f4c8c37b7b4a618574f8255b7b089b34f44a247e.tar.gz
tinygo-f4c8c37b7b4a618574f8255b7b089b34f44a247e.zip
nrf: add ADC oversampling support
This is a lot easier to support than on other chips, and results in noticeably better output when set to a higher value.
-rw-r--r--src/machine/machine_nrf52xxx.go31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/machine/machine_nrf52xxx.go b/src/machine/machine_nrf52xxx.go
index 2989e14dd..c4605d582 100644
--- a/src/machine/machine_nrf52xxx.go
+++ b/src/machine/machine_nrf52xxx.go
@@ -24,7 +24,8 @@ func (a ADC) Configure(config ADCConfig) {
// enabled.
nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos)
- // Configure ADC.
+ // Use fixed resolution of 12 bits.
+ // TODO: is it useful for users to change this?
nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit)
var configVal uint32 = nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESP_Pos |
@@ -69,6 +70,34 @@ func (a ADC) Configure(config ADCConfig) {
configVal |= nrf.SAADC_CH_CONFIG_TACQ_40us << nrf.SAADC_CH_CONFIG_TACQ_Pos
}
+ // Oversampling configuration.
+ burst := true
+ switch config.Samples {
+ default: // no oversampling
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Bypass)
+ burst = false
+ case 2:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over2x)
+ case 4:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over4x)
+ case 8:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over8x)
+ case 16:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over16x)
+ case 32:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over32x)
+ case 64:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over64x)
+ case 128:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over128x)
+ case 256:
+ nrf.SAADC.OVERSAMPLE.Set(nrf.SAADC_OVERSAMPLE_OVERSAMPLE_Over256x)
+ }
+ if burst {
+ // BURST=1 is needed when oversampling
+ configVal |= nrf.SAADC_CH_CONFIG_BURST
+ }
+
// Configure channel 0, which is the only channel we use.
nrf.SAADC.CH[0].CONFIG.Set(configVal)
}