Skip to content

Commit f086ce3

Browse files
committed
start adding argument and response module
1 parent c7231dd commit f086ce3

6 files changed

Lines changed: 853 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ env_logger = "0.11.8"
3232
flate2 = "1.0"
3333
hex-literal = "1.0.0"
3434
sha2 = "0.10"
35+
anyhow = "1"
3536

3637
[features]
3738
default = ["log"]

examples/sd_card_init.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
use anyhow::{Context as _, bail};
2+
use embedded_sdmmc::sdcard::argument::{Acmd6, Cmd7, Cmd9, Cmd13, OcrLower, VoltageSuppliedSelect};
3+
use embedded_sdmmc::sdcard::mock::SdCardMock;
4+
use embedded_sdmmc::sdcard::response::{self, R1, R3, R6, R7};
5+
use embedded_sdmmc::sdcard::{AcmdId, CmdId, argument};
6+
use embedded_sdmmc::sdcard::{cid::Cid, csd::Csd};
7+
8+
/// Negotiated as part of ACMD41 during SD card initialization.
9+
pub const VOLTAGE_LEVEL_CAPABILITIES: OcrLower = OcrLower::builder()
10+
.with__3_5_to_3_6v(false)
11+
.with__3_4_to_3_5v(false)
12+
.with__3_3_to_3_4v(false)
13+
.with__3_2_to_3_3v(true)
14+
.with__3_1_to_3_2v(false)
15+
.with__3_0_to_3_1v(false)
16+
.with__2_9_to_3_0v(false)
17+
.with__2_8_to_2_9v(false)
18+
.with__2_7_to_2_8v(false)
19+
.with_reserved_low_voltage(false)
20+
.build();
21+
22+
pub struct SdCardUninitialized(SdCardMock);
23+
24+
impl SdCardUninitialized {
25+
pub fn new(sd_mock: SdCardMock) -> Self {
26+
Self(sd_mock)
27+
}
28+
29+
/// Example initialization sequence for a SD card which can provide
30+
/// a reference example for a real driver.
31+
///
32+
/// It performs the initialization sequence specified in the SD card
33+
/// physical layer specification Ver9.10, p.68 and p.70.
34+
pub fn initialize(mut self) -> Result<SdCard, anyhow::Error> {
35+
self.0.insert_command(CmdId::CMD0_GoIdleState, 0);
36+
37+
// Voltage level negotiation. Send CMD8 first.
38+
self.0.insert_command(
39+
CmdId::CMD8_SendIfCond,
40+
argument::Cmd8::ZERO
41+
.with_voltage_supplied(
42+
embedded_sdmmc::sdcard::argument::VoltageSuppliedSelect::_2_7To3_6V,
43+
)
44+
.with_check_pattern(0xAA)
45+
.raw_value(),
46+
);
47+
48+
let r7 = R7::new_with_raw_value(self.0.read_reply_u32());
49+
if r7
50+
.voltage_accepted()
51+
.is_ok_and(|val| val != VoltageSuppliedSelect::_2_7To3_6V)
52+
{
53+
bail!("CMD8 reply R7: Voltage not accepted");
54+
}
55+
if r7.echo_check_pattern() != 0xAA {
56+
bail!("CMD8 reply R7: Check pattern missmatch");
57+
}
58+
59+
// Now send ACMD41.
60+
self.0.insert_acmd(
61+
AcmdId::ACMD41_SdSendOpCond,
62+
argument::Acmd41::builder()
63+
.with_host_capacity_support(
64+
embedded_sdmmc::sdcard::argument::HostCapacitySupport::SdhcOrSdxc,
65+
)
66+
.with_fast_boot(false)
67+
.with_xpc(embedded_sdmmc::sdcard::argument::PowerControl::MaximumPerformance)
68+
.with_s18r(false)
69+
.with_ocr(VOLTAGE_LEVEL_CAPABILITIES)
70+
.build()
71+
.raw_value(),
72+
);
73+
loop {
74+
// Now poll until the card initialization is complete. In real driver code, timeout
75+
// handling or an upper polling limit might be a good idea.
76+
self.0.insert_acmd(AcmdId::ACMD41_SdSendOpCond, 0);
77+
let r3 = R3::new_with_raw_value(self.0.read_reply_u32());
78+
if r3.initialization_complete() {
79+
break;
80+
}
81+
}
82+
83+
// Retrieve and cache the CID. This puts it into identification mode.
84+
self.0.insert_command(CmdId::CMD2_AllSendCid, 0);
85+
let cid_raw = self.0.read_reply_u128();
86+
let cid = embedded_sdmmc::sdcard::cid::Cid::new_with_raw_value(cid_raw);
87+
88+
// Send CMD3 to retrieve RCA required for card addressing, as well as put the card
89+
// into standby mode.
90+
self.0.insert_command(CmdId::CMD3_SendRelativeAddr, 0);
91+
let r6 = R6::new_with_raw_value(self.0.read_reply_u32());
92+
let rca = r6.rca();
93+
94+
// Retrieve and cache CSD, which also contains card specific data.
95+
self.0
96+
.insert_command(CmdId::CMD9_SendCsd, Cmd9::ZERO.with_rca(rca).raw_value());
97+
let cid_raw = self.0.read_reply_u128();
98+
let csd = Csd::new(&cid_raw.to_be_bytes()).with_context(|| "failed to parse CSD")?;
99+
100+
// CMD7 to put the SD card into transfer state.
101+
self.0
102+
.insert_command(CmdId::CMD7_SelectCard, Cmd7::ZERO.with_rca(rca).raw_value());
103+
104+
// Check that the card is in transfer mode.
105+
self.0.insert_command(
106+
CmdId::CMD13_SendStatus,
107+
Cmd13::ZERO.with_rca(rca).raw_value(),
108+
);
109+
let r1 = R1::new_with_raw_value(self.0.read_reply_u32());
110+
111+
if r1.state().is_ok_and(|state| state != response::State::Tran) {
112+
bail!("CMD8 reply R1: Not in transfer state");
113+
}
114+
115+
// In this example, we put the SD card into 4-bit mode. On real hardware, you usually
116+
// have to configure register bits on the controller side as well.
117+
self.0.insert_acmd(
118+
AcmdId::ACMD6_SetBusWidth,
119+
Acmd6::builder()
120+
.with_bus_width(argument::BusWidth::_4bits)
121+
.build()
122+
.raw_value(),
123+
);
124+
125+
Ok(SdCard {
126+
cid,
127+
csd,
128+
rca,
129+
sd_mock: self.0,
130+
})
131+
}
132+
}
133+
134+
#[derive(Debug)]
135+
pub struct SdCard {
136+
cid: Cid,
137+
csd: Csd,
138+
rca: u16,
139+
#[allow(unused)]
140+
sd_mock: SdCardMock,
141+
}
142+
143+
const MOCK_SD_RCA: u16 = 1;
144+
145+
fn main() -> Result<(), anyhow::Error> {
146+
let sd_mock = SdCardMock::new(MOCK_SD_RCA);
147+
let sd_card_uninit = SdCardUninitialized::new(sd_mock);
148+
let sd_card = sd_card_uninit
149+
.initialize()
150+
.context("failed to initialize SD card")?;
151+
println!("SD card initialized successfully",);
152+
println!("--------");
153+
println!("Relative Card Address: {}", sd_card.rca);
154+
println!("--------");
155+
println!("CSD: {:?}", sd_card.csd);
156+
println!("--------");
157+
println!("CID: {:?}", sd_card.cid);
158+
Ok(())
159+
}

src/sdcard/argument.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//! # Argument module
2+
//!
3+
//! Arguments are additional command parameters.
4+
5+
use arbitrary_int::u24;
6+
7+
/// Voltage settings supplied in CMD8.
8+
#[bitbybit::bitenum(u4, exhaustive = false)]
9+
#[derive(Debug, Default, PartialEq, Eq)]
10+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
11+
pub enum VoltageSuppliedSelect {
12+
/// Regular voltage range.
13+
#[default]
14+
_2_7To3_6V = 0b0001,
15+
/// Reserved for low voltage.
16+
LowVoltageRange = 0b0010,
17+
}
18+
19+
/// CMD8 argument.
20+
#[bitbybit::bitfield(u32, default = 0x0, debug, defmt_fields(feature = "defmt-log"))]
21+
pub struct Cmd8 {
22+
/// PCIe v1.2 support.
23+
#[bit(13, rw)]
24+
pcie_1_2v_support: bool,
25+
/// PCIe available.
26+
#[bit(12, rw)]
27+
pcie_availability: bool,
28+
/// Voltage settings.
29+
#[bits(8..=11, rw)]
30+
voltage_supplied: Option<VoltageSuppliedSelect>,
31+
/// Check pattern which is echoed.
32+
#[bits(0..=7, rw)]
33+
check_pattern: u8,
34+
}
35+
36+
/// Power control (XPC) settings.
37+
#[bitbybit::bitenum(u1, exhaustive = true)]
38+
#[derive(Debug, PartialEq, Eq)]
39+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
40+
pub enum PowerControl {
41+
/// Power saving.
42+
PowerSaving = 0,
43+
/// Maximum performance.
44+
MaximumPerformance = 1,
45+
}
46+
47+
/// HPC.
48+
#[bitbybit::bitenum(u1, exhaustive = true)]
49+
#[derive(Debug, PartialEq, Eq)]
50+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
51+
pub enum HostCapacitySupport {
52+
/// SDSC only.
53+
SdscOnly = 0,
54+
/// Extended capacity - SDHC or SDXC.
55+
SdhcOrSdxc = 1,
56+
}
57+
58+
/// Lower OCR bits used to negotiate voltage capabilities.
59+
#[bitbybit::bitfield(u24, default = 0x0, debug, defmt_fields(feature = "defmt-log"))]
60+
pub struct OcrLower {
61+
/// 3.5 to 3.6V
62+
#[bit(23, rw)]
63+
_3_5_to_3_6v: bool,
64+
/// 3.4 to 3.5V
65+
#[bit(22, rw)]
66+
_3_4_to_3_5v: bool,
67+
/// 3.3 to 3.4V
68+
#[bit(21, rw)]
69+
_3_3_to_3_4v: bool,
70+
/// 3.2 to 3.3V
71+
#[bit(20, rw)]
72+
_3_2_to_3_3v: bool,
73+
/// 3.1 to 3.2V
74+
#[bit(19, rw)]
75+
_3_1_to_3_2v: bool,
76+
/// 3.0 to 3.1V
77+
#[bit(18, rw)]
78+
_3_0_to_3_1v: bool,
79+
/// 2.9 to 3.0V
80+
#[bit(17, rw)]
81+
_2_9_to_3_0v: bool,
82+
/// 2.8 to 2.9V
83+
#[bit(16, rw)]
84+
_2_8_to_2_9v: bool,
85+
/// 2.7 to 2.8V
86+
#[bit(15, rw)]
87+
_2_7_to_2_8v: bool,
88+
/// Reserved for low voltage
89+
#[bit(7, rw)]
90+
reserved_low_voltage: bool,
91+
}
92+
93+
/// Bus width setting.
94+
#[bitbybit::bitenum(u2, exhaustive = false)]
95+
#[derive(Debug, PartialEq, Eq)]
96+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
97+
pub enum BusWidth {
98+
/// 1 bit bus.
99+
_1bit = 0b00,
100+
/// 4 bit bus.
101+
_4bits = 0b10,
102+
}
103+
104+
/// ACMD6 - Set bus width.
105+
#[bitbybit::bitfield(
106+
u32,
107+
default = 0x0,
108+
debug,
109+
defmt_fields(feature = "defmt-log"),
110+
forbid_overlaps
111+
)]
112+
pub struct Acmd6 {
113+
/// Bus width.
114+
#[bits(0..=1, rw)]
115+
bus_width: Option<BusWidth>,
116+
}
117+
118+
/// ACMD41 argument - Capability negotiation.
119+
#[bitbybit::bitfield(
120+
u32,
121+
default = 0x0,
122+
debug,
123+
defmt_fields(feature = "defmt-log"),
124+
forbid_overlaps
125+
)]
126+
pub struct Acmd41 {
127+
/// HPC.
128+
#[bit(30, rw)]
129+
host_capacity_support: HostCapacitySupport,
130+
/// FB.
131+
#[bit(29, rw)]
132+
fast_boot: bool,
133+
/// XPC.
134+
#[bit(28, rw)]
135+
xpc: PowerControl,
136+
/// Switch to 1.8V signal voltage request.
137+
#[bit(24, rw)]
138+
s18r: bool,
139+
/// If this is set to 0. ACMD41 is called "inquire CMD41" that does not start
140+
/// initialization and is used for getting OCR. Bits 24 to 31 are ignored.
141+
#[bits(0..=23, rw)]
142+
ocr: OcrLower,
143+
}
144+
145+
/// Relative Card Address (RCA) select.
146+
#[bitbybit::bitfield(
147+
u32,
148+
default = 0x0,
149+
debug,
150+
defmt_bitfields(feature = "defmt-log"),
151+
forbid_overlaps
152+
)]
153+
pub struct RcaSelect {
154+
/// RCA.
155+
#[bits(16..=31, rw)]
156+
rca: u16,
157+
}
158+
159+
/// CMD9 argument.
160+
pub type Cmd9 = RcaSelect;
161+
/// CMD7 argument.
162+
pub type Cmd7 = RcaSelect;
163+
164+
/// CMD13 argument.
165+
#[bitbybit::bitfield(
166+
u32,
167+
default = 0x0,
168+
debug,
169+
defmt_bitfields(feature = "defmt-log"),
170+
forbid_overlaps
171+
)]
172+
pub struct Cmd13 {
173+
/// RCA.
174+
#[bits(16..=31, rw)]
175+
rca: u16,
176+
/// Send task status instead of regular card status.
177+
#[bit(15, rw)]
178+
send_task_status: bool,
179+
}

0 commit comments

Comments
 (0)