#include "bsp.h"

extern unsigned char Flash_type;

u8 R_angle=0;
u8 FF_Update_Flag=0;														//FF

u8 ENDBYTE_Buf[4]={0xE4,0x1B,0x11,0xEE};

unsigned char TFT_color = 0;     //判断是那种颜色排列
unsigned char Flash_type = 0;    //0:NorFlash  ，1:NandFlash

unsigned short LCD_XSIZE_TFT = 800;
unsigned short LCD_YSIZE_TFT = 480;
unsigned short LCD_VBPD = 0;
unsigned short LCD_VFPD = 0;
unsigned short LCD_VSPW = 0;
unsigned short LCD_HBPD = 0;
unsigned short LCD_HFPD = 0;
unsigned short LCD_HSPW = 0;

unsigned char 		 gPCLKRISING = 0;
unsigned char 		 gHSYNCPolarity = 0;
unsigned char 		 gVSYNCPolarity = 0;
unsigned char 		 gDEPolarity = 0;

volatile unsigned int Lay_Canvas_Addr = 0x80000000;
volatile unsigned short Lay_Canvas_Width = 800;

#define BAUDRATE_ADDR  0x00000402    //3byte
#define XY_SIZE_ADDR   0x0000040a    //4byte
#define LCD_PARAM_ADDR 0x0000040e    //12byte
#define LCDRotate_ADDR 0x0000042A

unsigned int gBaudrate = 0;

DMAC_HandleTypeDef hdma2;


/*
* 函数名：Rx_CRC_CCITT
* 功能  ：CRC校验
* 输入  ：buf：数据缓存首地址
          len：数据长度
* 输出  ：无
* 作者  ：LEVETOP
*/
u16 Rx_CRC_CCITT(u8 *puchMsg, u16 usDataLen)
{
	u16 wCRCin = 0x0000;
	u16 wCPoly = 0x1021;
	u8 wChar = 0;
	int i;	
	while (usDataLen--)
	{
		wChar = *(puchMsg++);
		wCRCin ^= (wChar << 8);
		for (i = 0; i < 8; i++)
		{
			if (wCRCin & 0x8000)
				wCRCin = (wCRCin << 1) ^ wCPoly;
			else
				wCRCin = wCRCin << 1;
		}
	}
	return (wCRCin&0xffff);
}

/*
* 函数名：LT_ReadFlash
* 功能  ：从LT768外挂的FLASH中读取数据
* 输入  ：pBuffer:数据存储区
*         ReadAddr:开始读入的地址(24bit)
*         要读入的字节数(最大65535)
* 输出  ：无
* 作者  ：LEVETOP
*/
void LT_ReadFlash(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
//	SPI_Clock_Period(1);
	u16 byte_width;
	u16 byte_remain;
	u32 temp;
	u8 i=0;
	
	byte_width = NumByteToRead/4;
	byte_remain = NumByteToRead%4;
	if(Flash_type == 0)
	{		
		W25QXX_Read_QUAD(pBuffer,ReadAddr,NumByteToRead); 
	}
	else
	{
		 W25NXX_Read_NoCheck(pBuffer,ReadAddr,NumByteToRead,4); 
	}	
}


void DMAC_M2MTest2(DMAC_TypeDef *pdmac,uint32_t ch,uint32_t len,uint8_t *dstbuf,uint8_t *srcbuf)
{
    uint32_t i;
	if(hdma2.init.transfer_size==0)	return;
	
	hdma2.init.dst_addr = (uint32_t)dstbuf;
	hdma2.init.src_addr = (uint32_t)srcbuf;
    /*初始化DMAC*/
    HAL_DMAC_Init(&hdma2);
    /*开启非中断模式DMAC传输*/
    HAL_DMAC_StartPolling(&hdma2);
    /*轮询传输完成*/
    HAL_DMAC_PollForTransfer(&hdma2, 0, HAL_MAX_DELAY);
}
void DMAC_M2MTest3(DMAC_TypeDef *pdmac,uint32_t ch,uint32_t len,uint8_t *dstbuf,uint8_t *srcbuf)
{
    uint32_t i;
	if(hdma2.init.transfer_size==0)	return;
	
	hdma2.init.dst_addr = (uint32_t)dstbuf;
	hdma2.init.src_addr = (uint32_t)srcbuf;
    /*初始化DMAC*/
    HAL_DMAC_Init(&hdma2);
    /*开启非中断模式DMAC传输*/
    HAL_DMAC_StartPolling(&hdma2);
    /*轮询传输完成*/
//    HAL_DMAC_PollForTransfer(&hdma2, 0, HAL_MAX_DELAY);
}

void LT_ReadParam(void)
{
	unsigned char temp[25] = {0};
	u32 ID = 0;

/*	
	Flash_type = 0;	
	ID = W25QXX_ReadID_STD();
	if((ID<=0xEF18 && ID>=0xEF13 ) || ID==0x5e17)
	{	
		//printf("ID1:%x \r\n",ID);
		if(ID == 0xEF18)
		{
			W25Q256 = 1;
			W25QXX_Enter_4Byte_AddressMode_STD();
		}
	}
	else
	{
		Flash_type = 1;
		ID=W25NXX_ReadID();
		//printf("ID2:%x \r\n",ID);		
	}
	//printf("type:%d \r\n",Flash_type);
	
	LT_ReadFlash(temp,BAUDRATE_ADDR,3);
//	printf("%x   %x    %x\r\n",temp[0],temp[1],temp[2]);
	if(temp[0] == 0xFF && temp[1] == 0xFF && temp[2] == 0xFF)
	{
//		printf("%x   %x    %x\r\n",temp[0],temp[1],temp[2]);
		gBaudrate = 115200;
		LCD_XSIZE_TFT = 800;
		LCD_YSIZE_TFT = 480;
		LCD_VBPD = 23;
		LCD_VFPD = 22;
		LCD_VSPW = 3;
		LCD_HBPD = 46;
		LCD_HFPD = 210;
		LCD_HSPW = 20;
		gPCLKRISING = 1;
		gHSYNCPolarity = 0;
		gVSYNCPolarity = 0;
		gDEPolarity = 1;
		
		TFT_color = 0;
	}
	else
	{
		gBaudrate = temp[0];
		gBaudrate = (gBaudrate<<8) + temp[1];
		gBaudrate = (gBaudrate<<8) + temp[2];
//		printf("rate:%d\r\n",gBaudrate);
		
		LT_ReadFlash(temp,XY_SIZE_ADDR,4);
		LCD_XSIZE_TFT = temp[0];
		LCD_XSIZE_TFT = (LCD_XSIZE_TFT<<8) + temp[1];
		LCD_YSIZE_TFT = temp[2];
		LCD_YSIZE_TFT = (LCD_YSIZE_TFT<<8) + temp[3];
//		printf("w:%d   h:%d\r\n",LCD_XSIZE_TFT,LCD_YSIZE_TFT);
		
		LT_ReadFlash(temp,LCD_PARAM_ADDR,23);
		LCD_VBPD = temp[0];
		LCD_VBPD = (LCD_VBPD<<8) + temp[1];
		LCD_VFPD = temp[2];
		LCD_VFPD = (LCD_VFPD<<8) + temp[3];
		LCD_VSPW = temp[4];
		LCD_VSPW = (LCD_VSPW<<8) + temp[5];
		LCD_HBPD = temp[6];
		LCD_HBPD = (LCD_HBPD<<8) + temp[7];
		LCD_HFPD = temp[8];
		LCD_HFPD = (LCD_HFPD<<8) + temp[9];
		LCD_HSPW = temp[10];
		LCD_HSPW = (LCD_HSPW<<8) + temp[11];
		
		gPCLKRISING    = temp[12];
		gHSYNCPolarity = temp[13];
		gVSYNCPolarity = temp[14];
		gDEPolarity    = temp[15];
		
		TFT_color = temp[16];

		LT_ReadFlash(&R_angle,LCDRotate_ADDR,1);
//		printf("R_angle:%d \r\n",R_angle);
	}
*/

		gBaudrate = 115200;
#ifdef LCD1024x600
	// If flash data don't exist
		LCD_XSIZE_TFT = 1024;
		LCD_YSIZE_TFT = 600;
		LCD_VBPD = 23;
		LCD_VFPD = 210;
		LCD_VSPW = 3;
		LCD_HBPD = 46;
		LCD_HFPD = 210;
		LCD_HSPW = 20;
		
		gPCLKRISING = 0;
		gHSYNCPolarity = 0;
		gVSYNCPolarity = 0; 
		gDEPolarity = 1;		
#else			
		LCD_XSIZE_TFT = 800;
		LCD_YSIZE_TFT = 480;
		LCD_VBPD = 23;
		LCD_VFPD = 22;
		LCD_VSPW = 3;
		LCD_HBPD = 46;
		LCD_HFPD = 210;
		LCD_HSPW = 20;
		gPCLKRISING = 1;
		gHSYNCPolarity = 0;
		gVSYNCPolarity = 0;
		gDEPolarity = 1;
#endif
	
	
//		printf("LCD_VBPD:%d\r\n",LCD_VBPD);
//		printf("LCD_VFPD:%d\r\n",LCD_VFPD);
//		printf("LCD_VSPW:%d\r\n",LCD_VSPW);
//		printf("LCD_HBPD:%d\r\n",LCD_HBPD);
//		printf("LCD_HFPD:%d\r\n",LCD_HFPD);
//		printf("LCD_HSPW:%d\r\n",LCD_HSPW);
		
//		printf("gPCLKRISING:%d\r\n",gPCLKRISING);
//		printf("gHSYNCPolarity:%d\r\n",gHSYNCPolarity);
//		printf("gVSYNCPolarity:%d\r\n",gVSYNCPolarity);
//		printf("gDEPolarity:%d\r\n",gDEPolarity);

}

void Main_Image_Start_Address(unsigned long Addr)	
{

}
//[24h][25h]=========================================================================
void Main_Image_Width(unsigned short WX)	
{

}

void Main_Window_Start_XY(unsigned short WX,unsigned short HY)	
{

}

void Active_Window_XY(unsigned short WX,unsigned short HY)	
{

}
//[5Ah][5Bh][5Ch][5Dh]=========================================================================
void Active_Window_WH(unsigned short WX,unsigned short HY)	
{

}

void Canvas_Image_Start_address(unsigned int Addr)	
{
	Lay_Canvas_Addr = Addr;
}

void Canvas_image_width(unsigned short WX)	
{
	Lay_Canvas_Width = WX;
}


void LT768_BTE_Init(void)
{
	hdma2.instance = DMAC1;
	hdma2.init.channel_num = 0;//HAL_DMACC_CHANNEL_0;
    /*复位DMAC*/
    HAL_DMAC_Deinit(&hdma2);
	hdma2.init.dst_msize = HAL_DMAC_MSIZE_1;
	hdma2.init.src_msize = HAL_DMAC_MSIZE_1;
	hdma2.init.dst_inc = HAL_DMAC_DINC_INC;
	hdma2.init.src_inc = HAL_DMAC_SINC_INC;
	hdma2.init.dst_tr_width = HAL_DMAC_TR_WIDTH_32BITS;
	hdma2.init.src_tr_width = HAL_DMAC_TR_WIDTH_32BITS;
	hdma2.init.transfer_type = HAL_DMAC_TRANSFERTYPE_M2M_CONTROLLER_DMA;
	hdma2.init.peripheral_type = 0;
	hdma2.init.transfer_lli = HAL_DMAC_LLP_DIS;
	hdma2.init.transfer_size = LCD_XSIZE_TFT/2;;	
	HAL_DMAC_Init(&hdma2);
}

#define LEN_TEST 2048

void LT768_DrawPoint(UINT16 x,UINT16 y,UINT16 color)
{
	writew((Lay_Canvas_Addr+(y*Lay_Canvas_Width*2)+(x*2)),color);
}


/*显示内部_ASCII编码*/
//x,y:起始坐标
//size:字体大小
//mode: 0：背景透明  1：透明不背景
//FontColor:字体颜色
//BackGroundColor:背景颜色
//num:字符
void LT768_TFT_Show_Internal_Char(UINT16 x,UINT16 y,UINT8 size,UINT8 mode,UINT16 FontColor,UINT16 BackGroundColor,UINT8 num)
{
	UINT8 temp,t1,t;
	UINT16 y0=y;
	UINT16 ii,jj;
	UINT8 csize=(size/8+((size%8)?1:0))*(size/2);		//得到字体一个字符对应点阵集所占的字节数
//	printf("csize==%d \r\n",csize);
 	num=num-' ';//得到偏移后的值（ASCII字库是从空格开始取模，所以-' '就是对应字符的字库）
	
	t=0;

	if(size==32)
	{
		for(t=0;t<csize;t++)
		{
				for(jj=0;jj<size;jj++)
				{
					temp=asc2_3216[num][t++];	
					for(ii=0;ii<8;ii++)
					{
						if(temp&0x80)LT768_DrawPoint(x+ii,y+jj,FontColor);
						else if(mode==0)LT768_DrawPoint(x+ii,y+jj,BackGroundColor);
						
						temp<<=1;		
					}
					
					temp=asc2_3216[num][t++];	
					for(ii=8;ii<16;ii++)
					{
						if(temp&0x80)LT768_DrawPoint(x+ii,y+jj,FontColor);
						else if(mode==0)LT768_DrawPoint(x+ii,y+jj,BackGroundColor);
				
						temp<<=1;		
					}
				}
		}
	}
	else if(size==16||size==24)
	{
		for(t=0;t<csize;t++)
		{
			if(size==16)  temp=asc2_1608[num][t];	//调用1608字体	
			else if(size==24) temp=asc2_2412[num][t];	//调用2412字体
	//		else if(size==32) temp=asc2_3216[num][t];	//调用2412字体
	//		else return;								//没有的字库	
			
			for(t1=0;t1<8;t1++)
			{
				if(temp&0x80)LT768_DrawPoint(x,y,FontColor);
				else if(mode==0)LT768_DrawPoint(x,y,BackGroundColor);
				
				temp<<=1;	
				y++;
				if(y>=LCD_YSIZE_TFT)return;		//超区域了
				if((y-y0)==size)
				{
					y=y0;
					x++;
					if(x>=LCD_XSIZE_TFT)return;	//超区域了
					break;
				}
			}
		}
	}
	else return;								//没有的字库
}

//显示Ascll字符串
//x,y :汉字的坐标
//size:字体大小
//mode: 0：背景透明  1：透明不背景
//FlashAddr:字库地址
//FontColor:字体颜色
//BackGroundColor:背景颜色
//str:字符串
void LT768_TFT_ShowAscii(UINT16 x,UINT16 y,UINT8 size,UINT8 mode,UINT16 FontColor,UINT16 BackGroundColor,UINT8 *str)
{
//	UINT8 x0=x;
    while((*str<='~')&&(*str>=' '))//判断是不是非法字符!
    {
        if(x+size/2>LCD_XSIZE_TFT){x=0;y+=size;}
        if(y+size>LCD_YSIZE_TFT)break;//退出
        LT768_TFT_Show_Internal_Char(x,y,size,mode,FontColor,BackGroundColor,*str);
//        LCD_ShowChar(x,y,*p,size,0);
        x+=size/2;
        str++;
    }
}



//解压图片所用的变量
//ctb_data CTB;
//unsigned char  RTBuff[2048];
//__align(4) unsigned short*RowBuff;
//__align(4) unsigned short RowBuff_1[1024+1];
//__align(4) unsigned short RowBuff_2[1024+1];

//__align(4) unsigned short RowBuff_OLD;




