下面是一个朋友写的程序有不明白的地方请高人指点 /*****************************************************************************/ //解析FAT链表 void fat_load(unsigned long Cluster, //文件起始扇区 unsigned long *Block, //返回文件下一扇区 unsigned char *TMP_Buffer) //函数所需buffer /*****************************************************************************/ { unsigned int FAT_Byte_Addresse; unsigned long FAT_Block_Addresse; //读取FAT表,根据FAT制式搜寻扇区 if(FatFlags == FAT_**_16) { if(Cluster == 0xFFFF) { return; //文件已经到达末尾 } FAT_Byte_Addresse = ((Cluster<<1) & 0x1FF); FAT_Block_Addresse = ((Cluster<<1)/BlockSize) + fat_offset; //得到要读取得文件在 盘中的位置 // fat_offset-从BPB中获取FDT表的地址时已经得到 //BlockSize=512 mmcReadSector(FAT_Block_Addresse,TMP_Buffer); //读取fat琏表数据 *Block = (TMP_Buffer[FAT_Byte_Addresse + 1] << 8) + TMP_Buffer[FAT_Byte_Addresse];//读取下一簇地址 if(*Block == 0xFFFF) *Block = 0xFFFFFFFF; } else if(FatFlags == FAT_**_12) { if(Cluster == 0xFFF) { return; //文件已经到达末尾 } FAT_Byte_Addresse = (((Cluster*3)>>1) & 0x1FF); FAT_Block_Addresse = (((Cluster*3)>>1) / BlockSize) + fat_offset; if(FAT_Byte_Addresse == 0x1FF) { mmcReadSector(FAT_Block_Addresse,TMP_Buffer); if((Cluster % 2) == 0) { *Block = TMP_Buffer[FAT_Byte_Addresse]; } else { *Block = (TMP_Buffer[FAT_Byte_Addresse] >> 4); } mmcReadSector(FAT_Block_Addresse+1,TMP_Buffer); if((Cluster % 2) == 0) { *Block += ((TMP_Buffer[0] & 0x0F) << 8); } else { *Block += (TMP_Buffer[0] << 4); } } else { mmcReadSector(FAT_Block_Addresse,TMP_Buffer); if((Cluster % 2) == 0) { *Block = ((TMP_Buffer[FAT_Byte_Addresse + 1] & 0x0F) << 8) + TMP_Buffer[FAT_Byte_Addresse]; } else { *Block = (TMP_Buffer[FAT_Byte_Addresse + 1] << 4) + (TMP_Buffer[FAT_Byte_Addresse] >> 4); } } if(*Block == 0xFFF) *Block = 0xFFFFFFFF; } else if(FatFlags == FAT_**_32) { if(Cluster == 0xFFFFFFFF) { return; //文件已经到达末尾 } FAT_Byte_Addresse = ((Cluster<<2) & 0x1FF); FAT_Block_Addresse = ((Cluster<<2) / BlockSize) + fat_offset; mmcReadSector (FAT_Block_Addresse,TMP_Buffer); *Block = (TMP_Buffer[FAT_Byte_Addresse + 3] << 24) + (TMP_Buffer[FAT_Byte_Addresse + 2] << 16) + (TMP_Buffer[FAT_Byte_Addresse + 1] << 8) + TMP_Buffer[FAT_Byte_Addresse]; } return; }
//*****************************************************************************/ //读取文件内容每次512bytes unsigned char fat_read_file(unsigned int Cluster, unsigned char *buff, unsigned long BlockCount, unsigned char *blockserial) /*****************************************************************************/ { unsigned long Block; static unsigned char serial=0; //在这里申请的定态变量,函数返回后还能保存吗 if(BlockCount == 0) { fat_load(Cluster,&Block,buff); //文件下一扇区-----可程序中读数据没有次参数
mmcReadSector((Cluster-2)*SecPerClus+data_offset+serial,buff); //文件数据存放在 buff 中的 //上面函数参数问题比较多 // 1: Cluster-------带入是一个固定值(这里应该变化,如果这里是Block就对了) // 2: SecPerClus----是一个固定值 // 3: data_offset---是一个固定值 // 4:serial---------是一个变量 // 5:buff-----------是一个缓冲区 serial++; blocknow = Cluster; *blockserial = SecPerClus-serial; if(serial == SecPerClus) serial = 0; } else { fat_load(blocknow,&Block,buff); mmcReadSector((blocknow-2)*SecPerClus+data_offset+serial,buff); serial++; *blockserial = SecPerClus-serial; if(serial == SecPerClus) serial = 0; } if(serial == 0) blocknow = Block; if(blocknow == 0xFFFFFFFF) return 1; else return 0;
}
===================================================================== fat_load(Cluster,&Block,buff);//文件下一扇区-----可程序中读数据没有次参数 mmcReadSector((Cluster-2)*SecPerClus+data_offset+serial,buff); //上面函数参数问题比较多 1: Cluster-------带入是一个固定值(这里应该变化,如果这里是Block就对了) 2: SecPerClus----是一个固定值 3: data_offset---是一个固定值 4:serial---------是一个变量 5:buff-----------是一个缓冲区
|