pub struct SdCard<SPI, DELAYER>{ /* private fields */ }
Expand description
Driver for an SD Card on an SPI bus.
Built from an SpiDevice
implementation and a Chip Select pin.
Before talking to the SD Card, the caller needs to send 74 clocks cycles on the SPI Clock line, at 400 kHz, with no chip-select asserted (or at least, not the chip-select of the SD Card).
This kind of breaks the embedded-hal model, so how to do this is left to the caller. You could drive the SpiBus directly, or use an SpiDevice with a dummy chip-select pin. Or you could try just not doing the 74 clocks and see if your card works anyway - some do, some don’t.
All the APIs take &self
- mutability is handled using an inner RefCell
.
Implementations§
Source§impl<SPI, DELAYER> SdCard<SPI, DELAYER>
impl<SPI, DELAYER> SdCard<SPI, DELAYER>
Sourcepub fn new(spi: SPI, delayer: DELAYER) -> SdCard<SPI, DELAYER>
pub fn new(spi: SPI, delayer: DELAYER) -> SdCard<SPI, DELAYER>
Create a new SD/MMC Card driver using a raw SPI interface.
The card will not be initialised at this time. Initialisation is deferred until a method is called on the object.
Uses the default options.
Sourcepub fn new_with_options(
spi: SPI,
delayer: DELAYER,
options: AcquireOpts,
) -> SdCard<SPI, DELAYER>
pub fn new_with_options( spi: SPI, delayer: DELAYER, options: AcquireOpts, ) -> SdCard<SPI, DELAYER>
Construct a new SD/MMC Card driver, using a raw SPI interface and the given options.
See the docs of the SdCard
struct for more information about
how to construct the needed SPI
and CS
types.
The card will not be initialised at this time. Initialisation is deferred until a method is called on the object.
Sourcepub fn spi<T, F>(&self, func: F) -> T
pub fn spi<T, F>(&self, func: F) -> T
Get a temporary borrow on the underlying SPI device.
The given closure will be called exactly once, and will be passed a mutable reference to the underlying SPI object.
Useful if you need to re-clock the SPI, but does not perform card initialisation.
Sourcepub fn num_bytes(&self) -> Result<u64, Error>
pub fn num_bytes(&self) -> Result<u64, Error>
Return the usable size of this SD card in bytes.
This will trigger card (re-)initialisation.
Sourcepub fn erase_single_block_enabled(&self) -> Result<bool, Error>
pub fn erase_single_block_enabled(&self) -> Result<bool, Error>
Can this card erase single blocks?
This will trigger card (re-)initialisation.
Sourcepub fn mark_card_uninit(&self)
pub fn mark_card_uninit(&self)
Mark the card as requiring a reset.
The next operation will assume the card has been freshly inserted.
Sourcepub fn get_card_type(&self) -> Option<CardType>
pub fn get_card_type(&self) -> Option<CardType>
Get the card type.
This will trigger card (re-)initialisation.
Sourcepub unsafe fn mark_card_as_init(&self, card_type: CardType)
pub unsafe fn mark_card_as_init(&self, card_type: CardType)
Tell the driver the card has been initialised.
This is here in case you were previously using the SD Card, and then a previous instance of this object got destroyed but you know for certain the SD Card remained powered up and initialised, and you’d just like to read/write to/from the card again without going through the initialisation sequence again.
§Safety
Only do this if the SD Card has actually been initialised. That is, if
you have been through the card initialisation sequence as specified in
the SD Card Specification by sending each appropriate command in turn,
either manually or using another variable of this SdCard
. The card
must also be of the indicated type. Failure to uphold this will cause
data corruption.
Trait Implementations§
Source§impl<SPI, DELAYER> BlockDevice for SdCard<SPI, DELAYER>
impl<SPI, DELAYER> BlockDevice for SdCard<SPI, DELAYER>
Source§fn read(
&self,
blocks: &mut [Block],
start_block_idx: BlockIdx,
_reason: &str,
) -> Result<(), Self::Error>
fn read( &self, blocks: &mut [Block], start_block_idx: BlockIdx, _reason: &str, ) -> Result<(), Self::Error>
Read one or more blocks, starting at the given block index.
This will trigger card (re-)initialisation.
Source§fn write(
&self,
blocks: &[Block],
start_block_idx: BlockIdx,
) -> Result<(), Self::Error>
fn write( &self, blocks: &[Block], start_block_idx: BlockIdx, ) -> Result<(), Self::Error>
Write one or more blocks, starting at the given block index.
This will trigger card (re-)initialisation.
Source§fn num_blocks(&self) -> Result<BlockCount, Self::Error>
fn num_blocks(&self) -> Result<BlockCount, Self::Error>
Determine how many blocks this device can hold.
This will trigger card (re-)initialisation.