use embedded_hal_async::delay::DelayNs;
use embedded_hal_async::spi::{Operation, SpiBus, SpiDevice};
use crate::{BlockDevice, BLOCK_SIZE};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SpiError<E> {
Spi(E),
Timeout,
BadResponse(u8),
BadCrc,
UnsupportedCard,
OutOfRange,
WriteRejected(u8),
}
const READ_TOKEN_POLLS: u32 = 2_000;
const READ_TOKEN_POLL_US: u32 = 50;
const READ_RETRIES: usize = 2;
#[cfg(feature = "write")]
const WRITE_RETRIES: usize = 2;
#[cfg(feature = "write")]
const WRITE_FAST_POLLS: u32 = 64;
#[cfg(feature = "write")]
const WRITE_SLOW_POLLS: u32 = 500;
const R1_WINDOW: usize = 8;
const MIN_TOKEN_WINDOW: usize = 32;
const START_TOKEN_WINDOW: usize = 256;
const MAX_TOKEN_WINDOW: usize = 2_048;
const SCRATCH: usize = R1_WINDOW + MAX_TOKEN_WINDOW + 1 + BLOCK_SIZE + 2;
pub async fn send_init_clocks<B: SpiBus<u8>>(bus: &mut B) -> Result<(), B::Error> {
bus.write(&[0xff; 10]).await?;
bus.flush().await
}
pub struct SpiSdCard<SPI, D> {
spi: SPI,
delay: D,
high_capacity: bool,
blocks: u32,
check_crc: bool,
token_window: usize,
scratch: [u8; SCRATCH],
}
#[derive(Clone, Copy)]
enum Reply {
R1,
R1WithFourBytes,
}
#[derive(Clone, Copy)]
struct Response {
r1: u8,
extra: [u8; 4],
}
impl<SPI, D> SpiSdCard<SPI, D>
where
SPI: SpiDevice<u8>,
D: DelayNs,
{
pub async fn new(spi: SPI, delay: D) -> Result<Self, SpiError<SPI::Error>> {
Self::init(spi, delay).await.map_err(|(_, error)| error)
}
#[allow(clippy::type_complexity)]
pub async fn init(spi: SPI, delay: D) -> Result<Self, (SPI, SpiError<SPI::Error>)> {
let mut card = Self {
spi,
delay,
high_capacity: false,
blocks: 0,
check_crc: false,
token_window: START_TOKEN_WINDOW,
scratch: [0xff; SCRATCH],
};
match card.initialise().await {
Ok(()) => Ok(card),
Err(error) => Err((card.spi, error)),
}
}
async fn initialise(&mut self) -> Result<(), SpiError<SPI::Error>> {
self.delay.delay_ms(1).await;
let mut r1 = 0xff;
let mut timed_out = false;
for _ in 0..10 {
match self.command(0, 0, Reply::R1).await {
Ok(response) if response.r1 == 0x01 => {
r1 = 0x01;
break;
}
Ok(response) => {
r1 = response.r1;
timed_out = false;
}
Err(SpiError::Timeout) => {
r1 = 0xff;
timed_out = true;
}
Err(error) => return Err(error),
}
self.delay.delay_ms(1).await;
}
if r1 != 0x01 {
return Err(if timed_out {
SpiError::Timeout
} else {
SpiError::BadResponse(r1)
});
}
let interface = self.command(8, 0x1aa, Reply::R1WithFourBytes).await?;
let version_2 = interface.r1 & 0x04 == 0;
if version_2 && (interface.r1 != 0x01 || interface.extra != [0x00, 0x00, 0x01, 0xaa]) {
return Err(SpiError::BadResponse(interface.r1));
}
let host_capacity = if version_2 { 0x4000_0000 } else { 0 };
let mut ready = false;
for _ in 0..100 {
let app = self.command(55, 0, Reply::R1).await?;
if app.r1 > 0x01 {
return Err(SpiError::BadResponse(app.r1));
}
if self.command(41, host_capacity, Reply::R1).await?.r1 == 0x00 {
ready = true;
break;
}
self.delay.delay_ms(10).await;
}
if !ready {
return Err(SpiError::Timeout);
}
let ocr = self.command(58, 0, Reply::R1WithFourBytes).await?;
if ocr.r1 != 0x00 {
return Err(SpiError::BadResponse(ocr.r1));
}
self.high_capacity = ocr.extra[0] & 0x40 != 0;
if !self.high_capacity {
let length = self.command(16, BLOCK_SIZE as u32, Reply::R1).await?;
if length.r1 != 0x00 {
return Err(SpiError::BadResponse(length.r1));
}
}
self.check_crc = self.command(59, 1, Reply::R1).await?.r1 == 0x00;
let csd = self.read_csd().await?;
self.blocks = blocks_from_csd(&csd).ok_or(SpiError::UnsupportedCard)?;
Ok(())
}
pub fn into_spi(self) -> SPI {
self.spi
}
pub fn blocks(&self) -> u32 {
self.blocks
}
pub fn is_high_capacity(&self) -> bool {
self.high_capacity
}
pub fn spi_mut(&mut self) -> &mut SPI {
&mut self.spi
}
async fn read_block_inner(
&mut self,
block: u32,
buffer: &mut [u8; BLOCK_SIZE],
) -> Result<(), SpiError<SPI::Error>> {
let address = if self.high_capacity {
block
} else {
block
.checked_mul(BLOCK_SIZE as u32)
.ok_or(SpiError::OutOfRange)?
};
self.read_with_data(17, address, buffer).await
}
#[cfg(feature = "write")]
async fn write_block_inner(
&mut self,
block: u32,
buffer: &[u8; BLOCK_SIZE],
) -> Result<(), SpiError<SPI::Error>> {
if block >= self.blocks {
return Err(SpiError::OutOfRange);
}
let address = if self.high_capacity {
block
} else {
block
.checked_mul(BLOCK_SIZE as u32)
.ok_or(SpiError::OutOfRange)?
};
let mut result = self.write_block_once(address, buffer).await;
for _ in 0..WRITE_RETRIES {
if !matches!(result, Err(SpiError::BadCrc)) {
break;
}
result = self.write_block_once(address, buffer).await;
}
result
}
#[cfg(feature = "write")]
async fn write_block_once(
&mut self,
address: u32,
buffer: &[u8; BLOCK_SIZE],
) -> Result<(), SpiError<SPI::Error>> {
self.wait_not_busy().await?;
let frame = command_frame(24, address);
let crc = crc16(buffer).to_be_bytes();
let mut r1_window = [0xffu8; R1_WINDOW];
let mut response = [0xffu8; 1];
self.spi
.transaction(&mut [
Operation::Write(&frame),
Operation::TransferInPlace(&mut r1_window),
Operation::Write(&[0xfe]),
Operation::Write(buffer),
Operation::Write(&crc),
Operation::TransferInPlace(&mut response),
])
.await
.map_err(SpiError::Spi)?;
let r1 = r1_window
.iter()
.copied()
.find(|byte| *byte != 0xff)
.ok_or(SpiError::Timeout)?;
if r1 != 0x00 {
return Err(SpiError::BadResponse(r1));
}
if response[0] == 0xff {
return Err(SpiError::Timeout);
}
let status = match response[0] & 0x1f {
0x05 => Ok(()),
0x0b => Err(SpiError::BadCrc),
other => Err(SpiError::WriteRejected(other)),
};
self.wait_not_busy().await?;
status
}
#[cfg(feature = "write")]
async fn wait_not_busy(&mut self) -> Result<(), SpiError<SPI::Error>> {
for poll in 0..WRITE_FAST_POLLS + WRITE_SLOW_POLLS {
let mut byte = [0xffu8];
self.spi
.transaction(&mut [Operation::TransferInPlace(&mut byte)])
.await
.map_err(SpiError::Spi)?;
if byte[0] == 0xff {
return Ok(());
}
if poll < WRITE_FAST_POLLS {
self.delay.delay_us(READ_TOKEN_POLL_US).await;
} else {
self.delay.delay_ms(1).await;
}
}
Err(SpiError::Timeout)
}
async fn read_csd(&mut self) -> Result<[u8; 16], SpiError<SPI::Error>> {
let mut csd = [0u8; 16];
self.read_with_data(9, 0, &mut csd).await?;
Ok(csd)
}
async fn read_with_data(
&mut self,
index: u8,
argument: u32,
buffer: &mut [u8],
) -> Result<(), SpiError<SPI::Error>> {
let mut result = self.read_with_data_once(index, argument, buffer).await;
for _ in 0..READ_RETRIES {
if !matches!(
result,
Err(SpiError::BadResponse(_) | SpiError::Timeout | SpiError::BadCrc)
) {
break;
}
result = self.read_with_data_once(index, argument, buffer).await;
}
result
}
async fn read_with_data_once(
&mut self,
index: u8,
argument: u32,
buffer: &mut [u8],
) -> Result<(), SpiError<SPI::Error>> {
let frame = command_frame(index, argument);
let window = self.token_window;
let length = R1_WINDOW + window + 1 + buffer.len() + 2;
let scratch = &mut self.scratch[..length];
scratch.fill(0xff);
self.spi
.transaction(&mut [
Operation::Write(&frame),
Operation::TransferInPlace(scratch),
])
.await
.map_err(SpiError::Spi)?;
let r1_at = scratch[..R1_WINDOW]
.iter()
.position(|byte| *byte != 0xff)
.ok_or(SpiError::Timeout)?;
let r1 = scratch[r1_at];
if r1 != 0x00 {
return Err(SpiError::BadResponse(r1));
}
let first_idle = r1_at + 1;
let wanted = buffer.len() + 2;
let mut crc = [0xffu8; 2];
if let Some(gap) = scratch[first_idle..].iter().position(|byte| *byte != 0xff) {
let token_at = first_idle + gap;
let token = scratch[token_at];
let after = &scratch[token_at + 1..];
let have = after.len().min(wanted);
let data_have = have.min(buffer.len());
if token == 0xfe {
buffer[..data_have].copy_from_slice(&after[..data_have]);
crc[..have - data_have].copy_from_slice(&after[data_have..have]);
}
self.learn_gap(gap);
if token != 0xfe {
return Err(SpiError::BadResponse(token));
}
return if have == wanted {
self.check_data(buffer, u16::from_be_bytes(crc))
} else {
self.finish_block(buffer, data_have, crc, have - data_have)
.await
};
}
let mut waited = length - first_idle;
self.token_window = MAX_TOKEN_WINDOW;
let mut token = 0xff;
for _ in 0..READ_TOKEN_POLLS {
let mut byte = [0xff];
self.spi
.transaction(&mut [Operation::TransferInPlace(&mut byte)])
.await
.map_err(SpiError::Spi)?;
token = byte[0];
if token != 0xff {
break;
}
waited += 1;
self.delay.delay_us(READ_TOKEN_POLL_US).await;
}
if token != 0xfe {
return Err(SpiError::BadResponse(token));
}
self.learn_gap(waited);
self.finish_block(buffer, 0, crc, 0).await
}
async fn finish_block(
&mut self,
buffer: &mut [u8],
data_have: usize,
mut crc: [u8; 2],
crc_have: usize,
) -> Result<(), SpiError<SPI::Error>> {
buffer[data_have..].fill(0xff);
self.spi
.transaction(&mut [
Operation::TransferInPlace(&mut buffer[data_have..]),
Operation::TransferInPlace(&mut crc[crc_have..]),
])
.await
.map_err(SpiError::Spi)?;
self.check_data(buffer, u16::from_be_bytes(crc))
}
fn learn_gap(&mut self, gap: usize) {
self.token_window = (gap * 2 + MIN_TOKEN_WINDOW).clamp(MIN_TOKEN_WINDOW, MAX_TOKEN_WINDOW);
}
fn check_data(&self, buffer: &[u8], received: u16) -> Result<(), SpiError<SPI::Error>> {
if self.check_crc && crc16(buffer) != received {
return Err(SpiError::BadCrc);
}
Ok(())
}
async fn command(
&mut self,
index: u8,
argument: u32,
reply: Reply,
) -> Result<Response, SpiError<SPI::Error>> {
let frame = command_frame(index, argument);
let mut bytes = [0xffu8; 12];
self.spi
.transaction(&mut [
Operation::Write(&frame),
Operation::TransferInPlace(&mut bytes),
])
.await
.map_err(SpiError::Spi)?;
let position = bytes[..8]
.iter()
.position(|byte| *byte != 0xff)
.ok_or(SpiError::Timeout)?;
let mut extra = [0u8; 4];
if let Reply::R1WithFourBytes = reply {
extra.copy_from_slice(&bytes[position + 1..position + 5]);
}
Ok(Response {
r1: bytes[position],
extra,
})
}
}
impl<SPI, D> BlockDevice for SpiSdCard<SPI, D>
where
SPI: SpiDevice<u8>,
D: DelayNs,
{
type Error = SpiError<SPI::Error>;
async fn read_block(
&mut self,
block: u32,
buffer: &mut [u8; BLOCK_SIZE],
) -> Result<(), Self::Error> {
self.read_block_inner(block, buffer).await
}
async fn num_blocks(&mut self) -> Result<u32, Self::Error> {
Ok(self.blocks)
}
}
#[cfg(feature = "write")]
impl<SPI, D> crate::BlockWrite for SpiSdCard<SPI, D>
where
SPI: SpiDevice<u8>,
D: DelayNs,
{
async fn write_block(
&mut self,
block: u32,
buffer: &[u8; BLOCK_SIZE],
) -> Result<(), Self::Error> {
self.write_block_inner(block, buffer).await
}
}
fn command_frame(index: u8, argument: u32) -> [u8; 6] {
let mut frame = [
0x40 | index,
(argument >> 24) as u8,
(argument >> 16) as u8,
(argument >> 8) as u8,
argument as u8,
0,
];
frame[5] = crc7(&frame[..5]);
frame
}
fn blocks_from_csd(csd: &[u8; 16]) -> Option<u32> {
let structure = csd[0] >> 6;
let blocks = match structure {
0 => {
let read_bl_len = u32::from(csd[5] & 0x0f);
let c_size = (u64::from(csd[6] & 0x03) << 10)
| (u64::from(csd[7]) << 2)
| (u64::from(csd[8]) >> 6);
let c_size_mult = (u32::from(csd[9] & 0x03) << 1) | (u32::from(csd[10]) >> 7);
((c_size + 1) << (c_size_mult + 2 + read_bl_len)) / BLOCK_SIZE as u64
}
1 => {
let c_size =
(u64::from(csd[7] & 0x3f) << 16) | (u64::from(csd[8]) << 8) | u64::from(csd[9]);
(c_size + 1) * 1024
}
_ => return None,
};
Some(u32::try_from(blocks).unwrap_or(u32::MAX).max(1))
}
pub fn crc7(data: &[u8]) -> u8 {
let mut crc = 0u8;
for &byte in data {
for bit in (0..8).rev() {
crc <<= 1;
if ((byte >> bit) & 1) ^ ((crc >> 7) & 1) != 0 {
crc ^= 0x09;
}
}
}
((crc & 0x7f) << 1) | 1
}
pub fn crc16(data: &[u8]) -> u16 {
let mut crc = 0u16;
for &byte in data {
crc ^= u16::from(byte) << 8;
for _ in 0..8 {
crc = if crc & 0x8000 != 0 {
(crc << 1) ^ 0x1021
} else {
crc << 1
};
}
}
crc
}
#[cfg(test)]
mod tests {
use super::{blocks_from_csd, crc16, crc7};
#[test]
fn a_version_1_csd_with_the_largest_fields_does_not_overflow() {
let mut csd = [0u8; 16];
csd[5] = 0x0b;
csd[6] = 0x03;
csd[7] = 0xff;
csd[8] = 0xc0;
csd[9] = 0x03;
csd[10] = 0x80;
assert_eq!(blocks_from_csd(&csd), Some(1 << 23));
}
#[test]
fn a_version_2_csd_at_the_maximum_c_size_saturates_instead_of_overflowing() {
let mut csd = [0u8; 16];
csd[0] = 0x40;
csd[7] = 0x3f;
csd[8] = 0xff;
csd[9] = 0xff;
assert_eq!(blocks_from_csd(&csd), Some(u32::MAX));
}
#[test]
fn an_unknown_csd_structure_is_unsupported() {
let mut csd = [0u8; 16];
csd[0] = 0x80;
assert_eq!(blocks_from_csd(&csd), None);
}
#[test]
fn crc7_matches_the_sd_command_frames() {
assert_eq!(crc7(&[0x40, 0x00, 0x00, 0x00, 0x00]), 0x95);
assert_eq!(crc7(&[0x48, 0x00, 0x00, 0x01, 0xaa]), 0x87);
}
#[test]
fn crc16_matches_the_sd_data_reference() {
assert_eq!(crc16(b"123456789"), 0x31c3);
}
}