|
| 1 | +//! SD card device trait and provided implementations. |
| 2 | +
|
| 3 | +use core::cell::RefCell; |
| 4 | + |
| 5 | +use embedded_hal::{ |
| 6 | + digital::OutputPin, |
| 7 | + spi::{Operation, SpiBus}, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Trait for SD cards connected via SPI. |
| 11 | +pub trait SdCardDevice { |
| 12 | + /// Perform a transaction against the device. |
| 13 | + /// |
| 14 | + /// This is similar to [`embedded_hal::spi::SpiDevice::transaction`], except that this sends |
| 15 | + /// a dummy `0xFF` byte to the device after deasserting the CS pin but before unlocking the |
| 16 | + /// bus. |
| 17 | + fn transaction( |
| 18 | + &mut self, |
| 19 | + operations: &mut [Operation<'_, u8>], |
| 20 | + ) -> Result<(), SdCardDeviceError>; |
| 21 | + |
| 22 | + /// Do a read within a transaction. |
| 23 | + /// |
| 24 | + /// This is a convenience method equivalent to `device.transaction(&mut [Operation::Read(buf)])`. |
| 25 | + /// |
| 26 | + /// See also: [`SdCardDevice::transaction`], [`embedded_hal::spi::SpiBus::read`] |
| 27 | + #[inline] |
| 28 | + fn read(&mut self, buf: &mut [u8]) -> Result<(), SdCardDeviceError> { |
| 29 | + self.transaction(&mut [Operation::Read(buf)]) |
| 30 | + } |
| 31 | + |
| 32 | + /// Do a write within a transaction. |
| 33 | + /// |
| 34 | + /// This is a convenience method equivalent to `device.transaction(&mut [Operation::Write(buf)])`. |
| 35 | + /// |
| 36 | + /// See also: [`SdCardDevice::transaction`], [`embedded_hal::spi::SpiBus::write`] |
| 37 | + #[inline] |
| 38 | + fn write(&mut self, buf: &[u8]) -> Result<(), SdCardDeviceError> { |
| 39 | + self.transaction(&mut [Operation::Write(buf)]) |
| 40 | + } |
| 41 | + |
| 42 | + /// Do a transfer within a transaction. |
| 43 | + /// |
| 44 | + /// This is a convenience method equivalent to `device.transaction(&mut [Operation::Transfer(read, write)]`. |
| 45 | + /// |
| 46 | + /// See also: [`SdCardDevice::transaction`], [`embedded_hal::spi::SpiBus::transfer`] |
| 47 | + #[inline] |
| 48 | + fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), SdCardDeviceError> { |
| 49 | + self.transaction(&mut [Operation::Transfer(read, write)]) |
| 50 | + } |
| 51 | + |
| 52 | + /// Do an in-place transfer within a transaction. |
| 53 | + /// |
| 54 | + /// This is a convenience method equivalent to `device.transaction(&mut [Operation::TransferInPlace(buf)]`. |
| 55 | + /// |
| 56 | + /// See also: [`SdCardDevice::transaction`], [`embedded_hal::spi::SpiBus::transfer_in_place`] |
| 57 | + #[inline] |
| 58 | + fn transfer_in_place(&mut self, buf: &mut [u8]) -> Result<(), SdCardDeviceError> { |
| 59 | + self.transaction(&mut [Operation::TransferInPlace(buf)]) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Errors that can occur when using the [`SdCardDevice`]. |
| 64 | +#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 65 | +#[cfg_attr(feature = "defmt-log", derive(defmt::Format))] |
| 66 | +#[non_exhaustive] |
| 67 | +pub enum SdCardDeviceError { |
| 68 | + /// An operation on the inner SPI bus failed. |
| 69 | + Spi, |
| 70 | + /// Setting the value of the Chip Select (CS) pin failed. |
| 71 | + Cs, |
| 72 | +} |
| 73 | + |
| 74 | +impl<BUS, CS> SdCardDevice for (&RefCell<BUS>, CS) |
| 75 | +where |
| 76 | + BUS: SpiBus, |
| 77 | + CS: OutputPin, |
| 78 | +{ |
| 79 | + fn transaction( |
| 80 | + &mut self, |
| 81 | + operations: &mut [Operation<'_, u8>], |
| 82 | + ) -> Result<(), SdCardDeviceError> { |
| 83 | + let (bus, cs) = self; |
| 84 | + let mut bus = bus.borrow_mut(); |
| 85 | + bus_transaction(&mut *bus, cs, operations) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(feature = "embassy-sync-06")] |
| 90 | +impl<CS, BUS, M> SdCardDevice for (&embassy_sync_06::blocking_mutex::Mutex<M, RefCell<BUS>>, CS) |
| 91 | +where |
| 92 | + CS: OutputPin, |
| 93 | + BUS: SpiBus, |
| 94 | + M: embassy_sync_06::blocking_mutex::raw::RawMutex, |
| 95 | +{ |
| 96 | + fn transaction( |
| 97 | + &mut self, |
| 98 | + operations: &mut [Operation<'_, u8>], |
| 99 | + ) -> Result<(), SdCardDeviceError> { |
| 100 | + let (bus, cs) = self; |
| 101 | + bus.lock(|bus| { |
| 102 | + let mut bus = bus.borrow_mut(); |
| 103 | + bus_transaction(&mut *bus, cs, operations) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// `ExclusiveDevice` represents exclusive access to the bus so there's no need to send the dummy |
| 109 | +// byte after deasserting the CS pin. We can defer the implementation to the `embedded_hal` trait. |
| 110 | +#[cfg(feature = "embedded-hal-bus-03")] |
| 111 | +impl<CS, BUS, D> SdCardDevice for embedded_hal_bus_03::spi::ExclusiveDevice<BUS, CS, D> |
| 112 | +where |
| 113 | + BUS: SpiBus, |
| 114 | + CS: OutputPin, |
| 115 | + D: embedded_hal::delay::DelayNs, |
| 116 | +{ |
| 117 | + fn transaction( |
| 118 | + &mut self, |
| 119 | + operations: &mut [Operation<'_, u8>], |
| 120 | + ) -> Result<(), SdCardDeviceError> { |
| 121 | + <Self as embedded_hal::spi::SpiDevice>::transaction(self, operations) |
| 122 | + .map_err(|_| SdCardDeviceError::Spi) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +fn bus_transaction<BUS, CS>( |
| 127 | + bus: &mut BUS, |
| 128 | + cs: &mut CS, |
| 129 | + operations: &mut [Operation<'_, u8>], |
| 130 | +) -> Result<(), SdCardDeviceError> |
| 131 | +where |
| 132 | + BUS: SpiBus, |
| 133 | + CS: OutputPin, |
| 134 | +{ |
| 135 | + cs.set_low().map_err(|_| SdCardDeviceError::Cs)?; |
| 136 | + |
| 137 | + let op_res = operations.iter_mut().try_for_each(|op| match op { |
| 138 | + Operation::Read(buf) => bus.read(buf), |
| 139 | + Operation::Write(buf) => bus.write(buf), |
| 140 | + Operation::Transfer(read, write) => bus.transfer(read, write), |
| 141 | + Operation::TransferInPlace(buf) => bus.transfer_in_place(buf), |
| 142 | + Operation::DelayNs(_) => { |
| 143 | + // We don't use delays in SPI transations in this crate so it fine to panic here. |
| 144 | + panic!("Tried to use a delay in a SPI transaction. This is a bug in embedded-sdmmc.") |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + // On failure, it's important to still flush and deassert CS. |
| 149 | + let flush_res = bus.flush(); |
| 150 | + let cs_res = cs.set_high(); |
| 151 | + |
| 152 | + op_res.map_err(|_| SdCardDeviceError::Spi)?; |
| 153 | + flush_res.map_err(|_| SdCardDeviceError::Spi)?; |
| 154 | + cs_res.map_err(|_| SdCardDeviceError::Cs)?; |
| 155 | + |
| 156 | + // Write the dummy byte |
| 157 | + let dummy_res = bus.write(&[0xFF]); |
| 158 | + let flush_res = bus.flush(); |
| 159 | + |
| 160 | + dummy_res.map_err(|_| SdCardDeviceError::Spi)?; |
| 161 | + flush_res.map_err(|_| SdCardDeviceError::Spi)?; |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
0 commit comments