You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1326 lines
32 KiB

/************************************************************************
* Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
* @file    	main.c
* @author  	UartTFT Application Team
* @version  V1.0.0
* @date     2023-02-24
* @brief 	This file include main function.
*************************************************************************/
#include <stdio.h>
#include "sys.h"
#include "uart.h"
#include "delay.h"
#include "data.h"
#include "common.h"
#include "usb_const.h"
#include "string.h"
//#include "levetop.h"
#include "eflash_drv.h"
#include "cache_drv.h"
#include "spi_drv.h"
#include "eport_drv.h"
#include "ccm_drv.h"
#include "LT768_Lib.h"
#include "ioctrl_drv.h"
#include "pit32_drv.h"
#include "pwm.h"
#include "pit.h"
#include "dac.h"
#include "flash.h"
//#include "bsp.h"
#include "ctp.h"
#include "rtp.h"
#include "math.h"
#include "iomacros.h"
#include "wdt_drv.h"
#include "usb_drv.h"
#include "usb_const.h"
#include "UI_H.h"
#include "OTA_H.h"
#include "meg.h"
#include "CRC.h"
#include "cpm_drv.h"
#include "Updata.h"
#include "flash.h"
#include "LT768_Lib.h"
#include "Uart_Vcom.h"
#include "OTA_H.h"
extern unsigned int gBaudrate;
extern void my_APP(u32 appxaddr);
uint8_t  hid_rec_index;
/******************my debug**************************/
unsigned char ota_lt7689_flag = 0;
unsigned char ota_lt7689_flag1 = 0;
unsigned char ota_lt7689_flag2 = 0;
unsigned char ota_lt7689_buf1[512];
unsigned char ota_lt7689_buf2[512];
unsigned int  ota_lt7689_length1 =0;
unsigned int  ota_lt7689_length2 =0;
unsigned int  ota_address = 0;
#define LT_APP  0x08010000
unsigned int 	ota_offset = 0;
/********************************************/
static void Printf_Version(void)
{
	printf("/**************************************************/\n");
	printf(" date = %s,time = %s    \r\n", (char *)__DATE__, (char *)__TIME__);
	printf(" LT7689_LoadDown_Uart_V1.01  \r\n");
	printf(" system clk = %dMHz         \r\n", g_sys_clk / 1000000);
	printf(" ips clk    = %dMHz         \r\n", g_ips_clk / 1000000);
	printf("/**************************************************/\n");
}
extern UINT8 hid_rx_buf[512];
/******************bootloader****************************/
char showbuff[9];
void display_manu(u8 sd_ok)
{
		//提供 5M晶振
		PWM_OutputInit(PWM_PORT1,2,PWM_CLK_DIV_1,4,2,0);     //更改了库函数,根据实际设定打开PWM,作用提供LT768的晶振
							
		Parallel_Init();
		LT768_Init(); 
		
#if MCU_SPI		
		*(volatile unsigned char*)(0x40019000+0x03) |= (1<<(6));				//EPORT_ConfigGpio(EPORT_PIN6,GPIO_OUTPUT);EPORT6和LT768——TEST1连接
		*(volatile unsigned char*)(0x40019000+0x05) |= (1<<(6));				//EPORT_WriteGpioData(EPORT_PIN6,Bit_SET);
		MUC_SS_flag = 1;  //先SPI1.SS;
		SPI1_Flash_Init();
#if UartTFT_Flash_0
		MUC_CS0_OUTPUT
		MUC_CS0_SET
#endif	
		
#else 
		*(volatile unsigned char*)(0x40019000+0x03) |= (1<<(6));			//EPORT_ConfigGpio(EPORT_PIN6,GPIO_OUTPUT);EPORT6和LT768——TEST1连接
		*(volatile unsigned char*)(0x40019000+0x05) &= ~(1<<(6));			//EPORT_WriteGpioData(EPORT_PIN6,Bit_RESET);
		Close_SPI1();
		LT768_SPI_Init(1,1);    //先设定LT768的CS1
	
#endif	
		Main_Image_Start_Address(0);				
		Main_Image_Width(LCD_XSIZE_TFT);
		Main_Window_Start_XY(0,0);
		Canvas_Image_Start_address(0);
		Canvas_image_width(LCD_XSIZE_TFT);
		Active_Window_XY(0,0);
		Active_Window_WH(LCD_XSIZE_TFT,LCD_YSIZE_TFT);
		
		if(sd_ok)					LT768_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,Back_color);
		else								LT768_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,Blue2);
		LT768_Select_Internal_Font_Init(24,1,1,0,0);
		LT768_PWM1_Init(1,0,5,800,400);
		
		Display_ON();            // 开启显示
}
char  Flash_find(void)
{
		
		Flash_ID = W25QXX_ReadID();Flash_type = 0;
		if(Flash_ID == 0xef13) {strcpy(showbuff,"W25Q80");} 
		else if(Flash_ID == 0xef14)	{strcpy(showbuff,"W25Q16");} 
		else if(Flash_ID == 0xef15)	{strcpy(showbuff,"W25Q32");} 
		else if(Flash_ID == 0xef16)	{strcpy(showbuff,"W25Q64");} 
		else if(Flash_ID == 0xef17)	{strcpy(showbuff,"W25Q128");} 
		else if(Flash_ID == 0x5e17)	{strcpy(showbuff,"ZB25VQ128");} 
		else if(Flash_ID == 0xef18)	{strcpy(showbuff,"W25Q256");W25Q256 = 1;W25QXX_Enter_4Byte_AddressMode();} 
		else
		{
			Flash_ID = W25N01GV_ReadID();
	//		printf("%x \r\n",Flash_ID);
			if(Flash_ID == 0xefaa21){ Flash_type = 1;strcpy(showbuff,"W25N01GV");} 
			else 					{ Flash_type = 2;strcpy(showbuff,"No Flash");}
		}
	//	printf("%x \r\n",Flash_ID);
		LT768_DrawSquare_Fill(10,LINE0,300,LINE0+L_OFFSET,Back_color);
		LT768_Print_Internal_Font_String(10  ,LINE0,Black,White,"Flash Model:");
//		LT768_Print_Internal_Font_String(144,LINE0,File_name_color,White,showbuff);	
		
		if(Flash_type == 2)
		{
			LT768_Print_Internal_Font_String(164,LINE0,Warm_color,White,showbuff);	
			return 1;
		}
		else
		{
			LT768_Print_Internal_Font_String(164,LINE0,Black,White,showbuff);	
			return 0;
		}
}
typedef  void (*iapfun)(void);
iapfun jump2app; 
void APP(u32 appxaddr)
{
//		printf("***enter app()***\r\n");
		Disable_Interrupts;
		__set_PRIMASK(1);
	
		jump2app = (iapfun)*(vu32*)(appxaddr + 4);
		
		MSR_MSP(*(vu32*)appxaddr);
//		printf("***ready jumpapp()***\r\n");
//		DCACHE_Init(cacheOff, cacheOff, cacheOff, cacheOff);
//		ICACHE_Init(cacheOff, cacheOff, cacheOff, cacheOff);
		WDT_FeedDog();
		DelayMS(10);
//		printf("***ready jumpapp()***\r\n");
		jump2app();						
//		printf("***ready jumpapp() fail\r\n");
		Enable_Interrupts
}
void SCI1_Init(uint32_t pclk, uint32_t bound,uint8_t parity)	
{
			u32 band_rate=0;
			SCI1->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f;
			band_rate =(pclk*4/bound)>>6;
			SCI1->SCIBDH=(u8)((band_rate>>8)&0x00ff);
			SCI1->SCIBDL=(u8)(band_rate&0x00ff);
			SCI1->SCICR2 |= (1<<2)|(1<<3)|(1<<5);// |(1 << 4)
			NVIC_Init(1, 1, SCI1_IRQn, 2);
}
void SCI2_Init(uint32_t pclk, uint32_t bound,uint8_t parity)	
{
			u32 band_rate=0;
			SCI2->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f;
			band_rate =(pclk*4/bound)>>6;
			SCI2->SCIBDH=(u8)((band_rate>>8)&0x00ff);
			SCI2->SCIBDL=(u8)(band_rate&0x00ff);
			SCI2->SCICR2 |= (1<<2)|(1<<3)|(1<<5);
			NVIC_Init(0, 0, SCI2_IRQn, 2);
}
void SCI3_Init(uint32_t pclk, uint32_t bound,uint8_t parity)	
{
			u32 band_rate=0;
			SCI3->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f;
			band_rate =(pclk*4/bound)>>6;
			SCI3->SCIBDH=(u8)((band_rate>>8)&0x00ff);
			SCI3->SCIBDL=(u8)(band_rate&0x00ff);
			SCI3->SCICR2 |= (1<<2)|(1<<3)|(1<<5);//|(1 << 4)
			NVIC_Init(0, 0, SCI3_IRQn, 2);
}
int fputc(int ch, FILE *f)
{
	while ((SCI1->SCISR1 & 0x40) == 0x00)
	{
	};
	SCI1->SCIDRL = (uint8_t)ch; //SCI1->SCIDRL = (uint8_t)data;
	
//	while ((SCI2->SCISR1 & 0x40) == 0x00)
//	{
//	};
//	SCI2->SCIDRL = (uint8_t)ch; //SCI1->SCIDRL = (uint8_t)data;
	return ch;
}
void SCI2_IRQHandler(void)
{
//	uint8_t ch = 0;
	if ((SCI2->SCISR1 & 0x20) == 0x20)
	{
//		ch = SCI2->SCIDRL;
//		if (Rx_Count_long > 4096 + 2)
//			Rx_Count_long = 0;
//		Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = ch;
//		Rx_Num++;
//		mcu_buf[mcu_buf_length] = ch ;
//		mcu_buf_length ++ ;
		
		
	}
		if((SCI2->SCISR1 & 0x10) == 0x10) //空闲中断
	{
			//清除标志位
				uint8_t clear_idle_flag;
				clear_idle_flag	=	SCI2->SCIDRL;
				clear_idle_flag	= SCI2->SCISR1;
//				uart1_idle_flag = 1;		
	}
}
#if 0
void SCI1_IRQHandler(void)
{
//	u8 cjc=0;
	WDT_FeedDog();
	if((SCI1->SCISR1 & 0x20)== 0x20)
	{
//		cjc = SCI1->SCIDRL;
//		LT_SingleByteToPc_uart3(cjc);
//		uart_buf[uart_rx_number++] = SCI1->SCIDRL;
//			if(ota_lt7689_flag1==0)
//			{
				ota_lt7689_buf1[ota_lt7689_length1++]=SCI1->SCIDRL;
//			}
//			else if(ota_lt7689_flag2==0)
//			{
//				ota_lt7689_buf2[ota_lt7689_length++]=SCI1->SCIDRL;
//			}
//			printf("%x ",ota_lt7689_buf[ota_lt7689_length]);
//			ota_lt7689_length++;
//			ota_offset++;
			
//				if(uart_rx_number>(4096+512))
//							uart_rx_number =0 ;
//				PIT1_reload();
	}
	
		if((SCI1->SCISR1 & 0x10) == 0x10) //空闲中断
	{
			//清除标志位
				uint8_t clear_idle_flag;
	
				
		
				clear_idle_flag	=	SCI1->SCIDRL;
				clear_idle_flag	= SCI1->SCISR1;
				ota_lt7689_flag = 1;
				
	}
	
}
#endif
extern unsigned char app_flag;
/******************************************************************/
extern uint8_t Rx_Buffer_short[512] ;
extern uint8_t Rx_Buffer1_short[512] ;
extern uint16_t Rx_Count_short ;
unsigned int number1 =0;
unsigned int number =0;
//unsigned int time = 0;
extern unsigned char ui_buf[2050];
extern  unsigned int ui_offset;
unsigned int offset=0;
extern unsigned short block ;
extern unsigned int page ;
extern unsigned int ui_addr;
extern unsigned char last_flag;
extern unsigned char dma_flag;
//unsigned char flash[2050] = {0};
int main(void)
{
	NVIC_SetVectorTable(0x800A000, 0);//0x800A000 0x8010000
//		NVIC_SetVectorTable(0x800F800, 0);
	Sys_Init();
	Enable_Interrupts;
	SCI1_Init(g_ips_clk,921600 ,0);	//115200 921600
//	SCI2_Init(g_ips_clk, 115200,0);	
	SCI3_Init(g_ips_clk,38400 ,0);
	/********************升级*******************/
	ota_address = LT_APP;
	Make_CRC32_Table();
	ota_address = 0x08010000;
	printf("***bootloader1!!**\r\n");
//	SCI3_DMA_Init(g_ips_clk,38400);
//SCI3_DMA_Init(g_ips_clk,921600);//921600 38400
#if 0	
	while(1)
	{
			WDT_FeedDog();
			printf("***bootloader3***\r\n");
			printf("***Sd_card_exist=%d***\r\n",Sd_card_exist);
		printf("***CPM_PADWKINTCR=%d***\r\n",CPM->CPM_PADWKINTCR);
		printf("***CPM_PADWKINTCR&=%d***\r\n",CPM->CPM_PADWKINTCR&0x02000000);
	//通过检测wake up脚电平来判断SD card 是否有插入 
	if((CPM->CPM_PADWKINTCR&0x02000000)==0)   //0:有插入
	{
		printf("enter if 1\r\n");
		Sd_card_exist = 1;
//		CPM->CPM_PADWKINTCR |= 0x02;
	}
//	else if((CPM->CPM_PADWKINTCR&0x02000000)==0)   //0:有插入
//	{
//		printf("enter if 2\r\n");
//		Sd_card_exist = 1;
////		CPM->CPM_PADWKINTCR |= 0x02;
//	}
	DelayMS(1000*3);
	Sd_card_exist = 1;
	printf("***Sd_card_exist=%d***\r\n",Sd_card_exist);
	if(Sd_card_exist)
	{
		printf("Sd_card_exist!\r\n");
		printf("Sd_card_exist!\r\n");
		display_manu(1);
		
		signed char res = 0;		
		LT768_Print_Internal_Font_String(10,LINE1,File_name_color,White,"1: UartTFT-II_Flash.bin");
	
		LT768_Print_Internal_Font_String(10,LINE2,File_name_color,White,"2: MCU_Code.bin");
		LT768_Print_Internal_Font_String(310,LINE0,File_name_color,White,"V2.00");
#if UartTFT_Flash_0
		LT768_Print_Internal_Font_String(0,LINE4,File_name_color,White,"3: UartTFT_Flash_0.bin");
#endif
		
//		if(Flash_find()==0)  //识别出了Flash 才操作
//		{
//			printf("***find flash****\r\n");
//			res = Flash_Updata("0:/UartTFT_Flash/UartTFT-II_Flash.bin",1);
//			if(res==FR_NO_FILE||res==FR_NO_PATH)	res = Flash_Updata("0:/UartTFT_Flash_No_CRC/UartTFT-II_Flash.bin",2);
//			if(res == 0) //UartTFT-II_Flash OK
//			{
//				LT768_Print_Internal_Font_String(370,LINE1,OK_color,White,"OK");
//				LT768_Print_Internal_Font_String(310,LINE1,Progress_color,White,"100%");
//				Download_flag++;
//			}
//			else if(res>=1 && res <=19) //FatFs 故障
//			{
//				strcpy(showbuff,"None.");
//				sprintf(&showbuff[5],"%d",res);
//				LT768_Print_Internal_Font_String(310,LINE1,None_color,White,showbuff);
//			}
//			else if(res == -1) //CRC 错误
//			{
//				LT768_Print_Internal_Font_String(370,LINE1,NG_color,White,"NG");
//				LT768_Print_Internal_Font_String(46 ,LINE5,Black,White,"NG: CRC error");
//				LT768_Print_Internal_Font_String(10 ,LINE4,Black,White,"Result:");
//				LT768_Print_Internal_Font_String(96,LINE4,Red,White,"Failed");
//				while(1) WDT_FeedDog();
//			}
//			else if(res == -2)  //BBM 错误
//			{
//				LT768_Print_Internal_Font_String(370,LINE1,NG_color,White,"NG");
//				LT768_Print_Internal_Font_String(46 ,LINE5,Black,White,"NG: BBM error");
//				LT768_Print_Internal_Font_String(10 ,LINE4,Black,White,"Result:");
//				LT768_Print_Internal_Font_String(96,LINE4,Red,White,"Failed");
//				while(1) WDT_FeedDog();
//			}
//			printf("******res = %d \r\n",res);
//		}
//		else
//		{
//			LT768_Print_Internal_Font_String(10 ,LINE6,Black,White,"Result:");
//			LT768_Print_Internal_Font_String(96,LINE6,Red,White,"Fail");
//			printf("***enter esle****\r\n");
//			while(1) WDT_FeedDog();
//		}
		
#if UartTFT_Flash_0	
		
#if MCU_SPI 
#else
#endif
#endif
		printf("next APP_Updata!!!! \r\n");
		res = APP_Updata("0:/MCU_Code/MCU_Code1.bin");		
		
		if(res == 0) //UserInfo OK
		{
			LT768_Print_Internal_Font_String(370,LINE2,OK_color,White,"OK");
			LT768_Print_Internal_Font_String(310,LINE2,Progress_color,White,"100%");
			Download_flag++;
		}
		else if(res>=1 && res <=19) //FatFs 故障
		{
			strcpy(showbuff,"None.");
			sprintf(&showbuff[5],"%d",res);
			LT768_Print_Internal_Font_String(370,LINE2,None_color,White,showbuff);
		}
		else if(res == -1) //CRC 错误
		{
			LT768_Print_Internal_Font_String(370,LINE2,NG_color,White,"NG");
			LT768_Print_Internal_Font_String(46 ,LINE5,Black,White,"NG: CRC error");
			LT768_Print_Internal_Font_String(10 ,LINE4,Black,White,"Result:");
			LT768_Print_Internal_Font_String(96,LINE4,Red,White,"Failed");
			while(1) WDT_FeedDog();
		}
		if(Download_flag == 0)
		{
			LT768_Print_Internal_Font_String(10 ,LINE4,Black,White,"Result:");
			LT768_Print_Internal_Font_String(96,LINE4,Red,White,"Failed");
		}
		else
		{
			LT768_Print_Internal_Font_String(10 ,LINE4,Black,White,"Result:");
			LT768_Print_Internal_Font_String(96,LINE4,OK_color,White,"OK");
		}
		if(LCD_XSIZE_TFT >= 800)
		{
			LT768_Print_Internal_Font_String(10,LINE5,Warm_color,White,"Remove the SD card to enter the main program");
		}
		else
		{
			LT768_Print_Internal_Font_String(10,LINE5,Warm_color,White,"Remove the SD card");
			LT768_Print_Internal_Font_String(10,LINE6,Warm_color,White,"  to enter the main program");
		}
		printf("res =%d  \r\n",res);
		while(1)
		{
			if((CPM->CPM_PADWKINTCR&0x02000000) != 0)   //0:有插入
			{
					printf("***ready enter app***\r\n");
				
					APP(APP_ADDR);
		//		CPM->CPM_PADWKINTCR |= 0x02;
			}
			WDT_FeedDog();
		}
	
				
		
	}	
}
#endif
	#if 0
	
	/********************************************/
	PIT2_Init(23438);
	WDT_Init(0x4786*2);	
//		APP(0x8000000);
	
//	Uart3_Init(g_ips_clk, 115200);
	
	/*
	uint8_t mcu_package_buf[30];
	uint8_t cmd  = 0x10 ; 
	uint16_t  adress = 0x0901 ; 
	uint16_t  data[3] =  {0x0020,0x0010,0x1314};
	uint8_t data_amount = 3;
	send_mcu_package( mcu_package_buf , //数据包地址
										 cmd , 		//指令码
										  adress , // 变量地址
										 data , 	// 数据 2字节
										 data_amount);	//数据个数
	
	*/
//		for(unsigned char i = 0 ; i < 20 ; i++)
//	printf("flash: %x\r\n",EFLASH_ByteRead(flh_sAddr1 + i));
//	SaveData();
//GetAdjdata();
//	for(unsigned char i = 0 ; i < 20 ; i++)
//	printf("flash: %x\r\n",EFLASH_ByteRead(flh_sAddr1 + i));
//	printf("flash%x \r\n",*((unsigned int *)0x0807D800));
//	SaveData();
//	GetAdjdata();
//	printf("flash%x \r\n",*((unsigned int *)0x0807D800));
//	printf("flash%x \r\n",*((unsigned char *)(0x0807D800)));
//	printf("flash%x \r\n",(*(volatile UINT8 *)(0x0807D800)));
	//(*(volatile UINT8 *)(addr))
//	EFLASH_PageErase(0x0807D800);
//	printf("flash%x \r\n",EFLASH_ByteRead(0807D800));
//					unsigned short test = 0x0064;
//					unsigned short * test_p = &test;
//					printf("test = %x\r\n",test);
//					printf("test0 = %x test1 = %x\r\n",*((unsigned char *)test_p),*(((unsigned char *)test_p)+1));
//					
//					unsigned char test_array[2];
//						test_array[0] = test ;
//						test_array[1] = *((unsigned char *)&test+1 );
//						printf("array0 = %x array1 = %x\r\n",test_array[0],test_array[1]);
//					unsigned short addr = 0x0040;
//					unsigned char data[4];
//					data[0] = var[2 * (addr + 0)];
//					data[1] = var[2 * (addr + 0) + 1];
//					data[2] = var[2 * (addr + 0) + 2];
//					data[3] = var[2 * (addr + 0) + 3];
//					printf("\r\n ***0:%d 1:%d 2:%d 3:%d*** \r\n",data[0],data[1],data[2],data[3]);
//					printf("\r\n ***var:%d *** \r\n",var[2 * (addr + 0)]);
//					data[1] = 100;
//					var[2 * (addr + 0)] = data[0];
//					var[2 * (addr + 0) + 1] =data[1];
//	printf("***APP-A***\r\n");
//	printf("ID=%x\r\n",(*(volatile UINT32 *)(0x20000000+0x37000)));
	
//	printf("EFLASH=%x\r\n",(EFLASH_WordRead(0x8009E00)));
	
	device_working_status_buff.wifi_status = 1;
	printf("wifi_status=%d\r\n",device_working_status_buff.wifi_status);
//0x8043A00
//	0x800A000		0x8043A00   180kB
//	SaveData();
	WDT_FeedDog();
//	printf("OK!!\r\n");
//	printf("\r\n*******after********\r\n");
//	EFLASH_Init(g_sys_clk / 1000);
//	EFLASH_SetWritePermission();
//	EFLASH_PageErase(0x08043A00);
//	EFLASH_ClrWritePermission();
//	GetAdjdata(0x08043A00);
//	NVIC_SetVectorTable(0x08043A00, 0);
//	PeripheralInit();
//	PIT2_Init(23438);
//	WDT_Init(0x4786*2);
//	printf("*****APP-2*****\r\n");
//	printf("***enter APP_B***\r\n");
//	my_APP(0x8043A00);
	while(1)
	{
		
		WDT_FeedDog(); //Clear watchdog
//	printf("*****APP-2!!*****\r\n");
#if !IIC_BUS
#if (UARTBUS_OPTION == 0)
		Main_From_UartRxBuff();
		if (gUsartRx.Flag)
		{
			LT_ReceiveCmd(gUsartRx.Buf);
		}
		if(UI_update == 1)
		{ 
			while(1)
			{
				WDT_FeedDog(); //Clear watchdog;
				Main_From_UartRxBuff();
				if (gUsartRx.Flag)
				LT_ReceiveCmd(gUsartRx.Buf);
				
				if(UI_update == 0) break;
			}
		}
#elif (UARTBUS_OPTION == 1)
		LT_ModBus_REG_Cmd();
#elif (UARTBUS_OPTION == 2)
		if (Sum_ModbusTX)
			Uart_cmd_Send();
		LT_ModBus_REG_Cmd();		
#elif (UARTBUS_OPTION == 3 || UARTBUS_OPTION == 4)
		
		Hid_Main_From_UartRxBuff();
		if (gHidRx.Flag)
			LT_Hid_ReceiveCmd(gHidRx.Buf);	
		if(UI_update == 1)
		{ 
			while(1)
			{
				WDT_FeedDog(); //Clear watchdog;
				Hid_Main_From_UartRxBuff();
				if (gHidRx.Flag)
					LT_Hid_ReceiveCmd(gHidRx.Buf);	
				
				if(UI_update == 0) break;
			}
		}
#else
#endif
#endif
#if IIC_BUS
		// Based on the IIC protocol, executes the functions of the registers
		LT_IIC_REG_Cmd();
#endif
		if (var[0x700e * 2 + 1])
			BackLight_control(); // Backlight control
		if (page_refreshing > 20)
		{
			page_refreshing = 0;
			Get_date_variable_Id_Regular_update(pic_id);
		}
		if(Count_reg_num > 0)
		{
			Count_reg_num--;
			reg_operation(Count_reg[Count_reg_num]);
		}
		if (gWavFlag == 1 || gWavType == 1)
			LT_PlayWav_DAC();
#if !IIC_BUS
		/*The current hardware that CTP and IIC slave use the same set of IOs
		If you use the IIC slave function, you need to block the touch function first*/
		gTpInfo.scan();
// 	打印触摸屏的坐标
//		printf("gTpInfo.x[0]=%d gTpInfo.y[0]=%d gTpInfo.x[1]=%d gTpInfo.y[1]=%d\r\n" 
//				,gTpInfo.x[0],gTpInfo.y[0],gTpInfo.x[1],gTpInfo.y[1]);
#endif
		
#if 1
		
		
		if(all_flag == 1)
		{
			all_flag = 0;
			debug_flag();		
		}
		
		if(status_flag ==1)
		{
			status_flag = 0;
			debug_status();
		}
		
#endif
#if 0		
		show_gif();				// gif
		show_Clock();	 		// Analog clock
		show_RTC();		 		// Digital clock
		scroll_text();	 		// Text scroll1
		Display_Curve(); 		// Curve
		show_Count();			// Time counter
#endif
		Check_Var_Count();
		Check_Pointer();
		
#if encoder_on
		if (gEncoderFlag)
			encoder();
#endif
		if (Ges_sc_flag)
			slide_screen();		// gesture_sliding
		if (Gesture_flag)
			Gesture_touch(); 	// gesture_no_sliding
		
		Basic_touch();			// Basic touch control
		Adj_touch();		// Variable adjustment
		slideMune();			// Slide menu
#if 0	
		
		Progress_bar_sliding();	// Sliding progress bar
		data_input();			// Data input
		RingSld_touch();		// Ring progress bar with touch
		Ascii_input();			// ASCII keyboard
		GBK_input();			// GBK keyboard
		VarKey_touch();			// Multivariable adjustment
		pop_up_touch();			// Pop-up window
#endif
		
		/**** Screen test ****/
//		if (scr_flag == 1 && scr_time > 100)
//		{
//			scr_time = 0;
//			Detection_screen(scr_flag) ;
//		}
//		else if (scr_flag == 2)
//		{
//			if (gTpInfo.sta) // press down
//			{
//				scr_key = 1;
//			}
//			if (gTpInfo.sta == 0 &&scr_key == 1)
//			{
//				scr_key = 0;
//				Detection_screen(scr_flag) ;
//			}
//		}
#if 0
		/****Keyboard cursor display****/
		if (Cusor_time > 60)
		{
			Cusor_time = 0;
			if (DioKb_flag == 1)
			{
				if (Screen_DIR == 0)
				{
					if (gDio_Info[Dio_num].Alig == 0)
						cx = cur_x + input_w;
					else
						cx = cur_x;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gDio_Info[Dio_num].zk_h;
				}
				else if (Screen_DIR == 1)
				{
					if (gDio_Info[Dio_num].Alig == 0)
						cy = cur_y + input_w;
					else
						cy = cur_y;
					cx = cur_x;
					w_temp = gDio_Info[Dio_num].zk_h;
					h_temp = cur_w;
				}
				else if (Screen_DIR == 2)
				{
					if (gDio_Info[Dio_num].Alig == 0)
						cx = cur_x - input_w;
					else
						cx = cur_x;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gDio_Info[Dio_num].zk_h;
				}
				else if (Screen_DIR == 3)
				{
					if (gDio_Info[Dio_num].Alig == 0)
						cy = cur_y - input_w;
					else
						cy = cur_y;
					cx = cur_x;
					w_temp = gDio_Info[Dio_num].zk_h;
					h_temp = cur_w;
				}
			}
			else if (AioKb_flag == 1)
			{
				if (Screen_DIR == 0)
				{
					cx = cur_x + input_w;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gAio_Info[Aio_num].zk_h;
				}
				else if (Screen_DIR == 1)
				{
					cy = cur_y + input_w;
					cx = cur_x;
					w_temp = gAio_Info[Aio_num].zk_w;
					h_temp = cur_w;
				}
				else if (Screen_DIR == 2)
				{
					cx = cur_x - input_w;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gAio_Info[Aio_num].zk_h;
				}
				else if (Screen_DIR == 3)
				{
					cy = cur_y - input_w;
					cx = cur_x;
					w_temp = gAio_Info[Aio_num].zk_h;
					h_temp = cur_w;
				}
			}
			else if (GBKioKb_flag == 1)
			{
				if (Screen_DIR == 0)
				{
					cx = cur_x + input_w;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gGBKio_Info[GBKio_num].zk_h;
				}
				else if (Screen_DIR == 1)
				{
					cy = cur_y + input_w;
					cx = cur_x;
					w_temp = gGBKio_Info[GBKio_num].zk_w;
					h_temp = cur_w;
				}
				else if (Screen_DIR == 2)
				{
					cx = cur_x - input_w;
					cy = cur_y;
					w_temp = cur_w;
					h_temp = gGBKio_Info[GBKio_num].zk_h;
				}
				else if (Screen_DIR == 3)
				{
					cy = cur_y - input_w;
					cx = cur_x;
					w_temp = gGBKio_Info[GBKio_num].zk_h;
					h_temp = cur_w;
				}
			}
			if (Cusor_sw == 0)
			{
				Cusor_sw++;
				LT768_BTE_Memory_Copy(MAIN_BUFF, LCD_XSIZE_TFT, cx, cy,
									  MAIN_BUFF, LCD_XSIZE_TFT, cx, cy,
									  Display_Layer, LCD_XSIZE_TFT, cx, cy,
									  0x0c, w_temp, h_temp);
			}
			else
			{
				Cusor_sw = 0;
				LT768_BTE_Memory_Copy(LAY_BUFF5, LCD_XSIZE_TFT, cur_x, cur_y,
									  LAY_BUFF5, LCD_XSIZE_TFT, cur_x, cur_y,
									  Display_Layer, LCD_XSIZE_TFT, cx, cy,
									  0x0c, w_temp, h_temp);
			}
		}
#endif
	}
	#endif
//		APP(APP_ADDR);
display_manu(0);	
printf(" date = %s,time = %s    \r\n", (char *)__DATE__, (char *)__TIME__);	
//	for(unsigned short i = 0 ; i < 512 ; i++)
//		printf(" %x",Rx_Buffer_short[i]);
//		printf("--------------\r\n");
//	for(unsigned short i = 0 ; i < 512 ; i++)
//		printf(" %x",Rx_Buffer1_short[i]);
//		printf("--------------\r\n");
//	 char debug[256] = {0xA5,0x5A,0x01,0x0E,0x20,0xCD,0x02,0x01,0x00,0x00,0xC8,0x3A,0x7A,0xAC,0xFF 0x02 0x59 0x0F 0x90 0xF7 0x65 0xE0 0x44 0x80 0xF0 0x22 0x02 0x55 0x0D 0x12 0x79 0x00 0x12 0x5B 0xFE 0xD2 0xC5 0x12 0x5B 0xFE 0x00 0x00 0x12 0x77 0x6D 0x00 0x00 0x00 0x00 0x22 0x02 0x4C 0x57 0x90 0xF1 0x26 0xE0 0xFE 0xEF 0xF4 0xFF 0xEE 0x5F 0xF0 0x22 0x00 0x02 0x4E 0x7A 0xEF 0x14 0x60 0x22 0x14 0x60 0x31 0x14 0x60 40 14 60 4F 14 60 5E 24 05 70 6B EC 90 F1 31 F0 90 F1 30 ED F0 90 F1 29 E0 44 01 F0 22 EC 90 F1 33 F0 90 F1 32 ED F0 90 F1 29 E0 44 02 F0 22 EC 90 F1 35 F0 90 F1 34 ED F0 90 F1 29 E0 44 04 F0 22 EC 90 F1 37 F0 90 F1 36 ED F0 90 F1 29 E0 44 08 F0 22 EC 90 F1 39 F0 90 F1 38 ED F0 90 F1 29 E0 44 10 F0 22 EC 90 F1 3B F0 90 F1 3A ED F0 90 F1 29 E0 44 20 F0 22 D2 60 02 68 CE 02 41 DB 90 06 36 E4 F0 A3};
//		unsigned char check_number = CalCheckSum(debug,12);
//	 printf("check_number: %x\r\n",check_number);
	//A5 5A 01 1B 20 06 01 01 00 00 7A 3E FB
	while(1)
	{
		WDT_FeedDog();
//	printf("f:%d\r\n",ota_lt7689_flag);
		if(ota_lt7689_flag==1)
		{	
//			printf("ota_flag=%d\r\n",ota_lt7689_flag);
#if 0 //跳转程序
					if((Rx_Buffer_short[0]==0xA5)&&
						(Rx_Buffer_short[1]==0x5A)&&
						(Rx_Buffer_short[2]==0x08)&&
						(Rx_Buffer_short[3]==0x01)&&
						(Rx_Buffer_short[4]==0x00)&&
						(Rx_Buffer_short[5]==0x00)&&
						(Rx_Buffer_short[6]==0xFF)&&
						(Rx_Buffer_short[7]==0xFE)
					)
				{
						printf("enter APP_ADDR!\r\n");
						APP(APP_ADDR);
				}
				else if((Rx_Buffer_short[0]==0xA5)&&
								(Rx_Buffer_short[1]==0x5A)&&
								(Rx_Buffer_short[2]==0x08)&&
								(Rx_Buffer_short[3]==0x04)&&
								(Rx_Buffer_short[4]==0x90)&&
								(Rx_Buffer_short[5]==0x00)&&
								(Rx_Buffer_short[6]==0xFF)&&
								(Rx_Buffer_short[7]==0xFE)
								)
				{
					printf("enter APP2_ADDR!\r\n");
						APP(APP2_ADDR);
				}
					
#endif				
#if  0 //OTA BIN-core 初版
				ota_offset = Rx_Count_short;
//				printf("ota_address %x\r\n",ota_address);// 256		0x8010000
				printf("\r\n");
				printf("start:%x  length:%d \r\n",ota_address,Rx_Count_short);
				
				ota_lt7689(Rx_Buffer_short,Rx_Count_short,ota_address);
				
				
				printf("\r\n");
				ota_address += ota_offset;
				if(ota_offset<256)
				{
					printf("\r\n");
//					printf("end number:%d\r\n",number);
//					APP(APP_ADDR);
				}
				
				Rx_Count_short=0;
				ota_lt7689_flag=0;
#endif
			
				
				
				
#if 0 //OTA-UI-BIN 初版
					if((ui_addr%131072)==0)//128KB 擦除
					{
							W25N01GV_Erase_Block(block);
							printf("block=%d\r\n",block);
							block++;
					}
					for(offset=0;offset<Rx_Count_short;offset++)
					{
							ui_buf[offset+ui_offset]=Rx_Buffer_short[offset];
					}
					
					ui_offset+=Rx_Count_short;
					ui_addr+=Rx_Count_short;
					
					printf("addr=%x offset=%d \r\n",ui_addr,ui_offset);
					
					
					if((ui_offset%2048==0))
					{
						W25N01GV_WritePageAddr_Data(ui_buf,0,page,2048);
						printf("Write=%d\r\n",ui_offset);
						ui_offset = 0;
						printf("page=%d offset=%d\r\n",page,ui_offset);
						page++;
					}
					else if (last_flag == 1)
					{
						W25N01GV_WritePageAddr_Data(ui_buf,0,page,ui_offset);
						printf("Write=%d\r\n",ui_offset);
						ui_offset = 0;
						printf("page:%d offset:%d\r\n",page,ui_offset);
						page++;
						last_flag=0;
						printf("ui_addr=%x\r\n",ui_addr);
					}	
		
				Rx_Count_short=0;
				ota_lt7689_flag=0;
				
#endif
#if 0  //对比DMA
//			printf("ui_offset=%d\r\n",ui_offset);
			printf("\r\n**1**\r\n");
			
//		if(dma_flag==1)
//		{
//				printf("Rx_Buffer_short\r\n");
				for(unsigned short i = 0; i < Rx_Count_short ; i++) //Rx_Count_short ui_offset
				{
					printf(" %x",Rx_Buffer_short[i]);//Rx_Buffer_short[i] ui_buf[i]
//						printf(" %d",i);
				}		
//		}
//		else if (dma_flag==0)
//		{
//				printf("Rx_Buffer_short1\r\n");
//				for(unsigned short i = 0; i < Rx_Count_short ; i++) //Rx_Count_short ui_offset
//				{
//					printf(" %x",Rx_Buffer1_short[i]);//Rx_Buffer_short[i] ui_buf[i]
////						printf(" %d",i);
//				}				
//		}
				
//				ui_offset=0;
			printf("\r\n****\r\n");
				Rx_Count_short=0;
				ota_lt7689_flag=0;
#endif
		
//				printf("number=%d\r\n",number);
#if 1		//3-enhance //211
//	
//	 ota_analysis(Rx_Buffer_short,Rx_Count_short);
//		printf("\r\n--------\r\n");
//		for(unsigned char i = 0 ; i < Rx_Count_short; i++)
//		printf(" %x",Rx_Buffer_short[i]);
//		printf("\r\n %x \r\n",CalCheckSum(Rx_Buffer_short,Rx_Count_short-1));
		
//		if(Rx_Count_short<200)
//		{
//			printf("all: %d\r\n",);
//		}
//				pkg_seq = 0;
//				Rx_Count_short=0;
//				ota_lt7689_flag=0;		
		// 收到WIFI开始升级报文
		
		
		//收到WIFI固件发送完成
		
		
		//收到WIFI 请求固件失败
		
		
		//收到WIFI OTA超时
		
		
		//收到WIFI 发送固件内容
		
	/*******************rev_status**********************/	
		
	// 判断校验码是否准确 
//	ota_analysis(wifi_buf,mes_length);
		printf("rev:%d check:%x\r\n",mes_length,CalCheckSum( (char *)wifi_buf, mes_length-1));
		
		if(wifi_buf[mes_length-1]!= CalCheckSum( (char *)wifi_buf, mes_length-1))
		{
				printf("%x check_erro %x\r\n",CalCheckSum( (char *)wifi_buf, mes_length-1),wifi_buf[mes_length-1]);
//				return 1;
//				inf_wifi_data_check_error( inf_wifi_buf,wifi_buf[3]);
		}
		if((wifi_buf[0]!=0xA5)&&(wifi_buf[1]!=0x5A)&&(wifi_buf[2]!=0x01))
		{
				printf("illegal_new!\r\n");
//				return 2;
		}
		
				
				for(unsigned char i = 0 ; i < mes_length; i++)
				{
						printf(" %x",wifi_buf[i]);
				}
				printf("\r\n");
	//实现算法
		
		// 收到wifi-ota通知
		
		if((wifi_buf[4]==0x20)&&(wifi_buf[6]==0x01)&&(wifi_buf[7]==0x01))
		{
			
				pkg_seq = wifi_buf[3];
			
			//ACK
				
					unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
					
					inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
					send_length++;
					inf_wifi_buf_length = 0; // ACK
					mes_length =0;
					ota_lt7689_flag = 0;
					send_meg_wifi(inf_wifi_buf,send_length);
//					printf("ACK:");
//					for(unsigned char i = 0 ; i < send_length ; i++)
//					{
//						printf(" %x",inf_wifi_buf[i]);
//					}
//					printf("\r\n---\r\n");				
				
				//发送 通知wifi 开始发送固件
				DelayMS(30);//跳转Bootloader!!80
				unsigned char length=inf_wifi_ota_start(inf_wifi_buf, pkg_seq);
				
//				printf("inf:");
//					for(unsigned char i = 0 ; i < length ; i++)
//					{
//						printf(" %x",inf_wifi_buf[i]);
//					}
//					printf("\r\n---\r\n");
				inf_wifi_buf_length = 0;
					mes_length =0;
					ota_lt7689_flag = 0;
				printf("start updata\r\n");
				send_meg_wifi(inf_wifi_buf, length);
		}
		
		else if ((wifi_buf[4]==0x20)&&(wifi_buf[6]==0x02)&&(wifi_buf[7]==0x01))//wifi 发送固件内容报文
		{
//					printf("%d %d\r\n",wifi_buf[8],wifi_buf[9]);
//					unsigned short updata_seq = 256*wifi_buf[8]+wifi_buf[9];//(wifi_buf[8] || (wifi_buf[9]<<8));
//					printf("0***%d %d\r\n",updata_seq,ota_seq);
					
//					updata_seq = wifi_buf[9] || (wifi_buf[8]<<8);
//					printf("1***%d %d\r\n",updata_seq,ota_seq);
					printf("***%d %d\r\n",ota_seq,256*wifi_buf[8]+wifi_buf[9]);
					if(ota_seq==(256*wifi_buf[8]+wifi_buf[9]))
					{
							printf("bin:");
							for(unsigned char i = 0 ; i < wifi_buf[10]; i++)
							{
									printf(" %x",wifi_buf[i+11]);
							}
							printf("\r\n");
							
							ota_seq++;
							
				 }
				 else 
				 {
						printf("***repeat!\r\n");
				 }
				 
						//封装ACK
				pkg_seq = wifi_buf[3]; //包序号
				
				unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
				
				inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
				send_length++;	
				
	//		发送ACK
			
				inf_wifi_buf_length = 0; // ACK
				mes_length =0;
				ota_lt7689_flag = 0;
				send_meg_wifi(inf_wifi_buf,send_length);
					
			
			
		}
		//wifi 发送固件完成
		else if ((wifi_buf[4]==0x20)&&(wifi_buf[6]==0x01)&&(wifi_buf[7]==0x02))
		{
				printf("OTA-finish\r\n");
						//封装ACK
				pkg_seq = wifi_buf[3]; //包序号
				
				unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
				
				inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
				send_length++;	
				
	//		发送ACK
			
				inf_wifi_buf_length = 0; // ACK
				mes_length =0;
				ota_lt7689_flag = 0;
				ota_seq = 0;
				printf("ota_seq=%d\r\n",ota_seq);
				send_meg_wifi(inf_wifi_buf,send_length);
					
			
	// mcu inform wifi 
			
			
			
		}
		//wifi 请求失败
		else if ((wifi_buf[4]==0x20)&&(wifi_buf[6]==0x01)&&(wifi_buf[7]==0x02))
		{
				printf("OTA-request-fail\r\n");
						//封装ACK
				pkg_seq = wifi_buf[3]; //包序号
				
				unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
				
				inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
				send_length++;	
				
	//		发送ACK
			
				inf_wifi_buf_length = 0; // ACK
				mes_length =0;
				ota_lt7689_flag = 0;
				send_meg_wifi(inf_wifi_buf,send_length);
				
		}
		//wifi 超时
		else if ((wifi_buf[4]==0x20)&&(wifi_buf[6]==0x01)&&(wifi_buf[7]==0x02))
		{
				printf("OTA-timeout\r\n");
						//封装ACK
				pkg_seq = wifi_buf[3]; //包序号
				
				unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
				
				inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
				send_length++;	
				
	//		发送ACK
			
				inf_wifi_buf_length = 0; // ACK
				mes_length =0;
				ota_lt7689_flag = 0;
				send_meg_wifi(inf_wifi_buf,send_length);
				
		}
		
	else
		{
			
			printf("other new!\r\n");
	//封装ACK
				
				pkg_seq = wifi_buf[3]; //包序号
				
				unsigned char send_length=inf_wifi_ack(inf_wifi_buf,pkg_seq);
				
				inf_wifi_buf[send_length] = CalCheckSum( (char *)inf_wifi_buf,send_length);
				send_length++;	
				
	//		发送ACK
			
				inf_wifi_buf_length = 0; // ACK
				mes_length =0;
				ota_lt7689_flag = 0;
				send_meg_wifi(inf_wifi_buf,send_length);
				
		}
#endif
	
		}
		
		
	}
}