/***************************************************************************//**
 *   @file   HardwareProfile.h
 *   @brief  Définition des pattes du microcontrolleur pour le projet PLCD
 *   @author Kévin MARCHAND(Kevin.marchand@intelligence-electronique.com)
********************************************************************************
 * Copyright 2014(c) PLCD, Inc.
 *
 * Définition des pattes du microcontrolleur pour le projet PLCD
 * Plateforme utiliser :
 * - dsPIC33EP512MU810
 *
********************************************************************************
 *   SVN Revision: 1
*******************************************************************************/
 
#ifndef HARDWARE_PROFILE_H
#define HARDWARE_PROFILE_H
/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
//#include "Compiler.h"
#include "system.h"
#include "GenericTypeDefs.h"
/******************************************************************************/
/************************* Define Declarations ********************************/
/******************************************************************************/
// These directly influence timed events using the Tick module.  They also are used for UART and SPI baud rate generation.
#define GetSystemClock()                (SYS_FREQ)                              // Hz
#define GetInstructionClock()           (GetSystemClock()/2)                    // Normally GetSystemClock()/4 for PIC18, GetSystemClock()/2 for PIC24/dsPIC, and GetSystemClock()/1 for PIC32.  Might need changing if using Doze modes.
#define GetPeripheralClock()            (GetSystemClock()/2)                    // Normally GetSystemClock()/4 for PIC18, GetSystemClock()/2 for PIC24/dsPIC, and GetSystemClock()/1 for PIC32.  Divisor may be different if using a PIC32 since it's configurable.

//!************ LED/Bouton I/O pins *************
// LEDs
#define LED0_TRIS                       (TRISDbits.TRISD0)                      // Ref LED1
#define LED0_IO                         (LATDbits.LATD0)
#define LED1_TRIS                       (TRISDbits.TRISD1)                      // Ref LED2
#define LED1_IO                         (LATDbits.LATD1)
#define mLED1_On()                      (LED0_IO = 0)
#define mLED2_On()                      (LED1_IO = 0)
#define mLED1_Off()                     (LED0_IO = 1)
#define mLED2_Off()                     (LED1_IO = 1)
#define mLED1_Tgl()                     (LED0_IO = !LED0_IO)
#define mLED2_Tgl()                     (LED1_IO = !LED1_IO)

// boutons
#define BUTTON0_TRIS                    (TRISDbits.TRISD6)                      // Ref SW1
#define	BUTTON0_IO                      (PORTDbits.RD6)
#define BUTTON1_TRIS                    (TRISDbits.TRISD7)                      // Ref SW2
#define	BUTTON1_IO                      (PORTDbits.RD7)

/************* UART I/O pins *************/
// - UART1 : Interface RS232 I/O pins
#define UART1TX_TRIS                    (TRISFbits.TRISF5)
#define UART1TX_IO                      (PORTFbits.RF5)                         // RP101
#define UART1RX_TRIS                    (TRISFbits.TRISF4)
#define UART1RX_IO                      (PORTFbits.RF4)                         // RP100
#define UART1CTS_TRIS                   (TRISDbits.TRISD14)
#define UART1CTS_IO                     (PORTDbits.RD14)                        // RPI78
#define UART1RTS_TRIS                   (TRISDbits.TRISD15)
#define UART1RTS_IO                     (PORTDbits.RD15)                        // RP79
#define UART1SD_TRIS                    (TRISFbits.TRISF3)
#define UART1SD_IO                      (LATFbits.LATF3)

// - UART2 : Interface PROFIBUS I/O pins
#define UART2TX_TRIS                    (TRISFbits.TRISF2)
#define UART2TX_IO                      (PORTFbits.RF2)                         // RP98
#define UART2RX_TRIS                    (TRISFbits.TRISF8)
#define UART2RX_IO                      (PORTFbits.RF8)                         // RP104

// - UART3 : Interface RS485 I/O pins
#define UART3TX_TRIS                    (TRISDbits.TRISD3)
#define UART3TX_IO                      (PORTDbits.RD3)                         // RP67
#define UART3RX_TRIS                    (TRISDbits.TRISD12)
#define UART3RX_IO                      (PORTDbits.RD12)                        // RPI76
#define UART3TXEN_TRIS                  (TRISDbits.TRISD2)
#define UART3TXEN_IO                    (PORTDbits.RD2)                         // RP66

// - UART4 : Interface RS485 I/O pins
#define UART4TX_TRIS                    (TRISDbits.TRISD4)
#define UART4TX_IO                      (PORTDbits.RD4)                         // RP68
#define UART4RX_TRIS                    (TRISDbits.TRISD5)
#define UART4RX_IO                      (PORTDbits.RD5)                         // RP69
#define UART4TXEN_TRIS                  (TRISDbits.TRISD13)
#define UART4TXEN_IO                    (PORTDbits.RD13)

/************* ECAN I/O pins *************/
// - ECAN1 : Interface CAN I/O pins
#define ECANTX_TRIS                     (TRISGbits.TRISG0)
#define ECANTX_IO                       (PORTGbits.RG0)                         // RP112
#define ECANRX_TRIS                     (TRISAbits.TRISA6)
#define ECANRX_IO                       (PORTAbits.RA6)                         // RPI22

/************* Digital/Analog et INT I/O pins *************/
// Digital Inputs pins
#define IN0_TRIS                        (TRISBbits.TRISB0)
#define IN0_IO                          (PORTBbits.RB0)
#define IN1_TRIS                        (TRISBbits.TRISB1)
#define IN1_IO                          (PORTBbits.RB1)
#define IN2_TRIS                        (TRISBbits.TRISB2)
#define IN2_IO                          (PORTBbits.RB2)
#define IN3_TRIS                        (TRISBbits.TRISB3)
#define IN3_IO                          (PORTBbits.RB3)
#define IN4_TRIS                        (TRISBbits.TRISB4)
#define IN4_IO                          (PORTBbits.RB4)
#define IN5_TRIS                        (TRISBbits.TRISB5)
#define IN5_IO                          (PORTBbits.RB5)
#define IN6_TRIS                        (TRISAbits.TRISA4)
#define IN6_IO                          (PORTAbits.RA4)
#define IN7_TRIS                        (TRISAbits.TRISA3)
#define IN7_IO                          (PORTAbits.RA3)

#define C1_TRIS                         (TRISCbits.TRISC2)
#define C1_IO                           (PORTCbits.RC2)
#define C2_TRIS                         (TRISAbits.TRISA5)
#define C2_IO                           (PORTAbits.RA5)

// Analog Inputs pins
#define AN20_TRIS                       (TRISEbits.TRISE8)
#define AN20_IO                         (PORTEbits.RE8)                         // RPI88
#define AN21_TRIS                       (TRISEbits.TRISE9)
#define AN21_IO                         (PORTEbits.RE9)                         // RPI89

// Digital Outputs pins
#define OUT0_TRIS                       (TRISBbits.TRISB8)
#define OUT0_IO                         (LATBbits.LATB8)                        //(PORTBbits.RB8)
#define OUT1_TRIS                       (TRISBbits.TRISB9)
#define OUT1_IO                         (LATBbits.LATB9)
#define OUT2_TRIS                       (TRISBbits.TRISB10)
#define OUT2_IO                         (LATBbits.LATB10)
#define OUT3_TRIS                       (TRISBbits.TRISB11)
#define OUT3_IO                         (LATBbits.LATB11)
#define OUT4_TRIS                       (TRISBbits.TRISB12)
#define OUT4_IO                         (LATBbits.LATB12)
#define OUT5_TRIS                       (TRISBbits.TRISB13)
#define OUT5_IO                         (LATBbits.LATB13)
#define OUT6_TRIS                       (TRISBbits.TRISB14)
#define OUT6_IO                         (LATBbits.LATB14)
#define OUT7_TRIS                       (TRISBbits.TRISB15)
#define OUT7_IO                         (LATBbits.LATB15)

// Interruption I/O pins PPS sur Entrée INT1 et INT2
#define INT1_TRIS                       (TRISAbits.TRISA14)
#define	INT1_IO                         (PORTAbits.RA14)                        // RPI30
#define INT2_TRIS                       (TRISAbits.TRISA15)
#define	INT2_IO                         (PORTAbits.RA15)                        // RPI31

/************* SPI I/O pins *************/
// - SPI1 :  ADC 24 bits I/O pins
#define AD_CS_TRIS                      (TRISCbits.TRISC1)
#define AD_CS_IO                        (PORTCbits.RC1)         
#define AD_SCLK_TRIS                    (TRISEbits.TRISE7)
#define AD_SCLK_IO                      (PORTEbits.RE7)                         // RP87
#define AD_DOUT_TRIS                    (TRISEbits.TRISE5)
#define AD_DOUT_IO                      (PORTEbits.RE5)                         // RP85
#define AD_DIN_TRIS                     (TRISEbits.TRISE6)
#define AD_DIN_IO                       (PORTEbits.RE6)                         // RPI86
#define AD_SYNC_TRIS                    (TRISGbits.TRISG15)
#define AD_SYNC_IO                      (PORTGbits.RG15)                        // RP127
// - SPI2 : Ethernet ENC28J60 I/O pins (SPI2 pins non remappable)
//#define ENC_CS_TRIS                   (TRISFbits.TRISF12)
//#define ENC_CS_IO                     (LATFbits.LATF12)
#define ENC_CS_TRIS                     (TRISCbits.TRISC4)
#define ENC_CS_IO                       (LATCbits.LATC4)
// SPI SCK, SDI, SDO pins are automatically controlled by the PIC24/dsPIC SPI module
#define ENC_SCK_TRIS                    (TRISGbits.TRISG6)
#define ENC_SDI_TRIS                    (TRISGbits.TRISG7)
#define ENC_SDO_TRIS                    (TRISGbits.TRISG8)
// Registre du SPI
#define ENC_SPI_IF                      (IFS2bits.SPI2IF)
#define ENC_SSPBUF                      (SPI2BUF)
#define ENC_SPISTAT                     (SPI2STAT)
#define ENC_SPISTATbits                 (SPI2STATbits)
#define ENC_SPICON1                     (SPI2CON1)
#define ENC_SPICON1bits                 (SPI2CON1bits)
#define ENC_SPICON2                     (SPI2CON2)

// - no peripheral SPI FLASH 64Mo I/O pins
#define FLASH_CS_TRIS                   (TRISGbits.TRISG13)
#define FLASH_CS_IO                     (LATGbits.LATG13)
#define FLASH_SCK_TRIS                  (TRISEbits.TRISE2)                      // RP82
#define FLASH_SCK_IO                    (LATEbits.LATE2)                        // RP82
#define FLASH_SDI_TRIS                  (TRISEbits.TRISE4)
#define FLASH_SDI_IO                    (PORTEbits.RE4)                         // RPI83
#define FLASH_SDO_TRIS                  (TRISEbits.TRISE3)                      // RP84
#define FLASH_SDO_IO                    (LATEbits.LATE3)                        // RP84

// - SPI3 :  B40 module I/O pins
#define B40_CS_TRIS                     (TRISFbits.TRISF1)
#define B40_CS_IO                       (LATFbits.LATF1)
#define B40_SCLK_TRIS                   (TRISFbits.TRISF0)
//#define B40_SCLK_IO                   (PORTFbits.RF0)                         // RP96
#define B40_MOSI_TRIS                   (TRISDbits.TRISD7)
//#define B40_MOSI_IO                   (PORTDbits.RD7)                         // RP71
#define B40_MISO_TRIS                   (TRISDbits.TRISD11)
//#define B40_MISO_IO                   (PORTDbits.RD11)                        // RPI75
#define B40_RESET_TRIS                  (TRISGbits.TRISG1)
#define B40_RESET_IO                    (LATGbits.LATG1)
#define B40_MD_TRIS                     (TRISDbits.TRISD10)
#define B40_MD_IO                       (PORTDbits.RD10)
#define B40_IRQ_TRIS                    (TRISDbits.TRISD9)
#define B40_IRQ_IO                      (PORTDbits.RD9)                         // RPI73

// - SPI4 : EEPROM 25AA02E48 et FM25W256 256-Kbit nonvolatile memory I/O pins
#define EEPROM_CS_TRIS                  (TRISGbits.TRISG12)
#define EEPROM_CS_IO                    (LATGbits.LATG12)
#define EEPROM_SCK_TRIS                 (TRISGbits.TRISG14)                     // RP126
#define EEPROM_SDI_TRIS                 (TRISEbits.TRISE1)                      // RPI81
#define EEPROM_SDO_TRIS                 (TRISEbits.TRISE0)                      // RP80
// Registre du SPI
#define EEPROM_SPI_IF                   (IFS7bits.SPI4IF)
#define EEPROM_SSPBUF                   (SPI4BUF)
#define EEPROM_SPICON1                  (SPI4CON1)
#define EEPROM_SPICON1bits              (SPI4CON1bits)
#define EEPROM_SPICON2                  (SPI4CON2)
#define EEPROM_SPISTAT                  (SPI4STAT)
#define EEPROM_SPISTATbits              (SPI4STATbits)
// FRAM 
#define FRAM_CS_TRIS                    (TRISAbits.TRISA7)
#define FRAM_CS_IO                      (LATAbits.LATA7)
#define FRAM_SPI_IF                     (IFS7bits.SPI4IF)
#define FRAM_SSPBUF                     (SPI4BUF)
#define FRAM_SPICON1                    (SPI4CON1)
#define FRAM_SPICON1bits                (SPI4CON1bits)
#define FRAM_SPICON2                    (SPI4CON2)
#define FRAM_SPISTAT                    (SPI4STAT)
#define FRAM_SPISTATbits                (SPI4STATbits)
// Select which UART the STACK_USE_UART and STACK_USE_UART2TCP_BRIDGE
// options will use.  You can change these to U1BRG, U1MODE, etc. if you
// want to use the UART1 module instead of UART2.
/*#define UBRG                          U2BRG
#define UMODE                           U2MODE
#define USTA                            U2STA
#define BusyUART()                      BusyUART2()
#define CloseUART()                     CloseUART2()
#define ConfigIntUART(a)                ConfigIntUART2(a)
#define DataRdyUART()                   DataRdyUART2()
#define OpenUART(a,b,c)                 OpenUART2(a,b,c)
#define ReadUART()                      ReadUART2()
#define WriteUART(a)                    WriteUART2(a)
#define getsUART(a,b,c)                 getsUART2(a,b,c)
#define putsUART(a)                     putsUART2((unsigned int*)a)
#define getcUART()                      getcUART2()
#define putcUART(a)                     do{while(BusyUART()); WriteUART(a); while(BusyUART()); }while(0)
#define putrsUART(a)                    putrsUART2(a)*/

#endif // #ifndef HARDWARE_PROFILE_H
