Skip to content

Commit 13bea28

Browse files
committed
Flatten file structure where there is only one execution model
1 parent 8cac13a commit 13bea28

File tree

26 files changed

+987
-980
lines changed

26 files changed

+987
-980
lines changed

src/adc.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//! Analog-digital conversion traits
2+
3+
/// Non-blocking ADC traits
4+
pub mod nb {
5+
/// A marker trait to identify MCU pins that can be used as inputs to an ADC channel.
6+
///
7+
/// This marker trait denotes an object, i.e. a GPIO pin, that is ready for use as an input to the
8+
/// ADC. As ADCs channels can be supplied by multiple pins, this trait defines the relationship
9+
/// between the physical interface and the ADC sampling buffer.
10+
///
11+
/// ```
12+
/// # use core::marker::PhantomData;
13+
/// # use embedded_hal::adc::nb::Channel;
14+
///
15+
/// struct Adc1; // Example ADC with single bank of 8 channels
16+
/// struct Gpio1Pin1<MODE>(PhantomData<MODE>);
17+
/// struct Analog(()); // marker type to denote a pin in "analog" mode
18+
///
19+
/// // GPIO 1 pin 1 can supply an ADC channel when it is configured in Analog mode
20+
/// impl Channel<Adc1> for Gpio1Pin1<Analog> {
21+
/// type ID = u8; // ADC channels are identified numerically
22+
///
23+
/// fn channel(&self) -> Self::ID {
24+
/// 7_u8 // GPIO pin 1 is connected to ADC channel 7
25+
/// }
26+
/// }
27+
///
28+
/// struct Adc2; // ADC with two banks of 16 channels
29+
/// struct Gpio2PinA<MODE>(PhantomData<MODE>);
30+
/// struct AltFun(()); // marker type to denote some alternate function mode for the pin
31+
///
32+
/// // GPIO 2 pin A can supply an ADC channel when it's configured in some alternate function mode
33+
/// impl Channel<Adc2> for Gpio2PinA<AltFun> {
34+
/// type ID = (u8, u8); // ADC channels are identified by bank number and channel number
35+
///
36+
/// fn channel(&self) -> Self::ID {
37+
/// (0, 3) // bank 0 channel 3
38+
/// }
39+
/// }
40+
/// ```
41+
pub trait Channel<ADC> {
42+
/// Channel ID type
43+
///
44+
/// A type used to identify this ADC channel. For example, if the ADC has eight channels, this
45+
/// might be a `u8`. If the ADC has multiple banks of channels, it could be a tuple, like
46+
/// `(u8: bank_id, u8: channel_id)`.
47+
type ID: Copy;
48+
49+
/// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC
50+
/// channel, if Self::ID is u8.
51+
fn channel(&self) -> Self::ID;
52+
}
53+
54+
/// ADCs that sample on single channels per request, and do so at the time of the request.
55+
///
56+
/// This trait is the interface to an ADC that is configured to read a specific channel at the time
57+
/// of the request (in contrast to continuous asynchronous sampling).
58+
///
59+
/// ```
60+
/// use embedded_hal::adc::nb::{Channel, OneShot};
61+
///
62+
/// struct MyAdc; // 10-bit ADC, with 5 channels
63+
/// # impl MyAdc {
64+
/// # pub fn power_up(&mut self) {}
65+
/// # pub fn power_down(&mut self) {}
66+
/// # pub fn do_conversion(&mut self, chan: u8) -> u16 { 0xAA55_u16 }
67+
/// # }
68+
///
69+
/// impl<WORD, PIN> OneShot<MyAdc, WORD, PIN> for MyAdc
70+
/// where
71+
/// WORD: From<u16>,
72+
/// PIN: Channel<MyAdc, ID=u8>,
73+
/// {
74+
/// type Error = ();
75+
///
76+
/// fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
77+
/// let chan = 1 << pin.channel();
78+
/// self.power_up();
79+
/// let result = self.do_conversion(chan);
80+
/// self.power_down();
81+
/// Ok(result.into())
82+
/// }
83+
/// }
84+
/// ```
85+
pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
86+
/// Error type returned by ADC methods
87+
type Error;
88+
89+
/// Request that the ADC begin a conversion on the specified pin
90+
///
91+
/// This method takes a `Pin` reference, as it is expected that the ADC will be able to sample
92+
/// whatever channel underlies the pin.
93+
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
94+
}
95+
}

src/adc/mod.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/adc/nb.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/capture/nb.rs renamed to src/capture.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Input capture
22
3+
/// Non-blocking input capture traits
4+
pub mod nb {
35
/// Input capture
46
///
57
/// # Examples
@@ -92,3 +94,4 @@ pub trait Capture {
9294
where
9395
R: Into<Self::Time>;
9496
}
97+
}

src/capture/mod.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/delay.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Delays
2+
//!
3+
//! # What's the difference between these traits and the `timer::CountDown` trait?
4+
//!
5+
//! The `Timer` trait provides a *non-blocking* timer abstraction and it's meant to be used to build
6+
//! higher level abstractions like I/O operations with timeouts. OTOH, these delays traits only
7+
//! provide *blocking* functionality. Note that you can also use the `timer::CountDown` trait to
8+
//! implement blocking delays.
9+
10+
/// Blocking delay traits
11+
pub mod blocking {
12+
/// Millisecond delay
13+
///
14+
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
15+
/// implement this trait for different types of `UXX`.
16+
pub trait DelayMs<UXX> {
17+
/// Enumeration of `DelayMs` errors
18+
type Error;
19+
20+
/// Pauses execution for `ms` milliseconds
21+
fn delay_ms(&mut self, ms: UXX) -> Result<(), Self::Error>;
22+
}
23+
24+
/// Microsecond delay
25+
///
26+
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
27+
/// implement this trait for different types of `UXX`.
28+
pub trait DelayUs<UXX> {
29+
/// Enumeration of `DelayMs` errors
30+
type Error;
31+
32+
/// Pauses execution for `us` microseconds
33+
fn delay_us(&mut self, us: UXX) -> Result<(), Self::Error>;
34+
}
35+
}

src/delay/blocking.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/delay/mod.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)