Skip to content

Commit 7121e23

Browse files
committed
refactor(src/machine/nrf52xxx/adc): make resolution configurable, pointer receiver, move init code
Signed-off-by: Paul Schroeder <[email protected]>
1 parent efd3f3d commit 7121e23

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/machine/machine_nrf52xxx.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,31 @@ func CPUFrequency() uint32 {
4949

5050
// InitADC initializes the registers needed for ADC.
5151
func InitADC() {
52-
return // no specific setup on nrf52 machine.
53-
}
54-
55-
// Configure configures an ADC pin to be able to read analog data.
56-
func (a ADC) Configure(config ADCConfig) {
5752
// Enable ADC.
58-
// The ADC does not consume a noticeable amount of current simply by being
59-
// enabled.
53+
// The ADC does not consume a noticeable amount of current by being enabled.
6054
nrf.SAADC.ENABLE.Set(nrf.SAADC_ENABLE_ENABLE_Enabled << nrf.SAADC_ENABLE_ENABLE_Pos)
55+
}
6156

62-
// Use fixed resolution of 12 bits.
63-
// TODO: is it useful for users to change this?
64-
nrf.SAADC.RESOLUTION.Set(nrf.SAADC_RESOLUTION_VAL_12bit)
57+
// Configure configures an ADC pin to be able to read analog data.
58+
// Reference voltage can be 150, 300, 600, 1200, 1800, 2400, 3000(default), 3600 mV
59+
// Resolution can be 8, 10, 12(default), 14 bits
60+
// SampleTime will be ceiled to 3(default), 5, 10, 15, 20 or 40(max) µS respectively
61+
// Samples can be 1(default), 2, 4, 8, 16, 32, 64, 128, 256 samples
62+
func (a *ADC) Configure(config ADCConfig) {
63+
var resolution uint32
64+
switch config.Resolution {
65+
case 8:
66+
resolution = SAADC_RESOLUTION_VAL_8bit
67+
case 10:
68+
resolution = SAADC_RESOLUTION_VAL_10bit
69+
case 12:
70+
resolution = SAADC_RESOLUTION_VAL_12bit
71+
case 14:
72+
resolution = SAADC_RESOLUTION_VAL_14bit
73+
default:
74+
resolution = SAADC_RESOLUTION_VAL_12bit
75+
}
76+
nrf.SAADC.RESOLUTION.Set(resolution)
6577

6678
var configVal uint32 = nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESP_Pos |
6779
nrf.SAADC_CH_CONFIG_RESP_Bypass<<nrf.SAADC_CH_CONFIG_RESN_Pos |
@@ -86,7 +98,7 @@ func (a ADC) Configure(config ADCConfig) {
8698
case 3600: // 3.6V
8799
configVal |= nrf.SAADC_CH_CONFIG_GAIN_Gain1_6 << nrf.SAADC_CH_CONFIG_GAIN_Pos
88100
default:
89-
// TODO: return an error
101+
// TODO: return an error, will that interfere with any interfaced if one will be?
90102
}
91103

92104
// Source resistance, according to table 41 on page 676 of the nrf52832 datasheet.
@@ -138,7 +150,7 @@ func (a ADC) Configure(config ADCConfig) {
138150
}
139151

140152
// Get returns the current value of a ADC pin in the range 0..0xffff.
141-
func (a ADC) Get() uint16 {
153+
func (a *ADC) Get() uint16 {
142154
var pwmPin uint32
143155
var rawValue volatile.Register16
144156

0 commit comments

Comments
 (0)