void write_baseinfo(void)
{
	unsigned int startaddress = FIRMWARE_OFFSET;
	unsigned int data; 
	unsigned char info[16];
	unsigned char i;
	unsigned int buf[4] = {CMDSET_LENGTH,CMDSET_START_ADDR, WAVEFORM_LENGTH, FIRMWARE_OFFSET };
	unsigned char *Pcmd = (unsigned char *)buf;
	unsigned char temp;
	
	spi_lcm_exchange(BUS_TO_SPI);
	
	for(i=0; i < 16; i++)
	{
		flash_write_data(startaddress + i, Pcmd + i, 1);
	}
	for(i=0; i < 16; i++)
	{
		flash_read_data(startaddress + i,&temp, 1);
		
		if(temp != Pcmd[i])
		{
			APP_TRACE("write_baseinfo [%08x] = %02x -> %02xerror\n",startaddress + i,  temp,Pcmd[i] );
			
		}
	}	
	spi_lcm_exchange(BUS_TO_LCM);	
	
}	
void write_cmdset(void)
{
	unsigned long int i = 0;
	unsigned long int startaddress = CMDSET_START_ADDR;
	unsigned char *Pcmd = cmdset;
	unsigned char temp;
	
	total_len = 0;
	error_len = 0;
	spi_lcm_exchange(BUS_TO_SPI);
	for(i=0; i < CMDSET_LENGTH; i++)
	{
		flash_write_data(startaddress + i, Pcmd + i, 1);
	}
	
	total_len = CMDSET_LENGTH;
	
	for(i=0; i < CMDSET_LENGTH; i++)
	{
		flash_read_data(startaddress + i,&temp, 1);
		
		if(temp != Pcmd[i])
		{
			APP_TRACE("write_cmdset [%08x] = %02x -> %02x   error\n",startaddress + i,  temp,Pcmd[i] );
			error_len ++;
			
		}
	}	
	spi_lcm_exchange(BUS_TO_LCM);
	APP_TRACE("write_cmdset is OK! %d, %d \r\n", total_len, error_len);
}
/**************************************************************************//**
* @brief    Writes data after the last occupied location and returns the updated
* 			value for the last occupied location
*
* @param	data	is the pointer to the data that will be written to the flash
* @param	length	is the length of the data that will be written to the flash
*
* @return   \b flash_write_count 	is the last occupied location
******************************************************************************/
unsigned int
flash_write_wear_level(unsigned int *data, unsigned int length)
{

	unsigned int ii, index;
	unsigned int flash_write_count;

	flash_read_data(&flash_write_count, 0 ,1);
	switch(flash_write_count){
	// the case of a un-initialized array, brandnew device
	case 0xFFFF:
		flash_write_count = 1;
		flash_write_data(&flash_write_count, 0 ,1);
		flash_write_data(data, 1 ,length);
		break;
		// the case of having reached 50000 writes, reset the counter to 1
	case 0xC350:
		flash_write_count = 1;
		flash_erase_segment(0, SIZE_OF_STORAGE_ARRAY);
		flash_write_data(&flash_write_count, 0 ,1);
		flash_write_data(data, 1 ,length);
		break;
		// Normal operation, find the end of the current array, store new data
	default:
		// search the array for the end of the flash storage array, look for 0xFFFF
		for(ii=0;ii<SIZE_OF_STORAGE_ARRAY;ii=ii+length+1)
		{
			flash_read_data(&flash_write_count, ii ,1);
			if(flash_write_count == 0xFFFF)
			{
				flash_read_data(&flash_write_count, 0 ,1);
				index = ii;
				ii = SIZE_OF_STORAGE_ARRAY;
			}
		}
		// before writing to segment, check to see if we are out of bound
		if(index+length+1 > SIZE_OF_STORAGE_ARRAY )
		{
			// if we have reached the end, delete the entire array, increment counter and restart
			flash_read_data(&flash_write_count, 0 ,1);
			flash_erase_segment(0, SIZE_OF_STORAGE_ARRAY);
			flash_write_count++;
			flash_write_data(&flash_write_count, 0 ,1);
			flash_write_data(data, 1 ,length);
		} else
		{
			// Normal write to Flash array
			flash_write_data(&flash_write_count, index ,1);
			flash_write_data(data, index+1 ,length);
		}
		break;
	}
	return flash_write_count;
}
void flash_write_test()
{
	int i;
	int j;
	uint8_t buf[256];
	uint8_t rbuf[256];
	for(i = 0; i < 256; i++)
	{
		buf[i] = i;
	}
	flash_write_data(0x000000, buf, 256);
	flash_read_data(0x000000, rbuf, 256);
	for(i = 0; i < 256; i++)
	{
		if(i == rbuf[i])
		{
			continue;
		}
	}
}
void write_Data(unsigned int startaddress, char *Pcmd, unsigned Len)
{
	int SendLen;
	
	while(Len>0)
	{
		if(Len>=MAX_DATA_BUFFER_LENTH)
		{
			SendLen=MAX_DATA_BUFFER_LENTH;
		}
		else
		{
			SendLen=Len;
		}
		flash_write_data(startaddress, Pcmd, SendLen);
		Pcmd+=SendLen;
		startaddress+=SendLen;
		Len-=SendLen;


	}
}
uint32_t write_config_block(void)
{
    configData.config.validBlock = VALID_BLOCK;
    return flash_write_data((uint32_t*)configDataBlock.data,
                          configData.data, sizeof(configData.config));
}
void write_image(void)
{

	unsigned long int i = 0;
	unsigned long int len;
	unsigned long int startaddress = 0;
	unsigned char *Pcmd = gImage_image;
	unsigned char temp;
	
	unsigned char times;
	len = 0;
	error_len = 0;
	
	if(image_p == 0xff)
		return;	
	
	startaddress = IMAGE_START_ADD + 300*16*(image_p -1);
	spi_lcm_exchange(BUS_TO_SPI);
	
	if(image_p< 2)
		{
		  len = 300 * 16;
	  }
	else
	{
		len = IMAGE_LENGTH   -  300*16 * (image_p -1);
	}
	
	total_len = len;
	
	for(i=0; i < len; i++)
	{
		times = 0;
		flash_write_data(startaddress + i, Pcmd + i, 1);
		
		
		do{
		flash_read_data(startaddress + i,&temp, 1);
		
			if(temp != Pcmd[i])
			{
				flash_write_data(startaddress + i, Pcmd + i, 1);
				APP_TRACE("error addr: %08x\n", startaddress + i);
			}

		}while(times++ < 0);
		
	}
	
	
	for(i=0; i < len; i++)
	{
		flash_read_data(startaddress + i,&temp, 1);
		
		if(temp != Pcmd[i])
		{
			APP_TRACE("\r\nwrite_wav [%08x] = %02x -> %02xerror\r\n",startaddress + i,  temp,Pcmd[i] );
			error_len++;
			
		}
	}
	
	
	spi_lcm_exchange(BUS_TO_LCM);		

	APP_TRACE("write_image %d is OK! %d, %d \r\n",image_p, total_len, error_len);
	
}
void write_wav(void)
{

	unsigned long int i = 0;
	unsigned long int len;
	unsigned long int startaddress = 0;
	unsigned char *Pcmd = (unsigned char *)wfm_set;
	unsigned char temp;
	
	unsigned char times;
	
	error_len = 0;
	if(waveform_p == 0xff)
		return;	
	
	startaddress = WAVFORM_START_ADDR + 200*16*(waveform_p -1);
	spi_lcm_exchange(BUS_TO_SPI);
	
	if(waveform_p < 23){
		len = 200 * 16;
	}
	else
	{
		len = WAVEFORM_LENGTH -  200*16 * (waveform_p -1);
	}
	
	total_len = len;
	
	for(i=0; i < len; i++)
	{
		times = 0;
		flash_write_data(startaddress + i, Pcmd + i, 1);
		
		
		do{
		flash_read_data(startaddress + i,&temp, 1);
		
			if(temp != Pcmd[i])
			{
				flash_write_data(startaddress + i, Pcmd + i, 1);
				APP_TRACE("error addr: %08x\n", startaddress + i);
			}

		}while(times++ < 0);
		
	}
	
	
	for(i=0; i < len; i++)
	{
		flash_read_data(startaddress + i,&temp, 1);
		
		if(temp != Pcmd[i])
		{
			//APP_TRACE("\r\nwrite_wav [%08x] = %02x -> %02xerror\r\n",startaddress + i,  temp,Pcmd[i] );
			error_len++;
			
		}
	}
	
	
	spi_lcm_exchange(BUS_TO_LCM);		

	APP_TRACE("write_wav is OK! %d, %d \r\n", total_len, error_len);
	
}