mmc_sd.h File Reference

#include "config.h"

Include dependency graph for mmc_sd.h:

This graph shows which files directly or indirectly include this file:

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


Detailed Description

This file contains the low-level SD/MMC routines - Compiler: IAR EWAVR and GNU GCC for AVR

Definition in file mmc_sd.h.


Define Documentation

#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 70 of file mmc_sd.h.

Referenced by mmc_sd_read_sector_to_ram(), and mmc_sd_write_sector_from_ram().

#define byte_adr_of_block ( ad   )     (ad<<9)

Definition at line 73 of file mmc_sd.h.

#define MMC_CARD   0

Definition at line 78 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

Definition at line 79 of file mmc_sd.h.

Referenced by mmc_sd_init().

#define OP_UNLOCK   0x00

Definition at line 82 of file mmc_sd.h.

#define OP_LOCK   0x04

Definition at line 83 of file mmc_sd.h.

#define OP_RESET_PWD   0x02

Definition at line 84 of file mmc_sd.h.

#define OP_SET_PWD   0x01

Definition at line 85 of file mmc_sd.h.

#define OP_FORCED_ERASE   0x08

Definition at line 86 of file mmc_sd.h.

Referenced by mmc_sd_lock_operation().

#define MMC_GO_IDLE_STATE   0

initialize card to SPI-type access

Definition at line 89 of file mmc_sd.h.

Referenced by mmc_sd_check_presence(), and mmc_sd_init().

#define MMC_SEND_OP_COND   1

set card operational mode

Definition at line 90 of file mmc_sd.h.

Referenced by mmc_sd_init().

#define MMC_CMD2   2

illegal in SPI mode !

Definition at line 91 of file mmc_sd.h.

#define MMC_SEND_CSD   9

get card's CSD

Definition at line 92 of file mmc_sd.h.

Referenced by mmc_sd_get_csd().

#define MMC_SEND_CID   10

get card's CID

Definition at line 93 of file mmc_sd.h.

Referenced by mmc_sd_get_cid().

#define MMC_SEND_STATUS   13

Definition at line 94 of file mmc_sd.h.

Referenced by mmc_sd_get_status().

#define MMC_SET_BLOCKLEN   16

Set number of bytes to transfer per block.

Definition at line 95 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 96 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 97 of file mmc_sd.h.

Referenced by mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().

#define MMC_PROGRAM_CSD   27

Definition at line 98 of file mmc_sd.h.

#define MMC_SET_WRITE_PROT   28

Definition at line 99 of file mmc_sd.h.

#define MMC_CLR_WRITE_PROT   29

Definition at line 100 of file mmc_sd.h.

#define MMC_SEND_WRITE_PROT   30

Definition at line 101 of file mmc_sd.h.

#define SD_TAG_WR_ERASE_GROUP_START   32

Definition at line 102 of file mmc_sd.h.

Referenced by mmc_sd_erase_sector_group().

#define SD_TAG_WR_ERASE_GROUP_END   33

Definition at line 103 of file mmc_sd.h.

Referenced by mmc_sd_erase_sector_group().

#define MMC_TAG_SECTOR_START   32

Definition at line 104 of file mmc_sd.h.

#define MMC_TAG_SECTOR_END   33

Definition at line 105 of file mmc_sd.h.

#define MMC_UNTAG_SECTOR   34

Definition at line 106 of file mmc_sd.h.

#define MMC_TAG_ERASE_GROUP_START   35

Sets beginning of erase group (mass erase).

Definition at line 107 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 108 of file mmc_sd.h.

Referenced by mmc_sd_erase_sector_group().

#define MMC_UNTAG_ERASE_GROUP   37

Untag (unset) erase group (mass erase).

Definition at line 109 of file mmc_sd.h.

#define MMC_ERASE   38

Perform block/mass erase.

Definition at line 110 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 111 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 112 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 113 of file mmc_sd.h.

Referenced by mmc_sd_init().

#define MMC_CRC_ON_OFF   59

Turns CRC check on/off.

Definition at line 114 of file mmc_sd.h.

Referenced by mmc_sd_check_presence(), and mmc_sd_init().

#define MMC_R1_BUSY   0x80

R1 response: bit indicates card is busy.

Definition at line 116 of file mmc_sd.h.

#define MMC_R1_PARAMETER   0x40

Definition at line 117 of file mmc_sd.h.

#define MMC_R1_ADDRESS   0x20

Definition at line 118 of file mmc_sd.h.

#define MMC_R1_ERASE_SEQ   0x10

Definition at line 119 of file mmc_sd.h.

#define MMC_R1_COM_CRC   0x08

Definition at line 120 of file mmc_sd.h.

#define MMC_R1_ILLEGAL_COM   0x04

Definition at line 121 of file mmc_sd.h.

#define MMC_R1_ERASE_RESET   0x02

Definition at line 122 of file mmc_sd.h.

#define MMC_R1_IDLE_STATE   0x01

Definition at line 123 of file mmc_sd.h.

#define MMC_STARTBLOCK_READ   0xFE

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

Definition at line 125 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 126 of file mmc_sd.h.

Referenced by mmc_sd_lock_operation(), mmc_sd_write_sector(), and mmc_sd_write_sector_from_ram().

#define MMC_STARTBLOCK_MWRITE   0xFC

Definition at line 127 of file mmc_sd.h.

#define MMC_STOPTRAN_WRITE   0xFD

Definition at line 129 of file mmc_sd.h.

#define MMC_DE_MASK   0x1F

Definition at line 131 of file mmc_sd.h.

#define MMC_DE_ERROR   0x01

Definition at line 132 of file mmc_sd.h.

#define MMC_DE_CC_ERROR   0x02

Definition at line 133 of file mmc_sd.h.

#define MMC_DE_ECC_FAIL   0x04

Definition at line 134 of file mmc_sd.h.

#define MMC_DE_OUT_OF_RANGE   0x04

Definition at line 135 of file mmc_sd.h.

#define MMC_DE_CARD_LOCKED   0x04

Definition at line 136 of file mmc_sd.h.

#define MMC_DR_MASK   0x1F

Definition at line 138 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 139 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_REJECT_CRC   0x0B

Definition at line 140 of file mmc_sd.h.

#define MMC_DR_REJECT_WRITE_ERROR   0x0D

Definition at line 141 of file mmc_sd.h.


Function Documentation

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.

Warning:
Code:?? bytes (function code length)
Parameters:
none 
Returns:
none
/

Definition at line 88 of file mmc_sd.c.

00089 {
00090    Spi_select_master();
00091    Spi_set_mode(SPI_MODE_0);
00092    Spi_init_bus();
00093    Spi_set_rate(SPI_RATE_0);  // SCK freq == fosc/2.
00094    Spi_disable_ss();
00095    Spi_enable();
00096 }

bit mmc_sd_init ( void   ) 

This function initializes the Dataflash controller & the SPI bus(over which the MMC_SD is controlled).

Warning:
Code:?? bytes (function code length)
Parameters:
none 
Returns:
bit The memory is ready -> OK (always) /

Definition at line 110 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.

00111 {
00112   U16 retry;
00113 
00114   // INIT HARDWARE
00115   mmc_sd_spi_init();
00116 
00117   // RESET THE MEMORY CARD
00118   mmc_sd_init_done = FALSE;
00119   card_type = MMC_CARD;
00120   retry = 0;
00121   do
00122   {
00123     // reset card and go to SPI mode
00124     r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00125     Spi_write_data(0xFF);            // write dummy byte
00126     // do retry counter
00127     retry++;
00128     if(retry > 100)
00129       return KO;
00130   }
00131   while(r1 != 0x01);   // check memory enters idle_state
00132 
00133   // IDENTIFICATION OF THE CARD TYPE (SD or MMC)
00134   // Both cards will accept CMD55 command but only the SD card will respond to ACMD41
00135   r1 = mmc_sd_send_command(SD_APP_CMD55,0);
00136   Spi_write_data(0xFF);  // write dummy byte
00137 
00138   r1 = mmc_sd_send_command(SD_SEND_OP_COND_ACMD, 0);
00139   Spi_write_data(0xFF);  // write dummy byte
00140 
00141   if ((r1&0xFE) == 0)   // ignore "in_idle_state" flag bit
00142   {
00143     card_type = SD_CARD;    // card has accepted the command, this is a SD card
00144   }
00145   else
00146   {
00147     card_type = MMC_CARD;   // card has not responded, this is a MMC card
00148     // reset card again
00149     retry = 0;
00150     do
00151     {
00152       // reset card again
00153       r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00154       Spi_write_data(0xFF);            // write dummy byte
00155       // do retry counter
00156       retry++;
00157       if(retry > 100)
00158         return KO;
00159     }
00160     while(r1 != 0x01);   // check memory enters idle_state
00161   }
00162 
00163   // CONTINUE INTERNAL INITIALIZATION OF THE CARD
00164   // Continue sending CMD1 while memory card is in idle state
00165   retry = 0;
00166   do
00167   {
00168      // initializing card for operation
00169      r1 = mmc_sd_send_command(MMC_SEND_OP_COND, 0);
00170      Spi_write_data(0xFF);            // write dummy byte
00171      // do retry counter
00172      retry++;
00173      if(retry == 50000)    // measured approx. 500 on several cards
00174         return KO;
00175   }
00176   while (r1);
00177 
00178   // DISABLE CRC TO SIMPLIFY AND SPEED UP COMMUNICATIONS
00179   r1 = mmc_sd_send_command(MMC_CRC_ON_OFF, 0);  // disable CRC (should be already initialized on SPI init)
00180   Spi_write_data(0xFF);            // write dummy byte
00181 
00182   // SET BLOCK LENGTH TO 512 BYTES
00183   r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00184   Spi_write_data(0xFF);            // write dummy byte
00185   if (r1 != 0x00)
00186     return KO;    // card unsupported if block length of 512b is not accepted
00187 
00188   // GET CARD SPECIFIC DATA
00189   if (KO ==  mmc_sd_get_csd(csd))
00190     return KO;
00191 
00192   // GET CARD CAPACITY and NUMBER OF SECTORS
00193   mmc_sd_get_capacity();
00194 
00195   // GET CARD IDENTIFICATION DATA IF REQUIRED
00196 #if (MMC_SD_READ_CID == ENABLED)
00197   if (KO ==  mmc_sd_get_cid(cid))
00198     return KO;
00199 #endif
00200 
00201   mmc_sd_init_done = TRUE;
00202 
00203   return(OK);
00204 }

Here is the call graph for this function:

bit mmc_sd_check_presence ( void   ) 

This function check the presence of a memory card

Definition at line 559 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.

00560 {
00561   U16 retry;
00562 
00563   retry = 0;
00564   if (mmc_sd_init_done == FALSE)
00565   {
00566     // If memory is not initialized, try to initialize it (CMD0)
00567     // If no valid response, there is no card
00568     while ((r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01)
00569     {
00570       Spi_write_data(0xFF);            // write dummy byte
00571       retry++;
00572       if (retry > 10)
00573         return KO;
00574     }
00575     return OK;
00576   }
00577   else
00578   {
00579     // If memory already initialized, send a CRC command (CMD59) (supported only if card is initialized)
00580     /*
00581     retry = 0;
00582     while (retry != 50000)
00583     {
00584       r1 = mmc_sd_send_command(MMC_CMD2,0);   // unsupported command in SPI mode
00585       Spi_write_data(0xFF);            // write dummy byte
00586       if (r1 != 0)    // memory must answer with an error code (with bit7=0)
00587       {
00588         if (r1 < 0x80)
00589           return OK;
00590         else
00591         {
00592           mmc_sd_init_done = FALSE;
00593           return KO;
00594         }
00595       }
00596       retry++;
00597     }
00598     */
00599     if ((r1 = mmc_sd_send_command(MMC_CRC_ON_OFF,0)) == 0x00)
00600       return OK;
00601     mmc_sd_init_done = FALSE;
00602     return KO;
00603   }
00604 
00605   return KO;
00606 }

Here is the call graph for this function:

bit mmc_sd_mem_check ( void   ) 

This function performs a memory check on the MMC_SD.

Warning:
Code:?? bytes (function code length)
Parameters:
none 
Returns:
bit The memory is ready -> OK The memory check failed -> KO /

Definition at line 621 of file mmc_sd.c.

References FALSE, KO, mmc_sd_check_presence(), mmc_sd_init(), mmc_sd_init_done, OK, and TRUE.

00622 {
00623   if (mmc_sd_check_presence() == OK)
00624   {
00625     if (mmc_sd_init_done == FALSE)
00626     {
00627       mmc_sd_init();
00628     }
00629     if (mmc_sd_init_done == TRUE)
00630       return OK;
00631     else
00632       return KO;
00633   }
00634   return KO;
00635 }

Here is the call graph for this function:

bit mmc_sd_wait_not_busy ( void   ) 

This function waits until the MMC/SD is not busy.

Warning:
Code:?? bytes (function code length)
Parameters:
None 
Returns:
bit OK when card is not busy /

Definition at line 524 of file mmc_sd.c.

References KO, Mmc_sd_select, mmc_sd_send_and_read(), Mmc_sd_unselect, OK, and r1.

00525 {
00526   U16 retry;
00527 
00528   // Select the MMC_SD memory gl_ptr_mem points to
00529   Mmc_sd_select();
00530   retry = 0;
00531   while((r1 = mmc_sd_send_and_read(0xFF)) != 0xFF)
00532   {
00533     retry++;
00534     if (retry == 50000)
00535     {
00536       Mmc_sd_unselect();
00537       return KO;
00538     }
00539   }
00540   Mmc_sd_unselect();
00541   return OK;
00542 }

Here is the call graph for this function:

bit mmc_sd_get_csd ( U8 buffer  ) 

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

Warning:
Code:?? bytes (function code length)
Parameters:
buffer to fill
Returns:
bit OK / KO /

Definition at line 296 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.

00297 {
00298 U8 retry;
00299 
00300   // wait for MMC not busy
00301   if (KO == mmc_sd_wait_not_busy())
00302     return KO;
00303 
00304   Mmc_sd_select();                  // select MMC_SD
00305   // issue command
00306   r1 = mmc_sd_command(MMC_SEND_CSD, 0);
00307   // check for valid response
00308   if(r1 != 0x00)
00309   {
00310     Mmc_sd_unselect();     // unselect MMC_SD
00311     mmc_sd_init_done = FALSE;
00312     return KO;
00313   }
00314   // wait for block start
00315   retry = 0;
00316   while((r1 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00317   {
00318     if (retry > 8)
00319     {
00320       Mmc_sd_unselect();     // unselect MMC_SD
00321       return KO;
00322     }
00323     retry++;
00324   }
00325   for (retry = 0; retry <16; retry++)
00326   {
00327     Spi_write_data(0xFF);
00328     buffer[retry] = Spi_read_data();
00329   }
00330   Spi_write_data(0xFF);   // load CRC (not used)
00331   Spi_write_data(0xFF);
00332   Spi_write_data(0xFF);   // give clock again to end transaction
00333   Mmc_sd_unselect();     // unselect MMC_SD
00334   return OK;
00335 }

Here is the call graph for this function:

bit mmc_sd_get_cid ( U8 buffer  ) 

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

Warning:
Code:?? bytes (function code length)
Parameters:
buffer to fill
Returns:
bit OK / KO /

Definition at line 349 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.

00350 {
00351 U8 retry;
00352 
00353   // wait for MMC not busy
00354   if (KO == mmc_sd_wait_not_busy())
00355     return KO;
00356 
00357   Mmc_sd_select();                  // select MMC_SD
00358   // issue command
00359   r1 = mmc_sd_command(MMC_SEND_CID, 0);
00360   // check for valid response
00361   if(r1 != 0x00)
00362   {
00363     Mmc_sd_unselect();     // unselect MMC_SD
00364     mmc_sd_init_done = FALSE;
00365     return KO;
00366   }
00367   // wait for data block start
00368   retry = 0;
00369   while((r2 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00370   {
00371     if (retry > 8)
00372     {
00373       Mmc_sd_unselect();     // unselect MMC_SD
00374       return KO;
00375     }
00376     retry++;
00377   }
00378   // store valid data block
00379   for (retry = 0; retry <16; retry++)
00380   {
00381     Spi_write_data(0xFF);
00382     buffer[retry] = Spi_read_data();
00383   }
00384   Spi_write_data(0xFF);   // load CRC (not used)
00385   Spi_write_data(0xFF);
00386   Spi_write_data(0xFF);   // give clock again to end transaction
00387   Mmc_sd_unselect();     // unselect MMC_SD
00388   return OK;
00389 }

Here is the call graph for this function:

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

Warning:
Code:?? bytes (function code length)
Parameters:
None 
Returns:
bit OK /

Definition at line 424 of file mmc_sd.c.

References capacity, card_type, csd, erase_group_size, MMC_CARD, and mmc_sd_last_block_address.

00425 {
00426   U16 c_size;
00427   U8  c_size_mult;
00428   U8  read_bl_len;
00429   U8  erase_grp_size;
00430   U8  erase_grp_mult;
00431 
00432   // extract variables from CSD array
00433   c_size      = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6);
00434   c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7);
00435   read_bl_len = csd[5] & 0x0F;
00436   if (card_type == MMC_CARD)
00437   {
00438     erase_grp_size = ((csd[10] & 0x7C) >> 2);
00439     erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5);
00440   }
00441   else
00442   {
00443     erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7);
00444     erase_grp_mult = 0;
00445   }
00446 
00447   // compute last block addr
00448   mmc_sd_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1;
00449   if (read_bl_len > 9)  // 9 means 2^9 = 512b
00450     mmc_sd_last_block_address <<= (read_bl_len - 9);
00451 
00452   // compute card capacity in bytes
00453   capacity = (1 << read_bl_len) * (mmc_sd_last_block_address + 1);
00454 
00455   // compute block group size for erase operation
00456   erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1);
00457 }

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

Warning:
Code:?? bytes (function code length)
Parameters:
None 
Returns:
bit The open succeeded -> OK /

Definition at line 472 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.

00473 {
00474   U8 retry, spireg;
00475 
00476   // wait for MMC not busy
00477   if (KO == mmc_sd_wait_not_busy())
00478     return KO;
00479 
00480   Mmc_sd_select();       // select MMC_SD
00481 
00482   // send command
00483   Spi_write_data(MMC_SEND_STATUS | 0x40);  // send command
00484   Spi_write_data(0);                       // send parameter
00485   Spi_write_data(0);
00486   Spi_write_data(0);
00487   Spi_write_data(0);
00488   Spi_write_data(0x95);            // correct CRC for first command in SPI (CMD0)
00489                                   // after, the CRC is ignored
00490   // end command
00491   // wait for response
00492   // if more than 8 retries, card has timed-out and return the received 0xFF
00493   retry = 0;
00494   r2 = 0xFFFF;
00495   spireg = 0xFF;
00496   while((spireg = mmc_sd_send_and_read(0xFF)) == 0xFF)
00497   {
00498     retry++;
00499     if(retry > 10)
00500     {
00501       Mmc_sd_unselect();
00502       return KO;
00503     }
00504   }
00505   r2 = ((U16)(spireg) << 8) + mmc_sd_send_and_read(0xFF);    // first byte is MSb
00506 
00507   Spi_write_data(0xFF);   // give clock again to end transaction
00508   Mmc_sd_unselect();     // unselect MMC_SD
00509 
00510   return OK;
00511 }

Here is the call graph for this function:

U8 mmc_sd_send_and_read ( U8  data_to_send  ) 

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

Warning:
Code:?? bytes (function code length)
Parameters:
data_to_send byte to send over SPI
Returns:
U8 Byte read from the slave /

Definition at line 278 of file mmc_sd.c.

References Spi_read_data, and Spi_write_data.

00279 {
00280    Spi_write_data(data_to_send);
00281    return (Spi_read_data());
00282 }

U8 mmc_sd_send_command ( U8  command,
U32  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

Warning:
Code:?? bytes (function code length)
Parameters:
command command to send (see mmc_sd.h for command list) arg argument of the command
Returns:
U8 R1 response (R1 == 0xFF if time out error) /

Definition at line 220 of file mmc_sd.c.

References mmc_sd_command(), Mmc_sd_select, Mmc_sd_unselect, and r1.

00221 {
00222   Mmc_sd_select();                    // select MMC_SD
00223   r1 = mmc_sd_command(command, arg);
00224   Mmc_sd_unselect();                  // unselect MMC_SD
00225   return r1;
00226 }

Here is the call graph for this function:

U8 mmc_sd_command ( U8  command,
U32  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 commande during a large transmission

Warning:
Code:?? bytes (function code length)
Parameters:
command command to send (see mmc_sd.h for command list) arg argument of the command
Returns:
U8 R1 response (R1 == 0xFF time out error) /

Definition at line 240 of file mmc_sd.c.

References mmc_sd_send_and_read(), r1, and Spi_write_data.

00241 {
00242 U8 retry;
00243 
00244   Spi_write_data(0xFF);            // write dummy byte
00245   Spi_write_data(command | 0x40);  // send command
00246   Spi_write_data(arg>>24);         // send parameter
00247   Spi_write_data(arg>>16);
00248   Spi_write_data(arg>>8 );
00249   Spi_write_data(arg    );
00250   Spi_write_data(0x95);            // correct CRC for first command in SPI (CMD0)
00251                                   // after, the CRC is ignored
00252   // end command
00253   // wait for response
00254   // if more than 8 retries, card has timed-out and return the received 0xFF
00255   retry = 0;
00256   r1    = 0xFF;
00257   while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00258   {
00259     retry++;
00260     if(retry > 10) break;
00261   }
00262   return r1;
00263 }

Here is the call graph for this function:

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

Warning:
Code:?? bytes (function code length)
Parameters:
None 
Returns:
bit Password protected -> OK NOT password protected -> KO (or card not initialized) /

Definition at line 653 of file mmc_sd.c.

References card_type, csd, KO, MMC_CARD, mmc_sd_get_status(), OK, and r2.

00654 {
00655   if (card_type == MMC_CARD)
00656   {
00657     if (((csd[0] >> 2) & 0x0F) < 2) // lock feature is not present on the card since the MMC is v1.x released !
00658       return KO;
00659   }
00660   if (KO == mmc_sd_get_status())    // get STATUS response
00661     return KO;
00662   if ((r2&0x0001) != 0)             // check "card is locked" flag in R2 response
00663     return OK;
00664 
00665   return KO;
00666 }

Here is the call graph for this function:

bit mmc_sd_lock_operation ( U8  operation,
U8  pwd_lg,
U8 pwd 
)

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

Definition at line 698 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.

00699 {
00700   bit status = OK;
00701   U8 retry;
00702 
00703   // check parameters validity
00704   if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0))  // password length must be > 0
00705     return KO;
00706 
00707   // wait card not busy
00708   if (mmc_sd_wait_not_busy() == KO)
00709     return KO;
00710 
00711   // set block length
00712   if (operation == OP_FORCED_ERASE)
00713     r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 1);   // CMD
00714   else
00715     r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, pwd_lg+2);   // CMD + PWDSLEN + PWD
00716   Spi_write_data(0xFF);            // write dummy byte
00717   Spi_write_data(0xFF);            // write dummy byte
00718   Spi_write_data(0xFF);            // write dummy byte
00719   if (r1 != 0x00)
00720     return KO;
00721 
00722   // send the lock command to the card
00723   Mmc_sd_select();                // select MMC_SD
00724 
00725   // issue command
00726   r1 = mmc_sd_command(MMC_LOCK_UNLOCK, 0);
00727 
00728   // check for valid response
00729   if(r1 != 0x00)
00730   {
00731     status = KO;
00732   }
00733   // send dummy
00734   Spi_write_data(0xFF);   // give clock again to end transaction
00735 
00736   // send data start token
00737   Spi_write_data(MMC_STARTBLOCK_WRITE);
00738   // write data
00739   Spi_write_data(operation);
00740   if (operation != OP_FORCED_ERASE)
00741   {
00742     Spi_write_data(pwd_lg);
00743     for(retry=0 ; retry<pwd_lg ; retry++)
00744     {
00745       Spi_write_data(*(pwd+retry));
00746     }
00747   }
00748   Spi_write_data(0xFF);    // send CRC (field required but value ignored)
00749   Spi_write_data(0xFF);
00750 
00751   // check data response token
00752   retry = 0;
00753   r1 = mmc_sd_send_and_read(0xFF);
00754   if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT)
00755     status = KO;
00756 
00757   Spi_write_data(0xFF);    // dummy byte
00758   Mmc_sd_unselect();
00759 
00760   // wait card not busy
00761   if (operation == OP_FORCED_ERASE)
00762     retry = 100;
00763   else
00764     retry = 10;
00765   while (mmc_sd_wait_not_busy() == KO)
00766   {
00767     retry--;
00768     if (retry == 0)
00769     {
00770       status = KO;
00771       break;
00772     }
00773   }
00774 
00775   // get and check status of the operation
00776   if (KO == mmc_sd_get_status())    // get STATUS response
00777     status = KO;
00778   if ((r2&0x0002) != 0)   // check "lock/unlock cmd failed" flag in R2 response
00779     status = KO;
00780 
00781   // set original block length
00782   r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00783   Spi_write_data(0xFF);            // write dummy byte
00784   if (r1 != 0x00)
00785     status = KO;
00786 
00787   return status;
00788 }

Here is the call graph for this function:

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)

Warning:
Code:?? bytes (function code length)
Parameters:
pos Sector address
Returns:
bit The open succeeded -> OK /

Definition at line 802 of file mmc_sd.c.

References gl_ptr_mem, and mmc_sd_wait_not_busy().

00803 {
00804   // Set the global memory ptr at a Byte address.
00805   gl_ptr_mem = pos << 9;        // gl_ptr_mem = pos * 512
00806 
00807   // wait for MMC not busy
00808   return mmc_sd_wait_not_busy();
00809 }

Here is the call graph for this function:

void mmc_sd_read_close ( void   ) 

This function unselects the current MMC_SD memory.

Warning:
Code:?? bytes (function code length)
Parameters:
None 
Returns:
None /

Definition at line 821 of file mmc_sd.c.

00822 {
00823 
00824 }

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.

Warning:
Code:?? bytes (function code length)
Parameters:
pos Sector address
Returns:
bit The open succeeded -> OK /

Definition at line 841 of file mmc_sd.c.

References gl_ptr_mem, and mmc_sd_wait_not_busy().

00842 {
00843   // Set the global memory ptr at a Byte address.
00844   gl_ptr_mem = pos << 9;                    // gl_ptr_mem = pos * 512
00845 
00846   // wait for MMC not busy
00847   return mmc_sd_wait_not_busy();
00848 }

Here is the call graph for this function:

void mmc_sd_write_close ( void   ) 

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

Warning:
Code:?? bytes (function code length)
Parameters:
gl_ptr_mem memory ptr on MMC_SD [global][IN/OUT]
Returns:
None /

Definition at line 861 of file mmc_sd.c.

00862 {
00863 
00864 }

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:

Warning:
code:?? bytes (function code length)
Parameters:
nb_sector number of contiguous sectors to write [IN]
gl_ptr_mem memory ptr on MMC_SD [global][IN/OUT]
Returns:
bit The write succeeded -> OK /

Definition at line 1059 of file mmc_sd.c.

References gl_ptr_mem, i, 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.

01060 {
01061 U8 i;
01062 
01063   do
01064   {
01065     // wait card not busy
01066     i=0;
01067     while (KO == mmc_sd_wait_not_busy())
01068     {
01069       i++;
01070       if (i == 10)
01071         return KO;
01072     }
01073 
01074     Mmc_sd_select();                  // select MMC_SD
01075     // issue command
01076     r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01077     // check for valid response
01078     if(r1 != 0x00)
01079     {
01080       Mmc_sd_unselect();                  // unselect MMC_SD
01081       return KO;
01082     }
01083     // send dummy
01084     Spi_write_data(0xFF);   // give clock again to end transaction
01085     // send data start token
01086     Spi_write_data(MMC_STARTBLOCK_WRITE);
01087       // write data
01088   //#
01089   //# Write 8x64b = 512b from the USB FIFO OUT.
01090   //#
01091     for (i = 8; i != 0; i--)
01092     {
01093       // Wait end of rx in USB EPOUT.
01094       while(!Is_usb_read_enabled())
01095       {
01096          if(!Is_usb_endpoint_enabled())
01097            return KO; // USB Reset
01098       }
01099 
01100       Disable_interrupt();    // Global disable.
01101 
01102       // SPI write principle: send a Byte then clear the SPIF flag.
01103       // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
01104       //                                  (.) send a Byte read from USB,
01105       //                                  (.) 1st step to clear the SPIF bit.
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_write_data(Usb_read_byte());
01168       Spi_write_data(Usb_read_byte());
01169       Spi_write_data(Usb_read_byte());
01170       Spi_ack_write();        // Final step to clear the SPIF bit.
01171 
01172       Usb_ack_receive_out();  // USB EPOUT read acknowledgement.
01173 
01174       Enable_interrupt();     // Global re-enable.
01175     } // for (i = 8; i != 0; i--)
01176 
01177     Spi_write_data(0xFF);    // send dummy CRC
01178     Spi_write_data(0xFF);
01179 
01180     // read data response token
01181     Spi_write_data(0xFF);
01182     r1 = Spi_read_data();
01183     if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01184     {
01185       Mmc_sd_unselect();                  // unselect MMC_SD
01186       return r1;
01187     }
01188 
01189     // send dummy byte
01190     Spi_write_data(0xFF);
01191 
01192     // release chip select
01193     Mmc_sd_unselect();                  // unselect MMC_SD
01194     gl_ptr_mem += 512;        // Update the memory pointer.
01195     nb_sector--;              // 1 more sector written
01196   }
01197   while (nb_sector != 0);
01198 
01199   // wait card not busy after last programming operation
01200   i=0;
01201   while (KO == mmc_sd_wait_not_busy())
01202   {
01203     i++;
01204     if (i == 10)
01205       return KO;
01206   }
01207 
01208   return OK;                  // Write done
01209 }

Here is the call graph for this function:

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:

Warning:
code:?? bytes (function code length)
Parameters:
nb_sector number of contiguous sectors to read [IN]
gl_ptr_mem memory ptr on MMC_SD [global][IN/OUT]
Returns:
bit The read succeeded -> OK /

Definition at line 894 of file mmc_sd.c.

References FALSE, gl_ptr_mem, i, 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.

00895 {
00896   Byte i;
00897   U16  read_time_out;
00898 
00899   do
00900   {
00901     Mmc_sd_select();                  // select MMC_SD
00902     // issue command
00903     r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00904     // check for valid response
00905     if(r1 != 0x00)
00906     {
00907        Mmc_sd_unselect();                  // unselect MMC_SD
00908        return KO;
00909     }
00910 
00911     // wait for token (may be a datablock start token OR a data error token !)
00912     read_time_out = 30000;
00913     while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00914     {
00915        read_time_out--;
00916        if (read_time_out == 0)   // TIME-OUT
00917        {
00918          Mmc_sd_unselect();               // unselect MMC_SD
00919          return KO;
00920        }
00921     }
00922 
00923     // check token
00924     if (r1 != MMC_STARTBLOCK_READ)
00925     {
00926       Spi_write_data(0xFF);
00927       Mmc_sd_unselect();                  // unselect MMC_SD
00928       return KO;
00929     }
00930 
00931     //#
00932     //# Read 8x64b = 512b, put them in the USB FIFO IN.
00933     //#
00934     for (i = 8; i != 0; i--)
00935     {
00936        Disable_interrupt();    // Global disable.
00937 
00938        // Principle: send any Byte to get a Byte.
00939        // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
00940        // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
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        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01003        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01004        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01005        Enable_interrupt();     // Global re-enable.
01006 
01007        //#
01008        //# Send the USB FIFO IN content to the USB Host.
01009        //#
01010        Usb_send_in();       // Send the FIFO IN content to the USB Host.
01011        // Wait until the tx is done so that we may write to the FIFO IN again.
01012        while(Is_usb_write_enabled()==FALSE)
01013        {
01014           if(!Is_usb_endpoint_enabled())
01015              return KO; // USB Reset
01016        }
01017     } // for (i = 8; i != 0; i--)
01018 
01019     gl_ptr_mem += 512;     // Update the memory pointer.
01020     nb_sector--;           // 1 more sector read
01021     // read 16-bit CRC
01022     Spi_write_data(0xFF);
01023     Spi_write_data(0xFF);
01024     // dummy bytes
01025     Spi_write_data(0xFF);
01026     Spi_write_data(0xFF);
01027     // release chip select
01028     Mmc_sd_unselect();                  // unselect MMC_SD
01029   }
01030   while (nb_sector != 0);
01031 
01032   return OK;   // Read done.
01033 }

Here is the call graph for this function:

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:

Definition at line 1363 of file mmc_sd.c.

References gl_ptr_mem, i, 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.

01364 {
01365   U16  i;
01366   U16  read_time_out;
01367 
01368   // wait for MMC not busy
01369   if (KO == mmc_sd_wait_not_busy())
01370     return KO;
01371 
01372   Mmc_sd_select();          // select MMC_SD
01373   // issue command
01374   r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
01375 
01376   // check for valid response
01377   if (r1 != 0x00)
01378   {
01379     Mmc_sd_unselect();     // unselect MMC_SD
01380     return KO;
01381   }
01382 
01383   // wait for token (may be a datablock start token OR a data error token !)
01384   read_time_out = 30000;
01385   while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
01386   {
01387      read_time_out--;
01388      if (read_time_out == 0)   // TIME-OUT
01389      {
01390        Mmc_sd_unselect();               // unselect MMC_SD
01391        return KO;
01392      }
01393   }
01394 
01395   // check token
01396   if (r1 != MMC_STARTBLOCK_READ)
01397   {
01398     Spi_write_data(0xFF);
01399     Mmc_sd_unselect();                  // unselect MMC_SD
01400     return KO;
01401   }
01402 
01403   // store datablock
01404   Disable_interrupt();    // Global disable.
01405   for(i=0;i<MMC_SECTOR_SIZE;i++)
01406   {
01407     Spi_write_data(0xFF);
01408     *ram=Spi_read_data();
01409     ram++;
01410   }
01411   Enable_interrupt();     // Global re-enable.
01412   gl_ptr_mem += 512;     // Update the memory pointer.
01413 
01414   // load 16-bit CRC (ignored)
01415   Spi_write_data(0xFF);
01416   Spi_write_data(0xFF);
01417 
01418   // continue delivering some clock cycles
01419   Spi_write_data(0xFF);
01420   Spi_write_data(0xFF);
01421 
01422   // release chip select
01423   Mmc_sd_unselect();                  // unselect MMC_SD
01424 
01425   return OK;   // Read done.
01426 }

Here is the call graph for this function:

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) :

Definition at line 1446 of file mmc_sd.c.

References gl_ptr_mem, i, 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.

01447 {
01448   U16 i;
01449 
01450   // wait for MMC not busy
01451   if (KO == mmc_sd_wait_not_busy())
01452     return KO;
01453 
01454   Mmc_sd_select();                  // select MMC_SD
01455   // issue command
01456   r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01457   // check for valid response
01458   if(r1 != 0x00)
01459   {
01460     Mmc_sd_unselect();
01461     return KO;
01462   }
01463   // send dummy
01464   Spi_write_data(0xFF);   // give clock again to end transaction
01465 
01466   // send data start token
01467   Spi_write_data(MMC_STARTBLOCK_WRITE);
01468   // write data
01469   for(i=0;i<MMC_SECTOR_SIZE;i++)
01470   {
01471     Spi_write_data(*ram);
01472     ram++;
01473   }
01474 
01475   Spi_write_data(0xFF);    // send CRC (field required but value ignored)
01476   Spi_write_data(0xFF);
01477 
01478   // read data response token
01479   r1 = mmc_sd_send_and_read(0xFF);
01480   if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01481   {
01482      Spi_write_data(0xFF);    // send dummy bytes
01483      Spi_write_data(0xFF);
01484      Mmc_sd_unselect();
01485      return KO;
01486 //     return r1;             // return ERROR byte
01487   }
01488 
01489   Spi_write_data(0xFF);    // send dummy bytes
01490   Spi_write_data(0xFF);
01491 
01492   // release chip select
01493   Mmc_sd_unselect();                  // unselect MMC_SD
01494   gl_ptr_mem += 512;        // Update the memory pointer.
01495 
01496   // wait card not busy after last programming operation
01497   i=0;
01498   while (KO == mmc_sd_wait_not_busy())
01499   {
01500     i++;
01501     if (i == 10)
01502       return KO;
01503   }
01504 
01505   return OK;                  // Write done
01506 }

Here is the call graph for this function:

bit mmc_sd_erase_sector_group ( U32  adr_start,
U32  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) :

Definition at line 1298 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.

01299 {
01300   U8 cmd;
01301 
01302   // wait for MMC not busy
01303   if (KO == mmc_sd_wait_not_busy())
01304     return KO;
01305 
01306   Mmc_sd_select();          // select MMC_SD
01307 
01308   // send address of 1st group
01309   if (card_type == MMC_CARD)
01310   { cmd = MMC_TAG_ERASE_GROUP_START; }
01311   else
01312   { cmd = SD_TAG_WR_ERASE_GROUP_START; }
01313   if ((r1 = mmc_sd_command(cmd,(adr_start << 9))) != 0)
01314   {
01315     Mmc_sd_unselect();
01316     return KO;
01317   }
01318   Spi_write_data(0xFF);
01319 
01320   // send address of last group
01321   if (card_type == MMC_CARD)
01322   { cmd = MMC_TAG_ERASE_GROUP_END; }
01323   else
01324   { cmd = SD_TAG_WR_ERASE_GROUP_END; }
01325   if ((r1 = mmc_sd_command(cmd,(adr_end << 9))) != 0)
01326   {
01327     Mmc_sd_unselect();
01328     return KO;
01329   }
01330   Spi_write_data(0xFF);
01331 
01332   // send erase command
01333   if ((r1 = mmc_sd_command(MMC_ERASE,0)) != 0)
01334   {
01335     Mmc_sd_unselect();
01336     return KO;
01337   }
01338   Spi_write_data(0xFF);
01339 
01340   Mmc_sd_unselect();
01341 
01342   return OK;
01343 }

Here is the call graph for this function:


Variable Documentation

U8 csd[16]

Definition at line 61 of file mmc_sd.c.

volatile U32 capacity

Definition at line 62 of file mmc_sd.c.

volatile U32 mmc_sd_last_block_address

Definition at line 63 of file mmc_sd.c.

U16 erase_group_size

Definition at line 64 of file mmc_sd.c.

U8 r1

Definition at line 58 of file mmc_sd.c.

U16 r2

Definition at line 59 of file mmc_sd.c.

U8 card_type

Definition at line 65 of file mmc_sd.c.

bit mmc_sd_init_done

Definition at line 57 of file mmc_sd.c.


Generated on Wed Sep 23 09:17:10 2009 for ATMEL by  doxygen 1.5.3