/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F780/790
* DESCRIPTION:	SPI_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 "..\SPI\SPI.h"
#include "..\..\Utility\Utility.h"
#include "SPI_LCD.h"

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

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


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

/*****************************************************************************
* Function		: LCD_Init_ILI9341
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LCD_Init_ILI9341 (uint8_t bHorizontal, uint8_t bVertical)
{
 //************* Start Initial Sequence **********//
	__LCD_SET_RST_OUTPUT_LOW;	
	__LCD_SET_DC_OUTPUT_LOW;
	
	UT_DelayNms(150);
	
	__LCD_SET_RST_HIGH;
	
	UT_DelayNms(150);
	__SPI1_SEL0_SETTING;
	__SPI1_SET_SEL0;	
	
	SPI_LCDWrite_CMD(0xCF);  
	SPI_LCDWrite_DATA(0x00); 
	SPI_LCDWrite_DATA(0xD9); 
	SPI_LCDWrite_DATA(0X30); 
	 
	SPI_LCDWrite_CMD(0xED);  
	SPI_LCDWrite_DATA(0x64); 
	SPI_LCDWrite_DATA(0x03); 
	SPI_LCDWrite_DATA(0X12); 
	SPI_LCDWrite_DATA(0X81); 
	 
	SPI_LCDWrite_CMD(0xE8);  
	SPI_LCDWrite_DATA(0x85); 
	SPI_LCDWrite_DATA(0x10); 
	SPI_LCDWrite_DATA(0x78); 
	 
	SPI_LCDWrite_CMD(0xCB);  
	SPI_LCDWrite_DATA(0x39); 
	SPI_LCDWrite_DATA(0x2C); 
	SPI_LCDWrite_DATA(0x00); 
	SPI_LCDWrite_DATA(0x34); 
	SPI_LCDWrite_DATA(0x02); 
	 
	SPI_LCDWrite_CMD(0xF7);  
	SPI_LCDWrite_DATA(0x20); 
	 
	SPI_LCDWrite_CMD(0xEA);  
	SPI_LCDWrite_DATA(0x00); 
	SPI_LCDWrite_DATA(0x00); 
	 
	SPI_LCDWrite_CMD(0xC0);    //Power control 
	SPI_LCDWrite_DATA(0x21);   //VRH[5:0] 
	 
	SPI_LCDWrite_CMD(0xC1);    //Power control 
	SPI_LCDWrite_DATA(0x12);   //SAP[2:0];BT[3:0] 
	 
	SPI_LCDWrite_CMD(0xC5);    //VCM control 
	SPI_LCDWrite_DATA(0x32); 
	SPI_LCDWrite_DATA(0x3C); 
	 
	SPI_LCDWrite_CMD(0xC7);    //VCM control2 
	SPI_LCDWrite_DATA(0XC1); 
	 
	if((bHorizontal == 1) && (bVertical == 0))
	{	 
		SPI_LCDWrite_CMD(0x36);    // Memory Access Control 
		SPI_LCDWrite_DATA(0xA8);
	}
	else if((bHorizontal == 0) && (bVertical == 1))
	{
		SPI_LCDWrite_CMD(0x36);    // Memory Access Control 
		SPI_LCDWrite_DATA(0x08);		
	}	 
	
	SPI_LCDWrite_CMD(0x3A);   
	SPI_LCDWrite_DATA(0x55); 

	SPI_LCDWrite_CMD(0xB1);   
	SPI_LCDWrite_DATA(0x00);   
	SPI_LCDWrite_DATA(0x18); 
	 
	SPI_LCDWrite_CMD(0xB6);    // Display Function Control 
	SPI_LCDWrite_DATA(0x0A); 
	SPI_LCDWrite_DATA(0xA2); 	 
	 
	SPI_LCDWrite_CMD(0xF2);    // 3Gamma Function Disable 
	SPI_LCDWrite_DATA(0x00); 
	 
	SPI_LCDWrite_CMD(0x26);    //Gamma curve selected 
	SPI_LCDWrite_DATA(0x01); 
	 
	SPI_LCDWrite_CMD(0xE0);    //Set Gamma 
	SPI_LCDWrite_DATA(0x0F); 
	SPI_LCDWrite_DATA(0x20); 
	SPI_LCDWrite_DATA(0x1E); 
	SPI_LCDWrite_DATA(0x09); 
	SPI_LCDWrite_DATA(0x12); 
	SPI_LCDWrite_DATA(0x0B); 
	SPI_LCDWrite_DATA(0x50); 
	SPI_LCDWrite_DATA(0XBA); 
	SPI_LCDWrite_DATA(0x44); 
	SPI_LCDWrite_DATA(0x09); 
	SPI_LCDWrite_DATA(0x14); 
	SPI_LCDWrite_DATA(0x05); 
	SPI_LCDWrite_DATA(0x23); 
	SPI_LCDWrite_DATA(0x21); 
	SPI_LCDWrite_DATA(0x00); 
	 
	SPI_LCDWrite_CMD(0XE1);    //Set Gamma 
	SPI_LCDWrite_DATA(0x00); 
	SPI_LCDWrite_DATA(0x19); 
	SPI_LCDWrite_DATA(0x19); 
	SPI_LCDWrite_DATA(0x00); 
	SPI_LCDWrite_DATA(0x12); 
	SPI_LCDWrite_DATA(0x07); 
	SPI_LCDWrite_DATA(0x2D); 
	SPI_LCDWrite_DATA(0x28); 
	SPI_LCDWrite_DATA(0x3F); 
	SPI_LCDWrite_DATA(0x02); 
	SPI_LCDWrite_DATA(0x0A); 
	SPI_LCDWrite_DATA(0x08); 
	SPI_LCDWrite_DATA(0x25); 
	SPI_LCDWrite_DATA(0x2D); 
	SPI_LCDWrite_DATA(0x0F); 
	
	SPI_LCDWrite_CMD(0x11);    //Exit Sleep 

	UT_DelayNms(150);

	SPI_LCDWrite_CMD(0x29);    //Display on 
	
	if((bHorizontal == 1) && (bVertical == 0))
	{
		SPI_LCDWrite_CMD(0x2A);
		SPI_LCDWrite_DATA(0>>8);
		SPI_LCDWrite_DATA(0);
		SPI_LCDWrite_DATA((LCD_X_SIZE_H-1)>>8);
		SPI_LCDWrite_DATA(((LCD_X_SIZE_H-1)&0xFF));
		
		SPI_LCDWrite_CMD(0x2B);   
		SPI_LCDWrite_DATA(0>>8);
		SPI_LCDWrite_DATA(0);
		SPI_LCDWrite_DATA((LCD_Y_SIZE_H-1)>>8);
		SPI_LCDWrite_DATA(((LCD_Y_SIZE_H-1)&0xFF));
	}
	else if((bHorizontal == 0) && (bVertical == 1))
	{
		SPI_LCDWrite_CMD(0x2A);
		SPI_LCDWrite_DATA(0>>8);
		SPI_LCDWrite_DATA(0);
		SPI_LCDWrite_DATA((LCD_X_SIZE_V-1)>>8);
		SPI_LCDWrite_DATA(((LCD_X_SIZE_V-1)&0xFF));
		
		SPI_LCDWrite_CMD(0x2B);   
		SPI_LCDWrite_DATA(0>>8);
		SPI_LCDWrite_DATA(0);
		SPI_LCDWrite_DATA((LCD_Y_SIZE_V-1)>>8);
		SPI_LCDWrite_DATA(((LCD_Y_SIZE_V-1)&0xFF));
	}
	
  SPI_LCDWrite_CMD(0x2C);	

	LCD_Clear(BLACK);
}
/*****************************************************************************
* Function		: LCD_Clear
* Description	: Clear the LCD with one color
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LCD_Clear(uint16_t color)
{
	uint32_t i;
	
	//The panel size : 320*240 = 76800//	
	for(i=0;i<76800;i++)     
	{	
			SPI_LCDWrite_DATA(color>>8);
			SPI_LCDWrite_DATA(color);		
	}
}  

/*****************************************************************************
* Function		: SPI_LCDWrite_CMD
* Description	: 
* Input			: None
* Output		: 
* Return		: None
* Note			: None
*****************************************************************************/
void SPI_LCDWrite_CMD(uint8_t bCMD)
{
	__SPI1_CLR_SEL0;
	__LCD_SET_DC_LOW;	
	
	SPI1_TX_DATA(bCMD);
	
	__SPI1_SET_SEL0;		
}

/*****************************************************************************
* Function		: SPI_LCDWrite_DATA
* Description	: 
* Input			: None
* Output		: 
* Return		: None
* Note			: None
*****************************************************************************/
void SPI_LCDWrite_DATA(uint8_t bData)
{
	__SPI1_CLR_SEL0;
	__LCD_SET_DC_HIGH;
	
	SPI1_TX_DATA(bData);
	
	__SPI1_SET_SEL0;		
}
