Skip to content

Commit b7eeb4c

Browse files
committed
sdio: initial SDIO HAL implementation
Provides the initial skeleton for the `embedded_hal::mmc::MmcOps` implementation for the SDIO peripherals on the ESP32C6 microcontroller.
1 parent aeaab9a commit b7eeb4c

File tree

7 files changed

+250
-2
lines changed

7 files changed

+250
-2
lines changed

esp-hal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ bitflags = "2.9.0"
2727
bytemuck = "1.22.0"
2828
cfg-if = "1.0.0"
2929
critical-section = { version = "1.2.0", features = ["restore-state-u32"] }
30-
embedded-hal = "1.0.0"
31-
embedded-hal-async = "1.0.0"
30+
embedded-hal = { version = "1.0.0", git = "https://github.com/rmsyn/embedded-hal", branch = "embedded-hal/mmc" }
31+
embedded-hal-async = { version = "1.0.0", git = "https://github.com/rmsyn/embedded-hal", branch = "embedded-hal/mmc" }
3232
enumset = "1.1.5"
3333
paste = "1.0.15"
3434
portable-atomic = { version = "1.11.0", default-features = false }

esp-hal/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ pub mod i2c;
226226
pub mod peripheral;
227227
#[cfg(all(feature = "unstable", any(hmac, sha)))]
228228
mod reg_access;
229+
pub mod sdio;
229230
#[cfg(any(spi0, spi1, spi2, spi3))]
230231
pub mod spi;
231232
pub mod system;

esp-hal/src/sdio.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//! # Secure Digital I/O - Slave Mode
2+
//!
3+
//! ## Overiew
4+
//!
5+
//! The peripheral can be used to transfer data over the SDIO bus in `Slave` mode.
6+
7+
use embedded_hal::mmc::command::MmcCommand;
8+
use embedded_hal::mmc::response::MmcResponse;
9+
use embedded_hal::mmc::tuning::{TuningMode, TuningWidth};
10+
use embedded_hal::mmc::{CardMode, CardType, FifoStatus, MmcOps, Reset};
11+
12+
pub mod error;
13+
14+
use error::Error;
15+
16+
mod slc;
17+
mod slchost;
18+
19+
pub use slc::*;
20+
pub use slchost::*;
21+
22+
/// SDIO peripheral instance.
23+
pub trait PeripheralInstance: crate::private::Sealed {
24+
/// Represents the peripheral information type containing the register block.
25+
type Info;
26+
27+
/// Gets a static shared reference to the peripheral information.
28+
fn info(&self) -> &'static Self::Info;
29+
}
30+
31+
/// Represents the SDIO 2.0 peripheral for the microcontroller.
32+
#[derive(Debug)]
33+
pub struct Sdio<'d> {
34+
slc: AnySlc<'d>,
35+
slchost: AnySlchost<'d>,
36+
}
37+
38+
impl<'d> Sdio<'d> {
39+
/// Creates a new [Sdio].
40+
///
41+
/// # Example
42+
///
43+
/// ```rust,no_run
44+
/// use crate::peripheral::Peripherals;
45+
/// use esp_hal::sdio::Sdio;
46+
///
47+
/// let dp = Peripheral::take().unwrap();
48+
/// let _sdio = Sdio::new(dp.slc, dp.slchost);
49+
/// ```
50+
pub fn new(slc: impl SlcInstance + 'd, slchost: impl SlchostInstance + 'd) -> Self {
51+
Self {
52+
slc: slc.degrade(),
53+
slchost: slchost.degrade(),
54+
}
55+
}
56+
57+
/// Gets a static reference to the SLC information.
58+
pub fn slc(&self) -> &'static SlcInfo {
59+
self.slc.info()
60+
}
61+
62+
/// Gets a static reference to the SLCHOST information.
63+
pub fn slchost(&self) -> &'static SlchostInfo {
64+
self.slchost.info()
65+
}
66+
}
67+
68+
impl<'d> MmcOps for Sdio<'d> {
69+
type Error = Error;
70+
71+
fn card_type(&self) -> CardType {
72+
CardType::Sd
73+
}
74+
75+
fn card_mode(&self) -> CardMode {
76+
CardMode::Sdio
77+
}
78+
79+
fn setup_bus(&mut self) -> Result<(), Self::Error> {
80+
Err(Error::unimplemented())
81+
}
82+
83+
fn init(&mut self) -> Result<(), Self::Error> {
84+
Err(Error::unimplemented())
85+
}
86+
87+
fn set_sample_phase(&mut self, _sample_phase: u8) {}
88+
89+
fn fifo_ready(&self, _fifo_status: FifoStatus) -> Result<(), Self::Error> {
90+
Err(Error::unimplemented())
91+
}
92+
93+
fn wait_for_reset(&mut self, _reset: Reset, _timeout: u64) -> Result<(), Self::Error> {
94+
Err(Error::unimplemented())
95+
}
96+
97+
fn wait_while_busy(&mut self, _timout_us: u64) -> Result<(), Self::Error> {
98+
Err(Error::unimplemented())
99+
}
100+
101+
fn write_command<C: MmcCommand>(&mut self, _cmd: &C) -> Result<(), Self::Error> {
102+
Err(Error::unimplemented())
103+
}
104+
105+
fn read_response<C: MmcCommand, R: MmcResponse>(&mut self, _cmd: &C) -> Result<R, Self::Error> {
106+
Err(Error::unimplemented())
107+
}
108+
109+
fn response_bytes<const N: usize>(&mut self, _exp_crc: bool) -> Result<[u8; N], Self::Error> {
110+
Err(Error::unimplemented())
111+
}
112+
113+
fn read_data(&mut self, _data: &mut [u8]) -> Result<(), Self::Error> {
114+
Err(Error::unimplemented())
115+
}
116+
117+
fn write_data(&mut self, _data: &[u8]) -> Result<(), Self::Error> {
118+
Err(Error::unimplemented())
119+
}
120+
121+
fn send_tuning(&mut self, _mode: TuningMode, _width: TuningWidth) -> Result<(), Self::Error> {
122+
Err(Error::unimplemented())
123+
}
124+
125+
fn interrupt(&self) -> u32 {
126+
0
127+
}
128+
129+
fn set_interrupt(&mut self, _int: u32) {}
130+
131+
fn clear_all_interrupt(&mut self) {}
132+
133+
fn response_interrupt(&self) -> u32 {
134+
0
135+
}
136+
137+
fn set_response_interrupt(&mut self, _int: u32) {}
138+
139+
fn clear_all_response_interrupt(&mut self) {}
140+
}

esp-hal/src/sdio/error.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Error and result types for SDIO peripherals.
2+
3+
/// Represents the error variants for SDIO peripherals.
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5+
pub enum Error {
6+
/// The function and/or type is unimplemented.
7+
Unimplemented,
8+
}
9+
10+
impl Error {
11+
/// Creates a new [Error].
12+
pub const fn new() -> Self {
13+
Self::Unimplemented
14+
}
15+
16+
/// Creates an unimplemented [Error].
17+
pub const fn unimplemented() -> Self {
18+
Self::Unimplemented
19+
}
20+
}
21+
22+
impl Default for Error {
23+
fn default() -> Self {
24+
Self::new()
25+
}
26+
}
27+
28+
impl core::fmt::Display for Error {
29+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30+
match self {
31+
Self::Unimplemented => write!(f, "unimplemented"),
32+
}
33+
}
34+
}
35+
36+
impl core::error::Error for Error {}

esp-hal/src/sdio/slc.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::pac::slc;
2+
3+
use super::PeripheralInstance;
4+
5+
crate::any_peripheral! {
6+
/// Any SDIO SLC peripheral.
7+
pub peripheral AnySlc<'d> {
8+
Slc(crate::peripherals::SLC<'d>)
9+
}
10+
}
11+
12+
/// Represents the SLC registers for SDIO peripherals.
13+
pub struct SlcInfo {
14+
/// Represents the SLC register block.
15+
pub register_block: *const slc::RegisterBlock,
16+
}
17+
18+
unsafe impl Sync for SlcInfo {}
19+
20+
impl PeripheralInstance for AnySlc<'_> {
21+
type Info = SlcInfo;
22+
23+
fn info(&self) -> &'static Self::Info {
24+
static INFO: SlcInfo = SlcInfo {
25+
register_block: crate::peripherals::SLC::ptr(),
26+
};
27+
28+
&INFO
29+
}
30+
}
31+
32+
/// A peripheral singleton compatible with the SDIO SLC driver.
33+
pub trait SlcInstance: PeripheralInstance + IntoAnySlc {}
34+
35+
impl<'d> SlcInstance for AnySlc<'d> {}

esp-hal/src/sdio/slchost.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::pac::slchost;
2+
3+
use super::PeripheralInstance;
4+
5+
crate::any_peripheral! {
6+
/// Any SDIO peripheral.
7+
pub peripheral AnySlchost<'d> {
8+
Slchost(crate::peripherals::SLCHOST<'d>)
9+
}
10+
}
11+
12+
/// Represents the SLCHOST registers for SDIO peripherals.
13+
pub struct SlchostInfo {
14+
/// Represents the SLCHOST register block.
15+
pub register_block: *const slchost::RegisterBlock,
16+
}
17+
18+
unsafe impl Sync for SlchostInfo {}
19+
20+
impl PeripheralInstance for AnySlchost<'_> {
21+
type Info = SlchostInfo;
22+
23+
fn info(&self) -> &'static Self::Info {
24+
static INFO: SlchostInfo = SlchostInfo {
25+
register_block: crate::peripherals::SLCHOST::ptr(),
26+
};
27+
28+
&INFO
29+
}
30+
}
31+
32+
/// A peripheral singleton compatible with the SDIO SLCHOST driver.
33+
pub trait SlchostInstance: PeripheralInstance + IntoAnySlchost {}
34+
35+
impl<'d> SlchostInstance for AnySlchost<'d> {}

esp-hal/src/soc/esp32c6/peripherals.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ crate::peripherals! {
7979
RNG <= RNG,
8080
RSA <= RSA,
8181
SHA <= SHA,
82+
SLC <= SLC,
8283
SLCHOST <= SLCHOST,
8384
SOC_ETM <= SOC_ETM,
8485
SPI0 <= SPI0,

0 commit comments

Comments
 (0)