/*
	MGFLASH.C
	
	BL14xx/MicroG or BL15xx/MicroG2

	Write data to Flash eprom. Data space should have been previously
	allocated with:
	
	(1) initialized array
	i.e.  char buf[10] = {0};	// allocate 10 bytes of data in Flash
										// root code space
	The physical address of buf is given by function:

			phy_adr(buf);
			
	(2) initialized xdata
	i.e.	xdata xbuf {'0','0','0','0','0',};
										// allocate 5 byte of value '0' in Flash
										// xmem code space.
			"xbuf" is already in physical address form. 
*/

char yyy[150];       // allocate 150 bytes for uninitialized ram data
char zzz[150];       // allocate 150 bytes for uninitialized ram data
char xxx[150] = {0}; // initialized eprom data for 150 char bytes
							// data space is allocated from root code space
xdata kkk
{
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9',
	'0','1','2','3','4','5','6','7','8','9'
};    // initialized eprom xdata for 150 char bytes
		// data space is allocated from xmem code space

void ShowError(int error);
extern char OPMODE;

main()
{
int i;
int error;
	
	for(i=0;i<150;i++) yyy[i] = i;

	hitwd();
	printf("Before writing to flash location xxx.\n");
	printf("xxx[0]   >> %x, xxx[1]   >> %x\n", xxx[0], xxx[1]);
	printf("xxx[148] >> %x, xxx[149] >> %x\n", xxx[148], xxx[149]);

	xmem2root(kkk, zzz, 150);

	hitwd();
	printf("Before writing to flash location kkk.\n");
	printf("kkk[0]   >> %x, kkk[1]   >> %x\n", zzz[0], zzz[1]);
	printf("kkk[148] >> %x, kkk[149] >> %x\n", zzz[148], zzz[149]);

	hitwd();
	error = WriteFlash(phy_adr(xxx), yyy, 150);

	if(error<0) ShowError(error);      
	error = WriteFlash(kkk, yyy, 150);

	hitwd();
	if(error<0) ShowError(error);
	else {
		printf("\nAfter writing to flash locations xxx[0] to xxx[149].\n");
		printf("xxx[0]   >> %x, xxx[1]   >> %x\n", xxx[0], xxx[1]);
		printf("xxx[148] >> %x, xxx[149] >> %x\n", xxx[148], xxx[149]);

		hitwd();
		xmem2root(kkk, zzz, 150);     // has to access xdata with xmem2root
		printf("After writing to flash locations kkk[0] to kkk[149].\n");
		printf("kkk[0]   >> %x, kkk[1]   >> %x\n", zzz[0], zzz[1]);
		printf("kkk[148] >> %x, kkk[149] >> %x\n", zzz[148], zzz[149]);
	}

	hitwd();
	for(i=0;i<150;i++) yyy[i] = 0x77;   // try different values

	error = WriteFlash(phy_adr(xxx), yyy, 150);
	if(error<0) ShowError(error);      
	error = WriteFlash(kkk, yyy, 150);

	hitwd();
	if(error<0) ShowError(error);
	else
	{
		
		printf("\nAfter writing to flash locations xxx[0] to xxx[149].\n");
		printf("xxx[0]   >> %x, xxx[1]   >> %x\n", xxx[0], xxx[1]);
		printf("xxx[148] >> %x, xxx[149] >> %x\n", xxx[148], xxx[149]);

		hitwd();
		xmem2root(kkk, zzz, 150);
		printf("After writing to flash locations kkk[0] to kkk[149].\n");
		printf("kkk[0]   >> %x, kkk[1]   >> %x\n", zzz[0], zzz[1]);
		printf("kkk[148] >> %x, kkk[149] >> %x\n", zzz[148], zzz[149]);
	}
 
	yyy[0] = 0x55;       
	error = WriteFlash(phy_adr(xxx), yyy, 1);  // try 1 byte
	if(error<0) ShowError(error);      
	error = WriteFlash(kkk, yyy, 1);
	hitwd();
	if(error<0) ShowError(error);
	else
	{
		printf("\nAfter writing to flash location xxx[0].\n");
		printf("xxx[0]   >> %x, xxx[1]   >> %x\n", xxx[0], xxx[1]);
		printf("xxx[148] >> %x, xxx[149] >> %x\n", xxx[148], xxx[149]);
	
		hitwd();
		xmem2root(kkk, zzz, 150);
		printf("After writing to flash location kkk[0].\n");
		printf("kkk[0]   >> %x, kkk[1]   >> %x\n", zzz[0], zzz[1]);
		printf("kkk[148] >> %x, kkk[149] >> %x\n", zzz[148], zzz[149]);
	}
	
	error = WriteFlash(phy_adr(xxx+148), yyy, 1); // try 1 byte
	if(error<0) ShowError(error);      
	error = WriteFlash(kkk+148L, yyy, 1);
	hitwd();

	if(error<0) ShowError(error);
	else
	{
		printf("\nAfter writing to flash location xxx[148].\n");
		printf("xxx[0]   >> %x, xxx[1]   >> %x\n", xxx[0], xxx[1]);
		printf("xxx[148] >> %x, xxx[149] >> %x\n", xxx[148], xxx[149]);

		hitwd();
		xmem2root(kkk, zzz, 150);
		printf("After writing to flash location kkk[148].\n");
		printf("kkk[0]   >> %x, kkk[1]   >> %x\n", zzz[0], zzz[1]);
		printf("kkk[148] >> %x, kkk[149] >> %x\n", zzz[148], zzz[149]);
	}
	
	printf("\n\nFinished...");

	while(1)
	{
		hitwd();
		runwatch();
	}
}

// Decipher WriteFlash(...) return and print the message
void ShowError(int error)
{
	switch (error)
	{
		case -1:
					printf("\nFlash Eprom is not in used.\n");
					break;
		case -2:
					printf("\nFlash Eprom Address is in the Bios Area.\n");
					break;
		case -3:
					printf("\nFlash Eprom Address is in the Symbol Table Area.\n");
					break;
		case -4:
					printf("\nWrite timeout to the Flash.\n");
					break;
		default:
					printf("\nUnknown Message.\n");
	}
}




	
