Microchip® Advanced Software Framework

sd_mmc_spi.h File Reference
#include "conf_access.h"
#include "compiler.h"
#include "spi.h"

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