Struct SdCard

Source
pub struct SdCard<SPI, DELAYER>
where SPI: SpiDevice<u8>, DELAYER: DelayNs,
{ /* 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>
where SPI: SpiDevice<u8>, DELAYER: DelayNs,

Source

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.

Source

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.

Source

pub fn spi<T, F>(&self, func: F) -> T
where F: FnOnce(&mut SPI) -> 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.

Source

pub fn num_bytes(&self) -> Result<u64, Error>

Return the usable size of this SD card in bytes.

This will trigger card (re-)initialisation.

Source

pub fn erase_single_block_enabled(&self) -> Result<bool, Error>

Can this card erase single blocks?

This will trigger card (re-)initialisation.

Source

pub fn mark_card_uninit(&self)

Mark the card as requiring a reset.

The next operation will assume the card has been freshly inserted.

Source

pub fn get_card_type(&self) -> Option<CardType>

Get the card type.

This will trigger card (re-)initialisation.

Source

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>
where SPI: SpiDevice<u8>, DELAYER: DelayNs,

Source§

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>

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>

Determine how many blocks this device can hold.

This will trigger card (re-)initialisation.

Source§

type Error = Error

The errors that the BlockDevice can return. Must be debug formattable.

Auto Trait Implementations§

§

impl<SPI, DELAYER> !Freeze for SdCard<SPI, DELAYER>

§

impl<SPI, DELAYER> !RefUnwindSafe for SdCard<SPI, DELAYER>

§

impl<SPI, DELAYER> Send for SdCard<SPI, DELAYER>
where SPI: Send, DELAYER: Send,

§

impl<SPI, DELAYER> !Sync for SdCard<SPI, DELAYER>

§

impl<SPI, DELAYER> Unpin for SdCard<SPI, DELAYER>
where SPI: Unpin, DELAYER: Unpin,

§

impl<SPI, DELAYER> UnwindSafe for SdCard<SPI, DELAYER>
where SPI: UnwindSafe, DELAYER: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.