@@ -20,10 +20,12 @@ use embedded_hal::mmc::{
2020mod pins;
2121mod slc;
2222mod slchost;
23+ mod state;
2324
2425pub use pins:: Pins ;
2526pub use slc:: { AnySlc , SlcInfo , SlcInstance } ;
2627pub use slchost:: { AnySlchost , SlchostInfo , SlchostInstance } ;
28+ pub use state:: State ;
2729
2830/// SDIO peripheral instance.
2931pub 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
8285impl < ' 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
148171impl 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
179208impl 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}
@@ -211,6 +243,9 @@ impl<'d> MmcCommon for Sdio<'d> {
211243 }
212244
213245 fn init ( & mut self ) -> Result < ( ) , Self :: Error > {
246+ // TODO: perform peripheral configuration
247+ self . state_transition ( State :: Standby ) ?;
248+
214249 Err ( Error :: unimplemented ( ) )
215250 }
216251
0 commit comments