Skip to content

Commit 887ac28

Browse files
aykevldeadprogram
authored andcommitted
nrf: add ADC_VDDH which is an ADC pin for VDDH
VDDH is typically connected to USB (~5V) or directly to a battery. Measuring the voltage on VDDH is a way to measure the current battery voltage, which in turn can be useful to determine how much power is left in the battery. I've special cased the nrf52840 in a somewhat unusual way, maybe there is a better way, feel free to comment!
1 parent b1083d7 commit 887ac28

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/machine/machine_nrf52840.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,7 @@ func eraseBlockSize() int64 {
110110
}
111111

112112
const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT
113+
114+
// ADC instance for the VDDH input pin. This pin is typically connected to USB
115+
// input voltage (~5V) or directly to a battery.
116+
var ADC_VDDH = ADC{adcVDDHPin}

src/machine/machine_nrf52xxx.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func CPUFrequency() uint32 {
1212
return 64000000
1313
}
1414

15+
var adcVDDHPin = Pin(254) // special pin number for VDDH on the nrf52840
16+
1517
// InitADC initializes the registers needed for ADC.
1618
func InitADC() {
1719
// Enable ADC.
@@ -136,6 +138,12 @@ func (a *ADC) Get() uint16 {
136138
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput6
137139
case 31:
138140
adcPin = nrf.SAADC_CH_PSELP_PSELP_AnalogInput7
141+
case adcVDDHPin:
142+
if Device == "nrf52840" {
143+
adcPin = 0x0D // VDDHDIV5 on the nrf52840
144+
} else {
145+
return 0
146+
}
139147
default:
140148
return 0
141149
}

0 commit comments

Comments
 (0)