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

📄 flash.c

📁 名厂车载GPS通信终端
💻 C
字号:
#define FLASH_GLOBAL

#include "includes.h"
#pragma ROM INIT_PHONE[]
	const uchar far INIT_PHONE[]={
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0', 
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		'8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',//for limit//
		'0','4','0','2','4','3','\0','\0','\0','\0','\0','\0','\0','\0','\0',
		'0','4','0','2','4','3','\0','\0','\0','\0','\0','\0','\0','\0','\0',  //added by leon 050923
		'+','8','6','1','3','8','0','0','7','5','5','5','0','0','\0',
		'+','8','6','1','3','8','0','0','7','5','5','5','0','0','\0',  //added by leon 050919
		/*'8','6','1','3','0','4','9','8','9','0','6','4','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','6','3','1','5','8','5','1','0','7','\0','\0',
		  '8','6','1','3','0','4','9','8','9','0','6','4','7','\0','\0',*/
		'0','7','5','5','8','3','5','2','2','9','6','8','\0','\0','\0',   
		'0','7','5','5','8','3','5','2','2','9','6','8','\0','\0','\0',	//added by leon 050915
		0x0d,0x0a};
		
#pragma ROM INIT_SIMCARDMANAGE[]
	const uchar far INIT_SIMCARDMANAGE[]={
			'1','3','8','0','0','0','0','0','0','0','0','\0','\0','\0','\0',//lxo 112411 for factory configure
//			'2','0','2','.','1','0','5','.','1','1','4','.' ,'2' ,'0' ,'3' ,'\0',//lxo 112412 for factory configure
//			'8','0','0','1','\0','\0',//lxo 112413 for factory configure
			'2','0','2','.','1','0','5','.','1','1','4','.' ,'2' ,'0' ,'6' ,'\0',//lxo 121401 for factory configure
//			'7','8','8','8','\0','\0',//lxo 121402 for factory configure//Removed 1231011
			'8','0','0','1','\0','\0',//lxo 112413 for factory configure//Added 1231012
			'c','m','n','e','t','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,
			'1','3','8','0','0','0','0','0','0','0','0','\0','\0','\0','\0',//lxo 112414 for factory configure
//			'2','0','2','.','1','0','5','.','1','1','4','.' ,'2' ,'0' ,'3' ,'\0',//lxo 112415 for factory configure
//			'8','0','0','1','\0','\0',//lxo 112416 for factory configure
			'2','0','2','.','1','0','5','.','1','1','4','.' ,'2' ,'0' ,'6' ,'\0',//lxo 121403 for factory configure
//			'7','8','8','8','\0','\0',//lxo 121404 for factory configure//Removed 1231021
			'8','0','0','1','\0','\0',//lxo 112413 for factory configure//Added 1231022
			'c','m','n','e','t','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
			0,0,
	};

#pragma ROM INIT_JUDGESIMCARD[]						   //modified by leon 051013
	const uchar far INIT_JUDGESIMCARD[]={
			0xe5,0x20,0xd5,0x04, 0xf3,0xeb,0x79,0x18,  //Port 0  //lxo 121501 change
		   	0x4f,0xfa,0xd4,0x04, 0xfd,0x2b,0x7a,0x18,
			0xb3,0xe9,0xd4,0x04, 0xc2,0xf8,0x79,0x18,
			0xbd,0xc8,0xd4,0x04, 0xce,0x3f,0x7a,0x18,
			0x12,0x9f,0xd4,0x04, 0x5d,0x8d,0x79,0x18,
			0x54,0xf5,0xcb,0x04, 0x58,0xe2,0x8b,0x18,			
			0xcd,0x3e,0xd6,0x04, 0x34,0x27,0x7d,0x18,  //Port 1 
		   	0x5a,0x23,0xd6,0x04, 0x3b,0x39,0x7d,0x18,
			0x96,0x1c,0xd6,0x04, 0x06,0x0b,0x7d,0x18,
			0xd6,0x0b,0xd6,0x04, 0xed,0x51,0x7d,0x18,
			0x3f,0x0c,0xd6,0x04, 0xf7,0xf9,0x7c,0x18,
			0x97,0x92,0xd5,0x04, 0x17,0x6c,0x7e,0x18,			
			0xb7,0xd7,0xd6,0x04, 0xf8,0x72,0x82,0x18,  //Port 2
			0x3c,0xc7,0xd6,0x04, 0x63,0x99,0x82,0x18,
			0x4c,0xc3,0xd6,0x04, 0xea,0x5f,0x82,0x18,
			0x8d,0xb1,0xd6,0x04, 0x5a,0x7b,0x82,0x18,
			0x34,0xb6,0xd6,0x04, 0x9c,0x1d,0x82,0x18,
			0x92,0x76,0xd6,0x04, 0x44,0x8c,0x82,0x18,
			/*0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 3
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 4
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 5  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 6
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 7
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 8
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  //Port 9
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,  
			0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, */
			3,   //port_number  ---//lxo 121502 change from 0 to 3
			0,0, //crc
	};

#pragma ROM INIT_ALARMTIME[]       
	const uchar far INIT_ALARMTIME[]={
			//250,60,60,60,250,60,60,60,60,60,60,60,
            60,60,60,60,240,60,60,60,60,60,60,60,
			0,0,
	};
#pragma ROM INIT_BASEVALUE[]
	const uchar far INIT_BASEVALUE[]={
			0x009,0x00,
			0x00,0x60,
			0x01,0x00,
			0x0,   
			0x2, //lxo 112417 for factory configure
			0x50,0xc3,
			20,
			240,
			1,
			10,
			0x0,0x0,
			0,
			0x0,0x0,
	};
	
#pragma ROM INIT_PWD[]
	const uchar far INIT_PWD[]={
			'8','8','8','8','8','8','8','8',
			0x0,0x0,
	};
	
	uchar *flashptr;
	
#define PARA_0 0x02
#define PARA_1 MAX_FLAG_NUM+0x10
#define PARA_2 PARA_1+2*sizeof(_SIMCARDMANAGE)+0X10
#define PARA_3 PARA_2+sizeof(_JUDGESIMCARD)+0x10
#define PARA_4 PARA_3+sizeof(_ALARMTIME)+0x10
#define PARA_5 PARA_4+sizeof(_PhoneNumber)+0x10 
#define PARA_6 PARA_5+sizeof(_BASE_VALUE)+0x10 
#define PARA_7 PARA_6+sizeof(_USERDEFINATION)+0x10
	
#define MAX_PARA_NUM       6
#pragma ROM flash_init[]
	const FLASH_INIT far  flash_init[]={
		{PARA_1,2*sizeof(_SIMCARDMANAGE),INIT_SIMCARDMANAGE},
		{PARA_2,sizeof(_JUDGESIMCARD),INIT_JUDGESIMCARD},
		{PARA_3,sizeof(_ALARMTIME),INIT_ALARMTIME},
		{PARA_4,sizeof(_PhoneNumber),INIT_PHONE},
		{PARA_5,sizeof(_BASE_VALUE),INIT_BASEVALUE},
		{PARA_6,sizeof(_USERDEFINATION),INIT_PWD},
	};
	

void Init_Flash(void)
{   
	flashptr = &save.flag;
	//#ifndef DEBUG_FLASH
	Read_All_Para();
	//#else
	//   Init_All_Para();
	//#endif
	ReadSoftInfo();
}

uchar Isflash_Valid(uchar sector)
{
	uchar i;
	
	for(i=0; i<MAX_FLAG_NUM; i++)
		if(Read_Sector_Byte(sector,PARA_0+i)!= i )return FALSE;
		
		return TRUE ;
}

void Write_Flash_Head(uchar sector)
{
	uchar i;
	
	for(i=0; i<MAX_FLAG_NUM; i++)
	{
		Write_One_Byte(&i,PARA_0+i,sector);
	}
}

uint Cal_Offer(uchar para)
{
	uint offer;
	uchar i;
	
	offer =1;
	
	for(i=0; i<para; i++)offer += flash_init[i].datalen;
	
	return offer;
}

uchar Read_Sector(uchar sector)
{
	uint i,j;
	uint  offer;
	uchar *ptr;
	
	for(i=0; i<MAX_PARA_NUM; i++)
	{
		offer = Cal_Offer(i);
		ptr   = flashptr+offer;  
		for(j=0; j<flash_init[i].datalen; j++)
		{
			ptr[j] = Read_Sector_Byte(sector,flash_init[i].start_addr+j);
		}
		if(!crc_check(ptr,flash_init[i].datalen)) return FALSE;
	}
	return TRUE ;
}

void Init_All_Para(void)
{
	uint i,j;
	uint  offer;
	uchar *ptr;
	
	for(i=0; i<MAX_PARA_NUM; i++)
	{
		offer = Cal_Offer(i);
		ptr   = flashptr+offer;  
		for(j=0; j<flash_init[i].datalen; j++)
		{
			if(flash_init[i].init!=NULL)
				ptr[j] = flash_init[i].init[j];
			else
				ptr[j] = 0;
		}
		crc_gen(ptr,flash_init[i].datalen);
	}
	Store_Paras();
}

uchar Read_All_Para(void)
{
	uchar flag;
	
	flag = 0;
	if(Isflash_Valid(USER_SECTOR))
	{
		if(!Read_Sector(USER_SECTOR))
		{
			if(Isflash_Valid(USER_BAK_SECTOR))
			{
				if(Read_Sector(USER_BAK_SECTOR))
				{
					flag = 1;
					Store_Sector(USER_SECTOR);
				}
			}
		}
		else
		{
			flag = 1;
			if(!Isflash_Valid(USER_BAK_SECTOR))
			{
				Store_Sector(USER_BAK_SECTOR);
			}
			/*else if(!Read_Sector(USER_BAK_SECTOR))
			{
			Read_Sector(USER_SECTOR);
			Store_Sector(USER_BAK_SECTOR);
		}*/
		}
	}
	else if(Isflash_Valid(USER_BAK_SECTOR))
	{
		if(Read_Sector(USER_BAK_SECTOR))
		{
			flag = 1;
			Store_Sector(USER_SECTOR);
		}
	}
	if(flag == 0)                  //两块扇区均错误,重新初始化
	{
		Init_All_Para();
		//Store_Paras();
	}
	return flag;
}

void Para_Crc(uchar para)
{
	uint offer;
	uchar *ptr;
	uint len;
	
	offer = Cal_Offer(para);
	ptr   = flashptr+offer;
	len   = flash_init[para].datalen;
	crc_gen(ptr,len);
}

void Store_Sector(uint sector)
{
	
	uint i,j;
	uint  offer;
	uchar *ptr;
	
	Erase_One_Sector(sector);
	for(i=0; i<MAX_PARA_NUM; i++)
	{
		offer = Cal_Offer(i);
		ptr   = flashptr+offer;  
		for(j=0; j<flash_init[i].datalen; j++)
		{
			Write_One_Byte(ptr+j,flash_init[i].start_addr+j,sector);
		}
	}
	Write_Flash_Head(sector);
}

void Store_Paras(void) //without crc
{
	Store_Sector(USER_SECTOR);
	Store_Sector(USER_BAK_SECTOR);
}

void Store_All_Para(void)
{
	uchar i;
	
	for(i=0; i<MAX_PARA_NUM; i++)
	{
		Para_Crc(i);
	}
	Store_Paras();
}

void WriteSoftInfo(void)
{
	uchar *ptr;
	uchar len;
	uchar i;
	
	ptr = (uchar *)soft.sn;
	len = sizeof(_SOFTWAREINFO);
	crc_gen(ptr,len);
	for(i=0; i<len; i++)
	{
		Write_One_Byte(ptr+i,0x20,SOFTWARESECTOR);
	}
	Write_Flash_Head(SOFTWARESECTOR);
}

uchar Readsoft(void)
{
	uchar *ptr;
	uchar len;
	uchar i;
	ptr = (uchar *)soft.sn;
	len = sizeof(_SOFTWAREINFO);
	
	if(Isflash_Valid(SOFTWARESECTOR))
	{
		for(i=0; i<len; i++)
		{
			ptr[i]=Read_Sector_Byte(SOFTWARESECTOR,0x20+i);
		}
		if(!crc_check(ptr,len)){ return FALSE;}
		else {return TRUE;}
	}
	return FALSE;
}

void ClearFlashCode(void)
{
	uchar i;
	
	for(i=0; i<MAXUPDATESECTOR; i++)
	{
		Erase_One_Sector(i+UPDATESECTOR);
	}
}

void InitSoftWare(void)
{
	uchar far *ptr;
	uchar i;
	uchar len;
	
	ptr = (UCHAR far *)(0xfe000);
	for(i=0; i<16; i++)
	{
		if(ptr[i]==0)break;
	}
	len = i;
	
	soft.crc1        = 0xffff;
	soft.crc2        = 0xffff;
	soft.curpack     = 0x00;
	soft.totalpack   = 0x00;
	for(i=0; i<len; i++)
	{
		soft.sn[i] = ptr[i];
	}
	for(i=len; i<0x10; i++)
	{
		soft.sn[i] = 0;
	}
	WriteSoftInfo();
}

void ReadSoftInfo(void)
{
	uchar far *ptr;
	//  uchar buf[16];
	uchar len;
	uchar flag=0;
	uchar i;
	
	ptr = (UCHAR far *)(0xfe000);
	for(i=0; i<16; i++)
	{
		if(ptr[i]==0)break;
	}
	len = i;
	
	if(Readsoft())
	{
		for(i=0; i<len; i++)
		{
			if(ptr[i]!=soft.sn[i])
			{
				flag = 1;
				break;
			}
		}
		if(flag==1)
		{
			gsmstatus.update = TRUE;
		}
		else
		{
			gsmstatus.update = FALSE;
		}
	}
	else
	{
		gsmstatus.update = FALSE;
		InitSoftWare();
	}
}

⌨️ 快捷键说明

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