/*********************************************************************
 *
 *	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
 ********************************************************************/
#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__)
// PIC33 processor
#include <p33Fxxxx.h>
#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(__PIC24H__)
// PIC24H processor
#include <p24Hxxxx.h>
#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 SSD1928 internal SDCard
//#define USE_SSD1928_INTERFACE

// 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 SSD1928
#define ENABLE_SD_MSD_DEMO
//Define TFT panel here
#define DISPLAY_PANEL TFT_LVC75Z779V1S
#define COLOR_DEPTH 16

/*********************************************************************
* 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

/*********************************************************************
* Overview: Horizontal and vertical display resolution
*                  (from the glass datasheet).
*********************************************************************/
// Newly defined for SSD1928, virtual display width and height
// For Main Window Line Address Offset REG[78h] and REG[79h]
// Initialization under ResetDevice, ssd1928.c
// For visual page scroll & pan
//#define USE_VIRTUAL_DISPLAY

#define DISP_HOR_RESOLUTION				320
#define DISP_VER_RESOLUTION				240

#ifdef USE_VIRTUAL_DISPLAY
	#define DISP_VIRTUAL_HOR_RESOLUTION		480
	#define DISP_VIRTUAL_VER_RESOLUTION		272
#else
	#define DISP_VIRTUAL_HOR_RESOLUTION		DISP_HOR_RESOLUTION		
	#define DISP_VIRTUAL_VER_RESOLUTION		DISP_VER_RESOLUTION
#endif
/*********************************************************************
* Overview: Image orientation (can be 0, 90, 180, 270 degrees).
*********************************************************************/
#define DISP_ORIENTATION				0
/*********************************************************************
* Overview: Panel Data Width
*********************************************************************/
#define DISP_DATA_WIDTH             8
/*********************************************************************
* 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			2  
#define DISP_HOR_BACK_PORCH			66
#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
		
/* End of definition for DISPLAY_PANEL == LVC75Z779V1S */

/*********************************************************************
* IO FOR THE BEEPER
*********************************************************************/

#define BEEP_TRIS_BIT			TRISDbits.TRISD0
#define BEEP_LAT_BIT				LATDbits.LATD0

/*********************************************************************
* IOS FOR THE EEPROM SPI ON PIC24-EVAL Rev. C2 BOARD
*********************************************************************/

#define EEPROM_CS_TRIS			TRISGbits.TRISG13
#define EEPROM_CS_LAT			LATGbits.LATG13
#define EEPROM_SCK_TRIS			TRISGbits.TRISG6
#define EEPROM_SDO_TRIS			TRISGbits.TRISG7
#define EEPROM_SDI_TRIS			TRISGbits.TRISG8

/*********************************************************************
* IOS FOR THE SSD1928 CONTROLLER ON PIC24-EVAL Rev. C2 BOARD
*********************************************************************/

// Definition for SLEEP pin of SSD1928
#define SLEEP_TRIS_BIT			TRISGbits.TRISG15
#define SLEEP_LAT_BIT			LATGbits.LATG15

// Definition 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

// Definition 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

// Defintion for backlight control
// !! Deviation from TechToys !! SPI1 port required, so RG3 instead of 
// !! RF8 used for LCD enable (Hardware modification)
#define LED_TRIS_BIT				TRISGbits.TRISG3
#define LED_LAT_BIT				LATGbits.LATG3

/*********************************************************************
* IOS FOR THE TOUCH SCREEN ON PIC24-EVAL Rev. C2 BOARD
*********************************************************************/

// ADC channel constants
#ifdef __PIC32MX__
	#define ADC_TEMP				ADC_CH0_POS_SAMPLEA_AN1
	#define ADC_POT				ADC_CH0_POS_SAMPLEA_AN0
#else
	#define ADC_TEMP				1
	#define ADC_POT				0
#endif


#ifdef __PIC32MX__
	#define ADC_XPOS				ADC_CH0_POS_SAMPLEA_AN11
	#define ADC_YPOS				ADC_CH0_POS_SAMPLEA_AN10
#else
	#define ADC_XPOS				11
	#define ADC_YPOS				10
#endif

// X port definitions
#if defined(__dsPIC33F__) || defined(__PIC24H__)
	#define ADPCFG_XPOS				AD1PCFGLbits.PCFG11
#else
	#define ADPCFG_XPOS				AD1PCFGbits.PCFG11
#endif
#define LAT_XPOS					LATBbits.LATB11
#define LAT_XNEG					LATBbits.LATB13
#define TRIS_XPOS					TRISBbits.TRISB11
#define TRIS_XNEG					TRISBbits.TRISB13
    
// Y port definitions
#if defined(__dsPIC33F__) || defined(__PIC24H__)
	#define ADPCFG_YPOS				AD1PCFGLbits.PCFG10
#else
	#define ADPCFG_YPOS				AD1PCFGbits.PCFG10
#endif
#define LAT_YPOS					LATBbits.LATB10
#define LAT_YNEG					LATBbits.LATB12 
#define TRIS_YPOS					TRISBbits.TRISB10
#define TRIS_YNEG					TRISBbits.TRISB12 

/*********************************************************************
* 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

/*********************************************************************
* RTCC DEFAULT INITIALIZATION (these are values to initialize the RTCC
*********************************************************************/
#define RTCC_DEFAULT_DAY        16      // 16
#define RTCC_DEFAULT_MONTH      01      // January
#define RTCC_DEFAULT_YEAR       10      // 2010
#define RTCC_DEFAULT_WEEKDAY    06      // Saturday
#define RTCC_DEFAULT_HOUR       12      // 12:15:01
#define RTCC_DEFAULT_MINUTE     15
#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

