/********************************************/
/*Interrupts                                */
/*PIC32MX795F512L-80I/PF                    */
/********************************************/

#include <xc.h>
#include <sys/attribs.h>

#include "HX8357B.h"
#include "Interrupts.h"
#include "LED_Port.h"
#include "Delay.h"
#include "Screens.h"
#include "Flash.h"
#include "REN70V05.h"
#include "Binary2ASCIIHex.h"
//#include "USB.h"

void Interrupts_Init(void)
{
    //Set the interrupt controller for multi-vector mode
    INTCONSET = _INTCON_MVEC_MASK; 
    
    /*************************************/
    /*Priorities                         */
    /*************************************/
    //Set Core timer priority
    IPC0bits.CTIP = 7;
    IPC0bits.CTIS = 3;
    
    //USB priority
    IPC11bits.USBIP = 7;
    IPC11bits.USBIS = 3;
    
    //Set RTC interrupt priority to 4
    IPC8bits.RTCCIP = 7;
    IPC8bits.RTCCIS = 2;

    //Set Timer 2 Interrupt Priority to 7
    //IPC2bits.T2IP = 7;
//    //IPC2bits.T2IS = 1;
//    
//    //Set Timer 3 Interrupt Priority to 7
//    IPC3bits.T3IP = 7;
//    
//    //Set Timer 4 Interrupt Priority to 7
//    IPC4bits.T4IP = 7;
//    IPC4bits.T4IS = 1;
//    
//     //Set INT1 Interrupt Priority to 7
//    IPC1bits.INT1IP = 7;
//    
//     //Set INT2 Interrupt Priority to 7
//    IPC2bits.INT2IP = 7;
    
    /*************************************/
    /*Externals                          */
    /*************************************/
    
//    //Set INT1 edge
//    INTCONbits.INT1EP = 0;
//    IFS0bits.INT1IF = 0;
//   
//    //Set INT2 edge
//    INTCONbits.INT2EP = 0;
//    IFS0bits.INT2IF = 0;
    
    //Enable Interrupts
    //Core Timer 
    IEC0bits.CTIE = 1;
    
    //Real Time
    IEC1bits.RTCCIE = 1;
    
    //Timers
    IEC0bits.T1IE = 0;
    IEC0bits.T2IE = 0;
    IEC0bits.T3IE = 0;
    IEC0bits.T4IE = 0;
    IEC0bits.T5IE = 0;
    
    //enable /INTR interrupt
    IEC0bits.INT1IE = 0;
    
    //enable INT2 interrupt
    IEC0bits.INT2IE = 0;
    
    return;
}

//CPU Core Timer
void __ISR(0, IPL7SRS) CoreTimer_Srvc(void)
{
    int a = hchar;
    int b = vchar;
    int i;
    for(i = 0; i < 100000; i++)
    {    
        PORTBbits.RB7 = 1;
    }
    PORTBbits.RB7 = 0;
    
    int dly_time = 5000;
    
    //Lock the header bar
    if(clock_on == 1)
    {
        if(row_start < 90)
        {
            row_start = 90;
        }
         
    }
  ///////////////////////////////////////////////
 //      X AXIS                              //
/////////////////////////////////////////////
    //RG12 drives, RB3 sinks, RG14 open
    TRISGbits.TRISG14 = 1; //XL- 95
    
    AD1PCFGbits.PCFG3 = 1;          
    TRISBbits.TRISB3 = 0;
    PORTBbits.RB3 = 0; // YU+ 22
    
    TRISGbits.TRISG12 = 0;
    PORTGbits.RG12 = 1; //YD- 96 
    
    TRISBbits.TRISB2 = 1;
    //ADC reads the voltage on pin #23 (AN2)
    AD1PCFGbits.PCFG2 = 0;          //RB2/AN2 XR+ 
    AD1CHSbits.CH0SA = 2;           //Channel 0 positive input is AN2
    //wait for ADC
    Delay(dly_time);
    
    /**************************************************************************/
    IFS1bits.AD1IF = 0;             //Clear interrupt flag

    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert   

    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert

    data_x = ADC1BUF0;

    data_x = (data_x - 67) / 1.780;
    if(data_x >= 480)
    {
        data_x = 480;
    }
/**************************************************************************/

  ///////////////////////////////////////////////
 //      Y AXIS                               //
///////////////////////////////////////////////
    
    //RG14 drives, RB2 sinks, RG12 open
    TRISGbits.TRISG12 = 1; //YD- 96
    
    AD1PCFGbits.PCFG2 = 1; 
    
    TRISBbits.TRISB2 = 0; 
    PORTBbits.RB2 = 0;  //XR+ 23
       
    TRISGbits.TRISG14 = 0;
    PORTGbits.RG14 = 1; //XL- 95

    //ADC reads the voltage on pin #22
    AD1PCFGbits.PCFG3 = 0; //RB3/AN3 YU+ 22
    TRISBbits.TRISB3 = 1;
    AD1CHSbits.CH0SA = 3; //Channel 0 positive input is AN3
    
    //wait for ADC
    Delay(dly_time);
 
    
    IFS1bits.AD1IF = 0;             //Clear interrupt flag

    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert    
    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert

    data_y = ADC1BUF0;

    data_y = (data_y - 95) / 2.463;

    if(data_y >= 320)
    {
        data_y = 320;
    }
    
    _CP0_SET_COUNT(0);
    _CP0_SET_COMPARE(CorTimer_Compare);
    
    hchar = a;
    vchar = b;
    
    IFS0bits.CTIF = 0;
}

//This is the /INTR from the SRAM
void __ISR(7, IPL7SRS) INT1_Srvc(void)
{
    LED_Port(0x40);
    
    REN70V05_RD(0x3ff);
    
    IFS0bits.INT1IF = 0;
}   

//This is the /BUSYR from the SRAM
void __ISR(11, IPL7SRS) INT2_Srvc(void)
{
    LED_Port(0x80);
    IFS0bits.INT2IF = 0;
}

void __ISR(35, IPL7SRS) RTCC_Srvc(void)
{
    int a = hchar;
    int b = vchar;
    int i;
    
    //Blink LED2
    for(i = 0; i < 100000; i++)
    {    
        PORTBbits.RB6 = 1;
    }
    PORTBbits.RB6 = 0;
    
    RTC_delay_counter++;

    ///////////////////////////////////////////////
 //      Input Voltage Sense                              //
///////////////////////////////////////////////
    AD1CHSbits.CH0SA = 4;           //Channel 0 positive input is AN4
    
    Delay(5000);
    
    AD1CON1bits.ADON = 1;
    
    ADC1BUF0 = 0;
    IFS1bits.AD1IF = 0;             //Clear interrupt flag
    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert
    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert
    
    //Read the buffer
    data_v = ADC1BUF0;
    
    //calibration
    data_v = data_v + 45;
    
    //don't let data_v exceed 1023
    if(data_v > 1023)
    {
        data_v = 1023;
    }
  ///////////////////////////////////////////////
 //      I Sense                              //
///////////////////////////////////////////////
    AD1CHSbits.CH0SA = 9;           //Channel 0 positive input is AN9
    
    Delay(5000);
    
    AD1CON1bits.ADON = 1;
    
    ADC1BUF0 = 0;
    IFS1bits.AD1IF = 0;             //Clear interrupt flag
    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert
    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert
    
    //Read the buffer
    data_i = ADC1BUF0; 
    
    //calibration
    //data_i = data_i + 10;
    
    //Clock
    if(clock_on == 1)
    {
        hchar = 410;
        vchar = 90;   
        
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.SEC01);

        //Tens Seconds
        hchar = 340;
        vchar = 90;  
        
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.SEC10);
        
        //Minutes
        hchar = 245;
        vchar = 90;    
        
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.MIN01);
        
        //Tens Minutes
        hchar = 175;
        vchar = 90;    
        
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.MIN10);
                
        //Hours
        hchar = 80;
        vchar = 90;    
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.HR01);
        
        //Tens Hours
        hchar = 10;
        vchar = 90;    
        while(RTCCONbits.RTCSYNC == 1);
        Write_Digit(RTCTIMEbits.HR10);
        
    }
          
    hchar = a;
    vchar = b;
    
   IFS1bits.RTCCIF = 0;
}

void __ISR(8, IPL7SRS) Timer2_Srvc(void)
{
    TMR2 = 0;
    
    IFS0bits.T2IF = 0;
}   

void __ISR(12, IPL7SRS) Timer3_Srvc(void)
{
    TMR3 = 0;
    
    IFS0bits.T3IF = 0;
}   

void __ISR(16, IPL7SRS) Timer4_Srvc(void)
{
    TMR4 = 0;
    
    IFS0bits.T4IF = 0;

}   

