Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MEMORY - SD/MMC card access using SPI

Driver for SD (Secure Digital) / MMC (MultiMedia Card) access using SPI (Serial Peripheral Interface).

This driver is used to setup access to SD/MMC memory cards using the SPI interface.

Modules

 
 SD/MMC SPI Memory
 

Macros

#define byte_adr_of_block(ad)   (ad<<9)
 
#define MMC_CARD   0
 
#define MMC_CLR_WRITE_PROT   29
 
#define MMC_CMD2   2
 illegal in SPI mode ! More...
 
#define MMC_CRC_ON_OFF   59
 Turns CRC check on/off. More...
 
#define MMC_DE_CARD_LOCKED   0x04
 
#define MMC_DE_CC_ERROR   0x02
 
#define MMC_DE_ECC_FAIL   0x04
 
#define MMC_DE_ERROR   0x01
 
#define MMC_DE_MASK   0x1F
 
#define MMC_DE_OUT_OF_RANGE   0x04
 
#define MMC_DR_ACCEPT   0x05
 
#define MMC_DR_MASK   0x1F
 
#define MMC_DR_REJECT_CRC   0x0B
 
#define MMC_DR_REJECT_WRITE_ERROR   0x0D
 
#define MMC_ERASE   38
 Perform block/mass erase. More...
 
#define MMC_GO_IDLE_STATE   0
 initialize card to SPI-type access More...
 
#define MMC_LOCK_UNLOCK   42
 To start a lock/unlock/pwd operation. More...
 
#define MMC_PROGRAM_CSD   27
 
#define MMC_R1_ADDRESS   0x20
 
#define MMC_R1_BUSY   0x80
 R1 response: bit indicates card is busy. More...
 
#define MMC_R1_COM_CRC   0x08
 
#define MMC_R1_ERASE_RESET   0x02
 
#define MMC_R1_ERASE_SEQ   0x10
 
#define MMC_R1_IDLE_STATE   0x01
 
#define MMC_R1_ILLEGAL_COM   0x04
 
#define MMC_R1_PARAMETER   0x40
 
#define MMC_READ_SINGLE_BLOCK   17
 read a block More...
 
#define MMC_SECTOR_SIZE   512
 
#define MMC_SEND_CID   10
 get card's CID More...
 
#define MMC_SEND_CSD   9
 get card's CSD More...
 
#define MMC_SEND_IF_COND   8
 
#define MMC_SEND_OP_COND   1
 set card operational mode More...
 
#define MMC_SEND_STATUS   13
 
#define MMC_SEND_WRITE_PROT   30
 
#define MMC_SET_BLOCKLEN   16
 Set number of bytes to transfer per block. More...
 
#define MMC_SET_WRITE_PROT   28
 
#define MMC_STARTBLOCK_MWRITE   0xFC
 
#define MMC_STARTBLOCK_READ   0xFE
 when received from card, indicates that a block of data will follow More...
 
#define MMC_STARTBLOCK_WRITE   0xFE
 when sent to card, indicates that a block of data will follow More...
 
#define MMC_STOPTRAN_WRITE   0xFD
 
#define MMC_TAG_ERASE_GROUP_END   36
 Sets end of erase group (mass erase) More...
 
#define MMC_TAG_ERASE_GROUP_START   35
 Sets beginning of erase group (mass erase) More...
 
#define MMC_TAG_SECTOR_END   33
 
#define MMC_TAG_SECTOR_START   32
 
#define MMC_UNTAG_ERASE_GROUP   37
 Untag (unset) erase group (mass erase) More...
 
#define MMC_UNTAG_SECTOR   34
 
#define MMC_WRITE_BLOCK   24
 write a block More...
 
#define OP_FORCED_ERASE   0x08
 
#define OP_LOCK   0x04
 
#define OP_RESET_PWD   0x02
 
#define OP_SET_PWD   0x01
 
#define OP_UNLOCK   0x00
 
#define SD_APP_CMD55   55
 Use before any specific command (type ACMD) More...
 
#define SD_CARD   1
 
#define SD_CARD_2   2
 
#define SD_CARD_2_SDHC   3
 
#define SD_CARD_T   0
 
#define SD_FAILURE   -1
 
#define SD_MMC   0
 
#define SD_READ_OCR   58
 
#define SD_SEND_OP_COND_ACMD   41
 Same as MMC_SEND_OP_COND but specific to SD (must be preceded by CMD55) More...
 
#define SD_SPEC_1   0
 
#define SD_SPEC_2   1
 
#define SD_SPEC_SDHC   2
 
#define SD_TAG_WR_ERASE_GROUP_END   33
 
#define SD_TAG_WR_ERASE_GROUP_START   32
 
#define SDHC_CARD   1
 

Functions

bool is_sd_mmc_spi_write_pwd_locked (void)
 Protection functions (optionnal) More...
 
int sd_mmc_spi_check_hc (void)
 This function checks whether detected card is High Capacity SD card. More...
 
bool sd_mmc_spi_check_presence (void)
 This function check the presence of a memory card. More...
 
uint8_t sd_mmc_spi_command (uint8_t, uint32_t)
 This function sends a command WITH DATA STATE to the SD/MMC and waits for R1 response The memory /CS signal is not affected so this function can be used to send a command during a large transmission. More...
 
bool sd_mmc_spi_erase_sector_group (uint32_t, uint32_t)
 This function erase a group of sectors NOTE : Erasing operation concerns only groups of sectors and not one sector only The global variable "erase_group_size" (extracted from CSD) contains the sector group size boundary User specifies the addresses of the first group and the last group to erase (several contiguous groups can be selected for erase) An misaligned address will not generate an error since the memory card ignore the LSbs of the address Some examples (with "erase_group_size" = 0x20 = group boundary) : More...
 
void sd_mmc_spi_get_capacity (void)
 This function extracts structure information from CSD array and compute the number of blocks of the memory card (stored in global uint32_t sd_mmc_spi_last_block_address), its capacity in bytes (stored in global uint32_t capacity) and the block group size for an erase operation Here is defined the position of required fields in CSD array : READ_BL_LEN : [83:80] == data[5] && 0x0f C_SIZE : [73:72] == data[6] && 0x03 [71:64] == data[7] [63:62] == data[8] && 0xc0 C_SIZE_MULT : [49:48] == data[9] && 0x03 [47] == data[10] && 0x80 ERASE_GRP_SIZE (MMC card only) : [46:42] == data[10] && 0x7c ERASE_GRP_MULT (MMC card only) : [41:40] == data[10] && 0x03 [39:37] == data[11] && 0xe0 SECTOR_SIZE (SD card only) : [45:40] == data[10] && 0x3F [39] == data[11] && 0x80. More...
 
bool sd_mmc_spi_get_cid (uint8_t *)
 This function reads the CID (Card Identification Data) of the memory card. More...
 
bool sd_mmc_spi_get_csd (uint8_t *)
 This function reads the CSD (Card Specific Data) of the memory card. More...
 
int sd_mmc_spi_get_if (void)
 This function detects the card interface. More...
 
bool sd_mmc_spi_get_status (void)
 This function reads the STATUS register of the memory card After a read the error flags are automatically cleared. More...
 
bool sd_mmc_spi_init (spi_options_t spiOptions, unsigned int pba_hz)
 This function initializes the SD/MMC controller & the SPI bus(over which the SD_MMC is controlled). More...
 
bool sd_mmc_spi_internal_init (void)
 Low-level functions (basic management) More...
 
bool sd_mmc_spi_lock_operation (uint8_t, uint8_t, uint8_t *)
 This function manages locking operations for the SD/MMC card (password protection) More...
 
bool sd_mmc_spi_mem_check (void)
 This function performs a memory check on the SD_MMC. More...
 
bool sd_mmc_spi_read_close (void)
 This function unselects the current SD_MMC memory. More...
 
void sd_mmc_spi_read_close_PDCA (void)
 Stop PDCA transfer. More...
 
bool sd_mmc_spi_read_multiple_sector (uint16_t nb_sector)
 This function allow to read multiple sectors. More...
 
void sd_mmc_spi_read_multiple_sector_callback (const void *psector)
 
bool sd_mmc_spi_read_open (uint32_t)
 Functions for preparing block read/write. More...
 
bool sd_mmc_spi_read_open_PDCA (uint32_t)
 functions used to make a transfer from SD_MMC to RAM using the PDCA More...
 
bool sd_mmc_spi_read_sector (uint16_t)
 
bool sd_mmc_spi_read_sector_to_ram (void *ram)
 Functions to read/write one sector (512btes) with ram buffer pointer. More...
 
uint8_t sd_mmc_spi_send_and_read (uint8_t)
 This function sends a byte over SPI and returns the byte read from the slave. More...
 
uint8_t sd_mmc_spi_send_command (uint8_t, uint32_t)
 This function sends a command WITH NO DATA STATE to the SD/MMC and waits for R1 response This function also selects and unselects the memory => should be used only for single command transmission. More...
 
bool sd_mmc_spi_wait_not_busy (void)
 This function waits until the SD/MMC is not busy. More...
 
void sd_mmc_spi_write_close (void)
 This function fills the end of the logical sector (512B) and launch page programming. More...
 
bool sd_mmc_spi_write_multiple_sector (uint16_t nb_sector)
 This function allow to write multiple sectors. More...
 
void sd_mmc_spi_write_multiple_sector_callback (void *psector)
 
bool sd_mmc_spi_write_open (uint32_t)
 This function opens a SD_MMC memory in write mode at a given sector address. More...
 
bool sd_mmc_spi_write_sector (uint16_t)
 Functions to link USB DEVICE flow with MMC. More...
 
bool sd_mmc_spi_write_sector_from_ram (const void *ram)
 This function writes one MMC sector from a ram buffer. More...
 

Variables

volatile uint64_t capacity
 
volatile uint16_t capacity_mult
 
uint8_t card_type
 
uint8_t csd [16]
 
uint16_t erase_group_size
 
uint8_t r1
 
uint16_t r2
 
bool sd_mmc_spi_init_done
 
volatile uint32_t sd_mmc_spi_last_block_address
 

#define byte_adr_of_block (   ad)    (ad<<9)
#define MMC_CLR_WRITE_PROT   29
#define MMC_CMD2   2

illegal in SPI mode !

#define MMC_CRC_ON_OFF   59

Turns CRC check on/off.

Referenced by sd_mmc_spi_check_presence(), and sd_mmc_spi_internal_init().

#define MMC_DE_CARD_LOCKED   0x04
#define MMC_DE_CC_ERROR   0x02
#define MMC_DE_ECC_FAIL   0x04
#define MMC_DE_ERROR   0x01
#define MMC_DE_MASK   0x1F
#define MMC_DE_OUT_OF_RANGE   0x04
#define MMC_DR_ACCEPT   0x05
#define MMC_DR_MASK   0x1F
#define MMC_DR_REJECT_CRC   0x0B
#define MMC_DR_REJECT_WRITE_ERROR   0x0D
#define MMC_ERASE   38

Perform block/mass erase.

Referenced by sd_mmc_spi_erase_sector_group().

#define MMC_GO_IDLE_STATE   0

initialize card to SPI-type access

Referenced by sd_mmc_spi_check_presence(), sd_mmc_spi_command(), and sd_mmc_spi_internal_init().

#define MMC_LOCK_UNLOCK   42

To start a lock/unlock/pwd operation.

Referenced by sd_mmc_spi_lock_operation().

#define MMC_PROGRAM_CSD   27
#define MMC_R1_ADDRESS   0x20
#define MMC_R1_BUSY   0x80

R1 response: bit indicates card is busy.

#define MMC_R1_COM_CRC   0x08
#define MMC_R1_ERASE_RESET   0x02
#define MMC_R1_ERASE_SEQ   0x10
#define MMC_R1_IDLE_STATE   0x01
#define MMC_R1_ILLEGAL_COM   0x04

Referenced by sd_mmc_spi_get_if().

#define MMC_R1_PARAMETER   0x40
#define MMC_READ_SINGLE_BLOCK   17
#define MMC_SEND_CID   10

get card's CID

Referenced by sd_mmc_spi_get_cid().

#define MMC_SEND_CSD   9

get card's CSD

Referenced by sd_mmc_spi_get_csd().

#define MMC_SEND_IF_COND   8
#define MMC_SEND_OP_COND   1

set card operational mode

Referenced by sd_mmc_spi_internal_init().

#define MMC_SEND_STATUS   13

Referenced by sd_mmc_spi_get_status().

#define MMC_SEND_WRITE_PROT   30
#define MMC_SET_BLOCKLEN   16

Set number of bytes to transfer per block.

Referenced by sd_mmc_spi_internal_init(), and sd_mmc_spi_lock_operation().

#define MMC_SET_WRITE_PROT   28
#define MMC_STARTBLOCK_MWRITE   0xFC
#define MMC_STARTBLOCK_READ   0xFE

when received from card, indicates that a block of data will follow

Referenced by sd_mmc_spi_get_cid(), sd_mmc_spi_get_csd(), sd_mmc_spi_read_open_PDCA(), and sd_mmc_spi_read_sector_to_ram().

#define MMC_STARTBLOCK_WRITE   0xFE

when sent to card, indicates that a block of data will follow

Referenced by sd_mmc_spi_lock_operation(), and sd_mmc_spi_write_sector_from_ram().

#define MMC_STOPTRAN_WRITE   0xFD
#define MMC_TAG_ERASE_GROUP_END   36

Sets end of erase group (mass erase)

Referenced by sd_mmc_spi_erase_sector_group().

#define MMC_TAG_ERASE_GROUP_START   35

Sets beginning of erase group (mass erase)

Referenced by sd_mmc_spi_erase_sector_group().

#define MMC_TAG_SECTOR_END   33
#define MMC_TAG_SECTOR_START   32
#define MMC_UNTAG_ERASE_GROUP   37

Untag (unset) erase group (mass erase)

#define MMC_UNTAG_SECTOR   34
#define MMC_WRITE_BLOCK   24

write a block

Referenced by sd_mmc_spi_write_sector_from_ram().

#define OP_FORCED_ERASE   0x08
#define OP_LOCK   0x04
#define OP_RESET_PWD   0x02
#define OP_SET_PWD   0x01
#define OP_UNLOCK   0x00
#define SD_APP_CMD55   55

Use before any specific command (type ACMD)

Referenced by sd_mmc_spi_internal_init().

#define SD_CARD   1
#define SD_CARD_2   2
#define SD_CARD_T   0
#define SD_FAILURE   -1
#define SD_MMC   0

Referenced by sd_mmc_spi_get_if().

#define SD_READ_OCR   58

Referenced by sd_mmc_spi_check_hc().

#define SD_SEND_OP_COND_ACMD   41

Same as MMC_SEND_OP_COND but specific to SD (must be preceded by CMD55)

Referenced by sd_mmc_spi_internal_init().

#define SD_SPEC_1   0
#define SD_SPEC_2   1
#define SD_SPEC_SDHC   2
#define SD_TAG_WR_ERASE_GROUP_END   33
#define SD_TAG_WR_ERASE_GROUP_START   32
#define SDHC_CARD   1

Referenced by sd_mmc_spi_check_hc().

bool is_sd_mmc_spi_write_pwd_locked ( void  )

Protection functions (optionnal)

Protection functions (optionnal)

Returns
bit Password protected -> true NOT password protected -> false (or card not initialized)

References card_type, csd, MMC_CARD, r2, and sd_mmc_spi_get_status().

int sd_mmc_spi_check_hc ( void  )

This function checks whether detected card is High Capacity SD card.

Returns
bit SDHC_CARD Detected card is SDHC SD_CARD Detected card is SD ERROR

References r1, SD_FAILURE, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), SD_READ_OCR, SDHC_CARD, spi_selectChip(), and spi_unselectChip().

Referenced by sd_mmc_spi_internal_init().

bool sd_mmc_spi_check_presence ( void  )

This function check the presence of a memory card.

- if the card was already initialized (removal test), the host send a CRC_OFF command (CMD59) and check the answer
- if the card was not already initialized (insertion test), the host send a CMD0 reset command and check the answer
Returns
bit The memory is present (true) The memory does not respond (disconnected) (false)

References MMC_CRC_ON_OFF, MMC_GO_IDLE_STATE, r1, sd_mmc_spi_init_done, sd_mmc_spi_send_command(), and spi_write().

Referenced by ai_sd_mmc_get_device_status(), run_check_presence_test(), sd_mmc_spi_mem_2_ram(), sd_mmc_spi_mem_check(), and sd_mmc_spi_ram_2_mem().

uint8_t sd_mmc_spi_command ( uint8_t  command,
uint32_t  arg 
)

This function sends a command WITH DATA STATE to the SD/MMC and waits for R1 response The memory /CS signal is not affected so this function can be used to send a command during a large transmission.

Parameters
commandcommand to send (see sd_mmc_spi.h for command list)
argargument of the command
Returns
uint8_t R1 response (R1 == 0xFF time out error)

References MMC_GO_IDLE_STATE, MMC_SEND_IF_COND, r1, sd_mmc_spi_send_and_read(), and spi_write().

Referenced by sd_mmc_spi_check_hc(), sd_mmc_spi_erase_sector_group(), sd_mmc_spi_get_cid(), sd_mmc_spi_get_csd(), sd_mmc_spi_get_if(), sd_mmc_spi_lock_operation(), sd_mmc_spi_read_open_PDCA(), sd_mmc_spi_read_sector_to_ram(), sd_mmc_spi_send_command(), and sd_mmc_spi_write_sector_from_ram().

bool sd_mmc_spi_erase_sector_group ( uint32_t  adr_start,
uint32_t  adr_end 
)

This function erase a group of sectors NOTE : Erasing operation concerns only groups of sectors and not one sector only The global variable "erase_group_size" (extracted from CSD) contains the sector group size boundary User specifies the addresses of the first group and the last group to erase (several contiguous groups can be selected for erase) An misaligned address will not generate an error since the memory card ignore the LSbs of the address Some examples (with "erase_group_size" = 0x20 = group boundary) :

            - adr_start=0x100 and adr_end=0x100, all the sectors from 0x100 up to 0x11F will be erased
            - adr_start=0x90 and adr_end=0x100, all the sectors from 0x80 up to 0x11F will be erased (0x90 interpreted as 0x80)
            - adr_start=0x80 and adr_end=0x146, all the sectors from 0x80 up to 0x15F will be erased
          This function just initiates a transmission, user may get status register to check that operation has succeeded
          After an erase, a MMC card contains bits at 0, and SD can contains bits 0 or 1 (according to field DATA_STAT_AFTER_ERASE in the CSD)
Parameters
adr_startaddress of 1st group (sector address, not byte address)
adr_endaddress of last group (sector address, not byte address)
Returns
bit The erase operation succeeded (has been started) -> true The erase operation failed (not started) -> false

References card_type, cmd, MMC_CARD, MMC_ERASE, MMC_TAG_ERASE_GROUP_END, MMC_TAG_ERASE_GROUP_START, r1, SD_CARD_2_SDHC, sd_mmc_spi_command(), sd_mmc_spi_wait_not_busy(), SD_TAG_WR_ERASE_GROUP_END, SD_TAG_WR_ERASE_GROUP_START, spi_selectChip(), spi_unselectChip(), and spi_write().

void sd_mmc_spi_get_capacity ( void  )

This function extracts structure information from CSD array and compute the number of blocks of the memory card (stored in global uint32_t sd_mmc_spi_last_block_address), its capacity in bytes (stored in global uint32_t capacity) and the block group size for an erase operation Here is defined the position of required fields in CSD array : READ_BL_LEN : [83:80] == data[5] && 0x0f C_SIZE : [73:72] == data[6] && 0x03 [71:64] == data[7] [63:62] == data[8] && 0xc0 C_SIZE_MULT : [49:48] == data[9] && 0x03 [47] == data[10] && 0x80 ERASE_GRP_SIZE (MMC card only) : [46:42] == data[10] && 0x7c ERASE_GRP_MULT (MMC card only) : [41:40] == data[10] && 0x03 [39:37] == data[11] && 0xe0 SECTOR_SIZE (SD card only) : [45:40] == data[10] && 0x3F [39] == data[11] && 0x80.

Returns
bit true

References capacity, capacity_mult, card_type, csd, erase_group_size, MMC_CARD, SD_CARD_2_SDHC, and sd_mmc_spi_last_block_address.

Referenced by main(), run_multiple_sector_access_test(), run_sector_access_test(), and sd_mmc_spi_internal_init().

bool sd_mmc_spi_get_cid ( uint8_t *  buffer)

This function reads the CID (Card Identification Data) of the memory card.

Parameters
bufferto fill
Returns
bit true / false

References MMC_SEND_CID, MMC_STARTBLOCK_READ, r1, r2, sd_mmc_spi_command(), sd_mmc_spi_init_done, sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_read(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by sd_mmc_spi_internal_init().

bool sd_mmc_spi_get_csd ( uint8_t *  buffer)

This function reads the CSD (Card Specific Data) of the memory card.

Parameters
bufferto fill
Returns
bit true / false

References MMC_SEND_CSD, MMC_STARTBLOCK_READ, r1, sd_mmc_spi_command(), sd_mmc_spi_init_done, sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_read(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by sd_mmc_spi_internal_init().

int sd_mmc_spi_get_if ( void  )

This function detects the card interface.

Returns
int SD_FAILURE true SD_MMC

References MMC_R1_ILLEGAL_COM, MMC_SEND_IF_COND, r1, SD_FAILURE, SD_MMC, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_selectChip(), and spi_unselectChip().

Referenced by sd_mmc_spi_internal_init().

bool sd_mmc_spi_get_status ( void  )

This function reads the STATUS register of the memory card After a read the error flags are automatically cleared.

Returns
bit The open succeeded -> true

References MMC_SEND_STATUS, r2, sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by is_sd_mmc_spi_write_pwd_locked(), and sd_mmc_spi_lock_operation().

bool sd_mmc_spi_init ( spi_options_t  spiOptions,
unsigned int  pba_hz 
)

This function initializes the SD/MMC controller & the SPI bus(over which the SD_MMC is controlled).

Returns
bit The memory is ready -> true (always)

References sd_mmc_pba_hz, and sd_mmc_spi_internal_init().

Referenced by memories_initialization(), prv_sd_mmc_resources_init(), and sd_mmc_resources_init().

bool sd_mmc_spi_lock_operation ( uint8_t  operation,
uint8_t  pwd_lg,
uint8_t *  pwd 
)

This function manages locking operations for the SD/MMC card (password protection)

    - Once the card is locked, the only commands allowed are UNLOCK and FORCED_ERASE
    - Once the card is unlocked, the commands allowed are all the others
    - Before setting a new password (SET_PWD), the current one must be cleared (RESET_PWD)
    - If card contains a password (PWDSLEN != 0), the card will automatically be locked at start-up

/!\ Take care that old versions of MMC cards don't support this feature ! For a MMC, "lock protection" is featured only from v2.1 release ! => see CSD[0]<5:2> bits to know the version : 0x0=1.x, 0x1=1.4, 0x2=2.x, 0x3=3.x, 0x4=4.0 Moreover the OP_FORCED_ERASE command can also have no effect on some cards !

Parameters
operationOP_LOCK -> to lock the card (the current password must be specified) OP_UNLOCK -> to unlock the card (the current password must be specified) OP_RESET_PWD -> to clear the current password (the current password must be specified) OP_SET_PWD -> to set a new password to the card (the old password must have been cleared first) OP_FORCED_ERASE -> to erase completely the card and the password (no password needed)
pwd_lgPassword length
pwdPointer on the password (char array) to send
Returns
bit Operation succeeded -> true Operation failed -> false

References MMC_DR_ACCEPT, MMC_DR_MASK, MMC_LOCK_UNLOCK, MMC_SET_BLOCKLEN, MMC_STARTBLOCK_WRITE, OP_FORCED_ERASE, r1, r2, sd_mmc_spi_command(), sd_mmc_spi_get_status(), sd_mmc_spi_send_and_read(), sd_mmc_spi_send_command(), sd_mmc_spi_wait_not_busy(), spi_selectChip(), spi_unselectChip(), spi_write(), and status.

bool sd_mmc_spi_mem_check ( void  )

This function performs a memory check on the SD_MMC.

Returns
bit The memory is ready -> true The memory check failed -> false

References sd_mmc_spi_check_presence(), sd_mmc_spi_init_done, and sd_mmc_spi_internal_init().

Referenced by main(), run_memory_check_test(), and sd_mmc_spi_test_unit_ready().

bool sd_mmc_spi_read_close ( void  )

This function unselects the current SD_MMC memory.

References sd_mmc_spi_wait_not_busy().

Referenced by run_multiple_sector_access_test(), run_sector_access_test(), sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_usb_read_10().

void sd_mmc_spi_read_close_PDCA ( void  )

Stop PDCA transfer.

This function closes a PDCA read transfer page programming.

References spi_unselectChip(), and spi_write().

Referenced by pdca_int_handler().

bool sd_mmc_spi_read_multiple_sector ( uint16_t  nb_sector)

This function allow to read multiple sectors.

Parameters
nb_sectorthe number of sector to read
Returns
bit The read succeeded -> true

References sd_mmc_spi_read_multiple_sector_callback(), sd_mmc_spi_read_sector_to_ram(), and sector_buf.

Referenced by run_multiple_sector_access_test(), and sd_mmc_spi_usb_read_10().

bool sd_mmc_spi_read_open ( uint32_t  pos)

Functions for preparing block read/write.

Functions for preparing block read/write.

Parameters
posSector address
Returns
bit The open succeeded -> true

References gl_ptr_mem, and sd_mmc_spi_wait_not_busy().

Referenced by run_multiple_sector_access_test(), run_sector_access_test(), sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_usb_read_10().

bool sd_mmc_spi_read_open_PDCA ( uint32_t  pos)

functions used to make a transfer from SD_MMC to RAM using the PDCA

functions used to make a transfer from SD_MMC to RAM using the PDCA

This function opens a SD_MMC memory in read mode at a given sector address.

NOTE: If page buffer > 512 bytes, page content is first loaded in buffer to be partially updated by write_byte or write64 functions.

Parameters
posSector address
Returns
bit The open succeeded -> true /

References card_type, gl_ptr_mem, MMC_READ_SINGLE_BLOCK, MMC_STARTBLOCK_READ, r1, SD_CARD_2_SDHC, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by main().

bool sd_mmc_spi_read_sector ( uint16_t  )
bool sd_mmc_spi_read_sector_to_ram ( void *  ram)

Functions to read/write one sector (512btes) with ram buffer pointer.

Functions to read/write one sector (512btes) with ram buffer pointer.

     DATA FLOW is: SD/MMC => RAM

NOTE:

Parameters
rampointer to ram buffer
Returns
bit The read succeeded -> true The read failed (bad address, etc.) -> false /

References card_type, gl_ptr_mem, i, MMC_READ_SINGLE_BLOCK, MMC_SECTOR_SIZE, MMC_STARTBLOCK_READ, r1, ram, SD_CARD_2_SDHC, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_read(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by run_sector_access_test(), sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_read_multiple_sector().

uint8_t sd_mmc_spi_send_and_read ( uint8_t  data_to_send)

This function sends a byte over SPI and returns the byte read from the slave.

Parameters
data_to_sendbyte to send over SPI
Returns
uint8_t Byte read from the slave

References SPI_ERROR_TIMEOUT, spi_read(), and spi_write().

Referenced by sd_mmc_spi_check_hc(), sd_mmc_spi_command(), sd_mmc_spi_get_cid(), sd_mmc_spi_get_csd(), sd_mmc_spi_get_if(), sd_mmc_spi_get_status(), sd_mmc_spi_lock_operation(), sd_mmc_spi_read_open_PDCA(), sd_mmc_spi_read_sector_to_ram(), sd_mmc_spi_wait_not_busy(), and sd_mmc_spi_write_sector_from_ram().

uint8_t sd_mmc_spi_send_command ( uint8_t  command,
uint32_t  arg 
)

This function sends a command WITH NO DATA STATE to the SD/MMC and waits for R1 response This function also selects and unselects the memory => should be used only for single command transmission.

Parameters
commandcommand to send (see sd_mmc_spi.h for command list)
argargument of the command
Returns
uint8_t R1 response (R1 == 0xFF if time out error)

References r1, sd_mmc_spi_command(), spi_selectChip(), and spi_unselectChip().

Referenced by sd_mmc_spi_check_presence(), sd_mmc_spi_internal_init(), and sd_mmc_spi_lock_operation().

void sd_mmc_spi_write_close ( void  )

This function fills the end of the logical sector (512B) and launch page programming.

Referenced by run_multiple_sector_access_test(), run_sector_access_test(), sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_usb_write_10().

bool sd_mmc_spi_write_multiple_sector ( uint16_t  nb_sector)

This function allow to write multiple sectors.

Parameters
nb_sectorthe number of sector to write
Returns
bit The write succeeded -> true

References sd_mmc_spi_write_multiple_sector_callback(), sd_mmc_spi_write_sector_from_ram(), and sector_buf.

Referenced by run_multiple_sector_access_test(), and sd_mmc_spi_usb_write_10().

bool sd_mmc_spi_write_open ( uint32_t  pos)

This function opens a SD_MMC memory in write mode at a given sector address.

NOTE: If page buffer > 512 bytes, page content is first loaded in buffer to be partially updated by write_byte or write64 functions.

Parameters
posSector address
Returns
bit The open succeeded -> true

References gl_ptr_mem, and sd_mmc_spi_wait_not_busy().

Referenced by run_multiple_sector_access_test(), run_sector_access_test(), sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_usb_write_10().

bool sd_mmc_spi_write_sector ( uint16_t  )

Functions to link USB DEVICE flow with MMC.

bool sd_mmc_spi_write_sector_from_ram ( const void *  ram)

This function writes one MMC sector from a ram buffer.

    DATA FLOW is: RAM => SD/MMC

NOTE (please read) :

  • First call (if sequential write) must be preceded by a call to the sd_mmc_spi_write_open() function
  • An address error will not detected here, but with the call of sd_mmc_spi_get_status() function
  • The program exits the functions with the memory card busy !
Parameters
rampointer to ram buffer
Returns
bit The write succeeded -> true The write failed -> false

References card_type, gl_ptr_mem, i, MMC_DR_ACCEPT, MMC_DR_MASK, MMC_SECTOR_SIZE, MMC_STARTBLOCK_WRITE, MMC_WRITE_BLOCK, r1, ram, SD_CARD_2_SDHC, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), sd_mmc_spi_wait_not_busy(), spi_selectChip(), spi_unselectChip(), and spi_write().

Referenced by run_sector_access_test(), sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_write_multiple_sector().

volatile uint64_t capacity
volatile uint16_t capacity_mult

Referenced by sd_mmc_spi_get_capacity().

uint16_t erase_group_size

Referenced by sd_mmc_spi_get_capacity().

volatile uint32_t sd_mmc_spi_last_block_address