Skip to content

Commit aa37921

Browse files
committed
Add type state to Adc
This is going to be useful to implement continuous or asynchronous one-shot modes.
1 parent 41f04e8 commit aa37921

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/adc.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ pub enum SampleTime {
6262
}
6363

6464
/// Analog to Digital converter interface
65-
pub struct Adc {
65+
pub struct Adc<State> {
6666
rb: ADC,
6767
sample_time: SampleTime,
6868
align: Align,
6969
precision: Precision,
70+
_state: State,
7071
}
7172

72-
impl Adc {
73+
impl Adc<Ready> {
7374
pub fn new(adc: ADC, rcc: &mut Rcc) -> Self {
7475
// Enable ADC clocks
7576
rcc.rb.apb2enr.modify(|_, w| w.adcen().set_bit());
@@ -80,6 +81,7 @@ impl Adc {
8081
sample_time: SampleTime::T_1_5,
8182
align: Align::Right,
8283
precision: Precision::B_12,
84+
_state: Ready,
8385
}
8486
}
8587

@@ -122,19 +124,19 @@ impl Adc {
122124
}
123125

124126
pub trait AdcExt {
125-
fn constrain(self, rcc: &mut Rcc) -> Adc;
127+
fn constrain(self, rcc: &mut Rcc) -> Adc<Ready>;
126128
}
127129

128130
impl AdcExt for ADC {
129-
fn constrain(self, rcc: &mut Rcc) -> Adc {
131+
fn constrain(self, rcc: &mut Rcc) -> Adc<Ready> {
130132
Adc::new(self, rcc)
131133
}
132134
}
133135

134-
impl<WORD, PIN> OneShot<Adc, WORD, PIN> for Adc
136+
impl<WORD, PIN> OneShot<Adc<Ready>, WORD, PIN> for Adc<Ready>
135137
where
136138
WORD: From<u16>,
137-
PIN: Channel<Adc, ID = u8>,
139+
PIN: Channel<Adc<Ready>, ID = u8>,
138140
{
139141
type Error = ();
140142

@@ -173,6 +175,11 @@ where
173175
}
174176
}
175177

178+
179+
/// Indicates that the ADC peripheral is ready
180+
pub struct Ready;
181+
182+
176183
macro_rules! int_adc {
177184
($($Chan:ident: ($chan:expr, $en:ident)),+ $(,)*) => {
178185
$(
@@ -183,16 +190,16 @@ macro_rules! int_adc {
183190
Self {}
184191
}
185192

186-
pub fn enable(&mut self, adc: &mut Adc) {
193+
pub fn enable(&mut self, adc: &mut Adc<Ready>) {
187194
adc.rb.ccr.modify(|_, w| w.$en().set_bit());
188195
}
189196

190-
pub fn disable(&mut self, adc: &mut Adc) {
197+
pub fn disable(&mut self, adc: &mut Adc<Ready>) {
191198
adc.rb.ccr.modify(|_, w| w.$en().clear_bit());
192199
}
193200
}
194201

195-
impl Channel<Adc> for $Chan {
202+
impl Channel<Adc<Ready>> for $Chan {
196203
type ID = u8;
197204

198205
fn channel() -> u8 {
@@ -206,7 +213,7 @@ macro_rules! int_adc {
206213
macro_rules! adc_pins {
207214
($($Chan:ty: ($pin:ty, $chan:expr)),+ $(,)*) => {
208215
$(
209-
impl Channel<Adc> for $pin {
216+
impl Channel<Adc<Ready>> for $pin {
210217
type ID = u8;
211218

212219
fn channel() -> u8 { $chan }

0 commit comments

Comments
 (0)