sd_mmc_spi.c File Reference


Detailed Description

SD/MMC card driver using SPI interface.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file sd_mmc_spi.c.

#include "conf_access.h"
#include "compiler.h"
#include "board.h"
#include "gpio.h"
#include "spi.h"
#include "conf_sd_mmc_spi.h"
#include "sd_mmc_spi.h"

Go to the source code of this file.

Defines

#define NO_SUPPORT_USB_PING_PONG

Functions

Bool is_sd_mmc_spi_write_pwd_locked (void)
 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.
Bool sd_mmc_spi_check_presence (void)
 This function check the presence of a memory card
  • if the card was already initialized (removal test), the host send a CRC_OFF command (CMD59) and check the answer
  • if the card was not already initialized (insertion test), the host send a CMD0 reset command and check the answer.

U8 sd_mmc_spi_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.
Bool sd_mmc_spi_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) :
  • adr_start=0x100 and adr_end=0x100, all the sectors from 0x100 up to 0x11F will be erased
  • adr_start=0x90 and adr_end=0x100, all the sectors from 0x80 up to 0x11F will be erased (0x90 interpreted as 0x80)
  • adr_start=0x80 and adr_end=0x146, all the sectors from 0x80 up to 0x15F will be erased This function just initiates a transmission, user may get status register to check that operation has succeeded After an erase, a MMC card contains bits at 0, and SD can contains bits 0 or 1 (according to field DATA_STAT_AFTER_ERASE in the CSD).

void sd_mmc_spi_get_capacity (void)
 This function extracts structure information from CSD array and compute the number of blocks of the memory card (stored in global U32 sd_mmc_spi_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.
Bool sd_mmc_spi_get_cid (U8 *buffer)
 This function reads the CID (Card Identification Data) of the memory card.
Bool sd_mmc_spi_get_csd (U8 *buffer)
 This function reads the CSD (Card Specific Data) of the memory card.
Bool sd_mmc_spi_get_status (void)
 This function reads the STATUS regsiter of the memory card After a read the error flags are automatically cleared.
Bool sd_mmc_spi_init (spi_options_t spiOptions, unsigned int pba_hz)
 This function initializes the SD/MMC controller & the SPI bus(over which the SD_MMC is controlled).
Bool sd_mmc_spi_internal_init (void)
 This function initializes the SD/MMC controller.
Bool sd_mmc_spi_lock_operation (U8 operation, U8 pwd_lg, U8 *pwd)
 This function manages locking operations for the SD/MMC card (password protection)
  • Once the card is locked, the only commands allowed are UNLOCK and FORCED_ERASE
  • Once the card is unlocked, the commands allowed are all the others
  • Before setting a new password (SET_PWD), the current one must be cleared (RESET_PWD)
  • If card contains a password (PWDSLEN != 0), the card will automatically be locked at start-up.

Bool sd_mmc_spi_mem_check (void)
 This function performs a memory check on the SD_MMC.
void sd_mmc_spi_read_close (void)
 This function unselects the current SD_MMC memory.
void sd_mmc_spi_read_close_PDCA (void)
 Stop PDCA transfer.
Bool sd_mmc_spi_read_multiple_sector (U16 nb_sector)
 This function allow to read multiple sectors.
Bool sd_mmc_spi_read_open (U32 pos)
 This function opens a SD_MMC memory in read mode at a given sector address (not byte address).
Bool sd_mmc_spi_read_open_PDCA (U32 pos)
 Reading using PDCA.
Bool sd_mmc_spi_read_sector_to_ram (void *ram)
 This function read one MMC sector and load it into a ram buffer.
U8 sd_mmc_spi_send_and_read (U8 data_to_send)
 This function sends a byte over SPI and returns the byte read from the slave.
U8 sd_mmc_spi_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.
Bool sd_mmc_spi_wait_not_busy (void)
 This function waits until the SD/MMC is not busy.
void sd_mmc_spi_write_close (void)
 This function fills the end of the logical sector (512B) and launch page programming.
Bool sd_mmc_spi_write_multiple_sector (U16 nb_sector)
 This function allow to write multiple sectors.
Bool sd_mmc_spi_write_open (U32 pos)
 This function opens a SD_MMC memory in write mode at a given sector address.
Bool sd_mmc_spi_write_sector_from_ram (const void *ram)
 This function writes one MMC sector from a ram buffer.

Variables

volatile U32 capacity
U8 card_type
U8 csd [16]
U8 data_mem [513]
U16 erase_group_size
static U32 gl_ptr_mem
U8 r1
U16 r2
Bool sd_mmc_spi_init_done = FALSE
volatile U32 sd_mmc_spi_last_block_address
static U8 sector_buf [MMC_SECTOR_SIZE]


Define Documentation

#define NO_SUPPORT_USB_PING_PONG

Definition at line 63 of file sd_mmc_spi.c.


Function Documentation

Bool is_sd_mmc_spi_write_pwd_locked ( void   ) 

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.

Protection functions (optionnal).

Returns:
bit Password protected -> OK NOT password protected -> KO (or card not initialized)

Definition at line 587 of file sd_mmc_spi.c.

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

00587 {
00588   if (card_type == MMC_CARD)
00589   {
00590     if (((csd[0] >> 2) & 0x0F) < 2) // lock feature is not present on the card since the MMC is v1.x released !
00591       return KO;
00592   }
00593   if (KO == sd_mmc_spi_get_status())    // get STATUS response
00594     return KO;
00595   if ((r2&0x0001) != 0)             // check "card is locked" flag in R2 response
00596     return OK;
00597 
00598   return KO;
00599 }
00600 

Bool sd_mmc_spi_check_presence ( void   ) 

This function check the presence of a memory card

  • if the card was already initialized (removal test), the host send a CRC_OFF command (CMD59) and check the answer
  • if the card was not already initialized (insertion test), the host send a CMD0 reset command and check the answer.

Returns:
bit The memory is present (OK) The memory does not respond (disconnected) (KO)

Definition at line 525 of file sd_mmc_spi.c.

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

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

00525 {
00526   U16 retry;
00527 
00528   retry = 0;
00529   if (sd_mmc_spi_init_done == FALSE)
00530   {
00531     // If memory is not initialized, try to initialize it (CMD0)
00532     // If no valid response, there is no card
00533     while ((r1 = sd_mmc_spi_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01)
00534     {
00535   spi_write(SD_MMC_SPI,0xFF);            // write dummy byte
00536       retry++;
00537       if (retry > 10)
00538         return KO;
00539     }
00540     return OK;
00541   }
00542   else
00543   {
00544     if ((r1 = sd_mmc_spi_send_command(MMC_CRC_ON_OFF,0)) == 0x00)
00545       return OK;
00546     sd_mmc_spi_init_done = FALSE;
00547     return KO;
00548   }
00549 }
00550 

U8 sd_mmc_spi_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.

Parameters:
command command to send (see sd_mmc_spi.h for command list)
arg argument of the command
Returns:
U8 R1 response (R1 == 0xFF time out error)

Definition at line 230 of file sd_mmc_spi.c.

References r1, and sd_mmc_spi_send_and_read().

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

00231 {
00232 U8 retry;
00233 
00234   spi_write(SD_MMC_SPI,0xFF);            // write dummy byte
00235   spi_write(SD_MMC_SPI,command | 0x40);  // send command
00236   spi_write(SD_MMC_SPI,arg>>24);         // send parameter
00237   spi_write(SD_MMC_SPI,arg>>16);
00238   spi_write(SD_MMC_SPI,arg>>8 );
00239   spi_write(SD_MMC_SPI,arg    );
00240   spi_write(SD_MMC_SPI,0x95);            // correct CRC for first command in SPI (CMD0)
00241                                   // after, the CRC is ignored
00242   // end command
00243   // wait for response
00244   // if more than 8 retries, card has timed-out and return the received 0xFF
00245   retry = 0;
00246   r1    = 0xFF;
00247   while((r1 = sd_mmc_spi_send_and_read(0xFF)) == 0xFF)
00248   {
00249     retry++;
00250     if(retry > 10) break;
00251   }
00252   return r1;
00253 }
00254 

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

  • adr_start=0x100 and adr_end=0x100, all the sectors from 0x100 up to 0x11F will be erased
  • adr_start=0x90 and adr_end=0x100, all the sectors from 0x80 up to 0x11F will be erased (0x90 interpreted as 0x80)
  • adr_start=0x80 and adr_end=0x146, all the sectors from 0x80 up to 0x15F will be erased This function just initiates a transmission, user may get status register to check that operation has succeeded After an erase, a MMC card contains bits at 0, and SD can contains bits 0 or 1 (according to field DATA_STAT_AFTER_ERASE in the CSD).

Parameters:
adr_start address of 1st group (sector address, not byte address)
adr_end address of last group (sector address, not byte address)
Returns:
bit The erase operation succeeded (has been started) -> OK The erase operation failed (not started) -> KO

Definition at line 832 of file sd_mmc_spi.c.

References card_type, MMC_CARD, MMC_ERASE, MMC_TAG_ERASE_GROUP_END, MMC_TAG_ERASE_GROUP_START, r1, sd_mmc_spi_command(), sd_mmc_spi_wait_not_busy(), SD_TAG_WR_ERASE_GROUP_END, and SD_TAG_WR_ERASE_GROUP_START.

00832 {
00833   U8 cmd;
00834 
00835   // wait for MMC not busy
00836   if (KO == sd_mmc_spi_wait_not_busy())
00837     return KO;
00838 
00839   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00840 
00841   // send address of 1st group
00842   if (card_type == MMC_CARD)
00843   { cmd = MMC_TAG_ERASE_GROUP_START; }
00844   else
00845   { cmd = SD_TAG_WR_ERASE_GROUP_START; }
00846   if ((r1 = sd_mmc_spi_command(cmd,(adr_start << 9))) != 0)
00847   {
00848     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00849     return KO;
00850   }
00851     spi_write(SD_MMC_SPI,0xFF);
00852 
00853   // send address of last group
00854   if (card_type == MMC_CARD)
00855   { cmd = MMC_TAG_ERASE_GROUP_END; }
00856   else
00857   { cmd = SD_TAG_WR_ERASE_GROUP_END; }
00858   if ((r1 = sd_mmc_spi_command(cmd,(adr_end << 9))) != 0)
00859   {
00860     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00861     return KO;
00862   }
00863   spi_write(SD_MMC_SPI,0xFF);
00864 
00865   // send erase command
00866   if ((r1 = sd_mmc_spi_command(MMC_ERASE,0)) != 0)
00867   {
00868     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00869     return KO;
00870   }
00871   spi_write(SD_MMC_SPI,0xFF);
00872 
00873   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00874 
00875   return OK;
00876 }
00877 

void sd_mmc_spi_get_capacity ( void   ) 

This function extracts structure information from CSD array and compute the number of blocks of the memory card (stored in global U32 sd_mmc_spi_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.

Returns:
bit OK

Definition at line 405 of file sd_mmc_spi.c.

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

Referenced by main(), and sd_mmc_spi_internal_init().

00405 {
00406   U16 c_size;
00407   U8  c_size_mult;
00408   U8  read_bl_len;
00409   U8  erase_grp_size;
00410   U8  erase_grp_mult;
00411 
00412   // extract variables from CSD array
00413   c_size      = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6);
00414   c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7);
00415   read_bl_len = csd[5] & 0x0F;
00416   if (card_type == MMC_CARD)
00417   {
00418     erase_grp_size = ((csd[10] & 0x7C) >> 2);
00419     erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5);
00420   }
00421   else
00422   {
00423     erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7);
00424     erase_grp_mult = 0;
00425   }
00426 
00427   // compute last block addr
00428   sd_mmc_spi_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1;
00429   if (read_bl_len > 9)  // 9 means 2^9 = 512b
00430     sd_mmc_spi_last_block_address <<= (read_bl_len - 9);
00431 
00432   // compute card capacity in bytes
00433   capacity = (1 << read_bl_len) * (sd_mmc_spi_last_block_address + 1);
00434 
00435   // compute block group size for erase operation
00436   erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1);
00437 }
00438 

Bool sd_mmc_spi_get_cid ( U8 *  buffer  ) 

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

Parameters:
buffer to fill
Returns:
bit OK / KO

Definition at line 334 of file sd_mmc_spi.c.

References MMC_SEND_CID, MMC_STARTBLOCK_READ, r1, r2, sd_mmc_spi_command(), sd_mmc_spi_init_done, sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_internal_init().

00334 {
00335 U8 retry;
00336 unsigned short data_read;
00337   // wait for MMC not busy
00338   if (KO == sd_mmc_spi_wait_not_busy())
00339     return KO;
00340 
00341   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // select SD_MMC_SPI
00342   // issue command
00343   r1 = sd_mmc_spi_command(MMC_SEND_CID, 0);
00344   // check for valid response
00345   if(r1 != 0x00)
00346   {
00347     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00348     sd_mmc_spi_init_done = FALSE;
00349     return KO;
00350   }
00351   // wait for data block start
00352   retry = 0;
00353   while((r2 = sd_mmc_spi_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00354   {
00355     if (retry > 8)
00356     {
00357       spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00358       return KO;
00359     }
00360     retry++;
00361   }
00362   // store valid data block
00363   for (retry = 0; retry <16; retry++)
00364   {
00365    spi_write(SD_MMC_SPI,0xFF);
00366    spi_read(SD_MMC_SPI,&data_read);
00367    buffer[retry] = data_read;
00368   }
00369   spi_write(SD_MMC_SPI,0xFF);   // load CRC (not used)
00370   spi_write(SD_MMC_SPI,0xFF);
00371   spi_write(SD_MMC_SPI,0xFF);   // give clock again to end transaction
00372   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00373   return OK;
00374 }
00375 

Bool sd_mmc_spi_get_csd ( U8 *  buffer  ) 

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

Parameters:
buffer to fill
Returns:
bit OK / KO

Definition at line 284 of file sd_mmc_spi.c.

References MMC_SEND_CSD, MMC_STARTBLOCK_READ, r1, sd_mmc_spi_command(), sd_mmc_spi_init_done, sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_internal_init().

00284 {
00285 U8 retry;
00286 unsigned short data_read;
00287   // wait for MMC not busy
00288   if (KO == sd_mmc_spi_wait_not_busy())
00289     return KO;
00290 
00291   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00292   // issue command
00293   r1 = sd_mmc_spi_command(MMC_SEND_CSD, 0);
00294   // check for valid response
00295   if(r1 != 0x00)
00296   {
00297     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00298     sd_mmc_spi_init_done = FALSE;
00299     return KO;
00300   }
00301   // wait for block start
00302   retry = 0;
00303   while((r1 = sd_mmc_spi_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00304   {
00305     if (retry > 8)
00306     {
00307       spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00308       return KO;
00309     }
00310     retry++;
00311   }
00312   for (retry = 0; retry <16; retry++)
00313   {
00314    spi_write(SD_MMC_SPI,0xFF);
00315    spi_read(SD_MMC_SPI,&data_read);
00316     buffer[retry] = data_read;
00317   }
00318    spi_write(SD_MMC_SPI,0xFF);   // load CRC (not used)
00319    spi_write(SD_MMC_SPI,0xFF);
00320    spi_write(SD_MMC_SPI,0xFF);   // give clock again to end transaction
00321   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00322   return OK;
00323 }
00324 

Bool sd_mmc_spi_get_status ( void   ) 

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

Returns:
bit The open succeeded -> OK

Definition at line 448 of file sd_mmc_spi.c.

References MMC_SEND_STATUS, r2, sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by is_sd_mmc_spi_write_pwd_locked(), and sd_mmc_spi_lock_operation().

00448 {
00449   U8 retry, spireg;
00450 
00451   // wait for MMC not busy
00452   if (KO == sd_mmc_spi_wait_not_busy())
00453     return KO;
00454 
00455   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00456 
00457   // send command
00458   spi_write(SD_MMC_SPI,MMC_SEND_STATUS | 0x40);  // send command
00459   spi_write(SD_MMC_SPI,0);                       // send parameter
00460   spi_write(SD_MMC_SPI,0);
00461   spi_write(SD_MMC_SPI,0);
00462   spi_write(SD_MMC_SPI,0);
00463   spi_write(SD_MMC_SPI,0x95);            // correct CRC for first command in SPI (CMD0)
00464                                   // after, the CRC is ignored
00465   // end command
00466   // wait for response
00467   // if more than 8 retries, card has timed-out and return the received 0xFF
00468   retry = 0;
00469   r2 = 0xFFFF;
00470   spireg = 0xFF;
00471   while((spireg = sd_mmc_spi_send_and_read(0xFF)) == 0xFF)
00472   {
00473     retry++;
00474     if(retry > 10)
00475     {
00476       spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00477       return KO;
00478     }
00479   }
00480   r2 = ((U16)(spireg) << 8) + sd_mmc_spi_send_and_read(0xFF);    // first byte is MSb
00481 
00482   spi_write(SD_MMC_SPI,0xFF);   // give clock again to end transaction
00483   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00484 
00485   return OK;
00486 }
00487 

Bool sd_mmc_spi_init ( spi_options_t  spiOptions,
unsigned int  pba_hz 
)

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

Returns:
bit The memory is ready -> OK (always)

Definition at line 195 of file sd_mmc_spi.c.

References sd_mmc_spi_internal_init().

Referenced by sd_mmc_resources_init().

00196 {
00197   // Setup SPI registers according to spiOptions.
00198   spi_setupChipReg(SD_MMC_SPI, &spiOptions, pba_hz);
00199 
00200   // Initialize the SD/MMC controller.
00201   return sd_mmc_spi_internal_init();
00202 }

Bool sd_mmc_spi_internal_init ( void   ) 

This function initializes the SD/MMC controller.

Low-level functions (basic management).

Returns:
bit The memory is ready -> OK (always)

Definition at line 96 of file sd_mmc_spi.c.

References card_type, csd, MMC_CARD, MMC_CRC_ON_OFF, MMC_GO_IDLE_STATE, MMC_SEND_OP_COND, MMC_SET_BLOCKLEN, r1, SD_APP_CMD55, SD_CARD, sd_mmc_spi_get_capacity(), sd_mmc_spi_get_cid(), sd_mmc_spi_get_csd(), sd_mmc_spi_init_done, sd_mmc_spi_send_command(), and SD_SEND_OP_COND_ACMD.

Referenced by sd_mmc_spi_init(), sd_mmc_spi_mem_check(), and sd_mmc_spi_mem_init().

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

Bool sd_mmc_spi_lock_operation ( U8  operation,
U8  pwd_lg,
U8 *  pwd 
)

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

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

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

Parameters:
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)
pwd_lg Password length
pwd Pointer on the password (char array) to send
Returns:
bit Operation succeeded -> OK Operation failed -> KO

Definition at line 629 of file sd_mmc_spi.c.

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

00629 {
00630   Bool status = OK;
00631   U8 retry;
00632 
00633   // check parameters validity
00634   if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0))  // password length must be > 0
00635     return KO;
00636 
00637   // wait card not busy
00638   if (sd_mmc_spi_wait_not_busy() == KO)
00639     return KO;
00640 
00641   // set block length
00642   if (operation == OP_FORCED_ERASE)
00643     r1 = sd_mmc_spi_send_command(MMC_SET_BLOCKLEN, 1);   // CMD
00644   else
00645     r1 = sd_mmc_spi_send_command(MMC_SET_BLOCKLEN, pwd_lg+2);   // CMD + PWDSLEN + PWD
00646   spi_write(SD_MMC_SPI,0xFF);            // write dummy byte
00647   spi_write(SD_MMC_SPI,0xFF);            // write dummy byte
00648   spi_write(SD_MMC_SPI,0xFF);            // write dummy byte
00649   if (r1 != 0x00)
00650     return KO;
00651 
00652   // send the lock command to the card
00653   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00654 
00655   // issue command
00656   r1 = sd_mmc_spi_command(MMC_LOCK_UNLOCK, 0);
00657 
00658   // check for valid response
00659   if(r1 != 0x00)
00660   {
00661     status = KO;
00662   }
00663   // send dummy
00664   spi_write(SD_MMC_SPI,0xFF);   // give clock again to end transaction
00665 
00666   // send data start token
00667   spi_write(SD_MMC_SPI,MMC_STARTBLOCK_WRITE);
00668   // write data
00669   spi_write(SD_MMC_SPI,operation);
00670   if (operation != OP_FORCED_ERASE)
00671   {
00672   spi_write(SD_MMC_SPI,pwd_lg);
00673     for(retry=0 ; retry<pwd_lg ; retry++)
00674     {
00675   spi_write(SD_MMC_SPI,*(pwd+retry));
00676     }
00677   }
00678   spi_write(SD_MMC_SPI,0xFF);    // send CRC (field required but value ignored)
00679   spi_write(SD_MMC_SPI,0xFF);
00680 
00681   // check data response token
00682   retry = 0;
00683   r1 = sd_mmc_spi_send_and_read(0xFF);
00684   if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT)
00685     status = KO;
00686 
00687   spi_write(SD_MMC_SPI,0xFF);    // dummy byte
00688   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00689 
00690   // wait card not busy
00691   if (operation == OP_FORCED_ERASE)
00692     retry = 100;
00693   else
00694     retry = 10;
00695   while (sd_mmc_spi_wait_not_busy() == KO)
00696   {
00697     retry--;
00698     if (retry == 0)
00699     {
00700       status = KO;
00701       break;
00702     }
00703   }
00704 
00705   // get and check status of the operation
00706   if (KO == sd_mmc_spi_get_status())    // get STATUS response
00707     status = KO;
00708   if ((r2&0x0002) != 0)   // check "lock/unlock cmd failed" flag in R2 response
00709     status = KO;
00710 
00711   // set original block length
00712   r1 = sd_mmc_spi_send_command(MMC_SET_BLOCKLEN, 512);
00713   if (r1 != 0x00)
00714     status = KO;
00715 
00716   return status;
00717 }
00718 

Bool sd_mmc_spi_mem_check ( void   ) 

This function performs a memory check on the SD_MMC.

Returns:
bit The memory is ready -> OK The memory check failed -> KO

Definition at line 560 of file sd_mmc_spi.c.

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

Referenced by main(), and sd_mmc_spi_test_unit_ready().

00560 {
00561   if (sd_mmc_spi_check_presence() == OK)
00562   {
00563     if (sd_mmc_spi_init_done == FALSE)
00564     {
00565       sd_mmc_spi_internal_init();
00566     }
00567     if (sd_mmc_spi_init_done == TRUE)
00568       return OK;
00569     else
00570       return KO;
00571   }
00572   return KO;
00573 }
00574 

void sd_mmc_spi_read_close ( void   ) 

This function unselects the current SD_MMC memory.

Definition at line 742 of file sd_mmc_spi.c.

Referenced by sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_usb_read_10().

00742 {
00743 
00744 }
00745 

void sd_mmc_spi_read_close_PDCA ( void   ) 

Stop PDCA transfer.

This function closes a PDCA read transfer page programming.

Definition at line 884 of file sd_mmc_spi.c.

Referenced by pdca_int_handler().

00884 {
00885 
00886   // load 16-bit CRC (ignored)
00887   spi_write(SD_MMC_SPI,0xFF);
00888   spi_write(SD_MMC_SPI,0xFF);
00889 
00890   // continue delivering some clock cycles
00891   spi_write(SD_MMC_SPI,0xFF);
00892   spi_write(SD_MMC_SPI,0xFF);
00893 
00894   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00895   //print_dbg("SD_MMC_SPI is unselected\n");
00896 
00897  /* if(!sd_mmc_spi_wait_not_busy())
00898   print_dbg("SD_MMC_SPI is busy\n");*/
00899 //  Trace("\nSD_MMC_SPI is unselected");
00900 }
00901 

Bool sd_mmc_spi_read_multiple_sector ( U16  nb_sector  ) 

This function allow to read multiple sectors.

Parameters:
nb_sector the number of sector to read
Returns:
bit The read succeeded -> OK

Definition at line 784 of file sd_mmc_spi.c.

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

Referenced by sd_mmc_spi_usb_read_10().

00784 {
00785   while (nb_sector--)
00786   {
00787     // Read the next sector
00788     sd_mmc_spi_read_sector_to_ram(sector_buf);
00789     sd_mmc_spi_read_multiple_sector_callback(sector_buf);
00790   }
00791 
00792   return OK;
00793 }
00794 

Bool sd_mmc_spi_read_open ( U32  pos  ) 

This function opens a SD_MMC memory in read mode at a given sector address (not byte address).

Functions for preparing block read/write.

Parameters:
pos Sector address
Returns:
bit The open succeeded -> OK

Definition at line 729 of file sd_mmc_spi.c.

References gl_ptr_mem, and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_usb_read_10().

00729 {
00730   // Set the global memory ptr at a Byte address.
00731   gl_ptr_mem = pos << 9;        // gl_ptr_mem = pos * 512
00732 
00733   // wait for MMC not busy
00734   return sd_mmc_spi_wait_not_busy();
00735 }
00736 

Bool sd_mmc_spi_read_open_PDCA ( U32  pos  ) 

Reading using PDCA.

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

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

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

Parameters:
pos Sector address
Returns:
bit The open succeeded -> OK /

Definition at line 917 of file sd_mmc_spi.c.

References gl_ptr_mem, MMC_READ_SINGLE_BLOCK, MMC_STARTBLOCK_READ, r1, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by main().

00917 {
00918   U16 read_time_out;
00919 
00920   // Set the global memory ptr at a Byte address.
00921   gl_ptr_mem = pos << 9;                    // gl_ptr_mem = pos * 512
00922 
00923   // wait for MMC not busy
00924   if (KO == sd_mmc_spi_wait_not_busy())
00925     return KO;
00926 
00927 
00928   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);          // select SD_MMC_SPI
00929   // issue command
00930   r1 = sd_mmc_spi_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00931 
00932   // check for valid response
00933   if (r1 != 0x00)
00934   {
00935     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00936    // print_dbg("\n****** could not open SD_MMC_SPI for single block read\n");
00937    // Trace("\n****** could not open SD_MMC_SPI for single block read\n");
00938     return KO;
00939   }
00940 
00941   // wait for token (may be a datablock start token OR a data error token !)
00942   read_time_out = 30000;
00943   while((r1 = sd_mmc_spi_send_and_read(0xFF)) == 0xFF)
00944   {
00945      read_time_out--;
00946      if (read_time_out == 0)   // TIME-OUT
00947      {
00948        spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS); // unselect SD_MMC_SPI
00949    //    print_dbg("**** datablock start error or token error \n");
00950    // Trace("**** data error token SD_MMC_SPI\n");
00951        return KO;
00952      }
00953   }
00954 
00955   // check token
00956   if (r1 != MMC_STARTBLOCK_READ)
00957   {
00958     spi_write(SD_MMC_SPI,0xFF);
00959     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00960     //print_dbg("******bad SD_MMC_SPI token \n");
00961    //  Trace("******token SD_MMC_SPI\n");
00962     return KO;
00963   }
00964  // print_dbg("********* SD_MMC_SPI opened successfuly\n");
00965  // Trace("********* SD_MMC_SPI opened successfuly\n");
00966   return OK;   // Read done.
00967 }
00968 

Bool sd_mmc_spi_read_sector_to_ram ( void *  ram  ) 

This function read one MMC sector and load it into a ram buffer.

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

DATA FLOW is: SD/MMC => RAM

NOTE:

Parameters:
ram pointer to ram buffer
Returns:
bit The read succeeded -> OK The read failed (bad address, etc.) -> KO /

Definition at line 985 of file sd_mmc_spi.c.

References gl_ptr_mem, MMC_READ_SINGLE_BLOCK, MMC_SECTOR_SIZE, MMC_STARTBLOCK_READ, r1, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_mem_2_ram(), and sd_mmc_spi_read_multiple_sector().

00985 {
00986   U8 *_ram = ram;
00987   U16  i;
00988   U16  read_time_out;
00989   unsigned short data_read;
00990   // wait for MMC not busy
00991   if (KO == sd_mmc_spi_wait_not_busy())
00992     return KO;
00993 
00994   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00995   // issue command
00996   r1 = sd_mmc_spi_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00997 
00998   // check for valid response
00999   if (r1 != 0x00)
01000   {
01001     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
01002     return KO;
01003   }
01004 
01005   // wait for token (may be a datablock start token OR a data error token !)
01006   read_time_out = 30000;
01007   while((r1 = sd_mmc_spi_send_and_read(0xFF)) == 0xFF)
01008   {
01009      read_time_out--;
01010      if (read_time_out == 0)   // TIME-OUT
01011      {
01012        spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS); // unselect SD_MMC_SPI
01013        return KO;
01014      }
01015   }
01016 
01017   // check token
01018   if (r1 != MMC_STARTBLOCK_READ)
01019   {
01020     spi_write(SD_MMC_SPI,0xFF);
01021     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
01022     return KO;
01023   }
01024 
01025   // store datablock
01026   for(i=0;i<MMC_SECTOR_SIZE;i++)
01027   {
01028     spi_write(SD_MMC_SPI,0xFF);
01029     spi_read(SD_MMC_SPI,&data_read);
01030     *_ram++=data_read;
01031   }
01032   gl_ptr_mem += 512;     // Update the memory pointer.
01033 
01034   // load 16-bit CRC (ignored)
01035   spi_write(SD_MMC_SPI,0xFF);
01036   spi_write(SD_MMC_SPI,0xFF);
01037 
01038   // continue delivering some clock cycles
01039   spi_write(SD_MMC_SPI,0xFF);
01040   spi_write(SD_MMC_SPI,0xFF);
01041 
01042   // release chip select
01043   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
01044 
01045   return OK;   // Read done.
01046 }
01047 

U8 sd_mmc_spi_send_and_read ( U8  data_to_send  ) 

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

Parameters:
data_to_send byte to send over SPI
Returns:
U8 Byte read from the slave

Definition at line 266 of file sd_mmc_spi.c.

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

00266 {
00267    unsigned short data_read;
00268    spi_write(SD_MMC_SPI,data_to_send);
00269    if( SPI_ERROR_TIMEOUT == spi_read(SD_MMC_SPI,&data_read) )
00270      return 0xFF;
00271    return (data_read);
00272 }
00273 

U8 sd_mmc_spi_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.

Parameters:
command command to send (see sd_mmc_spi.h for command list)
arg argument of the command
Returns:
U8 R1 response (R1 == 0xFF if time out error)

Definition at line 213 of file sd_mmc_spi.c.

References r1, and sd_mmc_spi_command().

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

00214 {
00215   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
00216   r1 = sd_mmc_spi_command(command, arg);
00217   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
00218   return r1;
00219 }

Bool sd_mmc_spi_wait_not_busy ( void   ) 

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

Returns:
bit OK when card is not busy

Definition at line 495 of file sd_mmc_spi.c.

References r1, and sd_mmc_spi_send_and_read().

Referenced by sd_mmc_spi_erase_sector_group(), sd_mmc_spi_get_cid(), sd_mmc_spi_get_csd(), sd_mmc_spi_get_status(), sd_mmc_spi_lock_operation(), sd_mmc_spi_read_open(), sd_mmc_spi_read_open_PDCA(), sd_mmc_spi_read_sector_to_ram(), sd_mmc_spi_write_open(), and sd_mmc_spi_write_sector_from_ram().

00495 {
00496   U16 retry;
00497 
00498   // Select the SD_MMC memory gl_ptr_mem points to
00499   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00500   retry = 0;
00501   while((r1 = sd_mmc_spi_send_and_read(0xFF)) != 0xFF)
00502   {
00503     retry++;
00504     if (retry == 50000)
00505     {
00506       spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00507       return KO;
00508     }
00509   }
00510   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
00511   return OK;
00512 }
00513 

void sd_mmc_spi_write_close ( void   ) 

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

Definition at line 773 of file sd_mmc_spi.c.

Referenced by sd_mmc_spi_mem_2_ram(), sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_usb_write_10().

00773 {
00774 
00775 }
00776 

Bool sd_mmc_spi_write_multiple_sector ( U16  nb_sector  ) 

This function allow to write multiple sectors.

Parameters:
nb_sector the number of sector to write
Returns:
bit The write succeeded -> OK

Definition at line 802 of file sd_mmc_spi.c.

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

Referenced by sd_mmc_spi_usb_write_10().

00802 {
00803   while (nb_sector--)
00804   {
00805     // Write the next sector
00806     sd_mmc_spi_write_multiple_sector_callback(sector_buf);
00807     sd_mmc_spi_write_sector_from_ram(sector_buf);
00808   }
00809 
00810   return OK;
00811 }
00812 

Bool sd_mmc_spi_write_open ( U32  pos  ) 

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

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

Parameters:
pos Sector address
Returns:
bit The open succeeded -> OK

Definition at line 759 of file sd_mmc_spi.c.

References gl_ptr_mem, and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_usb_write_10().

00759 {
00760   // Set the global memory ptr at a Byte address.
00761   gl_ptr_mem = pos << 9;                    // gl_ptr_mem = pos * 512
00762 
00763   // wait for MMC not busy
00764   return sd_mmc_spi_wait_not_busy();
00765 }
00766 

Bool sd_mmc_spi_write_sector_from_ram ( const void *  ram  ) 

This function writes one MMC sector from a ram buffer.

DATA FLOW is: RAM => SD/MMC

NOTE (please read) :

  • First call (if sequential write) must be preceded by a call to the sd_mmc_spi_write_open() function
  • An address error will not detected here, but with the call of sd_mmc_spi_get_status() function
  • The program exits the functions with the memory card busy !

Parameters:
ram pointer to ram buffer
Returns:
bit The write succeeded -> OK The write failed -> KO

Definition at line 1066 of file sd_mmc_spi.c.

References gl_ptr_mem, MMC_DR_ACCEPT, MMC_DR_MASK, MMC_SECTOR_SIZE, MMC_STARTBLOCK_WRITE, MMC_WRITE_BLOCK, r1, sd_mmc_spi_command(), sd_mmc_spi_send_and_read(), and sd_mmc_spi_wait_not_busy().

Referenced by sd_mmc_spi_ram_2_mem(), and sd_mmc_spi_write_multiple_sector().

01066 {
01067   const U8 *_ram = ram;
01068   U16 i;
01069 
01070   // wait for MMC not busy
01071   if (KO == sd_mmc_spi_wait_not_busy())
01072     return KO;
01073 
01074   spi_selectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);    // select SD_MMC_SPI
01075   // issue command
01076   r1 = sd_mmc_spi_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01077   // check for valid response
01078   if(r1 != 0x00)
01079   {
01080     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
01081     return KO;
01082   }
01083   // send dummy
01084   spi_write(SD_MMC_SPI,0xFF);   // give clock again to end transaction
01085 
01086   // send data start token
01087   spi_write(SD_MMC_SPI,MMC_STARTBLOCK_WRITE);
01088   // write data
01089   for(i=0;i<MMC_SECTOR_SIZE;i++)
01090   {
01091     spi_write(SD_MMC_SPI,*_ram++);
01092   }
01093 
01094   spi_write(SD_MMC_SPI,0xFF);    // send CRC (field required but value ignored)
01095   spi_write(SD_MMC_SPI,0xFF);
01096 
01097   // read data response token
01098   r1 = sd_mmc_spi_send_and_read(0xFF);
01099   if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01100   {
01101     spi_write(SD_MMC_SPI,0xFF);    // send dummy bytes
01102     spi_write(SD_MMC_SPI,0xFF);
01103     spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);
01104      return KO;
01105 //     return r1;             // return ERROR byte
01106   }
01107 
01108   spi_write(SD_MMC_SPI,0xFF);    // send dummy bytes
01109   spi_write(SD_MMC_SPI,0xFF);
01110 
01111   // release chip select
01112   spi_unselectChip(SD_MMC_SPI, SD_MMC_SPI_NPCS);  // unselect SD_MMC_SPI
01113   gl_ptr_mem += 512;        // Update the memory pointer.
01114 
01115   // wait card not busy after last programming operation
01116   i=0;
01117   while (KO == sd_mmc_spi_wait_not_busy())
01118   {
01119     i++;
01120     if (i == 10)
01121       return KO;
01122   }
01123 
01124   return OK;                  // Write done
01125 }
01126 


Variable Documentation

volatile U32 capacity

Definition at line 76 of file sd_mmc_spi.c.

Referenced by main(), and sd_mmc_spi_get_capacity().

U8 csd[16]

U8 data_mem[513]

Definition at line 82 of file sd_mmc_spi.c.

Definition at line 78 of file sd_mmc_spi.c.

Referenced by sd_mmc_spi_get_capacity().

U32 gl_ptr_mem [static]

U8 r1

U16 r2

Bool sd_mmc_spi_init_done = FALSE

Definition at line 77 of file sd_mmc_spi.c.

Referenced by sd_mmc_spi_get_capacity(), and sd_mmc_spi_read_capacity().

U8 sector_buf[MMC_SECTOR_SIZE] [static]


Generated on Fri Feb 19 02:24:00 2010 for AVR32 UC3 - SD/MMC Driver by  doxygen 1.5.5