⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat.c

📁 PIC MMC and FAT Sample code
💻 C
字号:
void fatReadPartitionEntry(DWORD address) {
	debug("\n\rBoot Descriptor : %X",mmcReadSingleByte(address+0));
	debug("\n\rFirst Partition Sector : %X %X %X",mmcReadSingleByte(address+3),mmcReadSingleByte(address+2),mmcReadSingleByte(address+1));
	debug("\n\rFile System Descriptor : %X",mmcReadSingleByte(address+4));
	debug("\n\rLast Partition Sector : %X %X %X",mmcReadSingleByte(address+7),mmcReadSingleByte(address+6),mmcReadSingleByte(address+5));
	debug("\n\rFirst Sector Relative Position : %X %X %X %X",mmcReadSingleByte(address+11),mmcReadSingleByte(address+10),mmcReadSingleByte(address+9),mmcReadSingleByte(address+8));
	debug("\n\rNo of sectors in partition : %X %X %X %X",mmcReadSingleByte(address+15),mmcReadSingleByte(address+14),mmcReadSingleByte(address+13),mmcReadSingleByte(address+12));
	debug("\n\r");

	peStart=(make32(mmcReadSingleByte(address+11),mmcReadSingleByte(address+10),mmcReadSingleByte(address+9),mmcReadSingleByte(address+8))+1)*512;
}

void fatReadBootSector(DWORD address) {
	debug("\n\rBS_jmpBoot : %X %X %X",mmcReadSingleByte(address+0),mmcReadSingleByte(address+1),mmcReadSingleByte(address+2));
	debug("\n\rBS_OEMName : %C%C%C%C%C%C%C%C",mmcReadSingleByte(address+3),mmcReadSingleByte(address+4),mmcReadSingleByte(address+5),mmcReadSingleByte(address+6),mmcReadSingleByte(address+7),mmcReadSingleByte(address+8),mmcReadSingleByte(address+9),mmcReadSingleByte(address+10));
	debug("\n\rBPB_BytsPerSec : %X%X",mmcReadSingleByte(address+12),mmcReadSingleByte(address+11));
	debug("\n\rBPB_SecPerClus : %X",mmcReadSingleByte(address+13));
	debug("\n\rBPB_RsvdSecCnt : %X%X",mmcReadSingleByte(address+15),mmcReadSingleByte(address+14));
	debug("\n\rBPB_NumFATs : %X",mmcReadSingleByte(address+16));
	debug("\n\rBPB_RootEntCnt : %X%X",mmcReadSingleByte(address+18),mmcReadSingleByte(address+17));
	debug("\n\rBPB_TotSec16 : %X%X",mmcReadSingleByte(address+20),mmcReadSingleByte(address+19));
	debug("\n\rBPB_Media : %X",mmcReadSingleByte(address+21));
	debug("\n\rBPB_FATSz16 : %X%X",mmcReadSingleByte(address+23),mmcReadSingleByte(address+22));
	debug("\n\rBPB_SecPerTrk : %X%X",mmcReadSingleByte(address+25),mmcReadSingleByte(address+24));
	debug("\n\rBPB_NumHeads : %X%X",mmcReadSingleByte(address+27),mmcReadSingleByte(address+26));
	debug("\n\rBPB_HiddSec : %X%X%X%X",mmcReadSingleByte(address+31),mmcReadSingleByte(address+30),mmcReadSingleByte(address+29),mmcReadSingleByte(address+28));
	debug("\n\rBPB_TotSec32 : %X%X%X%X",mmcReadSingleByte(address+35),mmcReadSingleByte(address+34),mmcReadSingleByte(address+33),mmcReadSingleByte(address+32));
	debug("\n\rBS_DrvNum : %X",mmcReadSingleByte(address+36));
	debug("\n\rBS_Reserved1 : %X",mmcReadSingleByte(address+37));
	debug("\n\rBS_BootSig : %X",mmcReadSingleByte(address+38));
	debug("\n\rBSVolID : %X%X%X%X",mmcReadSingleByte(address+42),mmcReadSingleByte(address+41),mmcReadSingleByte(address+40),mmcReadSingleByte(address+39));
	debug("\n\rBS_VolLab : %C%C%C%C%C%C%C%C%C%C%C",mmcReadSingleByte(address+43),mmcReadSingleByte(address+44),mmcReadSingleByte(address+45),mmcReadSingleByte(address+46),mmcReadSingleByte(address+47),mmcReadSingleByte(address+48),mmcReadSingleByte(address+49),mmcReadSingleByte(address+50),mmcReadSingleByte(address+51),mmcReadSingleByte(address+52),mmcReadSingleByte(address+53));
	debug("\n\rBS_FilSysType : %C%C%C%C%C%C%C%C",mmcReadSingleByte(address+54),mmcReadSingleByte(address+55),mmcReadSingleByte(address+56),mmcReadSingleByte(address+57),mmcReadSingleByte(address+58),mmcReadSingleByte(address+59),mmcReadSingleByte(address+60),mmcReadSingleByte(address+61));
	debug("\n\r");

	fatStart=peStart+make32(mmcReadSingleByte(address+15),mmcReadSingleByte(address+14))*512;	
	rootDirStart=fatStart+make32(mmcReadSingleByte(address+23),mmcReadSingleByte(address+22))*512*make32(mmcReadSingleByte(address+16));
	dataSecStart=rootDirStart+make32(mmcReadSingleByte(address+18),mmcReadSingleByte(address+17))*32;
	fatSecPerClus=mmcReadSingleByte(address+13);

	debug("\n\rPartition Start : %X %X %X %X",make8(peStart,3),make8(peStart,2),make8(peStart,1),make8(peStart,0));
	debug("\n\rFAT Start : %X %X %X %X",make8(fatStart,3),make8(fatStart,2),make8(fatStart,1),make8(fatStart,0));
	debug("\n\rRootDir Start : %X %X %X %X",make8(rootDirStart,3),make8(rootDirStart,2),make8(rootDirStart,1),make8(rootDirStart,0));
	debug("\n\rData Start : %X %X %X %X",make8(dataSecStart,3),make8(dataSecStart,2),make8(dataSecStart,1),make8(dataSecStart,0));
	debug("\n\r");
}

WORD fatReadFAT(WORD cluster) {
	DWORD address;
	WORD fat;

	address=fatStart+cluster*2;
	fat=make16(mmcReadSingleByte(address+1),mmcReadSingleByte(address));

	return fat;
}

void fatWriteFAT(WORD cluster,WORD data) {
	DWORD address;

	address=fatStart+cluster*2;
	mmcWriteSingleByte(address,make8(data,0));
	mmcWriteSingleByte(address+1,make8(data,1));
}

WORD fatFindEmptyCluster() {
	int16 i,cnt;
	
	cnt=(rootDirStart-fatStart)>>4;	// Calculate no of FAT entries
	for(i=2;i<cnt;i++) {
		if(!fatReadFAT(i))
			break;
	}
	return i;
}

WORD fatReadDirEntry(WORD offset) {
	DWORD address;
	int16 cluster;
	address=rootDirStart+offset*32;

	debug("\n\rDIR_Name : %C%C%C%C%C%C%C%C.%C%C%C",mmcReadSingleByte(address+0),mmcReadSingleByte(address+1),mmcReadSingleByte(address+2),mmcReadSingleByte(address+3),mmcReadSingleByte(address+4),mmcReadSingleByte(address+5),mmcReadSingleByte(address+6),mmcReadSingleByte(address+7),mmcReadSingleByte(address+8),mmcReadSingleByte(address+9),mmcReadSingleByte(address+10));
	debug("\n\rDIR_Attr : %X",mmcReadSingleByte(address+11));
	debug("\n\rDIR_NTRes : %X",mmcReadSingleByte(address+12));
	debug("\n\rDIR_CrtTimeTenth : %X",mmcReadSingleByte(address+13));
	debug("\n\rDIR_CrtTime : %X %X",mmcReadSingleByte(address+15),mmcReadSingleByte(address+14));
	debug("\n\rDIR_CrtDate : %X %X",mmcReadSingleByte(address+17),mmcReadSingleByte(address+16));
	debug("\n\rDIR_LstAccDate : %X %X",mmcReadSingleByte(address+19),mmcReadSingleByte(address+18));
	debug("\n\rDIR_FstClusHI : %X %X",mmcReadSingleByte(address+21),mmcReadSingleByte(address+20));
	debug("\n\rDIR_WrtTime : %X %X",mmcReadSingleByte(address+23),mmcReadSingleByte(address+22));
	debug("\n\rDIR_WrtDate : %X %X",mmcReadSingleByte(address+25),mmcReadSingleByte(address+24));
	debug("\n\rDIR_FstClusLO : %X %X",mmcReadSingleByte(address+27),mmcReadSingleByte(address+26));
	debug("\n\rDIR_FileSize : %X%X%X%X",mmcReadSingleByte(address+31),mmcReadSingleByte(address+30),mmcReadSingleByte(address+29),mmcReadSingleByte(address+28));
	debug("\n\rFAT Code : %LX",fatReadFAT(make16(mmcReadSingleByte(address+27),mmcReadSingleByte(address+26))));
	debug("\n\rCluster Map : ");
	cluster=make16(mmcReadSingleByte(address+27),mmcReadSingleByte(address+26));
	debug("-> %LX",cluster);
	while((cluster=fatReadFAT(cluster))!=0xFFFF && cluster!=0xFFF8)
		debug("-> %LX",cluster);
	debug("\n\n\r");

	return (make16(mmcReadSingleByte(address+27),mmcReadSingleByte(address+26)));
}

void fatWriteDirEntry(WORD offset,BYTE *fileEntry) {
	DWORD address;
	int16 i;
	int j;
	address=rootDirStart+offset*32;

	mmcStartSingleWrite(mmcGetSector(address),TRUE);
	i=address & 0x000001FF;
	for(j=0;j<32;j++) {
		mmcWriteBuffer[i]=*fileEntry;
		i++;
		fileEntry++;
	}
	mmcEndSingleWrite(TRUE);
}

WORD fatQueryFile(WORD offset,char *name,BYTE *size) {
	int i;
	DWORD address;
	address=rootDirStart+offset*32;

	for(i=0;i<11;i++) {
		*name=mmcReadSingleByte(address+i);
		name++;
	}

	for(i=0;i<4;i++) {
		*size=mmcReadSingleByte(address+28+i);
		size++;
	}
	
	return make16(mmcReadSingleByte(address+27),mmcReadSingleByte(address+26));
}

void fatReadCluster(WORD cluster,BYTE *ptr,int count) {
	int i;
	DWORD address;

	address=dataSecStart+(cluster-2)*fatSecPerClus*512;
	debug("Cluster address : %X%X%X%X\n\r",make8(address,3),make8(address,2),make8(address,1),make8(address,0));
	for(i=0;i<count;i++) {
		*ptr=mmcReadSingleByte(address+i);
		ptr++;
	}
}

int32 fatGetClusFirstByte(int16 cluster) {
	return dataSecStart+(cluster-2)*fatSecPerClus*512;
}

int16 fatGetClusFirstSec(int16 cluster) {
	return (dataSecStart>>9)+(cluster-2)*fatSecPerClus;
}

WORD fatNewFileEntry() {
	char blankFile[11];
	char fileName[11];
	int16 i;
	DWORD fileSize;

	for(i=0;i<11;i++)
		blankFile[i]=0;
	for(i=0;i<512;i++) {
		fatQueryFile(i,fileName,&fileSize);
		if(!strncmp(fileName,blankFile,11))
			break;
	}

	return i;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -