The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Device::Chip::SDCard - chip driver for SD and MMC cards

SYNOPSIS

use Device::Chip::SDCard;
use Future::AsyncAwait;

my $card = Device::Chip::SDCard->new;

await $card->mount( Device::Chip::Adapter::...->new );

await $card->initialise;

my $bytes = await $card->read_block( 0 );

print "Read block zero:\n";
printf "%v02X\n", $bytes;

DESCRIPTION

This Device::Chip subclass provides specific communication to an SD or MMC storage card attached via an SPI adapter.

At present it only supports MMC and SDSC ("standard capacity") cards, not SDHC or SDXC.

METHODS

The following methods documented in an await expression return Future instances.

initialise

await $card->initialise;

Checks that an SD card is present, switches it into SPI mode and waits for its initialisation process to complete.

size

$n_bytes = await $card->size;

Returns the size of the media card in bytes.

read_csd

$data = await $card->read_csd;

Returns a HASH reference containing decoded fields from the SD card's CSD ("card-specific data") register.

This hash will contain the following fields:

TAAC
NSAC
TRAN_SPEED
CCC
READ_BL_LEN
READ_BL_LEN_PARTIAL
WRITE_BLK_MISALIGN
READ_BLK_MISALIGN
DSR_IMP
C_SIZE
VDD_R_CURR_MIN
VDD_R_CURR_MAX
VDD_W_CURR_MIN
VDD_W_CURR_MAX
C_SIZE_MULT
ERASE_BLK_EN
SECTOR_SIZE
WP_GRP_SIZE
WP_GRP_ENABLE
R2W_FACTOR
WRITE_BL_LEN
WRITE_BL_PARTIAL
FILE_FORMAT_GRP
COPY
PERM_WRITE_PROTECT
TEMP_WRITE_PROTECT
FILE_FORMAT

The hash will also contain the following calculated fields, derived from the decoded fields above for convenience of calling code.

blocks          # number of blocks implied by C_SIZE / C_SIZE_MULT
bytes           # number of bytes of storage, implied by blocks and READ_BL_LEN

read_ocr

$fields = await $card->read_ocr;

Returns a HASH reference containing decoded fields from the card's OCR ("operating conditions register").

This hash will contain the following fields:

BUSY
CCS
UHS_II
1V8_ACCEPTED
3V5, 3V4, 3V3, ..., 2V7

read_block

$bytes = await $card->read_block( $lba );

Returns a 512-byte bytestring containing data read from the given sector of the card.

TODO

  • Support block writing.

  • Support the different initialisation sequence (and block size requirements) of SDHC cards.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>