/********************************************************************
* Copyright (C) 2016 Microchip Technology Inc. and its subsidiaries
* (Microchip).  All rights reserved.
*
* You are permitted to use the software and its derivatives with Microchip
* products. See the license agreement accompanying this software, if any, for
* more info about your rights and obligations.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
* MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE. IN NO EVENT SHALL MICROCHIP, SMSC, OR ITS LICENSORS BE LIABLE OR
* OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH
* OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY FOR ANY DIRECT OR INDIRECT
* DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES, OR OTHER SIMILAR COSTS. To the fullest
* extend allowed by law, Microchip and its licensors liability will not exceed
* the amount of fees, if any, that you paid directly to Microchip to use this
* software.
*************************************************************************
*
*                           HardwareProfile.c
*
* Hardware Profile source file
*
*
* Hardware:
*  Flyback SMPS using PIC16F1769
*
* Author                          Date            Ver     Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Turcan Gheorghe M15725          2016.10.12      0.6       MPLABX V3.30  XC8 V1.37  MCC 3.15
******************************************************************************/

#include "AutomatedFunctions.h"

void AutomatedFunctionInit()
{   
   
#if (ShortCircuitProtection == ON )
    //Configure DAC4 as Comp4 positive input for Short Circuit protection
    DAC4CON0 =  0b10000000;     //DAC enabled, VDD as + source, VSS as - source
    DAC4REF =   ShortCircuitDetectValue;     

    //CMxCON: COMPARATOR Cx CONTROL REGISTER for Short Circuit protection
    CM4CON0 =   0b10010010;     //Comparator is enabled, Comparator output inverted
    CM4NSEL =   0b00000011;     //Comparator Negative input connected to CxIN3-
    CM4PSEL =   0b00001101;     //Comparator Positive input connected to DAC4_output

    INTCON |=   0b11000000;     //Enable Global I
#endif

    __delay_ms(0.1);
    
   
#if (SoftStartFunction == OFF )
    COG1CON0 |=      0b10000000;     //COG Enabled
    
#if (MaxDutyCycleLimit == OFF )
    CCPR2L	=       0x85;
#else
    CCPR2L	=       MaxDutyCycleValue;
#endif
        
#if ( ShortCircuitProtection == ON )
        __delay_ms(8);
        PIR2bits.C4IF = 0;
        PIE2bits.C4IE = 1;          //CM4 interrupt enabled
        CM4CON1bits.C4INTP = 1;
        
        if(CM4CON0bits.C4OUT == 1)
            {
                PIR2bits.C4IF = 1;
            }
        
#endif
#endif        
}

#if ( SoftStartFunction == ON )
void Flyback_SoftStart()
{    
    CCPR2L	=       0x02;   
    COG1CON0 =      0b10000000;     //COG Enabled  
    // CCP2 limiting the maximum duty cycle for implementing a SoftStart function
    // CCP2 limiting the maximum duty cycle for implementing a SoftStart function
    COG1FSIM0 = 0x04;
    __delay_ms(0.2);
    CCPR2L	=       0x06;
    __delay_ms(0.4);
    CCPR2L	=       0x0A;
    __delay_ms(0.4);
    CCPR2L	=       0x0F;
    __delay_ms(0.2);
    CCPR2L	=       0x12;
    __delay_ms(0.2);
    CCPR2L	=       0x15;
    __delay_ms(0.2);
    CCPR2L	=       0x25;
    __delay_ms(0.2);
    CCPR2L	=       0x3A;
    __delay_ms(0.2); 
//    __delay_ms(1000);
    #if ( MaxDutyCycleLimit == OFF ) 
        CCPR2L	=       0xCA;
    #else
        CCPR2L = MaxDutyCycleValue;
    #endif
    
    #if ( ShortCircuitProtection == ON )

        __delay_ms(15);

        PIR2bits.C4IF = 0;
        PIE2bits.C4IE = 1;          //CM4 interrupt enabled
        CM4CON1bits.C4INTP = 1;
        
        if(CM4CON0bits.C4OUT == 1)
            {
                PIR2bits.C4IF = 1;
            }   

    #endif
COG1FSIM0 = 0x00;
}
#endif

#if ( ShortCircuitProtection == ON )

#if ( ShortCircuitLimitedRetry == ON )
    static volatile int count = 1;
#endif
    
void  interrupt SC_ISR()
{
    if ((PIR2bits.C4IF)&&(PIE2bits.C4IE))
    {
        CM4CON1bits.C4INTP = 0;
        PIR2bits.C4IF = 0;        //Clear interrupt flag
        COG1ASD0 =  0b10001000;     //Shut Down COG 
        LATB5 = 0;                  //Stop Power delivery to RC4
        OSCCON =    0b01000000;     //OSC Frequency changed to 125kHz
        __delay_ms(SCTimeBetweenRetries*16);
        
        #if ( ShortCircuitLimitedRetry == ON )
                if (count < ShortCircuitRetryTimes)
                {
        #endif

        OSCCON =    0b01110000;     //OSC Frequency changed to 8MHz
        LATB5 = 1;                  //Deliver Power to RC4
        CCPR2L	=       0x02;
        COG1ASD0 =  0b00001000;     //Start COG

        #if ( SoftStartFunction == ON )
                Flyback_SoftStart();
        #else

            #if (MaxDutyCycleLimit == OFF )
                CCPR2L	=       0x85;
            #else
                CCPR2L	=       MaxDutyCycleValue;
            #endif

            __delay_ms(10);
            PIR2bits.C4IF = 0;
            PIE2bits.C4IE = 1;          //CM4 interrupt enabled
            CM4CON1bits.C4INTP = 1;

            if(CM4CON0bits.C4OUT == 1)
                {
                    PIR2bits.C4IF = 1;
                }

        #endif

        #if ( ShortCircuitLimitedRetry == ON )
                }
                else
                {
                    // Add your own code to handle short-circuit
                }

                count ++;
        #endif
    }
}
#endif

