Skip to main content

SpiSdCard

Struct SpiSdCard 

Source
pub struct SpiSdCard<SPI, D> { /* private fields */ }
Expand description

An SD card behind a SpiDevice, so the bus can be shared with other devices. Every device on a shared bus keeps its own clock and mode in its own SpiDevice; this driver never touches the bus configuration. The card is read-only unless the write feature is on.

The SD spec wants chip select low from the command through the data block. A read is therefore one transaction: the command frame, a window for the R1 and the idle bytes before the data token, and the block with its CRC. The window follows the gap the card shows (it clocks the whole window every time, so it stays as small as the card allows). If the token has not arrived when the window ends, the read falls back to polling for it with chip select released between polls, which a card that aborts on chip select rising will fail; the window then grows, so a retry takes the single transaction path.

The SpiDevice must be at a clock of 400 kHz or less for new, and can be raised afterwards through spi_mut. A card’s MISO driver can take a clock edge to let go of the line after chip select rises, so on a bus shared with a device that is selected right afterwards the first bit it sends can be corrupted; give the card its own bus if that matters.

Implementations§

Source§

impl<SPI, D> SpiSdCard<SPI, D>
where SPI: SpiDevice<u8>, D: DelayNs,

Source

pub async fn new(spi: SPI, delay: D) -> Result<Self, SpiError<SPI::Error>>

Initialises the card: resets it, negotiates SDHC/SDXC, turns on CRCs and reads its size. The SpiDevice must be at 400 kHz or less, and the card must already have had its 74 init clocks (see send_init_clocks).

Source

pub async fn init( spi: SPI, delay: D, ) -> Result<Self, (SPI, SpiError<SPI::Error>)>

new, giving the SPI device back when the card does not initialise (none fitted, a bad card), so the caller can try again.

Source

pub fn into_spi(self) -> SPI

The SPI device, when the card is no longer wanted.

Source

pub fn blocks(&self) -> u32

The card’s size in 512-byte blocks.

Source

pub fn is_high_capacity(&self) -> bool

Whether the card is SDHC or SDXC (block addressed) rather than standard capacity.

Source

pub fn spi_mut(&mut self) -> &mut SPI

The device, for raising its clock once the card is initialised.

Trait Implementations§

Source§

impl<SPI, D> BlockDevice for SpiSdCard<SPI, D>
where SPI: SpiDevice<u8>, D: DelayNs,

Source§

type Error = SpiError<<SPI as ErrorType>::Error>

The error a read can fail with.
Source§

async fn read_block( &mut self, block: u32, buffer: &mut [u8; 512], ) -> Result<(), Self::Error>

Reads block number block (counted from 0) into buffer.
Source§

async fn num_blocks(&mut self) -> Result<u32, Self::Error>

The number of blocks the device holds.

Auto Trait Implementations§

§

impl<SPI, D> Forget for SpiSdCard<SPI, D>

§

impl<SPI, D> Freeze for SpiSdCard<SPI, D>
where SPI: Freeze, D: Freeze,

§

impl<SPI, D> Move for SpiSdCard<SPI, D>

§

impl<SPI, D> RefUnwindSafe for SpiSdCard<SPI, D>

§

impl<SPI, D> Send for SpiSdCard<SPI, D>
where D: Send, SPI: Send,

§

impl<SPI, D> Sync for SpiSdCard<SPI, D>
where D: Sync, SPI: Sync,

§

impl<SPI, D> Unpin for SpiSdCard<SPI, D>
where D: Unpin, SPI: Unpin,

§

impl<SPI, D> UnsafeUnpin for SpiSdCard<SPI, D>
where SPI: UnsafeUnpin, D: UnsafeUnpin,

§

impl<SPI, D> UnwindSafe for SpiSdCard<SPI, D>
where SPI: UnwindSafe, D: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.