
#include <xc.h>
#include <stdint.h>
#include "sys_config.h"

// *****************************************************************************
/* Function:
    Map_GPIO_HW_Funtion()

  Summary:
    Routine to setup GPIO pin used as input/output analog/digital etc

  Description:
    Function initialises GPIO pins as input or output port pins,analog/digital 
    pins,remap the peripheral functions to desires RPx pins.

  Precondition:
    None.

  Parameters:
    None

  Returns:
    None.

  Remarks:
    None.
 */

void MapGPIOHWFuntion(void)
{
 
    /* ANALOG SIGNALS */
    // Configure OP-AMP/Comparator Pins
    // OA1 is not used in this application
    TRISBbits.TRISB2 = 1;   // PGEC1/OA1IN+/AN4/C1IN3-/C1IN1+/C2IN3-/RPI34/RB2
    ANSELBbits.ANSB2 = 1;
    TRISBbits.TRISB3 = 1;   // PGED1/OA1IN-/AN5/C1IN1-/(CTMUC)/RP35/RTCC/RB3
    ANSELBbits.ANSB3 = 1;
    TRISBbits.TRISB1 = 1;   // PGEC3/VREF+/CVREF+/OA1OUT/AN3/C1IN4-/C4IN2-/RPI33/CTED1/RB1
    ANSELBbits.ANSB1 = 1;

    // OA2 is used for amplifying Inverter A - PHASE2 Current (IPHASE2/IB)
    // SHUNT_HIGH_2_A(IPHASE2_POS_A/IB_POS_A) : PIM #73
    TRISAbits.TRISA1 = 1;    // OA2IN+/AN1/C2IN1+/RPI17/RA1
    ANSELAbits.ANSA1 = 1;
    // SHUNT_HIGH_SUM_A(IPHASE2_NEG_A/IB_NEG_A) : PIM #71
    TRISBbits.TRISB0 = 1;    // PGED3/OA2IN-/AN2/C2IN1-/SS1/RPI32/CTED2/RB0
    ANSELBbits.ANSB0 = 1;
    // IPHASE2_A/IB_A(OA2OUT) : PIM #72
    TRISAbits.TRISA0 = 1;    // OA2OUT/AN0/C2IN4-/C4IN3-/RPI16/RA0
    ANSELAbits.ANSA0 = 1;

    // OA5 is used for amplifying Inverter A - PHASE1 Current (IPHASE1/IA)
    // SHUNT_HIGH_1_A(IPHASE1_POS_A/IA_POS_A) : PIM #74
    TRISAbits.TRISA4 = 1;    // OA5IN+/AN24/C5IN3-/C5IN1+/SDO1/RP20/T1CK/RA4
    ANSELAbits.ANSA4 = 1;
    // SHUNT_HIGH_SUM_A(IPHASE1_NEG_A/IA_NEG_A) : PIM #81
    TRISBbits.TRISB9 = 1;    // TMS/OA5IN-/AN27//C5IN1-/RP41/RB9
    ANSELBbits.ANSB9 = 1;
    // IPHASE1_A/IB_A(OA5OUT) : PIM #77
    TRISBbits.TRISB7 = 1;    // OA5OUT/AN25/C5IN4-/RP39/INT0/RB7
    ANSELBbits.ANSB7 = 1;

    // OA3 is used for amplifying Inverter A - BUS Current (IBUS)
    // SHUNT_HIGH_SUM_A(IBUS_POS_A) : PIM #66
    TRISCbits.TRISC2 = 1; // OA3IN+/AN8/C3IN3-/C3IN1+/RPI50/U1RTS/BCLK1/FLT3/RC2
    ANSELCbits.ANSC2 = 1;
    // SHUNT_HIGH_SUM_A(IBUS_NEG_A) : PIM #67
    TRISCbits.TRISC1 = 1; // OA3IN-/AN7/C3IN1-/C4IN1-/RP49/RC1
    ANSELCbits.ANSC1 = 1;
    // IBUS_A(OA3OUT) : PIM #68
    TRISCbits.TRISC0 = 1; // OA3OUT/AN6/C3IN4-/C4IN4-/C4IN1+/RP48/OCFB/RC0
    ANSELCbits.ANSC0 = 1;

    // Inverter A DC bus voltage VDC_A
    // VBUS_A : PIM #35
    TRISAbits.TRISA12 = 1;          // AN10/RPI28/RA12
    ANSELAbits.ANSA12 = 1;

    // Back-EMF signals - Inverter A
    // VPHASE1_MA : PIM #22
    TRISEbits.TRISE8 = 1;           // AN21/RE8
    ANSELEbits.ANSE8 = 1;
    // VPHASE2_MA : PIM #21
    TRISEbits.TRISE15 = 1;          // AN15/RPI95/FLT8/PMPA0/RE15
    ANSELEbits.ANSE15 = 1;
    // VPHASE3_MA : PIM #20
    TRISEbits.TRISE9 = 1;           // AN20/RE9
    ANSELEbits.ANSE9 = 1;

    // RECN_MA/IPHASE3_MA/IBRAKE_A : PIM #24
    TRISCbits.TRISC11 = 1;          // AN11/C1IN2-/U1CTS/FLT4/PMPA12/RC11
    ANSELCbits.ANSC11 = 1;

    // DIGITAL INPUT/OUTPUT PINS

    // InverterA - PWM 0utputs
    // PWM1L_A : PIM #93  (RPI47/PWM1L1/T5CK/RB15)
    // PWM1H_A : PIM #94  (RPI46/PWM1H1/T3CK/RB14)
    // PWM2L_A : PIM #98  (RP45/PWM1L2/CTPLS/RB13)
    // PWM2H_A : PIM #99  (RP44/PWM1H2/RB12)
    // PWM3L_A : PIM #100 (RP43/PWM1L3/RB11)
    // PWM3H_A : PIM #03  (RP42/PWM1H3/RB10)
    TRISB = (TRISB & 0x03FF);        // 0b0000 00XX XXXX XXXX
    LATB  = (LATB & 0x03FF);         // Setting all PWM outputs as 'LOW'
    // BRAKE_EN_A : PIM #60
    TRISDbits.TRISD5 = 0 ;           // RP69/PMPRD/RD5
    
    // FAULT Pins
    // InvertA - Over Current fault O/P FAULT_MA
    // FAULT_MA : PIM #70
    LATCbits.LATC7 = 1;              // Setting FAULT_MA =  '1'
    TRISCbits.TRISC7 = 0;            // RP55/PMPBE/RC7

    // Hall inputs - Inverter A
    // HALLA_MA : PIM #80
    TRISAbits.TRISA8 = 1;            // SDA2/RPI24/PMPA9/RA8
    // HALLB_MA : PIM #47
    TRISCbits.TRISC6 = 1;            // RP54/RC6
    // HALLC_MA : PIM #48
    TRISFbits.TRISF0 = 1;            // RPI96/RF0
    // HOME_MA : PIM #61
    TRISCbits.TRISC10 = 1;           // AN48/CVREF2O/RPI58/PMPCS1/RC10

    // Gate Driver Interface
    // CE_A : PIM #82 (Output)
    TRISCbits.TRISC13 = 0;           // SOSCI/RP161/RC13
    // DE2_TX_A : PIM #76 (Output)
    TRISGbits.TRISG8 = 0;            // AN17/ASDA1/RP120/PMPA3/RG8
    // DE2_RX_A : PIM #83 (Input)
    TRISGbits.TRISG7 = 1;            // AN18/ASCL1/RP119/PMPA4/RG7

    /* INVERTER B SECTION */
    /* ANALOG SIGNALS */
    // Inverter B current sensing inputs
    // Op-AMP/CMP OA1/CMP1 is not used in this application
    // IBUS_MB : PIM #55
    TRISBbits.TRISB1 = 1;   // PGEC3/VREF+/CVREF+/OA1OUT/AN3/C1IN4-/C4IN2-/RPI33/CTED1/RB1
    ANSELBbits.ANSB1 = 1;
    // IPHASE1_MB(IA_B/IA_MB) : PIM #56
    TRISBbits.TRISB2 = 1;   // PGEC1/OA1IN+/AN4/C1IN3-/C1IN1+/C2IN3-/RPI34/RB2
    ANSELBbits.ANSB2 = 1;
    // IPHASE2_MB(IB_B/IB_MB) : PIM #57
    TRISBbits.TRISB3 = 1;   // PGED1/OA1IN-/AN5/C1IN1-/(CTMUC)/RP35/RTCC/RB3
    ANSELBbits.ANSB3 = 1;

    // Inverter B DC bus voltage VDC_B
    // VBUS_B : PIM #79
    TRISCbits.TRISC3 = 1;           // AN29/SCK1/RPI51/RC3
    ANSELCbits.ANSC3 = 1;

    // Back-EMF signals - Inverter A
    // VPHASE1_MB : PIM #14
    TRISGbits.TRISG9 = 1;           // AN16/RPI121/PMPA2/RG9
    ANSELGbits.ANSG9 = 1;
    // VPHASE2_MB : PIM #17
    TRISGbits.TRISG10 = 1;          // AN22/RG10
    ANSELGbits.ANSG10 = 1;
    // VPHASE3_MB : PIM #25
    TRISAbits.TRISA11 = 1;          // AN9/RPI27/RA11
    ANSELAbits.ANSA11 = 1;

    // RECN_MB/IPHASE3_MB/IBRAKE_B : PIM #44
    TRISAbits.TRISA9 = 1;           // AN28/SDI1/RPI25/RA9
    ANSELAbits.ANSA9 = 1;

    // DIGITAL INPUT/OUTPUT PINS

    // InverterB - PWM 0utputs
    // PWM1L_B : PIM #05  (TDI/PWM1L4/PMPD5/RA7)
    // PWM1H_B : PIM #04  (TDO/PWM1H4/PMPD4/RA10)
    // PWM2L_B : PIM #06  (PWM1L5/RD1)
    // PWM2H_B : PIM #07  (PWM1H5/RD2)
    // PWM3L_B : PIM #08  (PWM1L6/T9CK/RD3)
    // PWM3H_B : PIM #09  (PWM1H6/T8CK/RD4)
    TRISD = (TRISD & 0xFFE1);        // 0bXXXX XXXX XXX0 000X
    LATD  = (LATD & 0xFFE1);         // Setting four PWM outputs as 'LOW'
    TRISAbits.TRISA7 = 0;
    LATAbits.LATA7 = 0;
    TRISAbits.TRISA10 = 0;
    LATAbits.LATA10 = 0;

    // BRAKE_EN_B : PIM #53
    TRISFbits.TRISF5 = 0 ;           // AN45/RF5

    // FAULT Pins
    // InvertB - Over Current fault O/P FAULT_MB
    // FAULT_MB : PIM #10
    LATEbits.LATE1 = 1;              // Setting FAULT_MB =  '1'
    TRISEbits.TRISE1 = 0;            // AN41/RP81/RE1

    // Hall inputs - Inverter B
    // HALLA_MB : PIM #96
    TRISGbits.TRISG13 = 1;           // RP125/RG13
    // HALLB_MB : PIM #95
    TRISGbits.TRISG12 = 1;           // RP124/RG12
    // HALLC_MB : PIM #92
    TRISGbits.TRISG14 = 1;           // RP126/RG14
    // HOME_MB : PIM #89
    TRISGbits.TRISG0 = 1;            // RPI112/RG0

    // Gate Driver Interface
    // CE_B : PIM #90 (Output)
    TRISFbits.TRISF6 = 0;            // RF6
    LATFbits.LATF6 = 0;              // Setting CE_B = 'LOW'
    // DE2_TX_B : PIM #54 (Output)
    TRISBbits.TRISB8 = 0;            // TCK/AN26/CVREF1O/SOSCO/RP40/T4CK/RB8
    // DE2_RX_B : PIM #43 (Input)
    TRISEbits.TRISE0 = 1;            // AN40/RPI80/RE0

    // Other Functions

    /* ANALOG SIGNALS */
    // POT inputs
    // Potentiometer #1 input - used as Speed Reference
    // POT1 : PIM #32
    TRISEbits.TRISE13 = 1;          // AN13/C3IN2-/U2CTS/FLT6/PMPA10/RE13
    ANSELEbits.ANSE13 = 1;
    // Potentiometer #2 input - used as Speed Reference
    // POT2 : PIM #23
    TRISEbits.TRISE14 = 1;          // AN14/RPI94/FLT7/PMPA1/RE14
    ANSELEbits.ANSE14 = 1;

    // Debug LEDs
    // LED1 : PIM #01
    TRISDbits.TRISD6 = 0;           // RP70/RD6
    // LED2 : PIM #59
    TRISDbits.TRISD8 = 0;            // RPI72/RD8
    // LED3 : PIM #33
    TRISGbits.TRISG11 = 0;           // AN35/RG11
    // LED4 : PIM #34
    TRISFbits.TRISF13 = 0;           // AN36/RF13

    // Push button Switches
    // SW1 : PIM #69
    TRISCbits.TRISC4 = 1;            // AN30/SDA1/RPI52/RC4
    // SW2 : PIM #84
    TRISGbits.TRISG6 = 1;            // AN19/RP118/PMPA5/RG6
    // SW3 : PIM #40
    TRISDbits.TRISD15 = 1;           // AN39/RD15
    // SW4 : PIM #41
    TRISDbits.TRISD13 = 1;           // RPI77/RD13

    // UART - for RTDM/DMCI Communication
    // UART_RX : PIM #49 (Input)
    TRISCbits.TRISC5 = 1;            // AN31/SCL1/RPI53/RC5
    // UART_TX : PIM #50(Output)
    TRISFbits.TRISF1 = 0;            // RP97/RF1

    // DAC - Interface
    // DAC_DATA : PIM #88 (Output)
    TRISCbits.TRISC8 = 0;            // RP56/PMPWR/RC8
    // DAC_CLK : PIM #87(Output)
    TRISCbits.TRISC9 = 0;            // RP57/RC9
    // DAC_CS : PIM #91(Output)
    TRISFbits.TRISF7 = 0;            // RF7

    // FAULT Pins
    // Combined Fault input FAULT_AB = FAULT_A 'AND' FAULT_B
    // FAULT_AB : PIM #18
    TRISBbits.TRISB4 = 1;            // FLT32/SCL2/RP36/PMPA8/RB4

    /************************** Remappable PIn configuration ******************/

    //Unlock registers by clearing IOLOCK bit OSCCON(OSCONbits.IOLOCK = 0)
    __builtin_write_OSCCONL(OSCCON & (~(1 << 6))); 

    // Fault Input/Outputs
    // FAULT_MA : PIM #70 
    // Remap C4OUT(Comparator #4 Output) = FAULT_MA to RP55
    _RP55R = 0X32;                  // RP55/PMPBE/RC7
    // FAULT_MB : PIM #10
    // Remap FAULT_MB(RP81) as FLT1 input
    RPINR12bits.FLT1R = 81;         // AN41/RP81/RE1

    // RTDM Communication RX and TX configuration ( UART #2)
    // UART_RX : PIM #49 (Input)
    // Configure RP53 as U2RX
    RPINR19bits.U2RXR = 53;         // AN31/SCL1/RPI53/RC5
    // UART_TX : PIM #50 (Output)
    // Remap RP53 as U2RX
    _RP97R = 0x03;                  // RP97/RF1

    // DE2 Communication Interface between MCP8024(U8) and dsPIC (UART #1)
    // DE2_RX_A : PIM #83 (Input)
    // Configure RP119 as U1RX
    RPINR18bits.U1RXR = 119;        // AN18/ASCL1/RPI119/PMPA4/RG7
    // DE2_TX_A : PIM #76 (Output)
    // Configure RP120 as U1TX
    _RP120R = 0x01;                 // AN17/ASDA1/RP120/PMPA3/RG8

    // DE2 Communication Interface between MCP8024(U9) and dsPIC (UART #3)
    // DE2_RX_B : PIM #43 (Input)
    // Configure RPI80 as U3RX
    RPINR27bits.U3RXR = 80;         // AN40/RPI80/RE0
    // DE2_TX_B : PIM #54 (Output)
    // Configure RP40 as U3TX
    _RP40R = 0x1B;                  // TCK/AN26/CVREF1O/SOSCO/RP40/T4CK/RB8

    // Lock registers by setting IOLOCK bit OSCCON(OSCONbits.IOLOCK = 1)
    __builtin_write_OSCCONL(OSCCON | (1 << 6)); // Set bit 6

    /**************************************************************************/

    return;
}

/*EOF*/
