Skip to content

Commit fddb870

Browse files
authored
Merge #382
382: Add Internal VDD channels for nrf51 ADC r=jonas-schievink a=nedv-eu Adds channels for measuring VDD level on nrf51 ADC. Useful to monitor battery state if powered directly from 2 AA cells. Co-authored-by: nedv-eu <>
2 parents 6799f74 + 41cc047 commit fddb870

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

nrf-hal-common/src/adc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ where
8181
type Error = ();
8282

8383
fn read(&mut self, _pin: &mut PIN) -> nb::Result<i16, Self::Error> {
84+
let original_inpsel = self.0.config.read().inpsel();
8485
match PIN::channel() {
8586
0 => self.0.config.modify(|_, w| w.psel().analog_input0()),
8687
1 => self.0.config.modify(|_, w| w.psel().analog_input1()),
@@ -90,6 +91,8 @@ where
9091
5 => self.0.config.modify(|_, w| w.psel().analog_input5()),
9192
6 => self.0.config.modify(|_, w| w.psel().analog_input6()),
9293
7 => self.0.config.modify(|_, w| w.psel().analog_input7()),
94+
8 => self.0.config.modify(|_, w| w.inpsel().supply_one_third_prescaling()),
95+
9 => self.0.config.modify(|_, w| w.inpsel().supply_two_thirds_prescaling()),
9396
// This can never happen the only analog pins have already been defined
9497
// PAY CLOSE ATTENTION TO ANY CHANGES TO THIS IMPL OR THE `channel_mappings!` MACRO
9598
_ => unsafe { unreachable_unchecked() },
@@ -101,6 +104,8 @@ where
101104
while self.0.events_end.read().bits() == 0 {}
102105

103106
self.0.events_end.write(|w| unsafe { w.bits(0) });
107+
// Restore original input selection
108+
self.0.config.modify(|_, w| w.inpsel().variant(original_inpsel.variant().unwrap()));
104109

105110
// Max resolution is 10 bits so casting is always safe
106111
Ok(self.0.result.read().result().bits() as i16)
@@ -129,5 +134,10 @@ channel_mappings! {
129134
4 => crate::gpio::p0::P0_03<Input<Floating>>,
130135
5 => crate::gpio::p0::P0_04<Input<Floating>>,
131136
6 => crate::gpio::p0::P0_05<Input<Floating>>,
132-
7 => crate::gpio::p0::P0_06<Input<Floating>>
137+
7 => crate::gpio::p0::P0_06<Input<Floating>>,
138+
8 => crate::adc::InternalVddOneThird,
139+
9 => crate::adc::InternalVddTwoThirds
133140
}
141+
142+
pub struct InternalVddOneThird;
143+
pub struct InternalVddTwoThirds;

0 commit comments

Comments
 (0)