Skip to content

Commit 3ae7018

Browse files
committed
Add init function
1 parent af92c87 commit 3ae7018

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/sdcard/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ where
132132
inner.card_type
133133
}
134134

135+
/// Initialize the SD card.
136+
///
137+
/// This must be called before performing any operations on the card, with
138+
/// SPI frequency of 100 to 400 KHz. After this method returns
139+
/// successfully, the SPI frequency can be increased to the maximum
140+
/// supported by the card.
141+
pub fn init_card(&self) -> Result<(), Error> {
142+
let mut inner = self.inner.borrow_mut();
143+
inner.init()?;
144+
Ok(())
145+
}
146+
135147
/// Tell the driver the card has been initialised.
136148
///
137149
/// This is here in case you were previously using the SD Card, and then a
@@ -563,6 +575,13 @@ where
563575
}
564576
Ok(())
565577
}
578+
579+
fn init(&mut self) -> Result<(), Error> {
580+
self.spi
581+
.send_clock_pulses()
582+
.map_err(|_e| Error::Transport)?;
583+
Ok(())
584+
}
566585
}
567586

568587
/// Options for acquiring the card.

src/sdcard/sd_card_device.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub trait SdCardDevice {
1919
operations: &mut [Operation<'_, u8>],
2020
) -> Result<(), SdCardDeviceError>;
2121

22+
/// Send 80 clock pulses to the device with CS deasserted.
23+
fn send_clock_pulses(&mut self) -> Result<(), SdCardDeviceError>;
24+
2225
/// Do a read within a transaction.
2326
///
2427
/// This is a convenience method equivalent to `device.transaction(&mut [Operation::Read(buf)])`.
@@ -84,6 +87,12 @@ where
8487
let mut bus = bus.borrow_mut();
8588
bus_transaction(&mut *bus, cs, operations)
8689
}
90+
91+
fn send_clock_pulses(&mut self) -> Result<(), SdCardDeviceError> {
92+
let (bus, cs) = self;
93+
let mut bus = bus.borrow_mut();
94+
send_clock_pulses(&mut *bus, cs)
95+
}
8796
}
8897

8998
#[cfg(feature = "embassy-sync-06")]
@@ -103,6 +112,14 @@ where
103112
bus_transaction(&mut *bus, cs, operations)
104113
})
105114
}
115+
116+
fn send_clock_pulses(&mut self) -> Result<(), SdCardDeviceError> {
117+
let (bus, cs) = self;
118+
bus.lock(|bus| {
119+
let mut bus = bus.borrow_mut();
120+
send_clock_pulses(&mut *bus, cs)
121+
})
122+
}
106123
}
107124

108125
// `ExclusiveDevice` represents exclusive access to the bus so there's no need to send the dummy
@@ -121,6 +138,21 @@ where
121138
<Self as embedded_hal::spi::SpiDevice>::transaction(self, operations)
122139
.map_err(|_| SdCardDeviceError::Spi)
123140
}
141+
142+
fn send_clock_pulses(&mut self) -> Result<(), SdCardDeviceError> {
143+
let bus = self.bus_mut();
144+
145+
// There's no way to access the CS pin here so we can't set it high. Most like it already high so this is probbaly fine(?)
146+
147+
let send_res = bus.write(&[0xFF; 10]);
148+
149+
// On failure, it's important to still flush.
150+
let flush_res = bus.flush().map_err(|_| SdCardDeviceError::Spi);
151+
152+
send_res.map_err(|_| SdCardDeviceError::Spi)?;
153+
flush_res.map_err(|_| SdCardDeviceError::Spi)?;
154+
Ok(())
155+
}
124156
}
125157

126158
fn bus_transaction<BUS, CS>(
@@ -162,3 +194,20 @@ where
162194

163195
Ok(())
164196
}
197+
198+
fn send_clock_pulses<BUS, CS>(bus: &mut BUS, cs: &mut CS) -> Result<(), SdCardDeviceError>
199+
where
200+
BUS: SpiBus,
201+
CS: OutputPin,
202+
{
203+
cs.set_high().map_err(|_| SdCardDeviceError::Cs)?;
204+
let send_res = bus.write(&[0xFF; 10]);
205+
206+
// On failure, it's important to still flush.
207+
let flush_res = bus.flush().map_err(|_| SdCardDeviceError::Spi);
208+
209+
send_res.map_err(|_| SdCardDeviceError::Spi)?;
210+
flush_res.map_err(|_| SdCardDeviceError::Spi)?;
211+
212+
Ok(())
213+
}

0 commit comments

Comments
 (0)