Skip to content

Commit 1d45750

Browse files
committed
Move execution-model-independent definitions up
1 parent 18b9b54 commit 1d45750

File tree

15 files changed

+181
-167
lines changed

15 files changed

+181
-167
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Swap PWM channel arguments to references
1616
- All trait methods have been renamed to remove the `try_` prefix (i.e. `try_send` -> `send`) for consistency.
1717
- Moved all traits into two sub modules for each feature depending on the execution model: `blocking` and `nb` (non-blocking). For example, the spi traits can now be found under `embedded_hal::spi::blocking` or `embedded_hal::spi::nb`.
18+
- Execution-model-independent definitions have been moved into the feature module. For example, SPI `Phase` is now defined in `embedded_hal::spi::Phase`. For convenience, these definitions are reexported in both of its blocking and non-blocking submodules.
1819
- Re-export `nb::{block!, Error, Result}` to avoid version mismatches. These should be used instead of
1920
importing the `nb` crate directly in dependent crates.
2021
- `blocking::Serial`: renamed `bwrite_all` to `write`, `bflush` to `flush.

src/adc/mod.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
11
//! Analog-digital conversion traits
22
33
pub mod nb;
4+
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+
}

src/adc/nb.rs

+1-48
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,6 @@
11
//! Analog-digital conversion traits
22
3-
/// A marker trait to identify MCU pins that can be used as inputs to an ADC channel.
4-
///
5-
/// This marker trait denotes an object, i.e. a GPIO pin, that is ready for use as an input to the
6-
/// ADC. As ADCs channels can be supplied by multiple pins, this trait defines the relationship
7-
/// between the physical interface and the ADC sampling buffer.
8-
///
9-
/// ```
10-
/// # use core::marker::PhantomData;
11-
/// # use embedded_hal::adc::nb::Channel;
12-
///
13-
/// struct Adc1; // Example ADC with single bank of 8 channels
14-
/// struct Gpio1Pin1<MODE>(PhantomData<MODE>);
15-
/// struct Analog(()); // marker type to denote a pin in "analog" mode
16-
///
17-
/// // GPIO 1 pin 1 can supply an ADC channel when it is configured in Analog mode
18-
/// impl Channel<Adc1> for Gpio1Pin1<Analog> {
19-
/// type ID = u8; // ADC channels are identified numerically
20-
///
21-
/// fn channel(&self) -> Self::ID {
22-
/// 7_u8 // GPIO pin 1 is connected to ADC channel 7
23-
/// }
24-
/// }
25-
///
26-
/// struct Adc2; // ADC with two banks of 16 channels
27-
/// struct Gpio2PinA<MODE>(PhantomData<MODE>);
28-
/// struct AltFun(()); // marker type to denote some alternate function mode for the pin
29-
///
30-
/// // GPIO 2 pin A can supply an ADC channel when it's configured in some alternate function mode
31-
/// impl Channel<Adc2> for Gpio2PinA<AltFun> {
32-
/// type ID = (u8, u8); // ADC channels are identified by bank number and channel number
33-
///
34-
/// fn channel(&self) -> Self::ID {
35-
/// (0, 3) // bank 0 channel 3
36-
/// }
37-
/// }
38-
/// ```
39-
pub trait Channel<ADC> {
40-
/// Channel ID type
41-
///
42-
/// A type used to identify this ADC channel. For example, if the ADC has eight channels, this
43-
/// might be a `u8`. If the ADC has multiple banks of channels, it could be a tuple, like
44-
/// `(u8: bank_id, u8: channel_id)`.
45-
type ID: Copy;
46-
47-
/// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC
48-
/// channel, if Self::ID is u8.
49-
fn channel(&self) -> Self::ID;
50-
}
3+
pub use super::Channel;
514

525
/// ADCs that sample on single channels per request, and do so at the time of the request.
536
///

src/digital/blocking.rs

+1-39
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,7 @@
44
//! traits. To save boilerplate when that's the case a `Default` marker trait may be provided.
55
//! Implementing that marker trait will opt in your type into a blanket implementation.
66
7-
use core::{convert::From, ops::Not};
8-
9-
/// Digital output pin state
10-
///
11-
/// Conversion from `bool` and logical negation are also implemented
12-
/// for this type.
13-
/// ```rust
14-
/// # use embedded_hal::digital::blocking::PinState;
15-
/// let state = PinState::from(false);
16-
/// assert_eq!(state, PinState::Low);
17-
/// assert_eq!(!state, PinState::High);
18-
/// ```
19-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
20-
pub enum PinState {
21-
/// Low pin state
22-
Low,
23-
/// High pin state
24-
High,
25-
}
26-
27-
impl From<bool> for PinState {
28-
fn from(value: bool) -> Self {
29-
match value {
30-
false => PinState::Low,
31-
true => PinState::High,
32-
}
33-
}
34-
}
35-
36-
impl Not for PinState {
37-
type Output = PinState;
38-
39-
fn not(self) -> Self::Output {
40-
match self {
41-
PinState::High => PinState::Low,
42-
PinState::Low => PinState::High,
43-
}
44-
}
45-
}
7+
pub use super::PinState;
468

479
/// Single digital push-pull output pin
4810
pub trait OutputPin {

src/digital/mod.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
11
//! Digital I/O traits
22
33
pub mod blocking;
4+
5+
use core::{convert::From, ops::Not};
6+
7+
/// Digital output pin state
8+
///
9+
/// Conversion from `bool` and logical negation are also implemented
10+
/// for this type.
11+
/// ```rust
12+
/// # use embedded_hal::digital::blocking::PinState;
13+
/// let state = PinState::from(false);
14+
/// assert_eq!(state, PinState::Low);
15+
/// assert_eq!(!state, PinState::High);
16+
/// ```
17+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
18+
pub enum PinState {
19+
/// Low pin state
20+
Low,
21+
/// High pin state
22+
High,
23+
}
24+
25+
impl From<bool> for PinState {
26+
fn from(value: bool) -> Self {
27+
match value {
28+
false => PinState::Low,
29+
true => PinState::High,
30+
}
31+
}
32+
}
33+
34+
impl Not for PinState {
35+
type Output = PinState;
36+
37+
fn not(self) -> Self::Output {
38+
match self {
39+
PinState::High => PinState::Low,
40+
PinState::Low => PinState::High,
41+
}
42+
}
43+
}

src/i2c/blocking.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,7 @@
9999
//! }
100100
//! ```
101101
102-
use crate::private;
103-
104-
/// Address mode (7-bit / 10-bit)
105-
///
106-
/// Note: This trait is sealed and should not be implemented outside of this crate.
107-
pub trait AddressMode: private::Sealed {}
108-
109-
/// 7-bit address mode type
110-
pub type SevenBitAddress = u8;
111-
112-
/// 10-bit address mode type
113-
pub type TenBitAddress = u16;
114-
115-
impl AddressMode for SevenBitAddress {}
116-
117-
impl AddressMode for TenBitAddress {}
102+
pub use super::{AddressMode, SevenBitAddress, TenBitAddress};
118103

119104
/// Blocking read
120105
pub trait Read<A: AddressMode = SevenBitAddress> {

src/i2c/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
//! I2C traits
22
33
pub mod blocking;
4+
5+
use crate::private;
6+
7+
/// Address mode (7-bit / 10-bit)
8+
///
9+
/// Note: This trait is sealed and should not be implemented outside of this crate.
10+
pub trait AddressMode: private::Sealed {}
11+
12+
/// 7-bit address mode type
13+
pub type SevenBitAddress = u8;
14+
15+
/// 10-bit address mode type
16+
pub type TenBitAddress = u16;
17+
18+
impl AddressMode for SevenBitAddress {}
19+
20+
impl AddressMode for TenBitAddress {}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub mod timer;
428428
pub mod watchdog;
429429

430430
mod private {
431-
use crate::i2c::blocking::{SevenBitAddress, TenBitAddress};
431+
use crate::i2c::{SevenBitAddress, TenBitAddress};
432432
pub trait Sealed {}
433433

434434
impl Sealed for SevenBitAddress {}

src/qei/blocking.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Quadrature encoder interface
22
3+
pub use super::Direction;
4+
35
/// Quadrature encoder interface
46
///
57
/// # Examples
@@ -68,12 +70,3 @@ pub trait Qei {
6870
/// Returns the count direction
6971
fn direction(&self) -> Result<Direction, Self::Error>;
7072
}
71-
72-
/// Count direction
73-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
74-
pub enum Direction {
75-
/// 3, 2, 1
76-
Downcounting,
77-
/// 1, 2, 3
78-
Upcounting,
79-
}

src/qei/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
//! Quadrature encoder interface traits
22
33
pub mod blocking;
4+
5+
/// Count direction
6+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7+
pub enum Direction {
8+
/// 3, 2, 1
9+
Downcounting,
10+
/// 1, 2, 3
11+
Upcounting,
12+
}

src/spi/blocking.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! traits. To save boilerplate when that's the case a `Default` marker trait may be provided.
55
//! Implementing that marker trait will opt in your type into a blanket implementation.
66
7+
pub use super::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
78
/// Blocking transfer
89
pub trait Transfer<W> {
910
/// Error type

src/spi/mod.rs

+51
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,54 @@
22
33
pub mod blocking;
44
pub mod nb;
5+
6+
/// Clock polarity
7+
#[derive(Clone, Copy, PartialEq, Eq)]
8+
pub enum Polarity {
9+
/// Clock signal low when idle
10+
IdleLow,
11+
/// Clock signal high when idle
12+
IdleHigh,
13+
}
14+
15+
/// Clock phase
16+
#[derive(Clone, Copy, PartialEq, Eq)]
17+
pub enum Phase {
18+
/// Data in "captured" on the first clock transition
19+
CaptureOnFirstTransition,
20+
/// Data in "captured" on the second clock transition
21+
CaptureOnSecondTransition,
22+
}
23+
24+
/// SPI mode
25+
#[derive(Clone, Copy, PartialEq, Eq)]
26+
pub struct Mode {
27+
/// Clock polarity
28+
pub polarity: Polarity,
29+
/// Clock phase
30+
pub phase: Phase,
31+
}
32+
33+
/// Helper for CPOL = 0, CPHA = 0
34+
pub const MODE_0: Mode = Mode {
35+
polarity: Polarity::IdleLow,
36+
phase: Phase::CaptureOnFirstTransition,
37+
};
38+
39+
/// Helper for CPOL = 0, CPHA = 1
40+
pub const MODE_1: Mode = Mode {
41+
polarity: Polarity::IdleLow,
42+
phase: Phase::CaptureOnSecondTransition,
43+
};
44+
45+
/// Helper for CPOL = 1, CPHA = 0
46+
pub const MODE_2: Mode = Mode {
47+
polarity: Polarity::IdleHigh,
48+
phase: Phase::CaptureOnFirstTransition,
49+
};
50+
51+
/// Helper for CPOL = 1, CPHA = 1
52+
pub const MODE_3: Mode = Mode {
53+
polarity: Polarity::IdleHigh,
54+
phase: Phase::CaptureOnSecondTransition,
55+
};

0 commit comments

Comments
 (0)