Extract a mounted SD card

Hello folks!, I am working on a rcm4300 and have a question for you:

There is not a way to get back control on a mountyed SD card when you pull out it and insert it again? When this hapen, FAT file system show lots of errors, and if i try to unmount and mount again it claims that the SD is not formated or show an I/O error.
The only way I found to recover the SD FAT control is reboot tue board.

Regards, Alfredo.

Alfredo,

Have you found a solution or the reasoning behind this problem?

Clayton

In the http://www.rabbit.com/documentation/docs/modules/FileSystem/ModFAT40.htm, in Appendix A, Title A.8 it explains how to make a Hot-Swapping. It say that there is an example with the name FAT_HOT_SWAP_SD.C, but I don�t find it. Could you find it? The example explain that we need use the functions: fat_UnmountDevice(), �…, sdspi_debounce() and fat_AutoMount().

Hola!,
Los programas de demostrcion los tienes en C:\DCRABBIT_10.21\Samples\FileSystem\FAT , y el fat_Mount y fat_Unmounty funcionan bien. El problema que encuentro, es cuando extraes una tarjeta sin desmontarla: despu�s de esto, no consigo recuperar el control del sistema de FAT de ninguna manera -salvo reiniciando-. Ya s� que no se debe extraer la tarjeta sin desmontar, pero creo que esa posibilidad deber�a estar contemplada.

Un saludo!

Ya he encontrado el ejemplo mencionado en la nota de aplicaci�n. Estaba oculto en el zip DCRABBIT_10.21\Samples_FAT.zip. En el se muestra c�mo utilizar las primitivas del driver de la SD: sdspi_initDevice, sdspi_debounce. Con sdspi_debounce detectas la extracci�n de la SD y con alguna otra del estilo sdspi_resetDevice? puede que se consiga algo.

Adjunto c�digo del ejemplo:

#memmap xmem

#define GREEN “\x1b[32m” // Foreground colors for printf
#define RED “\x1b[31m”
#define BLUE “\x1b[34m”
#define BLACK “\x1b[30m”

#define FILESIZE 256
#define NFILES 30
#define NTESTS 20
#define NRETRY 50

//**** Define these macros for library debugging info to display
//*** in the STDIO window
//#define FAT_VERBOSE
//#define FAT_DEBUG
//#define FAT_HOTSWAP_VERBOSE

#define FAT_BLOCK // Set FAT library to blocking mode

#use “fat.lib”

#ifndef FAT_ALLOW_HOTSWAP
#error “Board type does not support hot swapping.”
#endif

///////////////////////////////////////////////////////////
/// provides milli-second delay
///////////////////////////////////////////////////////////
void msDelay(unsigned int delay)
{
auto unsigned long done_time;

done_time = MS_TIMER + delay;

while( (long) (MS_TIMER - done_time) < 0 );
}

int main()
{
FATfile my_file;
static char fbuff[FILESIZE];
auto int retries, rc, i, swapPending, j, nErrs;
auto char filename[13], buf[6];
auto int ntests;

sdspi_initDevice(0,&SD_dev0);

if(!sdspi_debounce(&SD[0])){
printf("

INSERT SD CARD");
}

swapPending = 2;
ntests = nErrs = 0;

while(ntests < NTESTS && !nErrs)
{
if(swapPending==2){

     // Busy wait while card not detected
     while(!sdspi_debounce(&amp;SD[0]));
     //msDelay (5000);

     // Mount SD card
     retries = 0;

     for (rc = 1; rc; ) {  // Retry loop
        rc = fat_AutoMount(FAT_SD_DEVICE | FDDF_MOUNT_PART_0 |
                 FDDF_COND_DEV_FORMAT | FDDF_COND_PART_FORMAT
                  );

        if(rc)   // If failed to mount SD card
        {
            retries++;
            printf("%sERROR: fat_AutoMount() returned  %d, Retrying

%s",
RED, rc, BLACK);

             if(retries &lt; NRETRY)
             {
               fatwtc_flushall(WTC_WAIT | WTC_PURGE);
               _fatwtc_init();
             }
             else {
                nErrs++;
                break;
             }
         }
     }

     if (rc) {
        break;
     }
     else
     {
        printf("

%sSD card mounted…
“, BLUE);
printf(”
Hit KB key when ready to swap cards
%s", BLACK);
swapPending = 0;
}
}

  if(swapPending==1){

     //*** UnmountDevice will flush SD card with WTC_PURGE flag
     rc = fat_UnmountDevice(FAT_SD_PART-&gt;dev);
     if(rc)
     {  // Unmount failure
        printf("%sERROR: fat_UnmountDevice() returned %d.

%s",
RED, rc, BLACK);
nErrs++;
continue; // Abort while loop
}
else {
printf("
%sCard unmounted, Switch SD cards now
%s",
RED, BLACK);
}

     // Busy wait while card detected
     while(sdspi_debounce(&amp;SD[0]));

     printf("

%sCard Removed, put new or same card in%s

",
RED, BLACK);
swapPending = 2; // Ready to auto-mount
}
else // Test file operations
{
for(j=0; j< NFILES ; j++){ // use NFILES files per device

        // Key hit while focus on stdio window
        if(kbhit()){
           swapPending = 1;
           getchar();  // Clear key hit
           break;
        }

        strcpy(filename, "file");
        itoa(j, buf);
        strcat(filename,buf);
        strcat(filename,".txt");

        rc = fat_Open(FAT_SD_PART, filename,
                FAT_FILE, FAT_CREATE, &amp;my_file, NULL);
        if (rc){
           printf("%sERROR: fat_Open() returned result code %d.

%s",
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}
// WRITE TO THE FILE
memset(fbuff, j, FILESIZE);
if(rc=fat_Seek(&my_file, 0, SEEK_SET )){
printf(“%sERROR: fat_Seek() returned result code %d.
%s”,
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}
rc = fat_Write(&my_file,fbuff,FILESIZE);
if(rc<0){
printf(“%sERROR: fat_Write() %d
%s”,RED,rc,BLACK);
nErrs++;
break; // Abort for loop
}
memset(fbuff,0,FILESIZE);

        if(rc=fat_Seek(&amp;my_file, 0, SEEK_SET )){
           printf("%sERROR: fat_Seek() returned result code %d.

%s",
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}

        // READ A FILE
        rc=fat_Read(&amp;my_file,fbuff,FILESIZE);
        if(rc&lt;0){
           printf("%sERROR: fat_Read() returned result code %d.

%s",
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}

        for(i=0; i &lt; FILESIZE; i++){
           if( fbuff[i] != j){
              printf("%sERROR: fat_Read() bad value

%s",RED,BLACK);
j = 0;
nErrs++;
break; // Abort for loop
}
}
rc = fat_Close(&my_file);
if(rc){
printf(“%sERROR: fat_Close() returned result code %d.
%s”,
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}
rc = fat_Delete( FAT_SD_PART, FAT_FILE, filename);
if(rc){
printf(“%sERROR: fat_Delete() returned result code %d.
%s”,
RED, rc, BLACK);
nErrs++;
break; // Abort for loop
}
} // End for j

     ntests++;

     if(swapPending) continue; // Start next while loop iteration

     if(j !=NFILES){
        printf("%sFAT error 

%s", RED, BLACK);
}
} // End else
} // End while

if(!nErrs){
printf("
%s SUCCESS
%s", BLUE, BLACK);
}
else {
printf("
%s %d FAT Errors occurred
%s", RED, nErrs, BLACK);
}

// Unmount and loop so we don’t wear out flash if program left
// running in run mode
printf("
%sUnmounting the SD Card, please wait.
%s", RED, BLACK);
fat_UnmountDevice(FAT_SD_PART->dev);
printf("
%sSD Card unmounted, press any key to exit.
%s", RED, BLACK);
while(1) if(kbhit()) break;
}