/*********************************************************************
 *
 *	Hardware specific definitions
 *
 *********************************************************************
 * FileName:        HardwareProfile.h
 * Dependencies:    None
 * Processor:       PIC24F, PIC24H, PIC32MX
 * Compiler:        Microchip C32 v1.00 or higher
 *					Microchip C30 v3.01 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * Copyright ?2002-2008 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
 ********************************************************************/


/*
 ********************************************************************
 * Hardware profile for a third party LCD module added for the hardware
 * 3.5" QVGA 16.7m color TFT LCD Module Evaluation Kit
 * Item number LVC75Z779 Eval Board (Version 1E)
 * stack on PIC24 Eval Board C2
 *
 * Author: John Leung
 * File: HardwareProfile.h
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 7th March 2009
 ********************************************************************
*/

/*
 ********************************************************************
 * Version 310309
 * (1) Add hardware profile for SD interface to PIC24-Eval-C2 board
 *	   to support MDD FS, the Microchip File System
 * 
 * Author: John Leung
 * File: HardwareProfile.h
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 31th March 2009
 ********************************************************************
*/

/*
 ********************************************************************
 * Version 080409
 * (1) Add support for PIC24FJ series
 * 
 * Author: John Leung
 * File: HardwareProfile.h
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 08 April 2009
 ********************************************************************
*/

#ifndef __HARDWARE_PROFILE_H
#define __HARDWARE_PROFILE_H

/*********************************************************************
* GetSystemClock() returns system clock frequency.
*
* GetPeripheralClock() returns peripheral clock frequency.
*
* GetInstructionClock() returns instruction clock frequency.
*
********************************************************************/
#include "GraphicsConfig.h"

#if defined(__PIC24F__)	|| defined(__PIC24H__)
    // PIC24 processor
#define	GetSystemClock()            (32000000ul)         // 8 MHz x 4 PLL 
#define	GetPeripheralClock()		(GetSystemClock()/2)
#define	GetInstructionClock()		(GetSystemClock()/2)

#elif defined(__PIC32MX__)
	// PIC32MX processor
#define	GetSystemClock()            (72000000ul)         // 8 MHz/2 x 18 PLL
#define	GetPeripheralClock()		(GetSystemClock()/(1<<OSCCONbits.PBDIV))
//#define	GetInstructionClock()		(GetSystemClock()/PFMWSbits.CHECON) //original
#define	GetInstructionClock()		(GetSystemClock())
#endif


/*********************************************************************
* IOS FOR THE DISPLAY CONTROLLER
*********************************************************************/
#if (GRAPHICS_PICTAIL_VERSION == 1)

// Definitions for reset pin
#define RST_TRIS_BIT       TRISCbits.TRISC1
#define RST_LAT_BIT        LATCbits.LATC1

// Definitions for RS pin
#define RS_TRIS_BIT        TRISBbits.TRISB15
#define RS_LAT_BIT         LATBbits.LATB15

// Definitions for CS pin
#define CS_TRIS_BIT        TRISDbits.TRISD8
#define CS_LAT_BIT         LATDbits.LATD8

// Definitions for FLASH CS pin 
#define CS_FLASH_LAT_BIT   LATDbits.LATD9
#define CS_FLASH_TRIS_BIT  TRISDbits.TRISD9


#elif (GRAPHICS_PICTAIL_VERSION == 2)

#if  (DISPLAY_CONTROLLER == LGDP4531)

// Definitions for reset pin
#define RST_TRIS_BIT       TRISCbits.TRISC1
#define RST_LAT_BIT        LATCbits.LATC1

// Definitions for RS pin
#define RS_TRIS_BIT        TRISCbits.TRISC2
#define RS_LAT_BIT         LATCbits.LATC2

// Definitions for CS pin
#define CS_TRIS_BIT        TRISDbits.TRISD10
#define CS_LAT_BIT         LATDbits.LATD10

// Definitions for FLASH CS pin 
#define CS_FLASH_LAT_BIT   LATDbits.LATD1
#define CS_FLASH_TRIS_BIT  TRISDbits.TRISD1

// Definitions for POWER ON pin
#define POWERON_LAT_BIT    LATCbits.LATC3
#define POWERON_TRIS_BIT   TRISCbits.TRISC3

#elif (DISPLAY_CONTROLLER == SSD1906)

// Definitions for reset line
#define RST_TRIS_BIT       TRISCbits.TRISC1
#define RST_LAT_BIT        LATCbits.LATC1

// Definitions for RS line
#define RS_TRIS_BIT        TRISCbits.TRISC2
#define RS_LAT_BIT         LATCbits.LATC2

// Definitions for CS line
#define CS_TRIS_BIT        TRISDbits.TRISD10
#define CS_LAT_BIT         LATDbits.LATD10

// Definitions for A0 line
#define A0_LAT_BIT         LATDbits.LATD3
#define A0_TRIS_BIT        TRISDbits.TRISD3

// Definitions for A17 line 
#define A17_LAT_BIT        LATGbits.LATG14
#define A17_TRIS_BIT       TRISGbits.TRISG14

#else

#error	GRAPHICS CONTROLLER IS NOT SUPPORTED

#endif  // (DISPLAY_CONTROLLER == ...

#elif (GRAPHICS_PICTAIL_VERSION == 3)

// Definitions for reset line
#define RST_TRIS_BIT       TRISCbits.TRISC1
#define RST_LAT_BIT        LATCbits.LATC1

// Definitions for RS line
#define RS_TRIS_BIT        TRISCbits.TRISC2
#define RS_LAT_BIT         LATCbits.LATC2

// Definitions for CS line
#define CS_TRIS_BIT        TRISDbits.TRISD10
#define CS_LAT_BIT         LATDbits.LATD10

#elif (GRAPHICS_PICTAIL_VERSION == GRAPHICS_THIRD_PARTY)
/*
*****************************************************************************
* Use 8080 system interface. PMP for PIC32MX series, otherwise, discrete
* IO method would be used for PIC24F series
*
* Function		mcu pins		LCD pins
* ==========	========		========
* SLEEP			RG15			SLEEP
* DATA LINES	PMD[7:0]		DAT[7:0] in 8-bit mode
* RESET			RA7				/RESET
* CHIP SELECT	RD11			/CS
* COMMAND/DATA	RA6				RS 
* WR STROBE		RD4/PMPWR		/WR
* RD STROBE		RD5/PMPRD		/RD
* LED backlight RF8				EN
*
*****************************************************************************
*/
//Definitions for SLEEP pin of SSD1928
#define SLEEP_TRIS_BIT	   TRISGbits.TRISG15
#define SLEEP_LAT_BIT	   LATGbits.LATG15

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

//Defintion for backlight control
#define LED_TRIS_BIT		TRISFbits.TRISF8
#define LED_LAT_BIT			LATFbits.LATF8

#endif // (GRAPHICS_PICTAIL_VERSION == ...


// Newly added for Version 310309
// 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 physical layer (SD-SPI.c and .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

#ifdef USE_SD_INTERFACE_WITH_SPI
	#ifdef __18CXX
		#error C18 NOT SUPPORTED YET
	#elif defined __PIC24F__
        // 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

        // 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 __18CXX
#endif //#ifdef USE_SD_INTERFACE_WITH_SPI

#endif // __HARDWARE_PROFILE_H
