(SD和MMC)FAT32初始化有问题==》DX们请进

2020-02-10 08:38发布

我在用PIC  CCSC时的FAT例子发现FAT不能初始化。 到CCS论坛上发现也有好多人问过这个问题,现还没解决。
说明:低层的SPI驱动是OK了,就是FAT不能初始化。程序如下:


/*
signed int fat_init()
Summary: Initializes global variables that are essential for this library working
Returns: EOF if there was a problem with the media, GOODEC if everything went okay.
Note: This must be called before any other function calls in this library.
*/
signed int fat_init()
{
   int ec = 0;

   int
      FATs,
      Sectors_Per_Cluster;

   int16
      Bytes_Per_Sector,
      Reserved_Sectors,
      Small_Sectors;

   int32
      Hidden_Sectors,
      Large_Sectors;

#ifdef FAT32
   int32 Sectors_Per_FAT;
#else // FAT16
   int16
      Root_Entries,
      Sectors_Per_FAT;
#endif // #ifdef FAT32

   // initialize the media
   ec += mmcsd_init();

   // start filling up variables
   ec += mmcsd_read_data(11, 2, &Bytes_Per_Sector);
   ec += mmcsd_read_data(13, 1, &Sectors_Per_Cluster);
   ec += mmcsd_read_data(14, 2, &Reserved_Sectors);
   ec += mmcsd_read_data(16, 1, &FATs);
#ifdef FAT16
   ec += mmcsd_read_data(17, 2, &Root_Entries);
#endif // #ifdef FAT16
   ec += mmcsd_read_data(19, 2, &Small_Sectors);
#ifdef FAT32
   ec += mmcsd_read_data(36, 4, &Sectors_Per_FAT);
#else // FAT16
   ec += mmcsd_read_data(22, 2, &Sectors_Per_FAT);
#endif // #ifdef FAT32
   ec += mmcsd_read_data(28, 4, &Hidden_Sectors);
   ec += mmcsd_read_data(32, 4, &Large_Sectors);
#ifdef FAT16
   Next_Free_Clust = 2;
#else
   ec += mmcsd_read_data(0x3EC, 4, &Next_Free_Clust);
#endif
   if(ec != GOODEC)
      return EOF;

   // figure out the size of a cluster
   Bytes_Per_Cluster = Sectors_Per_Cluster * Bytes_Per_Sector;

   // figure out how long one FAT is
   FAT_Length = Sectors_Per_FAT * (int32)Bytes_Per_Sector;

   // figure out where the FAT starts
   FAT_Start = Reserved_Sectors * Bytes_Per_Sector;

   // figure out where the root directory starts
   Root_Dir = FAT_Start + (FATs * FAT_Length);

   // figure out where data for files in the root directory starts
#ifdef FAT32
   Data_Start = Bytes_Per_Cluster + Root_Dir;
#else // FAT16
   Data_Start = (Root_Entries * 0x20) + (Bytes_Per_Sector - 1);
   Data_Start /= Bytes_Per_Sector;
   Data_Start += Reserved_Sectors + (FATs * Sectors_Per_FAT);
   Data_Start *= Bytes_Per_Sector;
#endif // #ifdef FAT32

   return GOODEC;
}

/*
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。