/**
  IC1 Generated Driver API Source File

  @Company
    Microchip Technology Inc.

  @File Name
    ic1.c

  @Summary
    This is the generated source file for the IC1 driver using PIC24 / dsPIC33 / PIC32MM MCUs

  @Description
    This source file provides APIs for driver for IC1.
    Generation Information :
        Product Revision  :  PIC24 / dsPIC33 / PIC32MM MCUs - 1.95-b-SNAPSHOT
        Device            :  PIC24FJ128GA010
    The generated drivers are tested against the following:
        Compiler          :  XC16 v1.36
        MPLAB 	          :  MPLAB X v5.10
*/

/*
    (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
*/

/**
  Section: Included Files
*/

#include <xc.h>
#include <stdbool.h>
#include "mcc.h"
#include "../main.h"

/**
  IC Mode.

  @Summary
    Defines the IC Mode.

  @Description
    This data type defines the IC Mode of operation.

*/

//static uint16_t         gIC1Mode;
//static uint16_t         gIC2Mode;
//static uint16_t         gIC3Mode;
//static uint16_t         gIC4Mode;

volatile uint32_t IC1_Current[4];
volatile uint32_t IC1_Previous;
volatile bool IC1_isFresh;
volatile uint32_t IC1_Sum;

volatile uint32_t IC2_Current[4];
volatile uint32_t IC2_Previous;
volatile bool IC2_isFresh;
volatile uint32_t IC2_Sum;

volatile uint32_t IC3_Current[4];
volatile uint32_t IC3_Previous;
volatile bool IC3_isFresh;
volatile uint32_t IC3_Sum;

volatile uint32_t IC4_Current[4];
volatile uint32_t IC4_Previous;
volatile bool IC4_isFresh;
volatile uint32_t IC4_Sum;

//extern volatile uint32_t _Tpwm_postcaler;

/**
  Section: Driver Interface
*/

void IC_Initialize (void)
{
    // ICSIDL disabled; ICM Prescaler Capture mode: Every 4th rising; ICTMR TMR3; ICI Every; 
    /* Rueda Izquierda*/
    /*================*/
//    IC1CON = 0x04; // Interrumpric cada captura. Capturar cada 4 flancos ascendentes
//    IC1CON = 0x63; // Interrumpric cada 4 capturas. Capturar cada flanco ascendente
    IC1CON = 0x03; // Interrumpric cada captura. Capturar cada flanco ascendente
//    IC3CON = 0x63;
    
//    gIC1Mode = IC1CONbits.ICM;
//    gIC3Mode = IC3CONbits.ICM;
    
    IFS0bits.IC1IF = false;
    IEC0bits.IC1IE = true;
    
    IFS2bits.IC3IF = false;
    IEC2bits.IC3IE = false;
    
    int i;
    for(i=0;i<4;i++){
        IC1_Current[i]=0;
    }
    
    /* Rueda Derecha*/
    /*==============*/
//    IC2CON = 0x04; // Interrumpric cada captura. Capturar cada 4 flancos ascendentes
//    IC2CON = 0x63; // Interrumpric cada 4 capturas. Capturar cada flanco ascendente  
    IC2CON = 0x03; // Interrumpric cada captura. Capturar cada flanco ascendente  
//    IC4CON = 0x63;
    
//    gIC2Mode = IC2CONbits.ICM;
//    gIC4Mode = IC4CONbits.ICM;
    
    IFS0bits.IC2IF = false;
    IEC0bits.IC2IE = true;
    
    IFS2bits.IC4IF = false;
    IEC2bits.IC4IE = false;
    
    for(i=0;i<4;i++){
        IC2_Current[i]=0;
    }
}


void __attribute__ ((weak)) IC_CallBack(void)
{
}

//void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC1Interrupt( void )
//{
//    IC1_Previous = IC1_Current;
//    IC1_Current = (uint32_t)IC1BUF + _Tpwm_Postcaler;
//    IC1_isFresh = true;
//    IC1_Direction? IC1_Sum-- : IC1_Sum++;
//    IFS0bits.IC1IF = 0;
//}
//void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC2Interrupt( void )
//{	
//    IC2_Previous = IC2_Current;
//    IC2_Current = (uint32_t)IC2BUF + _Tpwm_Postcaler;
//    IC2_isFresh = true;
//    IC2_Direction? IC2_Sum-- : IC2_Sum++;
//    IFS0bits.IC2IF = 0;
//}
unsigned char stt=0;
void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC1Interrupt( void )
{
    switch (stt++){
        case 0:
            IC1_Current[0] = (uint32_t)IC1BUF + _Tpwm_Postcaler;
            break;
        case 1:
            IC1_Current[1] = (uint32_t)IC1BUF + _Tpwm_Postcaler;
            break;
        case 2:
            IC1_Current[2] = (uint32_t)IC1BUF + _Tpwm_Postcaler;
            break;
        case 3:
            IC1_Previous = IC1_Current[3];
            IC1_Current[3] = (uint32_t)IC1BUF + _Tpwm_Postcaler;
            IC1_isFresh = true;
            stt = 0;
            break;
    }
    IC1_Direction? IC1_Sum-- : IC1_Sum++;
    IFS0bits.IC1IF = 0;
}
unsigned char stt2=0;
void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC2Interrupt( void )
{	
    switch (stt2++){
        case 0:
            IC2_Current[0] = (uint32_t)IC2BUF + _Tpwm_Postcaler;
            break;
        case 1:
            IC2_Current[1] = (uint32_t)IC2BUF + _Tpwm_Postcaler;
            break;
        case 2:
            IC2_Current[2] = (uint32_t)IC2BUF + _Tpwm_Postcaler;
            break;
        case 3:
            IC2_Previous = IC2_Current[3];
            IC2_Current[3] = (uint32_t)IC2BUF + _Tpwm_Postcaler;
            IC2_isFresh = true;
            stt2 = 0;
            break;
    }
//    uint32_t TPCap = _Tpwm_Postcaler;
//    IC2_Previous = IC2_Current[3];
//    IC2_Current[0] = (uint32_t)IC2BUF + TPCap;
//    IC2_Current[1] = (uint32_t)IC2BUF + TPCap;
//    IC2_Current[2] = (uint32_t)IC2BUF + TPCap;
//    IC2_Current[3] = (uint32_t)IC2BUF + TPCap;
//    IC2_isFresh = true;
    IC2_Direction? IC2_Sum-- : IC2_Sum++;
    IFS0bits.IC2IF = 0;
}
//void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC3Interrupt( void )
//{
//    IC3_Current[0] = (uint32_t)IC3BUF;
//    IC3_Current[1] = (uint32_t)IC3BUF;
//    IC3_Current[2] = (uint32_t)IC3BUF;
//    IC3_Current[3] = (uint32_t)IC3BUF;
//}
//void __attribute__ ( ( interrupt, no_auto_psv ) ) _ISR _IC4Interrupt( void )
//{
//    IC4_Current[0] = (uint32_t)IC4BUF;
//    IC4_Current[1] = (uint32_t)IC4BUF;
//    IC4_Current[2] = (uint32_t)IC4BUF;
//    IC4_Current[3] = (uint32_t)IC4BUF;
//}

void IC_Start( void )
{
//    IC1CONbits.ICM = gIC1Mode;
}

void IC_Stop( void )
{
    IC1CONbits.ICM = 0;
}

uint16_t IC_CaptureDataRead( void )
{
    return(IC1BUF);
}

bool IC_HasCaptureBufferOverflowed( void )
{
    return( IC1CONbits.ICOV );
}


bool IC1_IsCaptureBufferEmpty( void )
{
    return( ! IC1CONbits.ICBNE );
}
bool IC2_IsCaptureBufferEmpty( void )
{
    return( ! IC2CONbits.ICBNE );
}
//bool IC3_IsCaptureBufferEmpty( void )
//{
//    return( ! IC3CONbits.ICBNE );
//}
//bool IC4_IsCaptureBufferEmpty( void )
//{
//    return( ! IC4CONbits.ICBNE );
//}

/**
 End of File
*/
