/*
******************************************************************************
* Date: 17th April 2008
* This demonstration for QA example for the hardware platform
* PIC18LF4550-Eval-Rev4a + TY320T-240320-BO Rev 2C
* Microchip Graphics Library port to PIC18. Only Primitive functions ported
* with Font and Picture Drawn function removed just now.
*
* FileName:        ILI9320_LE.c
* Dependencies:    ILI9320_LE.h
* Processor:       PIC18LF4550
* Compiler:        MPLAB C18 student version
* Programmer : 	   John Leung (www.TechToys.com.hk)
* Note:		       Please refer to the original Software License Agreement
*				   from Microchip for details concerning Copyright
******************************************************************************
*/

/*****************************************************************************
 *  Module for Microchip Graphics Library
 *  ILITEK ILI9320 LCD controller driver
 *  Portrait orientation
 *****************************************************************************
 * FileName:        ILI9320P_8BIT.c
 * Dependencies:    Graphics.h
 * Processor:       PIC24
 * Compiler:       	MPLAB C30
 * Linker:          MPLAB LINK30
 * Company:         TechToys Company
 * Web presence:	www.TechToys.com.hk
 * Note:			Original source code from Microchip Graphic Library 
 *					ILI9320P_8BIT.c with Software License Agreement below
 *
 * Software License Agreement
 *
 * Copyright ?2007 Microchip Technology Inc.  All rights reserved.
 * Microchip licenses to you the right to use, modify, copy and distribute
 * Software only when embedded on a Microchip microcontroller or digital
 * signal controller, which is integrated into your product or third party
 * product (pursuant to the sublicense terms in the accompanying license
 * agreement).  
 *
 * You should refer to the license agreement accompanying this Software
 * for additional information regarding your rights and obligations.
 *
 * SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS?WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
 * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
 * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
 * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
 * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
 * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
 * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
 * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
 * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
 * OR OTHER SIMILAR COSTS.
 *
 * Author               Date        	Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * John Leung     		16 Nov 2007		Version 1.0 release
 *										GetPixel(SHORT x, SHORT y) not working yet
 *****************************************************************************/

#include "includes.h"

/////////////////////// LOCAL FUNCTIONS PROTOTYPES ////////////////////////////
void SetReg(BYTE index, WORD value);

// Color
WORD_VAL    _color;
// Clipping region control
SHORT 		_clipRgn;
// Clipping region borders
SHORT 		_clipLeft;
SHORT 		_clipTop;
SHORT 		_clipRight;
SHORT 		_clipBottom;

/*********************************************************************
* Function:  void  DelayMs(WORD time)
*
* PreCondition: none
*
* Input: time - delay in ms
*
* Output: none
*
* Side Effects: none
*
* Overview: delays execution on time specified in ms
*
* Note: Only true for a CPU clock of 5MHz for PIC18 series mcu
*
********************************************************************/
#define DELAY_1MS	Delay100TCYx(SYSCLK/100000)
void  DelayMs(WORD time){
	while(time--)
		DELAY_1MS;	
}

/*********************************************************************
* Function:  void  SetReg(BYTE index, WORD value)
*
* PreCondition: none
*
* Input: index - register number
*        value - value to be set
*
* Output: none
*
* Side Effects: none
*
* Overview: sets graphics controller register
*
* Note: none
*
********************************************************************/
void  SetReg(BYTE index, WORD value){

	WORD_VAL	Val;
	Val.Val = value;

    SetIndex(index);
    WriteData(Val.v[1],Val.v[0]);
}

/*********************************************************************
* Function:  void ResetDevice()
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: resets LCD, initializes PMP
*
* Note: none
*
********************************************************************/
void ResetDevice(void){

    // Set LCD CS pin as output
    CS_TRIS_BIT = 0;
    // Disable LCD 
    CS_LAT_BIT = 1;
    
    // Set reset pin as output
    RST_TRIS_BIT = 0;
    // Hold in reset
    RST_LAT_BIT = 0;
    
	// Set RS pin as output
	RS_TRIS_BIT = 0;
	RS_LAT_BIT = 0;

	// Set RD pin as output
	RD_TRIS_BIT = 0;
	RD_LAT_BIT = 1;

	// Set WR pin as output
	WR_TRIS_BIT = 0;
	WR_LAT_BIT = 1;

	// Set LE pin as output
	LE_TRIS_BIT = 0;
	LE_LAT_BIT  = 1;	//data latched from H->L

	// Set LED backlight as output high to turn ON the LCD backlight for 3.2" LCD module
	LED_TRIS_BIT = 0;
	LED_LAT_BIT	 = 0;

	// Set DATA BUS as output
	DAT_TRIS_BIT = 0x00;

    // Reset controller
    RST_LAT_BIT = 0;
    DelayMs(2);
    RST_LAT_BIT = 1;
    DelayMs(2);

    // Enable LCD 
    CS_LAT_BIT = 0;

	/*** Setup display for ILI9320 driver IC ***/ 
    SetReg(0xe5,0x8000);		//start osc
	SetReg(0x00,0x0001);
    DelayMs(10);
    SetReg(0xa4,0x0001);       	//calb
    
    SetReg(0x07,0x0000);		//display control(1)
	DelayMs(10);	

	/*** Display Setting ***/	
	SetReg(0x01, 0x0100);	//Driver output control (1)
	SetReg(0x02, 0x0700);	//LCD driving control
	SetReg(0x03, 0x1030); 	//Entry mode
	SetReg(0x04, 0x0000);	//Resize control
	SetReg(0x08, 0x0202);	//Display control (2)
	SetReg(0x09, 0x0000);	//Display control (3)

	/*** Power Control ***/
	SetReg(0x07, 0x0101);	//power control 1 BT, AP
	SetReg(0x17, 0x0001);
	SetReg(0x10, 0x0000);
	SetReg(0x11, 0x0007);	//power control 2 DC, VC
	SetReg(0x12, 0x0000);	//power control 3 VRH
	SetReg(0x13, 0x0000);	//power control 4 VCOM amplitude
	DelayMs(20);

  	SetReg(0x10, 0x16B0);             //power control 1 BT,AP
    SetReg(0x11, 0x0037);             //power control 2 DC,VC
	DelayMs(50);
    SetReg(0x12, 0x013E);             //power control 3 VRH
	DelayMs(50);
    SetReg(0x13, 0x1A00);             //power control 4 vcom amplitude
    SetReg(0x29, 0x000F);             //power control 7 VCOMH
	DelayMs(50);
    SetReg(0x20, 0x0000);              //Horizontal GRAM Address Set
    SetReg(0x21, 0x0000);              //Vertical GRAM Address Set

    SetReg(0x50, 0x0000);              //Horizontal Address Start Position
    SetReg(0x51, 0x00EF);              //Horizontal Address end Position (239)
    SetReg(0x52, 0x0000);              //Vertical Address Start Position
    SetReg(0x53, 0x013F);              //Vertical Address end Position (319)

    SetReg(0x60, 0x2700);              //Driver Output Control 2
    SetReg(0x61, 0x0001);              //Base Image Display Control
    SetReg(0x6a, 0x0000);              //Base Image Display Control

    SetReg(0x90, 0x0010);              //Panel Interface Control 1
    SetReg(0x92, 0x0000);              //Panel Interface Control 2
    SetReg(0x93, 0x0000);              //Panel Interface Control 3
    
 	/*** GAMMA Control ***/
    SetReg(0x30, 0x0007);
    SetReg(0x31, 0x0403);
    SetReg(0x32, 0x0404);   
    SetReg(0x35, 0x0002);
    SetReg(0x36, 0x0707);
    SetReg(0x37, 0x0606);
    SetReg(0x38, 0x0106);
    SetReg(0x39, 0x0007);
    SetReg(0x3c, 0x0700);
    SetReg(0x3d, 0x0707);

    SetReg(0x07, 0x0173);

    // Disable LCD 
    CS_LAT_BIT = 1;

    DelayMs(30);
}

/*********************************************************************
* Function: void PutPixel(SHORT x, SHORT y)
*
* PreCondition: none
*
* Input: x,y - pixel coordinates
*
* Output: none
*
* Side Effects: none
*
* Overview: puts pixel
*
* Note: none
*
********************************************************************/
void PutPixel(SHORT x, SHORT y){
DWORD_VAL address;
    if(_clipRgn){
        if(x<_clipLeft)
            return;
        if(x>_clipRight)
            return;
        if(y<_clipTop)
            return;
        if(y>_clipBottom)
            return;
    }
    CS_LAT_BIT = 0;
    address.Val = (long)LINE_MEM_PITCH*y + x;
    SetAddress(address.v[2],address.v[1],address.v[0]);
    WriteData(_color.v[1],_color.v[0]);   
    CS_LAT_BIT = 1;
}


/*********************************************************************
* Function: void Bar(SHORT left, SHORT top, SHORT right, SHORT bottom)
*
* PreCondition: none
*
* Input: left,top - top left corner coordinates,
*        right,bottom - bottom right corner coordinates
*
* Output: none
*
* Side Effects: none
*
* Overview: draws rectangle filled with current color
*
* Note: none
*
********************************************************************/
#ifdef USE_DRV_BAR
void Bar(SHORT left, SHORT top, SHORT right, SHORT bottom){
DWORD_VAL address;
register SHORT  x,y;

    if(_clipRgn){
        if(left<_clipLeft)
           left = _clipLeft;
        if(right>_clipRight)
           right= _clipRight;
        if(top<_clipTop)
           top = _clipTop;
        if(bottom>_clipBottom)
           bottom = _clipBottom;
    }

    address.Val = (DWORD)LINE_MEM_PITCH*top + left;

    CS_LAT_BIT = 0;
    for(y=top; y<bottom+1; y++){
        SetAddress(address.v[2],address.v[1],address.v[0]);
        for(x=left; x<right+1; x++){
            WriteData(_color.v[1],_color.v[0]);
        }
        address.Val += LINE_MEM_PITCH;
    }
    CS_LAT_BIT = 1;
}
#endif
/*********************************************************************
* Function: void ClearDevice(void)
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: clears screen with current color 
*
* Note: comment MoveTo(0,0) for now
*
********************************************************************/
#ifdef USE_DRV_CLEARDEVICE
void ClearDevice(void){
DWORD     counter;

    CS_LAT_BIT = 0;
    SetAddress(0,0,0);
    for(counter=0; counter<(DWORD)(GetMaxX()+1)*(GetMaxY()+1); counter++){
        WriteData(_color.v[1],_color.v[0]);
    }
    CS_LAT_BIT = 1;
    MoveTo(0,0);
}
#endif

