[En-Nut-Discussion] SDCARD Interfacing with SAM7X

ennut at thezachariasgroup.com ennut at thezachariasgroup.com
Tue Feb 20 22:00:14 CET 2007


   How big is your SDCARD? I found the largest card the OS will 
accept is 512 Meg.
My 1 Gig card did not work in the system.
All cards I've used work right out of the package.

Here is the code I use to access the SDCARD, in case you may have 
left out a step.



#define MY_FSDEV       devPhat0
#define MY_FSDEV_NAME  "PHAT0"

/* SAM7X-EK block device interface. */
#define MY_BLKDEV      devAt91SpiMmc0
#define MY_BLKDEV_NAME "MMC0"

FILE *  MMC_stream;



     /*
      * Register our device for the file system.
      */
   NutRegisterDevice(&MY_FSDEV, 0, 0);

   // Register SDMMC Card block device.
   printf("Registering block device '" MY_BLKDEV_NAME "'...");
   if (NutRegisterDevice(&MY_BLKDEV, 0, 0))
   {
     puts("failed");
     for (;;);
   }
   puts("OK");

   // Mount the SDCARD partition.
   printf("Mounting block device '" MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME "'...");
   if (_open(MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME, _O_RDWR | _O_BINARY) == -1)
   {
     puts("failed");
     for (;;);
   }
   puts("OK");


   printf("Opening READER_NAME.TXT... ");
   MMC_stream = fopen("PHAT0:/READER_NAME.TXT", "rb");

   if (MMC_stream != 0)
   {
     ucUnitType = READER;
     puts(" Opened OK");

     if (fseek (MMC_stream, 0l,  SEEK_END) == -1)
     {
       puts(" \n\r Seek to End of File Failed");
       // Error ??
     }
     else
     {
       printf("\n\r File Length is %ld ", ftell(MMC_stream));
       cReaderName = NutHeapAlloc(ftell(MMC_stream));
       if (cReaderName == 0)
       {
         printf("\n\r Memory Allocation Error");
       }

       if (fseek (MMC_stream, 0l,  SEEK_SET) == -1)
       {
         puts(" \n\r Seek to End of File Failed");
         // Error ??
       }
       i = 0;

       while (1)
       {
         if ( (iResult = fgetc(MMC_stream)) == -1)
         {
           break;
         }
         else
         {
           if ( (iResult == 0x0D) || (iResult == 0x0A) || (iResult == 0) )
           {
             break;
           }
           else
           {
             cReaderName[i++] = iResult;
           }
         }

       }
       cReaderName[i] = 0;
       printf("\n\e Reader Name is .%s. ", cReaderName);
       ucUnitType = READER;
     }
     fclose(MMC_stream);
   }



   Zack




More information about the En-Nut-Discussion mailing list