/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F780/790
* DESCRIPTION:	EBI8080_LCD related functions.
*____________________________________________________________________________
*	REVISION	Date				User	Description
*	1.0				2020/06/24	SA1		First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F790.h>
#include "..\EBI\EBI.h"
#include "EBI8080_LCD.h"
#include "..\..\Utility\Utility.h"


/*_____ D E C L A R A T I O N S ____________________________________________*/

/*_____ M A C R O S ________________________________________________________*/

uint32_t read_id;


/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: LCD_Init
* Description	: Initialization of LCD
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LCD_Init(uint8_t bHorizontal, uint8_t bVertical, uint8_t bBank)
{ 
	SN_SYS1->AHBCLKEN_b.EBICLKEN = 1;  //enable EBI AHB clock
	
	EBI_8080Init(bBank, EBI_BK8080MODE_A1D8);
	
	EBI_8080LCD_Init_ST7789(bHorizontal, bVertical);
}

/*****************************************************************************
* Function		: LCD_Clear
* Description	: Clear the LCD with one color
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LCD_Clear(uint16_t hwColor)
{
	uint32_t i;

	//The panel size : 320*240 = 76800//
	for(i=0;i<76800;i++)     
	{	
			EBI_8080Write_Data_8B(hwColor>>8);						
			EBI_8080Write_Data_8B(hwColor);						
	}
} 

/*****************************************************************************
* Function		: EBI_8080LCD_Init_ST7789
* Description	: Initialization of LCD driver
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void EBI_8080LCD_Init_ST7789(uint8_t bHorizontal, uint8_t bVertical)
{
	EBI_Clear_Intflag();
	
	_LCD_SET_RST_HIGH					UT_DelayNms(2);	
	_LCD_SET_RST_OUTPUT_LOW		UT_DelayNms(2);
	_LCD_SET_RST_HIGH					UT_DelayNms(2);
	
	EBI_8080Write_CMD_8B(CMD_RESET);
	UT_DelayNms(150);
	EBI_8080Write_CMD_8B(CMD_SLPOUT);					 //Sleep Out
	UT_DelayNms(150);

	//read ID
	EBI_8080Write_CMD_8B(0x4);			
	read_id = *(uint8_t *)(wRAM_Add); 
	read_id = *(uint8_t *)(wRAM_Add);
	if(read_id!=0x85)
			while(1);
	read_id = *(uint8_t *)(wRAM_Add); 
	if(read_id!=0x85)
			while(1);
	read_id = *(uint8_t *)(wRAM_Add); 
	if(read_id!=0x52)
			while(1);

	// -----------Display and color format setting ----------//
	if((bHorizontal == 1) && (bVertical == 0))
	{
		EBI_8080Write_CMD_8B(CMD_MADCTL);				//Memory Data Access Control
		EBI_8080Write_Data_8B(0xA0);							//MY 0 MX 0 MV 0 ML 0 RGB 0	
	}
	else if((bHorizontal == 0) && (bVertical == 1))
	{
		EBI_8080Write_CMD_8B(CMD_MADCTL);				//Memory Data Access Control
		EBI_8080Write_Data_8B(0x00);							//MY 0 MX 0 MV 0 ML 0 RGB 0	
	}
	
	EBI_8080Write_CMD_8B(CMD_COLMOD);					//Interface Pixel Format
	EBI_8080Write_Data_8B(0x55);						  	//16bit/pixel	
	
	EBI_8080Write_CMD_8B(CMD_RAMCTRL);					//RAM Control
	EBI_8080Write_Data_8B(0x00);						
	EBI_8080Write_Data_8B(0xF0);					
	
	EBI_8080Write_CMD_8B(CMD_RGBCTRL);		  		//RGB Interface Control
	EBI_8080Write_Data_8B(0x00);						
	EBI_8080Write_Data_8B(0x02);			
	EBI_8080Write_Data_8B(0x14);	
	
	// -----------Frame rate setting------------------------//
	EBI_8080Write_CMD_8B(CMD_PORCTRL);					//Porch Setting
	EBI_8080Write_Data_8B(0x08);						
	EBI_8080Write_Data_8B(0x08);						
	EBI_8080Write_Data_8B(0x00);						
	EBI_8080Write_Data_8B(0x22);						
	EBI_8080Write_Data_8B(0x22);							

	EBI_8080Write_CMD_8B(CMD_GCTRL);						//Gate Control
	EBI_8080Write_Data_8B(0x35);		
	
	// ----------- Adjust the Gamma Curve ------------------//
	EBI_8080Write_CMD_8B(CMD_PVGAMCTRL);				//Positive Voltage Gamma Control
	EBI_8080Write_Data_8B(0xd0);
	EBI_8080Write_Data_8B(0x00);
	EBI_8080Write_Data_8B(0x06);
	EBI_8080Write_Data_8B(0x09);
	EBI_8080Write_Data_8B(0x0b);
	EBI_8080Write_Data_8B(0x2a);
	EBI_8080Write_Data_8B(0x3c);
	EBI_8080Write_Data_8B(0x55);
	EBI_8080Write_Data_8B(0x4b);
	EBI_8080Write_Data_8B(0x08);
	EBI_8080Write_Data_8B(0x16);
	EBI_8080Write_Data_8B(0x14);
	EBI_8080Write_Data_8B(0x19);
	EBI_8080Write_Data_8B(0x20);
	
	EBI_8080Write_CMD_8B(CMD_NVGAMCTRL);				//Negative Voltage Gamma Control
	EBI_8080Write_Data_8B(0xd0);
	EBI_8080Write_Data_8B(0x00);
	EBI_8080Write_Data_8B(0x06);
	EBI_8080Write_Data_8B(0x09);
	EBI_8080Write_Data_8B(0x0b);
	EBI_8080Write_Data_8B(0x29);
	EBI_8080Write_Data_8B(0x36);
	EBI_8080Write_Data_8B(0x54);
	EBI_8080Write_Data_8B(0x4b);
	EBI_8080Write_Data_8B(0x0d);
	EBI_8080Write_Data_8B(0x16);
	EBI_8080Write_Data_8B(0x14);
	EBI_8080Write_Data_8B(0x21);
	EBI_8080Write_Data_8B(0x20);

	EBI_8080Write_CMD_8B(CMD_GAMSET);					//Gamma Set
	EBI_8080Write_Data_8B(0x01);

	EBI_8080Write_CMD_8B(CMD_DGMEN);						//Digital Gamma Enable
	EBI_8080Write_Data_8B(0x01);						

	//------------------ Set GRAM area ---------------------//
	if((bHorizontal == 1) && (bVertical == 0))
	{	
		EBI_8080Write_CMD_8B(CMD_CASET);					//Column Address Set
		EBI_8080Write_Data_8B(0x00);							//XS15-8
		EBI_8080Write_Data_8B(0x00);							//XS7-0
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_H >>8);								//XE15-8
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_H&0xFF);								//XE7-0
		
		EBI_8080Write_CMD_8B(CMD_RASET);					//Row Address Set
		EBI_8080Write_Data_8B(0x00);							//YS15-8
		EBI_8080Write_Data_8B(0x00);							//YS7-0
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_H>>8);									//YE15-8
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_H&0XFF);								//YE7-0
	}
	else if((bHorizontal == 0) && (bVertical == 1))
	{
		EBI_8080Write_CMD_8B(CMD_CASET);					//Column Address Set
		EBI_8080Write_Data_8B(0x00);							//XS15-8
		EBI_8080Write_Data_8B(0x00);							//XS7-0
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_V >>8);								//XE15-8
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_V&0xFF);								//XE7-0
		
		EBI_8080Write_CMD_8B(CMD_RASET);					//Row Address Set
		EBI_8080Write_Data_8B(0x00);							//YS15-8
		EBI_8080Write_Data_8B(0x00);							//YS7-0
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_V>>8);									//YE15-8
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_V&0XFF);								//YE7-0
	}
	
	EBI_8080Write_CMD_8B(CMD_VSCRDEF);					//Vertical Scrolling Definition
	EBI_8080Write_Data_8B(0x00);								//TFA15-8
	EBI_8080Write_Data_8B(0x00);								//TFA7-0
	EBI_8080Write_Data_8B(0x00);								//VSA15-8
	EBI_8080Write_Data_8B(0xEf);								//VSA7-0	
	EBI_8080Write_Data_8B(0x00);								//BFA15-8
	EBI_8080Write_Data_8B(0x00);					  		//BFA7-0		
	
	EBI_8080Write_CMD_8B(CMD_VSCSAD);					//Vertical Scroll Start Address of RAM
	EBI_8080Write_Data_8B(0x00);						
	EBI_8080Write_Data_8B(0x00);						
	
	EBI_8080Write_CMD_8B(CMD_INVOFF);
	//EBI_8080Write_CMD_8B(CMD_INVON);
	
	//------------Display--------------------------//
	EBI_8080Write_CMD_8B(CMD_NORON);						//Normal Display Mode On
	EBI_8080Write_CMD_8B(CMD_DISPON);					//Display On


	if((bHorizontal == 1) && (bVertical == 0))
	{
		EBI_8080Write_CMD_8B(0x2a);    					//column address set
		EBI_8080Write_Data_8B(0x00);
		EBI_8080Write_Data_8B(0);
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_H >>8);								//XE15-8
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_H&0xFF);								//XE7-0
		
		EBI_8080Write_CMD_8B(0x2b);    					//row address set
		EBI_8080Write_Data_8B(0X00);
		EBI_8080Write_Data_8B(0);
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_H>>8);							
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_H&0XFF);								
		EBI_8080Write_CMD_8B(0x2c);     					//memory(RAM) write
	}
	else if((bHorizontal == 0) && (bVertical == 1))
	{
		EBI_8080Write_CMD_8B(0x2a);    					//column address set
		EBI_8080Write_Data_8B(0x00);
		EBI_8080Write_Data_8B(0);
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_V >>8);								//XE15-8
		EBI_8080Write_Data_8B(LCD_XAXIS_MAX_V&0xFF);								//XE7-0
		
		EBI_8080Write_CMD_8B(0x2b);    					//row address set
		EBI_8080Write_Data_8B(0X00);
		EBI_8080Write_Data_8B(0);
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_V>>8);							
		EBI_8080Write_Data_8B(LCD_YAXIS_MAX_V&0XFF);								
		EBI_8080Write_CMD_8B(0x2c);     					//memory(RAM) write
	}	
	
	LCD_Clear(WHITE);
}
