#include "config.h"
Go to the source code of this file.
Defines | |
| #define | MMC_SECTOR_SIZE 512 |
| / 1 2 3 4 5 6 78 | <- view of MMC/SD card looking at contacts / 9 | Pins 8 and 9 are present only on SD cards | MMC/SD Card | | | |__________________| | |
| #define | byte_adr_of_block(ad) (ad<<9) |
| #define | MMC_CARD 0 |
| #define | SD_CARD 1 |
| #define | OP_UNLOCK 0x00 |
| #define | OP_LOCK 0x04 |
| #define | OP_RESET_PWD 0x02 |
| #define | OP_SET_PWD 0x01 |
| #define | OP_FORCED_ERASE 0x08 |
| #define | MMC_GO_IDLE_STATE 0 |
| initialize card to SPI-type access | |
| #define | MMC_SEND_OP_COND 1 |
| set card operational mode | |
| #define | MMC_CMD2 2 |
| illegal in SPI mode ! | |
| #define | MMC_SEND_CSD 9 |
| get card's CSD | |
| #define | MMC_SEND_CID 10 |
| get card's CID | |
| #define | MMC_SEND_STATUS 13 |
| #define | MMC_SET_BLOCKLEN 16 |
| Set number of bytes to transfer per block. | |
| #define | MMC_READ_SINGLE_BLOCK 17 |
| read a block | |
| #define | MMC_WRITE_BLOCK 24 |
| write a block | |
| #define | MMC_PROGRAM_CSD 27 |
| #define | MMC_SET_WRITE_PROT 28 |
| #define | MMC_CLR_WRITE_PROT 29 |
| #define | MMC_SEND_WRITE_PROT 30 |
| #define | SD_TAG_WR_ERASE_GROUP_START 32 |
| #define | SD_TAG_WR_ERASE_GROUP_END 33 |
| #define | MMC_TAG_SECTOR_START 32 |
| #define | MMC_TAG_SECTOR_END 33 |
| #define | MMC_UNTAG_SECTOR 34 |
| #define | MMC_TAG_ERASE_GROUP_START 35 |
| Sets beginning of erase group (mass erase). | |
| #define | MMC_TAG_ERASE_GROUP_END 36 |
| Sets end of erase group (mass erase). | |
| #define | MMC_UNTAG_ERASE_GROUP 37 |
| Untag (unset) erase group (mass erase). | |
| #define | MMC_ERASE 38 |
| Perform block/mass erase. | |
| #define | SD_SEND_OP_COND_ACMD 41 |
| Same as MMC_SEND_OP_COND but specific to SD (must be preceeded by CMD55). | |
| #define | MMC_LOCK_UNLOCK 42 |
| To start a lock/unlock/pwd operation. | |
| #define | SD_APP_CMD55 55 |
| Use before any specific command (type ACMD). | |
| #define | MMC_CRC_ON_OFF 59 |
| Turns CRC check on/off. | |
| #define | MMC_R1_BUSY 0x80 |
| R1 response: bit indicates card is busy. | |
| #define | MMC_R1_PARAMETER 0x40 |
| #define | MMC_R1_ADDRESS 0x20 |
| #define | MMC_R1_ERASE_SEQ 0x10 |
| #define | MMC_R1_COM_CRC 0x08 |
| #define | MMC_R1_ILLEGAL_COM 0x04 |
| #define | MMC_R1_ERASE_RESET 0x02 |
| #define | MMC_R1_IDLE_STATE 0x01 |
| #define | MMC_STARTBLOCK_READ 0xFE |
| when received from card, indicates that a block of data will follow | |
| #define | MMC_STARTBLOCK_WRITE 0xFE |
| when sent to card, indicates that a block of data will follow | |
| #define | MMC_STARTBLOCK_MWRITE 0xFC |
| #define | MMC_STOPTRAN_WRITE 0xFD |
| #define | MMC_DE_MASK 0x1F |
| #define | MMC_DE_ERROR 0x01 |
| #define | MMC_DE_CC_ERROR 0x02 |
| #define | MMC_DE_ECC_FAIL 0x04 |
| #define | MMC_DE_OUT_OF_RANGE 0x04 |
| #define | MMC_DE_CARD_LOCKED 0x04 |
| #define | MMC_DR_MASK 0x1F |
| #define | MMC_DR_ACCEPT 0x05 |
| #define | MMC_DR_REJECT_CRC 0x0B |
| #define | MMC_DR_REJECT_WRITE_ERROR 0x0D |
Functions | |
| void | mmc_sd_spi_init (void) |
| Low-level functions (basic management). | |
| bit | mmc_sd_init (void) |
| bit | mmc_sd_check_presence (void) |
| bit | mmc_sd_mem_check (void) |
| bit | mmc_sd_wait_not_busy (void) |
| bit | mmc_sd_get_csd (U8 *) |
| bit | mmc_sd_get_cid (U8 *) |
| void | mmc_sd_get_capacity (void) |
| bit | mmc_sd_get_status (void) |
| U8 | mmc_sd_send_and_read (U8) |
| U8 | mmc_sd_send_command (U8, U32) |
| U8 | mmc_sd_command (U8, U32) |
| bit | is_mmc_sd_write_pwd_locked (void) |
| Protection functions (optionnal). | |
| bit | mmc_sd_lock_operation (U8, U8, U8 *) |
| bit | mmc_sd_read_open (U32) |
| Functions for preparing block read/write. | |
| void | mmc_sd_read_close (void) |
| bit | mmc_sd_write_open (U32) |
| void | mmc_sd_write_close (void) |
| bit | mmc_sd_write_sector (U16) |
| Funtions to link USB DEVICE flow with MMC. | |
| bit | mmc_sd_read_sector (U16) |
| bit | mmc_sd_read_sector_to_ram (U8 *ram) |
| Functions to read/write one sector (512btes) with ram buffer pointer. | |
| bit | mmc_sd_write_sector_from_ram (U8 *ram) |
| bit | mmc_sd_erase_sector_group (U32, U32) |
Variables | |
| U8 | csd [16] |
| volatile U32 | capacity |
| volatile U32 | mmc_sd_last_block_address |
| U16 | erase_group_size |
| U8 | r1 |
| U16 | r2 |
| U8 | card_type |
| bit | mmc_sd_init_done |
Definition in file mmc_sd.h.
| #define MMC_SECTOR_SIZE 512 |
/ 1 2 3 4 5 6 78 | <- view of MMC/SD card looking at contacts / 9 | Pins 8 and 9 are present only on SD cards | MMC/SD Card | | | |__________________|
1 - CS (chip select) - wire to any available I/O pin(*) 2 - DIN (data in, card<-host) - wire to SPI MOSI pin 3 - VSS (ground) - wire to ground 4 - VDD (power, 3.3V only?) - wire to power (MIGHT BE 3.3V ONLY!) 5 - SCLK (data clock) - wire to SPI SCK pin 6 - VSS (ground) - wire to ground 7 - DOUT (data out, card->host) - wire to SPI MISO pin
Definition at line 67 of file mmc_sd.h.
Referenced by mmc_sd_read_sector_to_ram(), and mmc_sd_write_sector_from_ram().
| #define MMC_CARD 0 |
Definition at line 75 of file mmc_sd.h.
Referenced by is_mmc_sd_write_pwd_locked(), mmc_sd_erase_sector_group(), mmc_sd_get_capacity(), and mmc_sd_init().
| #define SD_CARD 1 |
| #define OP_FORCED_ERASE 0x08 |
| #define MMC_GO_IDLE_STATE 0 |
initialize card to SPI-type access
Definition at line 86 of file mmc_sd.h.
Referenced by mmc_sd_check_presence(), and mmc_sd_init().
| #define MMC_SEND_OP_COND 1 |
| #define MMC_SEND_CSD 9 |
| #define MMC_SEND_CID 10 |
| #define MMC_SEND_STATUS 13 |
| #define MMC_SET_BLOCKLEN 16 |
Set number of bytes to transfer per block.
Definition at line 92 of file mmc_sd.h.
Referenced by mmc_sd_init(), and mmc_sd_lock_operation().
| #define MMC_READ_SINGLE_BLOCK 17 |
read a block
Definition at line 93 of file mmc_sd.h.
Referenced by mmc_sd_read_sector(), and mmc_sd_read_sector_to_ram().
| #define MMC_WRITE_BLOCK 24 |
write a block
Definition at line 94 of file mmc_sd.h.
Referenced by mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().
| #define SD_TAG_WR_ERASE_GROUP_START 32 |
| #define SD_TAG_WR_ERASE_GROUP_END 33 |
| #define MMC_TAG_ERASE_GROUP_START 35 |
Sets beginning of erase group (mass erase).
Definition at line 104 of file mmc_sd.h.
Referenced by mmc_sd_erase_sector_group().
| #define MMC_TAG_ERASE_GROUP_END 36 |
Sets end of erase group (mass erase).
Definition at line 105 of file mmc_sd.h.
Referenced by mmc_sd_erase_sector_group().
| #define MMC_UNTAG_ERASE_GROUP 37 |
| #define MMC_ERASE 38 |
Perform block/mass erase.
Definition at line 107 of file mmc_sd.h.
Referenced by mmc_sd_erase_sector_group().
| #define SD_SEND_OP_COND_ACMD 41 |
Same as MMC_SEND_OP_COND but specific to SD (must be preceeded by CMD55).
Definition at line 108 of file mmc_sd.h.
Referenced by mmc_sd_init().
| #define MMC_LOCK_UNLOCK 42 |
To start a lock/unlock/pwd operation.
Definition at line 109 of file mmc_sd.h.
Referenced by mmc_sd_lock_operation().
| #define SD_APP_CMD55 55 |
Use before any specific command (type ACMD).
Definition at line 110 of file mmc_sd.h.
Referenced by mmc_sd_init().
| #define MMC_CRC_ON_OFF 59 |
Turns CRC check on/off.
Definition at line 111 of file mmc_sd.h.
Referenced by mmc_sd_check_presence(), and mmc_sd_init().
| #define MMC_R1_BUSY 0x80 |
| #define MMC_STARTBLOCK_READ 0xFE |
when received from card, indicates that a block of data will follow
Definition at line 122 of file mmc_sd.h.
Referenced by mmc_sd_get_cid(), mmc_sd_get_csd(), mmc_sd_read_sector(), and mmc_sd_read_sector_to_ram().
| #define MMC_STARTBLOCK_WRITE 0xFE |
when sent to card, indicates that a block of data will follow
Definition at line 123 of file mmc_sd.h.
Referenced by mmc_sd_lock_operation(), mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().
| #define MMC_DR_MASK 0x1F |
Definition at line 135 of file mmc_sd.h.
Referenced by mmc_sd_lock_operation(), mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().
| #define MMC_DR_ACCEPT 0x05 |
Definition at line 136 of file mmc_sd.h.
Referenced by mmc_sd_lock_operation(), mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().
| void mmc_sd_spi_init | ( | void | ) |
Low-level functions (basic management).
This function initializes the SPI communication link between the MMC_SD and the MMC_SD driver.
| none |
Definition at line 85 of file mmc_sd.c.
00086 { 00087 Spi_select_master(); 00088 Spi_set_mode(SPI_MODE_0); 00089 Spi_init_bus(); 00090 Spi_set_rate(SPI_RATE_0); // SCK freq == fosc/2. 00091 Spi_disable_ss(); 00092 Spi_enable(); 00093 }
| bit mmc_sd_init | ( | void | ) |
This function initializes the Dataflash controller & the SPI bus(over which the MMC_SD is controlled).
| none |
Definition at line 107 of file mmc_sd.c.
References card_type, cid, csd, FALSE, KO, MMC_CARD, MMC_CRC_ON_OFF, MMC_GO_IDLE_STATE, mmc_sd_get_capacity(), mmc_sd_get_cid(), mmc_sd_get_csd(), mmc_sd_init_done, mmc_sd_send_command(), mmc_sd_spi_init(), MMC_SEND_OP_COND, MMC_SET_BLOCKLEN, OK, r1, SD_APP_CMD55, SD_CARD, SD_SEND_OP_COND_ACMD, Spi_write_data, and TRUE.
00108 { 00109 U16 retry; 00110 00111 // INIT HARDWARE 00112 mmc_sd_spi_init(); 00113 00114 // RESET THE MEMORY CARD 00115 mmc_sd_init_done = FALSE; 00116 card_type = MMC_CARD; 00117 retry = 0; 00118 do 00119 { 00120 // reset card and go to SPI mode 00121 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0); 00122 Spi_write_data(0xFF); // write dummy byte 00123 // do retry counter 00124 retry++; 00125 if(retry > 100) 00126 return KO; 00127 } 00128 while(r1 != 0x01); // check memory enters idle_state 00129 00130 // IDENTIFICATION OF THE CARD TYPE (SD or MMC) 00131 // Both cards will accept CMD55 command but only the SD card will respond to ACMD41 00132 r1 = mmc_sd_send_command(SD_APP_CMD55,0); 00133 Spi_write_data(0xFF); // write dummy byte 00134 00135 r1 = mmc_sd_send_command(SD_SEND_OP_COND_ACMD, 0); 00136 Spi_write_data(0xFF); // write dummy byte 00137 00138 if ((r1&0xFE) == 0) // ignore "in_idle_state" flag bit 00139 { 00140 card_type = SD_CARD; // card has accepted the command, this is a SD card 00141 } 00142 else 00143 { 00144 card_type = MMC_CARD; // card has not responded, this is a MMC card 00145 // reset card again 00146 retry = 0; 00147 do 00148 { 00149 // reset card again 00150 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0); 00151 Spi_write_data(0xFF); // write dummy byte 00152 // do retry counter 00153 retry++; 00154 if(retry > 100) 00155 return KO; 00156 } 00157 while(r1 != 0x01); // check memory enters idle_state 00158 } 00159 00160 // CONTINUE INTERNAL INITIALIZATION OF THE CARD 00161 // Continue sending CMD1 while memory card is in idle state 00162 retry = 0; 00163 do 00164 { 00165 // initializing card for operation 00166 r1 = mmc_sd_send_command(MMC_SEND_OP_COND, 0); 00167 Spi_write_data(0xFF); // write dummy byte 00168 // do retry counter 00169 retry++; 00170 if(retry == 50000) // measured approx. 500 on several cards 00171 return KO; 00172 } 00173 while (r1); 00174 00175 // DISABLE CRC TO SIMPLIFY AND SPEED UP COMMUNICATIONS 00176 r1 = mmc_sd_send_command(MMC_CRC_ON_OFF, 0); // disable CRC (should be already initialized on SPI init) 00177 Spi_write_data(0xFF); // write dummy byte 00178 00179 // SET BLOCK LENGTH TO 512 BYTES 00180 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512); 00181 Spi_write_data(0xFF); // write dummy byte 00182 if (r1 != 0x00) 00183 return KO; // card unsupported if block length of 512b is not accepted 00184 00185 // GET CARD SPECIFIC DATA 00186 if (KO == mmc_sd_get_csd(csd)) 00187 return KO; 00188 00189 // GET CARD CAPACITY and NUMBER OF SECTORS 00190 mmc_sd_get_capacity(); 00191 00192 // GET CARD IDENTIFICATION DATA IF REQUIRED 00193 #if (MMC_SD_READ_CID == ENABLED) 00194 if (KO == mmc_sd_get_cid(cid)) 00195 return KO; 00196 #endif 00197 00198 mmc_sd_init_done = TRUE; 00199 00200 return(OK); 00201 }
| bit mmc_sd_check_presence | ( | void | ) |
This function check the presence of a memory card
| none |
Definition at line 556 of file mmc_sd.c.
References FALSE, KO, MMC_CRC_ON_OFF, MMC_GO_IDLE_STATE, mmc_sd_init_done, mmc_sd_send_command(), OK, r1, and Spi_write_data.
00557 { 00558 U16 retry; 00559 00560 retry = 0; 00561 if (mmc_sd_init_done == FALSE) 00562 { 00563 // If memory is not initialized, try to initialize it (CMD0) 00564 // If no valid response, there is no card 00565 while ((r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01) 00566 { 00567 Spi_write_data(0xFF); // write dummy byte 00568 retry++; 00569 if (retry > 10) 00570 return KO; 00571 } 00572 return OK; 00573 } 00574 else 00575 { 00576 // If memory already initialized, send a CRC command (CMD59) (supported only if card is initialized) 00577 /* 00578 retry = 0; 00579 while (retry != 50000) 00580 { 00581 r1 = mmc_sd_send_command(MMC_CMD2,0); // unsupported command in SPI mode 00582 Spi_write_data(0xFF); // write dummy byte 00583 if (r1 != 0) // memory must answer with an error code (with bit7=0) 00584 { 00585 if (r1 < 0x80) 00586 return OK; 00587 else 00588 { 00589 mmc_sd_init_done = FALSE; 00590 return KO; 00591 } 00592 } 00593 retry++; 00594 } 00595 */ 00596 if ((r1 = mmc_sd_send_command(MMC_CRC_ON_OFF,0)) == 0x00) 00597 return OK; 00598 mmc_sd_init_done = FALSE; 00599 return KO; 00600 } 00601 00602 return KO; 00603 }
| bit mmc_sd_mem_check | ( | void | ) |
This function performs a memory check on the MMC_SD.
| none |
Definition at line 618 of file mmc_sd.c.
References FALSE, KO, mmc_sd_check_presence(), mmc_sd_init(), mmc_sd_init_done, OK, and TRUE.
00619 { 00620 if (mmc_sd_check_presence() == OK) 00621 { 00622 if (mmc_sd_init_done == FALSE) 00623 { 00624 mmc_sd_init(); 00625 } 00626 if (mmc_sd_init_done == TRUE) 00627 return OK; 00628 else 00629 return KO; 00630 } 00631 return KO; 00632 }
| bit mmc_sd_wait_not_busy | ( | void | ) |
This function waits until the MMC/SD is not busy.
| None |
Definition at line 521 of file mmc_sd.c.
References KO, Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, OK, and r1.
00522 { 00523 U16 retry; 00524 00525 // Select the MMC_SD memory gl_ptr_mem points to 00526 Mmc_sd_select(); 00527 retry = 0; 00528 while((r1 = mmc_sd_send_and_read(0xFF)) != 0xFF) 00529 { 00530 retry++; 00531 if (retry == 50000) 00532 { 00533 Mmc_sd_unselect(); 00534 return KO; 00535 } 00536 } 00537 Mmc_sd_unselect(); 00538 return OK; 00539 }
| bit mmc_sd_get_csd | ( | U8 * | buffer | ) |
This function reads the CSD (Card Specific Data) of the memory card
| buffer | to fill |
Definition at line 293 of file mmc_sd.c.
References FALSE, KO, mmc_sd_command(), mmc_sd_init_done, Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SEND_CSD, MMC_STARTBLOCK_READ, OK, r1, Spi_read_data, and Spi_write_data.
00294 { 00295 U8 retry; 00296 00297 // wait for MMC not busy 00298 if (KO == mmc_sd_wait_not_busy()) 00299 return KO; 00300 00301 Mmc_sd_select(); // select MMC_SD 00302 // issue command 00303 r1 = mmc_sd_command(MMC_SEND_CSD, 0); 00304 // check for valid response 00305 if(r1 != 0x00) 00306 { 00307 Mmc_sd_unselect(); // unselect MMC_SD 00308 mmc_sd_init_done = FALSE; 00309 return KO; 00310 } 00311 // wait for block start 00312 retry = 0; 00313 while((r1 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ) 00314 { 00315 if (retry > 8) 00316 { 00317 Mmc_sd_unselect(); // unselect MMC_SD 00318 return KO; 00319 } 00320 retry++; 00321 } 00322 for (retry = 0; retry <16; retry++) 00323 { 00324 Spi_write_data(0xFF); 00325 buffer[retry] = Spi_read_data(); 00326 } 00327 Spi_write_data(0xFF); // load CRC (not used) 00328 Spi_write_data(0xFF); 00329 Spi_write_data(0xFF); // give clock again to end transaction 00330 Mmc_sd_unselect(); // unselect MMC_SD 00331 return OK; 00332 }
| bit mmc_sd_get_cid | ( | U8 * | buffer | ) |
This function reads the CID (Card Identification Data) of the memory card
| buffer | to fill |
Definition at line 346 of file mmc_sd.c.
References FALSE, KO, mmc_sd_command(), mmc_sd_init_done, Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SEND_CID, MMC_STARTBLOCK_READ, OK, r1, r2, Spi_read_data, and Spi_write_data.
00347 { 00348 U8 retry; 00349 00350 // wait for MMC not busy 00351 if (KO == mmc_sd_wait_not_busy()) 00352 return KO; 00353 00354 Mmc_sd_select(); // select MMC_SD 00355 // issue command 00356 r1 = mmc_sd_command(MMC_SEND_CID, 0); 00357 // check for valid response 00358 if(r1 != 0x00) 00359 { 00360 Mmc_sd_unselect(); // unselect MMC_SD 00361 mmc_sd_init_done = FALSE; 00362 return KO; 00363 } 00364 // wait for data block start 00365 retry = 0; 00366 while((r2 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ) 00367 { 00368 if (retry > 8) 00369 { 00370 Mmc_sd_unselect(); // unselect MMC_SD 00371 return KO; 00372 } 00373 retry++; 00374 } 00375 // store valid data block 00376 for (retry = 0; retry <16; retry++) 00377 { 00378 Spi_write_data(0xFF); 00379 buffer[retry] = Spi_read_data(); 00380 } 00381 Spi_write_data(0xFF); // load CRC (not used) 00382 Spi_write_data(0xFF); 00383 Spi_write_data(0xFF); // give clock again to end transaction 00384 Mmc_sd_unselect(); // unselect MMC_SD 00385 return OK; 00386 }
| void mmc_sd_get_capacity | ( | void | ) |
This function extracts structure information from CSD array and compute the number of blocks of the memory card (stored in global U32 mmc_sd_last_block_address), its capacity in bytes (stored in global U32 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
| None |
Definition at line 421 of file mmc_sd.c.
References capacity, card_type, csd, erase_group_size, MMC_CARD, and mmc_sd_last_block_address.
00422 { 00423 U16 c_size; 00424 U8 c_size_mult; 00425 U8 read_bl_len; 00426 U8 erase_grp_size; 00427 U8 erase_grp_mult; 00428 00429 // extract variables from CSD array 00430 c_size = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6); 00431 c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7); 00432 read_bl_len = csd[5] & 0x0F; 00433 if (card_type == MMC_CARD) 00434 { 00435 erase_grp_size = ((csd[10] & 0x7C) >> 2); 00436 erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5); 00437 } 00438 else 00439 { 00440 erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7); 00441 erase_grp_mult = 0; 00442 } 00443 00444 // compute last block addr 00445 mmc_sd_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1; 00446 if (read_bl_len > 9) // 9 means 2^9 = 512b 00447 mmc_sd_last_block_address <<= (read_bl_len - 9); 00448 00449 // compute card capacity in bytes 00450 capacity = (1 << read_bl_len) * (mmc_sd_last_block_address + 1); 00451 00452 // compute block group size for erase operation 00453 erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1); 00454 }
| bit mmc_sd_get_status | ( | void | ) |
This function reads the STATUS regsiter of the memory card After a read the error flags are automatically cleared
| None |
Definition at line 469 of file mmc_sd.c.
References KO, Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SEND_STATUS, OK, r2, and Spi_write_data.
00470 { 00471 U8 retry, spireg; 00472 00473 // wait for MMC not busy 00474 if (KO == mmc_sd_wait_not_busy()) 00475 return KO; 00476 00477 Mmc_sd_select(); // select MMC_SD 00478 00479 // send command 00480 Spi_write_data(MMC_SEND_STATUS | 0x40); // send command 00481 Spi_write_data(0); // send parameter 00482 Spi_write_data(0); 00483 Spi_write_data(0); 00484 Spi_write_data(0); 00485 Spi_write_data(0x95); // correct CRC for first command in SPI (CMD0) 00486 // after, the CRC is ignored 00487 // end command 00488 // wait for response 00489 // if more than 8 retries, card has timed-out and return the received 0xFF 00490 retry = 0; 00491 r2 = 0xFFFF; 00492 spireg = 0xFF; 00493 while((spireg = mmc_sd_send_and_read(0xFF)) == 0xFF) 00494 { 00495 retry++; 00496 if(retry > 10) 00497 { 00498 Mmc_sd_unselect(); 00499 return KO; 00500 } 00501 } 00502 r2 = ((U16)(spireg) << 8) + mmc_sd_send_and_read(0xFF); // first byte is MSb 00503 00504 Spi_write_data(0xFF); // give clock again to end transaction 00505 Mmc_sd_unselect(); // unselect MMC_SD 00506 00507 return OK; 00508 }
This function sends a byte over SPI and returns the byte read from the slave.
| data_to_send | byte to send over SPI |
Definition at line 275 of file mmc_sd.c.
References Spi_read_data, and Spi_write_data.
00276 { 00277 Spi_write_data(data_to_send); 00278 return (Spi_read_data()); 00279 }
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
| command | command to send (see mmc_sd.h for command list) arg argument of the command |
Definition at line 217 of file mmc_sd.c.
References mmc_sd_command(), Mmc_sd_select, Mmc_sd_unselect, and r1.
00218 { 00219 Mmc_sd_select(); // select MMC_SD 00220 r1 = mmc_sd_command(command, arg); 00221 Mmc_sd_unselect(); // unselect MMC_SD 00222 return r1; 00223 }
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 commande during a large transmission
| command | command to send (see mmc_sd.h for command list) arg argument of the command |
Definition at line 237 of file mmc_sd.c.
References mmc_sd_send_and_read(), r1, and Spi_write_data.
00238 { 00239 U8 retry; 00240 00241 Spi_write_data(0xFF); // write dummy byte 00242 Spi_write_data(command | 0x40); // send command 00243 Spi_write_data(arg>>24); // send parameter 00244 Spi_write_data(arg>>16); 00245 Spi_write_data(arg>>8 ); 00246 Spi_write_data(arg ); 00247 Spi_write_data(0x95); // correct CRC for first command in SPI (CMD0) 00248 // after, the CRC is ignored 00249 // end command 00250 // wait for response 00251 // if more than 8 retries, card has timed-out and return the received 0xFF 00252 retry = 0; 00253 r1 = 0xFF; 00254 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF) 00255 { 00256 retry++; 00257 if(retry > 10) break; 00258 } 00259 return r1; 00260 }
| bit is_mmc_sd_write_pwd_locked | ( | void | ) |
Protection functions (optionnal).
This function checks if the card is password-locked Old versions of MMC card don't support this feature ! For a MMC, "lock protection" is featured 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
| None |
Definition at line 650 of file mmc_sd.c.
References card_type, csd, KO, MMC_CARD, mmc_sd_get_status(), OK, and r2.
00651 { 00652 if (card_type == MMC_CARD) 00653 { 00654 if (((csd[0] >> 2) & 0x0F) < 2) // lock feature is not present on the card since the MMC is v1.x released ! 00655 return KO; 00656 } 00657 if (KO == mmc_sd_get_status()) // get STATUS response 00658 return KO; 00659 if ((r2&0x0001) != 0) // check "card is locked" flag in R2 response 00660 return OK; 00661 00662 return KO; 00663 }
This function manages locking operations for the SD/MMC card (password protection)
| U8 | operation OP_LOCK -> to lock the card (the current pasword 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) U8 pwd_lg Password length U8 * pwd Pointer on the password (char array) to send |
Definition at line 695 of file mmc_sd.c.
References KO, MMC_DR_ACCEPT, MMC_DR_MASK, MMC_LOCK_UNLOCK, mmc_sd_command(), mmc_sd_get_status(), Mmc_sd_select, mmc_sd_send_and_read(), mmc_sd_send_command(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SET_BLOCKLEN, MMC_STARTBLOCK_WRITE, OK, OP_FORCED_ERASE, r1, r2, and Spi_write_data.
00696 { 00697 bit status = OK; 00698 U8 retry; 00699 00700 // check parameters validity 00701 if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0)) // password length must be > 0 00702 return KO; 00703 00704 // wait card not busy 00705 if (mmc_sd_wait_not_busy() == KO) 00706 return KO; 00707 00708 // set block length 00709 if (operation == OP_FORCED_ERASE) 00710 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 1); // CMD 00711 else 00712 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, pwd_lg+2); // CMD + PWDSLEN + PWD 00713 Spi_write_data(0xFF); // write dummy byte 00714 Spi_write_data(0xFF); // write dummy byte 00715 Spi_write_data(0xFF); // write dummy byte 00716 if (r1 != 0x00) 00717 return KO; 00718 00719 // send the lock command to the card 00720 Mmc_sd_select(); // select MMC_SD 00721 00722 // issue command 00723 r1 = mmc_sd_command(MMC_LOCK_UNLOCK, 0); 00724 00725 // check for valid response 00726 if(r1 != 0x00) 00727 { 00728 status = KO; 00729 } 00730 // send dummy 00731 Spi_write_data(0xFF); // give clock again to end transaction 00732 00733 // send data start token 00734 Spi_write_data(MMC_STARTBLOCK_WRITE); 00735 // write data 00736 Spi_write_data(operation); 00737 if (operation != OP_FORCED_ERASE) 00738 { 00739 Spi_write_data(pwd_lg); 00740 for(retry=0 ; retry<pwd_lg ; retry++) 00741 { 00742 Spi_write_data(*(pwd+retry)); 00743 } 00744 } 00745 Spi_write_data(0xFF); // send CRC (field required but value ignored) 00746 Spi_write_data(0xFF); 00747 00748 // check data response token 00749 retry = 0; 00750 r1 = mmc_sd_send_and_read(0xFF); 00751 if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT) 00752 status = KO; 00753 00754 Spi_write_data(0xFF); // dummy byte 00755 Mmc_sd_unselect(); 00756 00757 // wait card not busy 00758 if (operation == OP_FORCED_ERASE) 00759 retry = 100; 00760 else 00761 retry = 10; 00762 while (mmc_sd_wait_not_busy() == KO) 00763 { 00764 retry--; 00765 if (retry == 0) 00766 { 00767 status = KO; 00768 break; 00769 } 00770 } 00771 00772 // get and check status of the operation 00773 if (KO == mmc_sd_get_status()) // get STATUS response 00774 status = KO; 00775 if ((r2&0x0002) != 0) // check "lock/unlock cmd failed" flag in R2 response 00776 status = KO; 00777 00778 // set original block length 00779 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512); 00780 Spi_write_data(0xFF); // write dummy byte 00781 if (r1 != 0x00) 00782 status = KO; 00783 00784 return status; 00785 }
| bit mmc_sd_read_open | ( | U32 | pos | ) |
Functions for preparing block read/write.
This function opens a MMC_SD memory in read mode at a given sector address (not byte address)
| pos | Sector address |
Definition at line 799 of file mmc_sd.c.
References gl_ptr_mem, and mmc_sd_wait_not_busy().
00800 { 00801 // Set the global memory ptr at a Byte address. 00802 gl_ptr_mem = pos << 9; // gl_ptr_mem = pos * 512 00803 00804 // wait for MMC not busy 00805 return mmc_sd_wait_not_busy(); 00806 }
| void mmc_sd_read_close | ( | void | ) |
| bit mmc_sd_write_open | ( | U32 | pos | ) |
This function opens a MMC_SD 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.
| pos | Sector address |
Definition at line 838 of file mmc_sd.c.
References gl_ptr_mem, and mmc_sd_wait_not_busy().
00839 { 00840 // Set the global memory ptr at a Byte address. 00841 gl_ptr_mem = pos << 9; // gl_ptr_mem = pos * 512 00842 00843 // wait for MMC not busy 00844 return mmc_sd_wait_not_busy(); 00845 }
| void mmc_sd_write_close | ( | void | ) |
| bit mmc_sd_write_sector | ( | U16 | nb_sector | ) |
Funtions to link USB DEVICE flow with MMC.
This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory DATA FLOW is: USB => MMC_SD NOTE:
| nb_sector | number of contiguous sectors to write [IN] | |
| gl_ptr_mem | memory ptr on MMC_SD [global][IN/OUT] |
Definition at line 1056 of file mmc_sd.c.
References gl_ptr_mem, Is_usb_endpoint_enabled, Is_usb_read_enabled, KO, MMC_DR_ACCEPT, MMC_DR_MASK, mmc_sd_command(), Mmc_sd_select, Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_STARTBLOCK_WRITE, MMC_WRITE_BLOCK, OK, r1, Spi_ack_write, Spi_read_data, Spi_write_data, Usb_ack_receive_out, and Usb_read_byte.
01057 { 01058 U8 i; 01059 01060 do 01061 { 01062 // wait card not busy 01063 i=0; 01064 while (KO == mmc_sd_wait_not_busy()) 01065 { 01066 i++; 01067 if (i == 10) 01068 return KO; 01069 } 01070 01071 Mmc_sd_select(); // select MMC_SD 01072 // issue command 01073 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem); 01074 // check for valid response 01075 if(r1 != 0x00) 01076 { 01077 Mmc_sd_unselect(); // unselect MMC_SD 01078 return KO; 01079 } 01080 // send dummy 01081 Spi_write_data(0xFF); // give clock again to end transaction 01082 // send data start token 01083 Spi_write_data(MMC_STARTBLOCK_WRITE); 01084 // write data 01085 //# 01086 //# Write 8x64b = 512b from the USB FIFO OUT. 01087 //# 01088 for (i = 8; i != 0; i--) 01089 { 01090 // Wait end of rx in USB EPOUT. 01091 while(!Is_usb_read_enabled()) 01092 { 01093 if(!Is_usb_endpoint_enabled()) 01094 return KO; // USB Reset 01095 } 01096 01097 Disable_interrupt(); // Global disable. 01098 01099 // SPI write principle: send a Byte then clear the SPIF flag. 01100 // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit, 01101 // (.) send a Byte read from USB, 01102 // (.) 1st step to clear the SPIF bit. 01103 Spi_write_data(Usb_read_byte()); 01104 Spi_write_data(Usb_read_byte()); 01105 Spi_write_data(Usb_read_byte()); 01106 Spi_write_data(Usb_read_byte()); 01107 Spi_write_data(Usb_read_byte()); 01108 Spi_write_data(Usb_read_byte()); 01109 Spi_write_data(Usb_read_byte()); 01110 Spi_write_data(Usb_read_byte()); 01111 Spi_write_data(Usb_read_byte()); 01112 Spi_write_data(Usb_read_byte()); 01113 Spi_write_data(Usb_read_byte()); 01114 Spi_write_data(Usb_read_byte()); 01115 Spi_write_data(Usb_read_byte()); 01116 Spi_write_data(Usb_read_byte()); 01117 Spi_write_data(Usb_read_byte()); 01118 Spi_write_data(Usb_read_byte()); 01119 Spi_write_data(Usb_read_byte()); 01120 Spi_write_data(Usb_read_byte()); 01121 Spi_write_data(Usb_read_byte()); 01122 Spi_write_data(Usb_read_byte()); 01123 Spi_write_data(Usb_read_byte()); 01124 Spi_write_data(Usb_read_byte()); 01125 Spi_write_data(Usb_read_byte()); 01126 Spi_write_data(Usb_read_byte()); 01127 Spi_write_data(Usb_read_byte()); 01128 Spi_write_data(Usb_read_byte()); 01129 Spi_write_data(Usb_read_byte()); 01130 Spi_write_data(Usb_read_byte()); 01131 Spi_write_data(Usb_read_byte()); 01132 Spi_write_data(Usb_read_byte()); 01133 Spi_write_data(Usb_read_byte()); 01134 Spi_write_data(Usb_read_byte()); 01135 Spi_write_data(Usb_read_byte()); 01136 Spi_write_data(Usb_read_byte()); 01137 Spi_write_data(Usb_read_byte()); 01138 Spi_write_data(Usb_read_byte()); 01139 Spi_write_data(Usb_read_byte()); 01140 Spi_write_data(Usb_read_byte()); 01141 Spi_write_data(Usb_read_byte()); 01142 Spi_write_data(Usb_read_byte()); 01143 Spi_write_data(Usb_read_byte()); 01144 Spi_write_data(Usb_read_byte()); 01145 Spi_write_data(Usb_read_byte()); 01146 Spi_write_data(Usb_read_byte()); 01147 Spi_write_data(Usb_read_byte()); 01148 Spi_write_data(Usb_read_byte()); 01149 Spi_write_data(Usb_read_byte()); 01150 Spi_write_data(Usb_read_byte()); 01151 Spi_write_data(Usb_read_byte()); 01152 Spi_write_data(Usb_read_byte()); 01153 Spi_write_data(Usb_read_byte()); 01154 Spi_write_data(Usb_read_byte()); 01155 Spi_write_data(Usb_read_byte()); 01156 Spi_write_data(Usb_read_byte()); 01157 Spi_write_data(Usb_read_byte()); 01158 Spi_write_data(Usb_read_byte()); 01159 Spi_write_data(Usb_read_byte()); 01160 Spi_write_data(Usb_read_byte()); 01161 Spi_write_data(Usb_read_byte()); 01162 Spi_write_data(Usb_read_byte()); 01163 Spi_write_data(Usb_read_byte()); 01164 Spi_write_data(Usb_read_byte()); 01165 Spi_write_data(Usb_read_byte()); 01166 Spi_write_data(Usb_read_byte()); 01167 Spi_ack_write(); // Final step to clear the SPIF bit. 01168 01169 Usb_ack_receive_out(); // USB EPOUT read acknowledgement. 01170 01171 Enable_interrupt(); // Global re-enable. 01172 } // for (i = 8; i != 0; i--) 01173 01174 Spi_write_data(0xFF); // send dummy CRC 01175 Spi_write_data(0xFF); 01176 01177 // read data response token 01178 Spi_write_data(0xFF); 01179 r1 = Spi_read_data(); 01180 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT) 01181 { 01182 Mmc_sd_unselect(); // unselect MMC_SD 01183 return r1; 01184 } 01185 01186 // send dummy byte 01187 Spi_write_data(0xFF); 01188 01189 // release chip select 01190 Mmc_sd_unselect(); // unselect MMC_SD 01191 gl_ptr_mem += 512; // Update the memory pointer. 01192 nb_sector--; // 1 more sector written 01193 } 01194 while (nb_sector != 0); 01195 01196 // wait card not busy after last programming operation 01197 i=0; 01198 while (KO == mmc_sd_wait_not_busy()) 01199 { 01200 i++; 01201 if (i == 10) 01202 return KO; 01203 } 01204 01205 return OK; // Write done 01206 }
| bit mmc_sd_read_sector | ( | U16 | nb_sector | ) |
This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller DATA FLOW is: MMC_SD => USB NOTE:
| nb_sector | number of contiguous sectors to read [IN] | |
| gl_ptr_mem | memory ptr on MMC_SD [global][IN/OUT] |
Definition at line 891 of file mmc_sd.c.
References FALSE, gl_ptr_mem, Is_usb_endpoint_enabled, Is_usb_write_enabled, KO, MMC_READ_SINGLE_BLOCK, mmc_sd_command(), Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, MMC_STARTBLOCK_READ, OK, r1, Spi_read_data, Spi_write_data, Usb_send_in, and Usb_write_byte.
00892 { 00893 Byte i; 00894 U16 read_time_out; 00895 00896 do 00897 { 00898 Mmc_sd_select(); // select MMC_SD 00899 // issue command 00900 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem); 00901 // check for valid response 00902 if(r1 != 0x00) 00903 { 00904 Mmc_sd_unselect(); // unselect MMC_SD 00905 return KO; 00906 } 00907 00908 // wait for token (may be a datablock start token OR a data error token !) 00909 read_time_out = 30000; 00910 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF) 00911 { 00912 read_time_out--; 00913 if (read_time_out == 0) // TIME-OUT 00914 { 00915 Mmc_sd_unselect(); // unselect MMC_SD 00916 return KO; 00917 } 00918 } 00919 00920 // check token 00921 if (r1 != MMC_STARTBLOCK_READ) 00922 { 00923 Spi_write_data(0xFF); 00924 Mmc_sd_unselect(); // unselect MMC_SD 00925 return KO; 00926 } 00927 00928 //# 00929 //# Read 8x64b = 512b, put them in the USB FIFO IN. 00930 //# 00931 for (i = 8; i != 0; i--) 00932 { 00933 Disable_interrupt(); // Global disable. 00934 00935 // Principle: send any Byte to get a Byte. 00936 // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit. 00937 // Spi_read_data(): get the Byte + final step to clear the SPIF bit. 00938 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00939 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00940 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00941 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00942 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00943 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00944 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00945 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00946 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00947 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00948 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00949 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00950 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00951 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00952 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00953 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00954 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00955 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00956 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00957 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00958 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00959 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00960 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00961 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00962 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00963 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00964 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00965 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00966 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00967 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00968 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00969 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00970 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00971 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00972 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00973 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00974 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00975 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00976 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00977 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00978 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00979 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00980 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00981 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00982 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00983 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00984 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00985 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00986 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00987 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00988 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00989 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00990 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00991 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00992 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00993 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00994 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00995 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00996 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00997 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00998 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 00999 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 01000 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 01001 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data()); 01002 Enable_interrupt(); // Global re-enable. 01003 01004 //# 01005 //# Send the USB FIFO IN content to the USB Host. 01006 //# 01007 Usb_send_in(); // Send the FIFO IN content to the USB Host. 01008 // Wait until the tx is done so that we may write to the FIFO IN again. 01009 while(Is_usb_write_enabled()==FALSE) 01010 { 01011 if(!Is_usb_endpoint_enabled()) 01012 return KO; // USB Reset 01013 } 01014 } // for (i = 8; i != 0; i--) 01015 01016 gl_ptr_mem += 512; // Update the memory pointer. 01017 nb_sector--; // 1 more sector read 01018 // read 16-bit CRC 01019 Spi_write_data(0xFF); 01020 Spi_write_data(0xFF); 01021 // dummy bytes 01022 Spi_write_data(0xFF); 01023 Spi_write_data(0xFF); 01024 // release chip select 01025 Mmc_sd_unselect(); // unselect MMC_SD 01026 } 01027 while (nb_sector != 0); 01028 01029 return OK; // Read done. 01030 }
| bit mmc_sd_read_sector_to_ram | ( | U8 * | ram | ) |
Functions to read/write one sector (512btes) with ram buffer pointer.
This function read one MMC sector and load it into a ram buffer DATA FLOW is: MMC/SD => RAM NOTE:
| *ram | pointer to ram buffer |
Definition at line 1360 of file mmc_sd.c.
References gl_ptr_mem, KO, MMC_READ_SINGLE_BLOCK, mmc_sd_command(), Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SECTOR_SIZE, MMC_STARTBLOCK_READ, OK, r1, Spi_read_data, and Spi_write_data.
01361 { 01362 U16 i; 01363 U16 read_time_out; 01364 01365 // wait for MMC not busy 01366 if (KO == mmc_sd_wait_not_busy()) 01367 return KO; 01368 01369 Mmc_sd_select(); // select MMC_SD 01370 // issue command 01371 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem); 01372 01373 // check for valid response 01374 if (r1 != 0x00) 01375 { 01376 Mmc_sd_unselect(); // unselect MMC_SD 01377 return KO; 01378 } 01379 01380 // wait for token (may be a datablock start token OR a data error token !) 01381 read_time_out = 30000; 01382 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF) 01383 { 01384 read_time_out--; 01385 if (read_time_out == 0) // TIME-OUT 01386 { 01387 Mmc_sd_unselect(); // unselect MMC_SD 01388 return KO; 01389 } 01390 } 01391 01392 // check token 01393 if (r1 != MMC_STARTBLOCK_READ) 01394 { 01395 Spi_write_data(0xFF); 01396 Mmc_sd_unselect(); // unselect MMC_SD 01397 return KO; 01398 } 01399 01400 // store datablock 01401 Disable_interrupt(); // Global disable. 01402 for(i=0;i<MMC_SECTOR_SIZE;i++) 01403 { 01404 Spi_write_data(0xFF); 01405 *ram=Spi_read_data(); 01406 ram++; 01407 } 01408 Enable_interrupt(); // Global re-enable. 01409 gl_ptr_mem += 512; // Update the memory pointer. 01410 01411 // load 16-bit CRC (ignored) 01412 Spi_write_data(0xFF); 01413 Spi_write_data(0xFF); 01414 01415 // continue delivering some clock cycles 01416 Spi_write_data(0xFF); 01417 Spi_write_data(0xFF); 01418 01419 // release chip select 01420 Mmc_sd_unselect(); // unselect MMC_SD 01421 01422 return OK; // Read done. 01423 }
| bit mmc_sd_write_sector_from_ram | ( | U8 * | ram | ) |
This function write one MMC sector from a ram buffer DATA FLOW is: RAM => MMC/SD NOTE (please read) :
| *ram | pointer to ram buffer |
Definition at line 1443 of file mmc_sd.c.
References gl_ptr_mem, KO, MMC_DR_ACCEPT, MMC_DR_MASK, mmc_sd_command(), Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_SECTOR_SIZE, MMC_STARTBLOCK_WRITE, MMC_WRITE_BLOCK, OK, r1, and Spi_write_data.
01444 { 01445 U16 i; 01446 01447 // wait for MMC not busy 01448 if (KO == mmc_sd_wait_not_busy()) 01449 return KO; 01450 01451 Mmc_sd_select(); // select MMC_SD 01452 // issue command 01453 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem); 01454 // check for valid response 01455 if(r1 != 0x00) 01456 { 01457 Mmc_sd_unselect(); 01458 return KO; 01459 } 01460 // send dummy 01461 Spi_write_data(0xFF); // give clock again to end transaction 01462 01463 // send data start token 01464 Spi_write_data(MMC_STARTBLOCK_WRITE); 01465 // write data 01466 for(i=0;i<MMC_SECTOR_SIZE;i++) 01467 { 01468 Spi_write_data(*ram); 01469 ram++; 01470 } 01471 01472 Spi_write_data(0xFF); // send CRC (field required but value ignored) 01473 Spi_write_data(0xFF); 01474 01475 // read data response token 01476 r1 = mmc_sd_send_and_read(0xFF); 01477 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT) 01478 { 01479 Spi_write_data(0xFF); // send dummy bytes 01480 Spi_write_data(0xFF); 01481 Mmc_sd_unselect(); 01482 return KO; 01483 // return r1; // return ERROR byte 01484 } 01485 01486 Spi_write_data(0xFF); // send dummy bytes 01487 Spi_write_data(0xFF); 01488 01489 // release chip select 01490 Mmc_sd_unselect(); // unselect MMC_SD 01491 gl_ptr_mem += 512; // Update the memory pointer. 01492 01493 // wait card not busy after last programming operation 01494 i=0; 01495 while (KO == mmc_sd_wait_not_busy()) 01496 { 01497 i++; 01498 if (i == 10) 01499 return KO; 01500 } 01501 01502 return OK; // Write done 01503 }
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) :
| U32 | adr_start address of 1st group (sector address, not byte address) U32 adr_end address of last group (sector address, not byte address) |
Definition at line 1295 of file mmc_sd.c.
References card_type, KO, MMC_CARD, MMC_ERASE, mmc_sd_command(), Mmc_sd_select, Mmc_sd_unselect, mmc_sd_wait_not_busy(), MMC_TAG_ERASE_GROUP_END, MMC_TAG_ERASE_GROUP_START, OK, r1, SD_TAG_WR_ERASE_GROUP_END, SD_TAG_WR_ERASE_GROUP_START, and Spi_write_data.
01296 { 01297 U8 cmd; 01298 01299 // wait for MMC not busy 01300 if (KO == mmc_sd_wait_not_busy()) 01301 return KO; 01302 01303 Mmc_sd_select(); // select MMC_SD 01304 01305 // send address of 1st group 01306 if (card_type == MMC_CARD) 01307 { cmd = MMC_TAG_ERASE_GROUP_START; } 01308 else 01309 { cmd = SD_TAG_WR_ERASE_GROUP_START; } 01310 if ((r1 = mmc_sd_command(cmd,(adr_start << 9))) != 0) 01311 { 01312 Mmc_sd_unselect(); 01313 return KO; 01314 } 01315 Spi_write_data(0xFF); 01316 01317 // send address of last group 01318 if (card_type == MMC_CARD) 01319 { cmd = MMC_TAG_ERASE_GROUP_END; } 01320 else 01321 { cmd = SD_TAG_WR_ERASE_GROUP_END; } 01322 if ((r1 = mmc_sd_command(cmd,(adr_end << 9))) != 0) 01323 { 01324 Mmc_sd_unselect(); 01325 return KO; 01326 } 01327 Spi_write_data(0xFF); 01328 01329 // send erase command 01330 if ((r1 = mmc_sd_command(MMC_ERASE,0)) != 0) 01331 { 01332 Mmc_sd_unselect(); 01333 return KO; 01334 } 01335 Spi_write_data(0xFF); 01336 01337 Mmc_sd_unselect(); 01338 01339 return OK; 01340 }
| volatile U32 mmc_sd_last_block_address |
| bit mmc_sd_init_done |
1.5.3