px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2

Description

Driver that communicates with an SD (Secure Digital) Card using SPI.

File(s):

Reference:

Data Structures

struct  px_sd_cid_t
 CID - Card ID register; Ref 1. Paragraph "5.2 CID Register", page 113. More...
 
struct  px_sd_csd_t
 CSD - Card Specific Data register; Paragraph "5.3 CSD Register", page 114. More...
 
union  px_sd_csd_t.ver
 
struct  px_sd_csd_t.ver.csd_1_0
 
struct  px_sd_csd_t.ver.csd_2_0
 

Macros

#define PX_SD_MAX_SPI_CLOCK_HZ   25000000ul
 Maximum SPI Clock rate. More...
 
#define PX_SD_SPI_MODE   PX_SPI_MODE0
 SPI Clock / Data phase. More...
 
#define PX_SD_SPI_DATA_ORDER   PX_SPI_DATA_ORDER_MSB
 SPI Data order. More...
 
#define PX_SD_SPI_MO_DUMMY_BYTE   0xff
 SPI Master Out dummy byte. More...
 
#define PX_SD_BLOCK_SIZE   512ul
 Size of each data block. More...
 

Functions

void px_sd_init (px_spi_handle_t *handle)
 Initialise driver. More...
 
bool px_sd_reset (void)
 Detect and reset SD card. More...
 
bool px_sd_rd_cid (px_sd_cid_t *cid)
 Read Card ID Register. More...
 
bool px_sd_rd_csd (px_sd_csd_t *csd)
 Read Card Specific Data. More...
 
bool px_sd_get_status (uint16_t *status)
 Read Card Status. More...
 
uint32_t px_sd_get_capacity_in_blocks (const px_sd_csd_t *csd)
 Extract and calculate card capacity from Card Specific Data (CSD). More...
 
bool px_sd_rd_block (uint8_t *data, uint32_t block_adr)
 Read a data block from the SD card. More...
 
uint8_t px_sd_rd_blocks (uint8_t *data, uint32_t block_adr, uint8_t nr_of_blocks)
 Read a number of data blocks from the SD card. More...
 
bool px_sd_wr_block (const uint8_t *data, uint32_t block_adr)
 Write a data block to the SD card. More...
 
uint8_t px_sd_wr_blocks (const uint8_t *data, uint32_t block_adr, uint8_t nr_of_blocks)
 Write a number of data blocks to the SD card. More...
 
bool px_sd_wait_wr_is_finished (void)
 Wait up to 500 ms for write block transaction to finish. More...
 

Data Structure Documentation

◆ px_sd_cid_t

struct px_sd_cid_t

CID - Card ID register; Ref 1. Paragraph "5.2 CID Register", page 113.

Definition at line 68 of file px_sd.h.

Data Fields
uint8_t mid Manufacturer ID.
char oid[2] OEM/Application ID [2 ASCII chars].
char pnm[5] Product name [5 ASCII chars].
uint8_t prv BCD Product revision [Major.Minor].
uint32_t psn Product serial number
uint8_t year_hi: 4 Manufacturing date [xYYM].
uint8_t res1: 4
uint8_t year_lo: 4 Manufacturing date [xYYM].
uint8_t month: 4 Manufacturing date [xYYM].
uint8_t crc7_end_bit

◆ px_sd_csd_t

struct px_sd_csd_t

CSD - Card Specific Data register; Paragraph "5.3 CSD Register", page 114.

Definition at line 83 of file px_sd.h.

Data Fields
uint8_t reserved1: 6
uint8_t csd_structure: 2 CSD structure version 1.0 or 2.0.
uint8_t taac data read access-time-1
uint8_t nsac data read access-time-2 in CLK cycles (NSAC*100)
uint8_t tran_speed max. data transfer rate
uint8_t ccc_hi card command classes
uint8_t read_bl_len: 4 max. read data block length
uint8_t ccc_lo: 4 card command classes
union px_sd_csd_t.ver ver
uint8_t write_bl_len_hi: 2 max. write data block length
uint8_t r2w_factor: 3 write speed factor
uint8_t reserved3: 2
uint8_t wp_grp_enable: 1 write protect group enable
uint8_t reserved4: 5
uint8_t write_partial: 1 partial blocks for write allowed
uint8_t write_bl_len_low: 2 max. write data block length
uint8_t reserved5: 2
uint8_t file_format: 2 File format.
uint8_t tmp_write_protect: 1 temporary write protection
uint8_t perm_write_protect: 1 permanent write protection
uint8_t copy: 1 copy flag
uint8_t file_format_grp: 1 File format.
uint8_t crc7_end_bit CRC.

◆ px_sd_csd_t.ver

union px_sd_csd_t.ver

Definition at line 100 of file px_sd.h.

◆ px_sd_csd_t.ver.csd_1_0

struct px_sd_csd_t.ver.csd_1_0

Definition at line 102 of file px_sd.h.

Data Fields
uint8_t c_size_hi: 2 device size
uint8_t reserved2: 2
uint8_t dsr_imp: 1 DSR implemented.
uint8_t read_blk_misalign: 1 read block misalignment
uint8_t write_blk_misalign: 1 write block misalignment
uint8_t read_bl_partial: 1 partial blocks for read allowed
uint8_t c_size_mid device size
uint8_t vdd_r_curr_max: 3 max. read current at VDD max
uint8_t vdd_r_curr_min: 3 max. read current at VDD min
uint8_t c_size_lo: 2 device size
uint8_t c_size_mult_hi: 2 device size multiplier
uint8_t vdd_w_cur_max: 3 max. write current at VDD max
uint8_t vdd_w_curr_min: 3 max. write current at VDD min
uint8_t sector_size_hi: 6 erase sector size
uint8_t erase_blk_en: 1 erase single block enable
uint8_t c_size_mult_lo: 1 device size multiplier
uint8_t wp_grp_size: 7 write protect group size
uint8_t sector_size_lo: 1 erase sector size

◆ px_sd_csd_t.ver.csd_2_0

struct px_sd_csd_t.ver.csd_2_0

Definition at line 130 of file px_sd.h.

Data Fields
uint8_t reserved2: 4
uint8_t dsr_imp: 1 DSR implemented.
uint8_t read_blk_misalign: 1 read block misalignment
uint8_t write_blk_misalign: 1 write block misalignment
uint8_t read_bl_partial: 1 partial blocks for read allowed
uint8_t c_size_hi: 6 device size
uint8_t reserved3: 2
uint8_t c_size_mid: 8 device size
uint8_t c_size_lo: 8 device size
uint8_t sector_size_hi: 6 erase sector size
uint8_t erase_blk_en: 1 erase single block enable
uint8_t reserved4: 1
uint8_t wp_grp_size: 7 write protect group size
uint8_t sector_size_lo: 1 erase sector size

Macro Definition Documentation

◆ PX_SD_MAX_SPI_CLOCK_HZ

#define PX_SD_MAX_SPI_CLOCK_HZ   25000000ul

Maximum SPI Clock rate.

Definition at line 47 of file px_sd.h.

◆ PX_SD_SPI_MODE

#define PX_SD_SPI_MODE   PX_SPI_MODE0

SPI Clock / Data phase.

Definition at line 49 of file px_sd.h.

◆ PX_SD_SPI_DATA_ORDER

#define PX_SD_SPI_DATA_ORDER   PX_SPI_DATA_ORDER_MSB

SPI Data order.

Definition at line 51 of file px_sd.h.

◆ PX_SD_SPI_MO_DUMMY_BYTE

#define PX_SD_SPI_MO_DUMMY_BYTE   0xff

SPI Master Out dummy byte.

Definition at line 53 of file px_sd.h.

◆ PX_SD_BLOCK_SIZE

#define PX_SD_BLOCK_SIZE   512ul

Size of each data block.

Definition at line 56 of file px_sd.h.

Function Documentation

◆ px_sd_init()

void px_sd_init ( px_spi_handle_t handle)

Initialise driver.

Parameters
handleSPI handle to use for SPI slave device

Definition at line 325 of file px_sd.c.

◆ px_sd_reset()

bool px_sd_reset ( void  )

Detect and reset SD card.

Return values
trueSD card detected and reset to known state
falseSD card not detected or not supported

Definition at line 331 of file px_sd.c.

◆ px_sd_rd_cid()

bool px_sd_rd_cid ( px_sd_cid_t cid)

Read Card ID Register.

Parameters
cidStructure to store data
Return values
trueCard ID register succesfully read
falseError while trying to read Card ID register

Definition at line 514 of file px_sd.c.

◆ px_sd_rd_csd()

bool px_sd_rd_csd ( px_sd_csd_t csd)

Read Card Specific Data.

Parameters
csdStructure to store data
Return values
trueCard Specific Data succesfully read
falseError while trying to read Card Specific Data

Definition at line 539 of file px_sd.c.

◆ px_sd_get_status()

bool px_sd_get_status ( uint16_t *  status)

Read Card Status.

Parameters
statusVariable to store card status in
Return values
trueCard Status succesfully read
falseError while trying to read Card Status

Definition at line 571 of file px_sd.c.

◆ px_sd_get_capacity_in_blocks()

uint32_t px_sd_get_capacity_in_blocks ( const px_sd_csd_t csd)

Extract and calculate card capacity from Card Specific Data (CSD).

Parameters
csdCard Specific Data
Returns
uint32_t Card capacity in number of blocks (see PX_SD_BLOCK_SIZE)

Definition at line 593 of file px_sd.c.

◆ px_sd_rd_block()

bool px_sd_rd_block ( uint8_t *  data,
uint32_t  block_adr 
)

Read a data block from the SD card.

The data block size is PX_SD_BLOCK_SIZE and the specified array must be big enough to accept the whole data block.

Parameters
dataPointer to array where data block must be stored
block_adrAddress of block to read.
Return values
trueData block succcesfully read
falseError while trying to read data block

Definition at line 652 of file px_sd.c.

◆ px_sd_rd_blocks()

uint8_t px_sd_rd_blocks ( uint8_t *  data,
uint32_t  block_adr,
uint8_t  nr_of_blocks 
)

Read a number of data blocks from the SD card.

The data block size is PX_SD_BLOCK_SIZE and the specified array must be big enough to accept (nr_of_blocks x PX_SD_BLOCK_SIZE).

Parameters
dataPointer to array where data blocks must be stored
block_adrAddress of start block to read
nr_of_blocksNumber of blocks to read
Returns
uint8_t Number of blocks succesfully read

Definition at line 685 of file px_sd.c.

◆ px_sd_wr_block()

bool px_sd_wr_block ( const uint8_t *  data,
uint32_t  block_adr 
)

Write a data block to the SD card.

The data block size is PX_SD_BLOCK_SIZE and the specified array must contain the whole data block. Poll px_sd_wait_write_finished() to find out when write operation has finished, e.g. before powering down SD card.

Parameters
dataPointer to array containing data block content to be written
block_adrAddress of block to write.
Return values
trueData block succcesfully written
falseError while trying to write data block

Definition at line 743 of file px_sd.c.

◆ px_sd_wr_blocks()

uint8_t px_sd_wr_blocks ( const uint8_t *  data,
uint32_t  block_adr,
uint8_t  nr_of_blocks 
)

Write a number of data blocks to the SD card.

The data block size is PX_SD_BLOCK_SIZE and the specified array must contain (nr_of_blocks x PX_SD_BLOCK_SIZE). Poll px_sd_wait_write_finished() to find out when write operation has finished, e.g. before powering down SD card.

Parameters
dataPointer to array containing content to be written
block_adrAddress of start block to write.
nr_of_blocksNumber of blocks to write
Returns
uint8_t Number of blocks succesfully written

Definition at line 779 of file px_sd.c.

◆ px_sd_wait_wr_is_finished()

bool px_sd_wait_wr_is_finished ( void  )

Wait up to 500 ms for write block transaction to finish.

Return values
trueWrite operation finished
falseTimed-out waiting for SD card to be ready

Definition at line 858 of file px_sd.c.