@@ -14,20 +14,17 @@ func CPUFrequency() uint32 {
14
14
15
15
// InitADC initializes the registers needed for ADC.
16
16
func InitADC () {
17
- return // no specific setup on nrf52 machine.
18
- }
19
-
20
- // Configure configures an ADC pin to be able to read analog data.
21
- func (a ADC ) Configure (config ADCConfig ) {
22
17
// Enable ADC.
23
- // The ADC does not consume a noticeable amount of current simply by being
24
- // enabled.
18
+ // The ADC does not consume a noticeable amount of current by being enabled.
25
19
nrf .SAADC .ENABLE .Set (nrf .SAADC_ENABLE_ENABLE_Enabled << nrf .SAADC_ENABLE_ENABLE_Pos )
20
+ }
26
21
27
- // Use fixed resolution of 12 bits.
28
- // TODO: is it useful for users to change this?
29
- nrf .SAADC .RESOLUTION .Set (nrf .SAADC_RESOLUTION_VAL_12bit )
30
-
22
+ // Configure configures an ADC pin to be able to read analog data.
23
+ // Reference voltage can be 150, 300, 600, 1200, 1800, 2400, 3000(default), 3600 mV
24
+ // Resolution can be 8, 10, 12(default), 14 bits
25
+ // SampleTime will be ceiled to 3(default), 5, 10, 15, 20 or 40(max) µS respectively
26
+ // Samples can be 1(default), 2, 4, 8, 16, 32, 64, 128, 256 samples
27
+ func (a * ADC ) Configure (config ADCConfig ) {
31
28
var configVal uint32 = nrf .SAADC_CH_CONFIG_RESP_Bypass << nrf .SAADC_CH_CONFIG_RESP_Pos |
32
29
nrf .SAADC_CH_CONFIG_RESP_Bypass << nrf .SAADC_CH_CONFIG_RESN_Pos |
33
30
nrf .SAADC_CH_CONFIG_REFSEL_Internal << nrf .SAADC_CH_CONFIG_REFSEL_Pos |
@@ -51,11 +48,26 @@ func (a ADC) Configure(config ADCConfig) {
51
48
case 3600 : // 3.6V
52
49
configVal |= nrf .SAADC_CH_CONFIG_GAIN_Gain1_6 << nrf .SAADC_CH_CONFIG_GAIN_Pos
53
50
default :
54
- // TODO: return an error
51
+ // TODO: return an error, will that interfere with any interfaced if one will be?
52
+ }
53
+
54
+ var resolution uint32
55
+ switch config .Resolution {
56
+ case 8 :
57
+ resolution = nrf .SAADC_RESOLUTION_VAL_8bit
58
+ case 10 :
59
+ resolution = nrf .SAADC_RESOLUTION_VAL_10bit
60
+ case 12 :
61
+ resolution = nrf .SAADC_RESOLUTION_VAL_12bit
62
+ case 14 :
63
+ resolution = nrf .SAADC_RESOLUTION_VAL_14bit
64
+ default :
65
+ resolution = nrf .SAADC_RESOLUTION_VAL_12bit
55
66
}
67
+ nrf .SAADC .RESOLUTION .Set (resolution )
56
68
57
- // Source resistance, according to table 89 on page 364 of the nrf52832 datasheet.
58
- // https://infocenter .nordicsemi.com/pdf/nRF52832_PS_v1.4 .pdf
69
+ // Source resistance, according to table 41 on page 676 of the nrf52832 datasheet.
70
+ // https://docs-be .nordicsemi.com/bundle/ps_nrf52840/attach/nRF52840_PS_v1.11 .pdf?_LANG=enus
59
71
if config .SampleTime <= 3 { // <= 10kΩ
60
72
configVal |= nrf .SAADC_CH_CONFIG_TACQ_3us << nrf .SAADC_CH_CONFIG_TACQ_Pos
61
73
} else if config .SampleTime <= 5 { // <= 40kΩ
@@ -102,36 +114,28 @@ func (a ADC) Configure(config ADCConfig) {
102
114
nrf .SAADC .CH [0 ].CONFIG .Set (configVal )
103
115
}
104
116
105
- // Get returns the current value of a ADC pin in the range 0..0xffff.
106
- func (a ADC ) Get () uint16 {
117
+ // Get returns the current value of an ADC pin in the range 0..0xffff.
118
+ func (a * ADC ) Get () uint16 {
107
119
var pwmPin uint32
108
120
var rawValue volatile.Register16
109
121
110
122
switch a .Pin {
111
123
case 2 :
112
124
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput0
113
-
114
125
case 3 :
115
126
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput1
116
-
117
127
case 4 :
118
128
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput2
119
-
120
129
case 5 :
121
130
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput3
122
-
123
131
case 28 :
124
132
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput4
125
-
126
133
case 29 :
127
134
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput5
128
-
129
135
case 30 :
130
136
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput6
131
-
132
137
case 31 :
133
138
pwmPin = nrf .SAADC_CH_PSELP_PSELP_AnalogInput7
134
-
135
139
default :
136
140
return 0
137
141
}
@@ -164,13 +168,22 @@ func (a ADC) Get() uint16 {
164
168
}
165
169
nrf .SAADC .EVENTS_STOPPED .Set (0 )
166
170
167
- value := int16 (rawValue .Get ())
168
- if value < 0 {
169
- value = 0
171
+ // convert to 16 bit resolution/value
172
+ var resolutionAdjustment uint8
173
+ switch nrf .SAADC .RESOLUTION .Get () {
174
+ case nrf .SAADC_RESOLUTION_VAL_8bit :
175
+ resolutionAdjustment = 8
176
+ case nrf .SAADC_RESOLUTION_VAL_10bit :
177
+ resolutionAdjustment = 6
178
+ case nrf .SAADC_RESOLUTION_VAL_12bit :
179
+ resolutionAdjustment = 4
180
+ case nrf .SAADC_RESOLUTION_VAL_14bit :
181
+ resolutionAdjustment = 2
182
+ default :
183
+ resolutionAdjustment = 4 // 12bit
170
184
}
171
185
172
- // Return 16-bit result from 12-bit value.
173
- return uint16 (value << 4 )
186
+ return rawValue .Get () << resolutionAdjustment
174
187
}
175
188
176
189
// SPI on the NRF.
@@ -196,7 +209,7 @@ type SPIConfig struct {
196
209
Mode uint8
197
210
}
198
211
199
- // Configure is intended to setup the SPI interface.
212
+ // Configure is intended to set up the SPI interface.
200
213
func (spi SPI ) Configure (config SPIConfig ) error {
201
214
// Disable bus to configure it
202
215
spi .Bus .ENABLE .Set (nrf .SPIM_ENABLE_ENABLE_Disabled )
0 commit comments