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

📄 fat.c

📁 data logging in mmc in KML format
💻 C
📖 第 1 页 / 共 2 页
字号:
	}
	if (CSVcounter==0){							//the new CSVfile in rootdirectory will be written in a new data block, can't use CSVcounter-32 later
		err=ReadMMC(CSVaddress-512,CSV_file);
		for (i=480,j=0;j<8;i++,j++) prevfilename[j]=CSV_file[i];
		prevfilename[j]=0;
//		printf("     Prevfilename = %s     ",prevfilename);
		err=ReadMMC(CSVaddress,CSV_file);
	}else{
		err=ReadMMC(CSVaddress,CSV_file);
		for (i=CSVcounter-32,j=0;j<8;i++,j++) prevfilename[j]=CSV_file[i];
		prevfilename[j]=0;
//		printf("     Prevfilename = %s     ",prevfilename);
	}
	for (j=0,err=0;j<6;j++){ //compare the first 6 bytes
		if (prevfilename[j]!=filename[j]) err=1;
	}											//err will be 1 if different, 0 if same, i at this instance is the location of the first appended bit
	if (err==0){
		if (prevfilename[7]=='9'){				//makes files from 00 to 99 then A0-9, B0-9, up to Z0-9
			filename[7]='0';
			if (prevfilename[6]=='9') filename[6]='A';
			else filename[6]=prevfilename[6]+1;
		}else{
			filename[7]=prevfilename[7]+1;
			filename[6]=prevfilename[6];
		}
	}else{
		filename[7]='0';
		filename[6]='0';
	}
	filename[8]=0;
//	printf("New filename = %s\n",filename);
	for (i=CSVcounter,j=0;((j<strlen(filename))&&(j<8));i++,j++) CSV_file[i]=filename[j]; //put filename
	for (;j<8;i++,j++) CSV_file[i]=32; //sp
	if (isfolder==0){
		CSV_file[i++]= 75;	//K
		CSV_file[i++]= 77;	//M
		CSV_file[i++]= 76;	//L
		CSV_file[i]=32;	//Archive
	}else{
		for (;j<11;i++,j++) CSV_file[i]=32; //sp
		CSV_file[i]=16;	//Subdirectory or Folder type
	}
	i+=5;
	tempdate[0]=filename[2];	//day
	tempdate[1]=filename[3];
	tempdate[2]=0;
	intdate=atoi(tempdate);
	CSV_file[i]=intdate;
	tempdate[0]=filename[0];	//month
	tempdate[1]=filename[1];
	tempdate[2]=0;
	intdate=atoi(tempdate);
	if (intdate>=8){
		CSV_file[i+1]=1;
		intdate-=8;
	}
	CSV_file[i]=(intdate<<5)|(CSV_file[i]);
	CSV_file[i+2]=CSV_file[i];
	CSV_file[i+8]=CSV_file[i];
	tempdate[0]=filename[4];	//year
	tempdate[1]=filename[5];
	tempdate[2]=0;
	intdate=atoi(tempdate);
	intdate+=20;
	CSV_file[++i]=(intdate<<1)|CSV_file[i];
	CSV_file[i+2]=CSV_file[i];
	CSV_file[i+8]=CSV_file[i];
	FATaddressincrement=0;
	err=ReadMMC(FAT1_entries_start,FATentries); //from experiment, FATentries[511] will not be zero if the block is full
	for (;FATentries[511]!=0;){ //find the first not full 512 block in FAT1
		FATaddressincrement+=512;
		err=ReadMMC(FAT1_entries_start+FATaddressincrement,FATentries); //from experiment, FATentries[511] will not be zero if the block is full
	}	//updates global variables FATaddressincrement and FATentries at the same time and the for loop below updates FATcounter
	for (FATcounter=510;((FATentries[FATcounter-2]==0)&&(FATentries[FATcounter-1]==0)&&(FATcounter!=0));FATcounter-=2); //510 is the position of the last LO byte in a block
	FstClusLO=(FATaddressincrement+FATcounter)/2; //FstClusLO is just the distance from the start of fat entry
	CSV_file[CSVcounter+27]=FstClusLO/256;		//get the High Byte of j
	CSV_file[CSVcounter+26]=FstClusLO%256;		//low Byte of j
	FATgreaterthanonecluster=(FATaddressincrement+FATcounter+2)/2; //the counter for LowByte HighByte for FATentries larger than one cluster
	FAThigh=FATgreaterthanonecluster/256;		//High Byte for Fat counter
	FATlow=FATgreaterthanonecluster%256; 		//Low Byte for FAT counter
	for (i=0;i<512;i++) DATAarray[i]=0;			//initialize DATAarray to zero
	DATAcounter = 0;							//at the first byte
	DATAaddress=(FstClusLO-2)*8192+data_region_addr; //8192=0x2000 (bytes in a cluster for 512mb mmc), because the first file have 2 as FstClusLO and dataregion 0x41000
	DATAsectorcount=0;
	if (DATAaddress>=last_addr){
		printf("FULL MMC/SD Dataaddress: %lx Last_addr: %lx\n",DATAaddress,last_addr);
		while (1){
			PDOUT |= 0x02; //light up a LED and stay here
			PAOUT |= 0x10;
		}				
	}
	do{
		err=InitMMC();
		err=CSV_file[0];
		WriteMMC(CSVaddress,CSV_file);
		CSV_file[0]=0;
		ReadMMC(CSVaddress,CSV_file);
		if (CSV_file[0]!=err){
			if (errorprinted==0){
				printf("Error writting or No MMC/SD inserted\n");
				PDOUT |= 0x01;						//LED to show it can't write
				PBOUT |= 0x80;
				errorprinted=1;
			}
			CSV_file[0]=err;					//so it will be back to the true
 		    err=1;
		}else{ err=0;}
	}while (err==1);
	errorprinted=0;
	PDOUT &= ~0x01;								//turn off the LED
	PBOUT &= ~0x80;
	if (isfolder==1){
		CSV_file[0]=0x2E;
		for (i=1;i<11;i++) CSV_file[i]=0x20;
		for (;i<32;i++) CSV_file[i]=CSV_file[CSVcounter+i];
		CSV_file[i++]=0x2E;
		CSV_file[i++]=0x2E;
		for (;i<43;i++) CSV_file[i]=0x20;
		for (;i<58;i++) CSV_file[i]=CSV_file[CSVcounter+i-32];
		if (!inrootdir){
			CSV_file[i++]=previousfolderhighbyte;
			CSV_file[i++]=previousfolderlowbyte;
		}
		previousfolderhighbyte=CSV_file[CSVcounter+27];
		previousfolderlowbyte=CSV_file[CSVcounter+26];
		for (;i<512;i++) CSV_file[i]=0;
		foldercontentsaddress=DATAaddress;
		do{
			err=InitMMC();
			err=CSV_file[0];
			WriteMMC(foldercontentsaddress,CSV_file);
			CSV_file[0]=0;
			ReadMMC(foldercontentsaddress,CSV_file);
			if (CSV_file[0]!=err){
				if (errorprinted==0){
					printf("Error writting or No MMC/SD inserted\n");
					PDOUT |= 0x01;						//LED to show it can't write
					PBOUT |= 0x80;
					errorprinted=1;
				}
				CSV_file[0]=err;					//so it will be back to the true
 			    err=1;
			}else{ err=0;}
		}while (err==1);
		errorprinted=0;
		for (i=0;i<512;i++) CSV_file[i]=0;
		for (i=512;i<8192;i+=512) WriteMMC(foldercontentsaddress+i,CSV_file);	//8192 is 16*512 erase remaining bytes in one cluster
		FATentries[FATcounter++]=0xFF;			//FATcounter should not increment here
		FATentries[FATcounter++]=0xFF;			//nor here
		err=WriteMMC(FAT1_entries_start+FATaddressincrement,FATentries); //write FAT1 and FAT2, they are identical
		err=WriteMMC(FAT2_entries_start+FATaddressincrement,FATentries);
	}
	return;
}

char writetommc(char *newdata, char *newfilename, unsigned char OnLED){
	unsigned int i;
	unsigned char err=0;
	unsigned char firstbyte=0;
	char errorprinted=0;
	char timeout=0;
	if (OnLED==1){
		// 4 lines below will turn ON LED2 then turn OFF, there's delay so we can see it, but if running a loop of writetommc,it delays writting
		PDOUT |= 0x02;
		PAOUT |= 0x10;
	}	//below it will turn this off
	for (i=0;i<6;i++){
		if (filename[i]!=newfilename[i]){
			create_folder_file(newfilename,0,0);
			for (i=0;i<6;i++) filename[i]=newfilename[i];
			break;
		}
	}
	for (i=0;i<strlen(newdata);i++){
//		if (((i==0)&&(newdata[i+1]==','))||(((newdata[i-1]==',')||(newdata[i-1]=='/')||(newdata[i-1]==':'))&&((newdata[i+1]==',')||(newdata[i+1]=='/')||(newdata[i+1]==':')||(newdata[i+1]==' ')))){
//			DATAarray[DATAcounter++]='0';
//		}
		if (DATAcounter==512) DATAfull();
		DATAarray[DATAcounter++]=newdata[i];
		if (DATAcounter==512) DATAfull();
	}
	do{
		err=InitMMC();
		err=DATAarray[0];
		WriteMMC(DATAaddress,DATAarray);
		DATAarray[0]=0;
		ReadMMC(DATAaddress,DATAarray);
		if (DATAarray[0]!=err){
			if (errorprinted==0){
				printf("Error writting or No MMC/SD inserted\n");
				PDOUT |= 0x01;						//LED to show it can't write
				PBOUT |= 0x80;
				errorprinted=1;
			}
			DATAarray[0]=err;					//so it will be back to the true
			err=1;
		}else{ err=0;}
	}while (err==1);
	errorprinted=0;
	PDOUT &= ~0x01;								//turn off the LED
	PBOUT &= ~0x80;
	check_updateFAT();
	updateCSVsize();
	if (OnLED==1){
		// 4 lines below will turn ON LED2 then turn OFF, there's delay so we can see it, but if running a loop of writetommc,it delays writting
		for(i=0;i<0x1000;i++);
		PDOUT &= ~0x02;
		PAOUT &= ~0x10;
	}
	return 0;
}

void DATAfull(void){
	unsigned int i;
	unsigned char err=0,firstbyte;
	char errorprinted=0;
	do{
		err=InitMMC();
		err=DATAarray[0];
		WriteMMC(DATAaddress,DATAarray);
		DATAarray[0]=0;
		ReadMMC(DATAaddress,DATAarray);
		if (DATAarray[0]!=err){
			if (errorprinted==0){
				printf("Error writting or No MMC/SD inserted\n");
				PDOUT |= 0x01;						//LED to show it can't write
				PBOUT |= 0x80;
				errorprinted=1;
			}
			DATAarray[0]=err;					//so it will be back to the true
			err=1;
		}else{ err=0;}
	}while (err==1);
	errorprinted=0;
	PDOUT &= ~0x01;								//turn off the LED
	PBOUT &= ~0x80;
	DATAaddress+=512;
	if (DATAaddress>=last_addr){
		printf("FULL MMC/SD Dataaddress: %lx Last_addr: %lx\n",DATAaddress,last_addr);
		while (1){
			PDOUT |= 0x02;	//light up a LED and stay here
			PAOUT |= 0X10;
		}				
	}
	for (i=0;i<512;i++) DATAarray[i]=0;			//initialize DATAarray to zero
	DATAcounter=0;
	DATAsectorcount++;
}

void check_updateFAT(void){
	unsigned int i;
	unsigned char err=0;
	if (((DATAsectorcount%16)==0)&&(DATAsectorcount!=DATAsectorcountflag)&&(DATAsectorcount>0)&&(DATAcounter>0)){	//FATcounter is only incremented after one cluster is written, for a 512mb,one cluster is 16sectors
		DATAsectorcountflag=DATAsectorcount;
		FATentries[FATcounter++]=FATlow++;
		FATentries[FATcounter++]=FAThigh;
		if (FATlow==0) FAThigh++;
		if (FATcounter==512){
			err=WriteMMC(FAT1_entries_start+FATaddressincrement,FATentries); //write FAT1 and FAT2, they are identical
			err=WriteMMC(FAT2_entries_start+FATaddressincrement,FATentries);
			FATaddressincrement+=512;
			FATcounter=0;
			for (i=0;i<512;i++) FATentries[i]=0; //initialize FATentries to zero
		}
	}
	if ((FATentries[FATcounter]!=0xFF)||(FATentries[FATcounter+1]!=0xFF)){
		FATentries[FATcounter]=0xFF;			//FATcounter should not increment here
		FATentries[FATcounter+1]=0xFF;			//nor here
		err=WriteMMC(FAT1_entries_start+FATaddressincrement,FATentries); //write FAT1 and FAT2, they are identical
		err=WriteMMC(FAT2_entries_start+FATaddressincrement,FATentries);
	}	//else do nothing
}

void updateCSVsize(void){	//4bytes at the last of the 32 bytes CSV file in the root directory
	unsigned long int temp=DATAsectorcount;
	unsigned char err=0;
	int i;
	CSV_file[CSVcounter+31]=temp/32768;			//highest byte
	temp=temp%32768;
	CSV_file[CSVcounter+30]=temp/128;			//2nd highest byte
	temp=temp%128;
	CSV_file[CSVcounter+29]=temp*2+DATAcounter/256; //3rd
	CSV_file[CSVcounter+28]=DATAcounter%256;	//lowest byte
	err=WriteMMC(CSVaddress,CSV_file);
}

⌨️ 快捷键说明

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