/*********************************************************************
 *
 *	Hardware specific definitions
 *
 *********************************************************************
 * FileName:        HardwareProfile_xxx.h
 * Dependencies:    None
 * Processor:       PIC24F, PIC24H, dsPIC, PIC32
 * Compiler:        Microchip C32 v1.00 or higher
 *	                 Microchip C30 v3.01 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * Copyright ?2002-2009 Microchip Technology Inc.  All rights 
 * reserved.
 *
 * Microchip licenses to you the right to use, modify, copy, and 
 * distribute: 
 * (i)  the Software when embedded on a Microchip microcontroller or 
 *      digital signal controller product (“Device? which is 
 *      integrated into Licensee’s product; or
 * (ii) ONLY the Software driver source files ENC28J60.c and 
 *      ENC28J60.h ported to a non-Microchip device used in 
 *      conjunction with a Microchip ethernet controller for the 
 *      sole purpose of interfacing with the ethernet controller. 
 *
 * You should refer to the license agreement accompanying this 
 * Software for additional information regarding your rights and 
 * obligations.
 *
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS?WITHOUT 
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT 
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 
 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR 
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF 
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS 
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE 
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER 
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT 
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 *
 * Author               Date		Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Howard Schlunder		10/03/06	Original, copied from Compiler.h
 * Jayanth Murthy       06/25/09    dsPIC & PIC24H support 
 * Pradeep Budagutta	15 Sep 2009 Added PIC24FJ256DA210 Development Board Support
 ********************************************************************/

/*
******************************************************************************
* Revision:
* This file modified for Solomon SSD1963 driver development
* Programmer: John Leung
* Company: TechToys Co.
* Web: www.TechToys.com.hk
* Date: 16th Jan 2010
* (1) Add 	support for the hardware platform PIC24/32 Eval Rev2C with
*			PIC32MX360F512L MCU onboard + SSD1963 Eval Rev2A.
*			TFT panels can be TY700TFT800480 (7" TFT), TY430TFT480272 (4.3" TFT),
*			LVC75Z779V1S (3.5" TFT) with #define in this file
******************************************************************************
*/

#ifndef __HARDWARE_PROFILE_H
    #define __HARDWARE_PROFILE_H

/*********************************************************************
* GetSystemClock() returns system clock frequency.
* GetPeripheralClock() returns peripheral clock frequency.
* GetInstructionClock() returns instruction clock frequency.
********************************************************************/

#if defined(__PIC24F__)
// PIC24 processor
#define	GetSystemClock()			(32000000ul)         // 8 MHz x 4 PLL 
#define	GetPeripheralClock()		(GetSystemClock()/2)
#define	GetInstructionClock()	(GetSystemClock()/2)
#define _DI()	__asm__ volatile("disi #0x3FFF")
#define _EI()	__asm__ volatile("disi #0")

#elif defined(__dsPIC33F__) || defined(__PIC24H__)
// PIC33 or PIC24H processor
#define	GetSystemClock()			(80000000ul)         // 8 MHz x 4 PLL 
#define	GetPeripheralClock()		(GetSystemClock()/2)
#define	GetInstructionClock()	(GetSystemClock()/2)
#define _DI()	__asm__ volatile("disi #0x3FFF")
#define _EI()	__asm__ volatile("disi #0")

#elif defined(__PIC32MX__)
// PIC32MX processor
#define	GetSystemClock()			(80000000ul)         // 8 MHz/2 x 20 PLL
#define	GetPeripheralClock()		(GetSystemClock()/2)
#define	GetInstructionClock()	(GetSystemClock())
//#define	_PIC32_STARTER_
#endif

/*********************************************************************
* SELECT IO INTERFACE FOR SDCARD
*********************************************************************/

// Select your interface type for MDD FS
// This library currently only supports a single physical interface 
// layer at a time
// Description: Macro used to enable the SD-SPI phys. layer (SD-SPI.c/.h)
#define USE_SD_INTERFACE_WITH_SPI

// Description: Macro used to enable the CF-PMP physical layer (CF-PMP.c and .h)
//#define USE_CF_INTERFACE_WITH_PMP

// Description: Macro used to enable the CF-Manual physical layer 
// (CF-Bit transaction.c and .h)                                                                
//#define USE_MANUAL_CF_INTERFACE

// Description: Macro used to enable the USB Host physical layer (USB host MSD library)
//#define USE_USB_INTERFACE

/*********************************************************************
* UART SETTINGS
*********************************************************************/
#define BAUDRATE2               115200UL
#define BRG_DIV2                4
#define BRGH2                   1

/*********************************************************************
* START OF GRAPHICS RELATED MACROS
********************************************************************/

#define GRAPHICS_HARDWARE_PLATFORM PIC_EVK_V2C
#define USE_16BIT_PMP
//Add support for PIC32MX Starter Kit
//#define PIC32_STARTER_KIT
#define DISPLAY_CONTROLLER SSD1963
//Define TFT panel here
//#define DISPLAY_PANEL TY700TFT800480
//#define DISPLAY_PANEL TY430TFT480272
#define DISPLAY_PANEL LVC75Z779V1S
#define COLOR_DEPTH 16

#ifdef _GRAPHICS_H

/*********************************************************************
* DISPLAY SETTINGS 
********************************************************************/

// Error Checking
#ifndef GRAPHICS_HARDWARE_PLATFORM
	#error "Error: GRAPHICS_HARDWARE_PLATFORM is not defined!"
#endif

#ifndef DISPLAY_CONTROLLER
	#error "Error: DISPLAY_CONTROLLER not defined"
#endif

#if ((GRAPHICS_HARDWARE_PLATFORM == PIC_EVK_V2C) && !defined(DISPLAY_PANEL))
	#error "Error: DISPLAY_PANEL not defined"
#endif

#if (DISPLAY_PANEL == TY700TFT800480)
	/*********************************************************************
	* Overview: Image orientation (can be 0, 90, 180, 270 degrees).
	*********************************************************************/	
  	#define DISP_ORIENTATION    0
	/*********************************************************************
	* Overview: Panel Data Width (R,G,B) in (6,6,6)
	*********************************************************************/
	#define DISP_DATA_WIDTH                 18
	/*********************************************************************
	* Overview: Horizontal and vertical display resolution
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_RESOLUTION 800
	#define DISP_VER_RESOLUTION 480
	/*********************************************************************
	* Overview: Horizontal synchronization timing in pixels
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_PULSE_WIDTH		1
	#define DISP_HOR_BACK_PORCH			210
	#define DISP_HOR_FRONT_PORCH		45	
	/*********************************************************************
	* Overview: Vertical synchronization timing in lines
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_VER_PULSE_WIDTH		1
	#define DISP_VER_BACK_PORCH			34	
	#define DISP_VER_FRONT_PORCH		10
	/*********************************************************************
	* Definition for SPI interface for SSD1963 
	* Hardware dependent!
	*********************************************************************/
	#define GPIO3 3
	#define GPIO2 2
	#define GPIO1 1
	#define GPIO0 0
	#define LCD_RESET 	0
	#define LCD_SPENA	0
	#define LCD_SPCLK	0
	#define LCD_SPDAT	0
	/* End of definition for DISPLAY_PANEL == TY700TFT800480 */

#elif (DISPLAY_PANEL == TY430TFT480272)
	/*********************************************************************
	* Overview: Horizontal and vertical display resolution
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_RESOLUTION				480
	#define DISP_VER_RESOLUTION				272
	/*********************************************************************
	* Overview: Image orientation (can be 0, 90, 180, 270 degrees).
	*********************************************************************/
	#define DISP_ORIENTATION				0
	/*********************************************************************
	* Overview: Panel Data Width
	*********************************************************************/
	#define DISP_DATA_WIDTH                 24
	/*********************************************************************
	* Overview: LSHIFT Polarity Swap
	* If defined LSHIFT is a falling trigger
	*********************************************************************/
	//#define DISP_INV_LSHIFT
	/*********************************************************************
	* Overview: Horizontal synchronization timing in pixels
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_PULSE_WIDTH		41  
	#define DISP_HOR_BACK_PORCH			2
	#define DISP_HOR_FRONT_PORCH		2	
	/*********************************************************************
	* Overview: Vertical synchronization timing in lines
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_VER_PULSE_WIDTH		10
	#define DISP_VER_BACK_PORCH			2	
	#define DISP_VER_FRONT_PORCH		2
		
	/*********************************************************************
	* Definition for SPI interface for SSD1963 
	* Hardware dependent!
	*********************************************************************/
	#define GPIO3 3
	#define GPIO2 2
	#define GPIO1 1
	#define GPIO0 0
	#define LCD_RESET (1<<GPIO0)		//LCD Reset signal (Reset for display panel, NOT ssd1963)
	#define LCD_SPENA	0
	#define LCD_SPCLK	0
	#define LCD_SPDAT	0
	/* End of definition for DISPLAY_PANEL == TY430TFT480272 */

#elif (DISPLAY_PANEL == LVC75Z779V1S)
	/*********************************************************************
	* Overview: Horizontal and vertical display resolution
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_RESOLUTION				320
	#define DISP_VER_RESOLUTION				240
	/*********************************************************************
	* Overview: Image orientation (can be 0, 90, 180, 270 degrees).
	*********************************************************************/
	#define DISP_ORIENTATION				0
	/*********************************************************************
	* Overview: Panel Data Width
	*********************************************************************/
	#define DISP_DATA_WIDTH                 24
	/*********************************************************************
	* Overview: LSHIFT Polarity Swap
	* If defined LSHIFT is a falling trigger
	*********************************************************************/
	//#define DISP_INV_LSHIFT
	/*********************************************************************
	* Overview: Horizontal synchronization timing in pixels
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_HOR_PULSE_WIDTH		20  
	#define DISP_HOR_BACK_PORCH			48
	#define DISP_HOR_FRONT_PORCH		20	
	/*********************************************************************
	* Overview: Vertical synchronization timing in lines
	*                  (from the glass datasheet).
	*********************************************************************/
	#define DISP_VER_PULSE_WIDTH		2
	#define DISP_VER_BACK_PORCH			16
	#define DISP_VER_FRONT_PORCH		4
		
	/*********************************************************************
	* Definition for SPI interface for HIMAX 8238-A relevant to hardware 
	* layout; Hardware dependent!
	*********************************************************************/
	#define GPIO3 3
	#define GPIO2 2
	#define GPIO1 1
	#define GPIO0 0
	#define LCD_RESET (1<<GPIO0)		//LCD Reset signal (Reset for display panel, NOT ssd1963)
	#define LCD_SPENA (1<<GPIO1)		//SPI EN signal
	#define LCD_SPCLK (1<<GPIO2)		//SPI CLK
	#define LCD_SPDAT (1<<GPIO3)		//SPI DATA
	/* End of definition for DISPLAY_PANEL == LVC75Z779V1S */

#else
	#error "Graphics controller is not defined"
#endif // DISPLAY_CONTROLLER == ABC

/*********************************************************************
* IOS FOR THE DISPLAY CONTROLLER
*********************************************************************/

#if (GRAPHICS_HARDWARE_PLATFORM == PIC_EVK_V2C)
	/*
	*****************************************************************************
	* Use 8080 system interface. PMP for PIC32MX series & PIC24 series
	* The mother board is PIC24/32-Eval RevC2
	*
	* Function		mcu pins		LCD pins
	* ==========	========		========
	* TE			RG15			TE
	* DATA LINES	PMD[15:0]		D[15:0] in 16-bit addressing
	* RESET			RA7				/RESET
	* CHIP SELECT	RD11/PMCS1		/CS
	* COMMAND/DATA	RA6				RS 
	* WR STROBE		RD4/PMPWR		/WR
	* RD STROBE		RD5/PMPRD		/RD
	*
	*****************************************************************************
	*/
	//Definitions for TE pin of SSD1963
	#define TE_TRIS_BIT	   	TRISGbits.TRISG15
	#define TE_PORT_BIT	   	PORTGbits.RG15
	
	// Definitions for reset pin
	#define RST_TRIS_BIT       TRISAbits.TRISA7
	#define RST_LAT_BIT        LATAbits.LATA7
	
	//Definition for RS pin
	#define RS_TRIS_BIT			TRISAbits.TRISA6
	#define RS_LAT_BIT			LATAbits.LATA6
	
	// Definitions for CS pin
	#define CS_TRIS_BIT        TRISDbits.TRISD11
	#define CS_LAT_BIT         LATDbits.LATD11
	
	// Definition for WR pin
	#define WR_TRIS_BIT		   TRISDbits.TRISD4
	#define WR_LAT_BIT		   LATDbits.LATD4
	
	// Definition for RD pin
	#define RD_TRIS_BIT		   TRISDbits.TRISD5
	#define RD_LAT_BIT		   LATDbits.LATD5

#endif // (GRAPHICS_HARDWARE_PLATFORM == ...

/*********************************************************************
* IO FOR THE BEEPER
*********************************************************************/
    #if defined(__dsPIC33FJ128GP804__) || defined(__PIC24HJ128GP504__)
        #define BEEP_TRIS_BIT   TRISBbits.TRISB4
        #define BEEP_LAT_BIT    LATBbits.LATB4
    #else
        #define BEEP_TRIS_BIT   TRISDbits.TRISD0
        #define BEEP_LAT_BIT    LATDbits.LATD0
    #endif

/*********************************************************************
* IOS FOR THE SIDE BUTTONS
*********************************************************************/

#define BTN_S3						PORTAbits.RA14
#define BTN_S4						PORTAbits.RA15
#define BTN_S5						PORTDbits.RD8
#define BTN_S6						PORTDbits.RD9

/*********************************************************************
* IOS FOR THE LEDs
*********************************************************************/

#define LED0						PORTDbits.RD0
#define LED1						PORTDbits.RD1
#define LED2						PORTDbits.RD2
#define LED3						PORTDbits.RD3

/*********************************************************************
* IOS FOR THE EEPROM SPI
*********************************************************************/
#if defined (__C30__)
    #if defined( __PIC24FJ256GB110__ )
       #error "EEPROM for PIC24FJ256GB series not supported yet"
    #else
        #define EEPROM_CS_TRIS       TRISGbits.TRISG13
        #define EEPROM_CS_LAT        LATGbits.LATG13
    #endif
#elif defined( __PIC32MX__ )
    #define EEPROM_CS_TRIS       TRISGbits.TRISG13
    #define EEPROM_CS_LAT      	 LATGbits.LATG13
#endif
#define EEPROM_SCK_TRIS      TRISGbits.TRISG6
#define EEPROM_SDO_TRIS      TRISGbits.TRISG8
#define EEPROM_SDI_TRIS      TRISGbits.TRISG7

/*********************************************************************
* IOS FOR THE TOUCH SCREEN
*********************************************************************/

// ADC channel constants
#if defined(__PIC32MX__)
	#define ADC_POT     ADC_CH0_POS_SAMPLEA_AN0
#else
	#define ADC_POT     0
#endif

#if (GRAPHICS_HARDWARE_PLATFORM == PIC_EVK_V2C) //TechToys
	#ifdef __PIC32MX__
		#define ADC_XPOS        ADC_CH0_POS_SAMPLEA_AN10
		#define ADC_YPOS        ADC_CH0_POS_SAMPLEA_AN13
	#else
		#define ADC_XPOS        10
		#define ADC_YPOS        13
	#endif
	    
	// Y port definitions
	#define ADPCFG_XPOS     AD1PCFGbits.PCFG10
	#define LAT_XPOS        LATBbits.LATB10
	#define LAT_XNEG        LATBbits.LATB12
	#define TRIS_XPOS       TRISBbits.TRISB10
	#define TRIS_XNEG       TRISBbits.TRISB12
	    
	// X port definitions
	#define ADPCFG_YPOS     AD1PCFGbits.PCFG13
	#define LAT_YPOS        LATBbits.LATB13
	#define LAT_YNEG        LATBbits.LATB11 
	#define TRIS_YPOS       TRISBbits.TRISB13
	#define TRIS_YNEG       TRISBbits.TRISB11 
#endif

#endif //ifdef _GRAPHICS_H

/*********************************************************************
* IOS FOR THE UART
*********************************************************************/
    #if defined(__dsPIC33FJ128GP804__) || defined(__PIC24HJ128GP504__)
        #define TX_TRIS TRISCbits.TRISC1
        #define RX_TRIS TRISCbits.TRISC0
	#elif defined(__PIC24FJ256DA210__)    
        #define TX_TRIS TRISFbits.TRISF3
        #define RX_TRIS TRISCbits.TRISC14
    #else
        #define TX_TRIS TRISFbits.TRISF5
        #define RX_TRIS TRISFbits.TRISF4
    #endif

/*********************************************************************
* RTCC DEFAULT INITIALIZATION (these are values to initialize the RTCC
*********************************************************************/
    #define RTCC_DEFAULT_DAY        18      // 18
    #define RTCC_DEFAULT_MONTH      11      // November
    #define RTCC_DEFAULT_YEAR       9       // 2009
    #define RTCC_DEFAULT_WEEKDAY    05      // Friday
    #define RTCC_DEFAULT_HOUR       10      // 10:10:01
    #define RTCC_DEFAULT_MINUTE     10
    #define RTCC_DEFAULT_SECOND     01


/*********************************************************************
* IOS FOR THE SDCard SPI INTERFACE
*********************************************************************/
#ifdef USE_SD_INTERFACE_WITH_SPI
	#if defined(__PIC24F__) || defined(__dsPIC33F__) || defined(__PIC24H__)
		// Description: SD-SPI Chip Select Output bit
		#define SD_CS				PORTGbits.RG9
		// Description: SD-SPI Chip Select TRIS bit
		#define SD_CS_TRIS		TRISGbits.TRISG9
        
		// PIC33F has no RE8. Use the same physical pin, which is RA12
		#if defined(__dsPIC33F__) || defined(__PIC24H__)
			// Description: SD-SPI Card Detect Input bit
			#define SD_CD				PORTAbits.RA12
			// Description: SD-SPI Card Detect TRIS bit
			#define SD_CD_TRIS		TRISAbits.TRISA12
		#else        
			// Description: SD-SPI Card Detect Input bit
			#define SD_CD				PORTEbits.RE8
			// Description: SD-SPI Card Detect TRIS bit
			#define SD_CD_TRIS		TRISEbits.TRISE8
		#endif

		// Description: SD-SPI Write Protect Check Input bit
		#define SD_WE				PORTCbits.RC3
		// Description: SD-SPI Write Protect Check TRIS bit
		#define SD_WE_TRIS		TRISCbits.TRISC3

		// Registers for the SPI module you want to use

		// Description: The main SPI control register
		#define SPICON1			SPI2CON1
		// Description: The SPI status register
		#define SPISTAT			SPI2STAT
		// Description: The SPI Buffer
		#define SPIBUF				SPI2BUF
		// Description: The receive buffer full bit in the SPI status register
		#define SPISTAT_RBF		SPI2STATbits.SPIRBF
		// Description: The bitwise define for the SPI control register (i.e. _____bits)
		#define SPICON1bits		SPI2CON1bits
		// Description: The bitwise define for the SPI status register (i.e. _____bits)
		#define SPISTATbits		SPI2STATbits
		// Description: The enable bit for the SPI module
		#define SPIENABLE			SPISTATbits.SPIEN

		// Tris pins for SCK/SDI/SDO lines

		// Description: The TRIS bit for the SCK pin
		#define SPICLOCK			TRISGbits.TRISG6
		// Description: The TRIS bit for the SDI pin
		#define SPIIN				TRISGbits.TRISG7
		// Description: The TRIS bit for the SDO pin
		#define SPIOUT				TRISGbits.TRISG8

		// Will generate an error if the clock speed is too low to interface to the card
		#if (GetSystemClock() < 100000)
			#error Clock speed must exceed 100 kHz
		#endif    
	
	#elif defined __PIC32MX__ 
		// Registers for the SPI module you want to use
		//SPI Configuration
		#define SPI_START_CFG_1		(PRI_PRESCAL_64_1 | SEC_PRESCAL_8_1 | MASTER_ENABLE_ON | SPI_CKE_ON | SPI_SMP_ON)
		#define SPI_START_CFG_2		(SPI_ENABLE)

		// Define the SPI frequency
		#define SPI_FREQUENCY		(20000000)
            
		// Description: SD-SPI Chip Select Output bit
		#define SD_CS              PORTGbits.RG9
		// Description: SD-SPI Chip Select TRIS bit
		#define SD_CS_TRIS         TRISGbits.TRISG9
            
		// Description: SD-SPI Card Detect Input bit
		#define SD_CD              PORTEbits.RE8
		// Description: SD-SPI Card Detect TRIS bit
		#define SD_CD_TRIS         TRISEbits.TRISE8

		// Description: SD-SPI Write Protect Check Input bit
		#define SD_WE              PORTCbits.RC3
		// Description: SD-SPI Write Protect Check TRIS bit
		#define SD_WE_TRIS         TRISCbits.TRISC3
            
		// Description: The main SPI control register
		#define SPICON1            SPI2CON
		// Description: The SPI status register
		#define SPISTAT            SPI2STAT
		// Description: The SPI Buffer
		#define SPIBUF             SPI2BUF
		// Description: The receive buffer full bit in the SPI status register
		#define SPISTAT_RBF        SPI2STATbits.SPIRBF
		// Description: The bitwise define for the SPI control register (i.e. _____bits)
		#define SPICON1bits        SPI2CONbits
		// Description: The bitwise define for the SPI status register (i.e. _____bits)
		#define SPISTATbits        SPI2STATbits
		// Description: The enable bit for the SPI module
		#define SPIENABLE          SPI2CONbits.ON
		// Description: The definition for the SPI baud rate generator register (PIC32)
		#define SPIBRG			    SPI2BRG

		// Tris pins for SCK/SDI/SDO lines

		// Description: The TRIS bit for the SCK pin
		#define SPICLOCK           TRISGbits.TRISG6
		// Description: The TRIS bit for the SDI pin
		#define SPIIN              TRISGbits.TRISG7
		// Description: The TRIS bit for the SDO pin
		#define SPIOUT             TRISGbits.TRISG8
		//SPI library functions
		#define putcSPI            putcSPI2
		#define getcSPI            getcSPI2
		#define OpenSPI(config1, config2)   OpenSPI2(config1, config2)
      
		// Will generate an error if the clock speed is too low to interface to the card
		#if (GetSystemClock() < 100000)
			#error Clock speed must exceed 100 kHz
		#endif    
	#endif //#ifdef __PIC24F__
#endif //#ifdef USE_SD_INTERFACE_WITH_SPI


#endif // __HARDWARE_PROFILE_H


