Skip to content

Commit c38418c

Browse files
committed
sdio: add peripheral state
Adds the `State` type to represent the SDIO states + transitions.
1 parent d1796b6 commit c38418c

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

esp-hal/src/sdio.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ use embedded_hal::mmc::{
2020
mod pins;
2121
mod slc;
2222
mod slchost;
23+
mod state;
2324

2425
pub use pins::Pins;
2526
pub use slc::{AnySlc, SlcInfo, SlcInstance};
2627
pub use slchost::{AnySlchost, SlchostInfo, SlchostInstance};
28+
pub use state::State;
2729

2830
/// SDIO peripheral instance.
2931
pub trait PeripheralInstance: crate::private::Sealed {
@@ -77,6 +79,7 @@ pub struct Sdio<'d> {
7779
slc: AnySlc<'d>,
7880
slchost: AnySlchost<'d>,
7981
pins: Pins<'d>,
82+
state: State,
8083
}
8184

8285
impl<'d> Sdio<'d> {
@@ -108,6 +111,7 @@ impl<'d> Sdio<'d> {
108111
slc: slc.degrade(),
109112
slchost: slchost.degrade(),
110113
pins,
114+
state: State::new(),
111115
}
112116
}
113117

@@ -130,6 +134,18 @@ impl<'d> Sdio<'d> {
130134
pub const fn bus_mode(&self) -> Mode {
131135
self.pins.mode()
132136
}
137+
138+
/// Gets the current [State] of the SDIO peripheral.
139+
pub const fn state(&self) -> State {
140+
self.state
141+
}
142+
143+
/// Transitions the SDIO peripheral to the requested [State].
144+
pub(crate) fn state_transition(&mut self, state: State) -> Result<(), Error> {
145+
self.state
146+
.valid_transition(state)
147+
.map(|_| self.state = state)
148+
}
133149
}
134150

135151
/// Represents the error variants for SDIO peripherals.
@@ -143,6 +159,13 @@ pub enum Error {
143159
Crc,
144160
/// The function and/or type is unimplemented.
145161
Unimplemented,
162+
/// Indicates an invalid state transition.
163+
InvalidTransition {
164+
/// Represents the current state.
165+
from: State,
166+
/// Represents the transition state.
167+
to: State,
168+
},
146169
}
147170

148171
impl Error {
@@ -174,6 +197,12 @@ impl Error {
174197
pub const fn unimplemented() -> Self {
175198
Self::Unimplemented
176199
}
200+
201+
/// Creates an invalid state transition [Error].
202+
#[inline]
203+
pub const fn invalid_transition(from: State, to: State) -> Self {
204+
Self::InvalidTransition { from, to }
205+
}
177206
}
178207

179208
impl Default for Error {
@@ -189,6 +218,9 @@ impl core::fmt::Display for Error {
189218
Self::IllegalCommand => write!(f, "illegal command"),
190219
Self::Crc => write!(f, "CRC"),
191220
Self::Unimplemented => write!(f, "unimplemented"),
221+
Self::InvalidTransition { from, to } => {
222+
write!(f, "invalid state transition, from: {from}, to: {to}")
223+
}
192224
}
193225
}
194226
}
@@ -210,14 +242,10 @@ impl<'d> MmcCommon for Sdio<'d> {
210242
Err(Error::unimplemented())
211243
}
212244

213-
<<<<<<< Updated upstream
214-
fn init(&mut self) -> Result<(), Self::Error> {
215-
=======
216245
fn init(&mut self) -> Result<(), Error> {
217246
// TODO: perform peripheral configuration
218247
self.state_transition(State::Standby)?;
219248

220-
>>>>>>> Stashed changes
221249
Err(Error::unimplemented())
222250
}
223251

esp-hal/src/sdio/state.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use super::Error;
2+
3+
/// Represents valid states for the SDIO peripheral.
4+
#[repr(u8)]
5+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6+
pub enum State {
7+
/// Represents the initial state.
8+
///
9+
/// The peripheral is preparing for operation, e.g. configuring bus width,
10+
/// mode, speed etc.
11+
Init,
12+
/// Represents the standby state.
13+
///
14+
/// The peripheral is in standby waiting for a command.
15+
Standby,
16+
/// Represents the command state.
17+
///
18+
/// The peripheral is processing a command from the host.
19+
Command,
20+
/// Represents the transfer state.
21+
///
22+
/// The peripheral is sending/receiving a data transfer.
23+
Transfer,
24+
}
25+
26+
impl State {
27+
/// Creates a new [State].
28+
pub const fn new() -> Self {
29+
Self::Init
30+
}
31+
32+
/// Checks if the [State] transition is valid.
33+
pub const fn valid_transition(self, state: Self) -> Result<(), Error> {
34+
match (self, state) {
35+
(Self::Init, Self::Standby) => Ok(()),
36+
(Self::Standby, Self::Command) => Ok(()),
37+
(Self::Command, Self::Init | Self::Standby | Self::Transfer) => Ok(()),
38+
(Self::Transfer, Self::Init | Self::Command) => Ok(()),
39+
_ => Err(Error::invalid_transition(self, state)),
40+
}
41+
}
42+
}
43+
44+
impl Default for State {
45+
fn default() -> Self {
46+
Self::new()
47+
}
48+
}
49+
50+
impl core::fmt::Display for State {
51+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
52+
match self {
53+
Self::Init => write!(f, "init"),
54+
Self::Standby => write!(f, "standby"),
55+
Self::Command => write!(f, "command"),
56+
Self::Transfer => write!(f, "transfer"),
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)