/**********************************************************************
* © 2012 Microchip Technology Inc.
*
* SOFTWARE LICENSE AGREEMENT:
* Microchip Technology Incorporated ("Microchip") retains all ownership and 
* intellectual property rights in the code accompanying this message and in all 
* derivatives hereto.  You may use this code, and any derivatives created by 
* any person or entity by or on your behalf, exclusively with Microchip's
* proprietary products.  Your acceptance and/or use of this code constitutes 
* agreement to the terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".  NO 
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
* TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A 
* PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S 
* PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. 
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER 
* IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), 
* STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, 
* PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF 
* ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN 
* ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT 
* ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO 
* THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO 
* HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and 
* determining its suitability.  Microchip has no obligation to modify, test, 
* certify, or support the code.
*
******************************************************************************/

/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/*                    include files                                           */
/*                                                                            */
/******************************************************************************/
/******************************************************************************/

#include "general.h"
#include "periph.h"

void DDelay(void);
/******************************************************************************/
/* Configuration bits                                                         */
/******************************************************************************/
_FPOR(ALTI2C1_OFF & ALTI2C2_OFF);
_FWDT(PLLKEN_ON & FWDTEN_OFF);
_FOSCSEL(FNOSC_FRC & IESO_OFF & PWMLOCK_OFF);
_FGS(GWRP_OFF & GCP_OFF);

_FICD(ICS_PGD2 & JTAGEN_OFF);	// PGD3 for 28pin 	PGD2 for 44pin
_FOSC(FCKSM_CSECMD & POSCMD_XT);		//XT W/PLL
//_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE); // FRC W/PLL

/********************Setting Configuration Bits End********************************/



/******************************************************************************/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/******************************************************************************/

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/******************************************************************************/
/* Function name: InitPeriph                                                  */
/* Function parameters: None                                                  */
/* Function return: None                                                      */
/* Description: Init the peripherals: PLL, ADC, PWM & GPIO                    */
/******************************************************************************/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void InitPeriph(void)
{
  //The settings below set up the oscillator and PLL for x MIPS as
    //follows:
    //                  Fin  * M
    // Fosc       =     ---------
    //                   N2 * N1   
    //
    // Fin   	  = 7.37 MHz
    // Fosc       = x MHz
    // Fcy        = Fosc/2 = 

	/****************** Clock definitions *********************************/
 
/*
   // 70 MIPS (70.015 Mhz) (i.e (7.37 * (76/4))/2)

    PLLFBD =  74; 			    // M = 76    74
    CLKDIVbits.PLLPOST = 0; 	// N2 = (2 x (PLLPOST + 1)) = 2   0
    CLKDIVbits.PLLPRE = 0;		// N1 = (PLLPRE + 2) = 2        0
*/
    // 70 MIPS (70 Mhz) (i.e (8 * (70/4))/2)

	PLLFBD = 68; // dPLL derived in UserParms.h
	CLKDIVbits.PLLPOST = 0;		// N1=2
	CLKDIVbits.PLLPRE = 0;		// N2=2
    
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)

    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(0x01);
    while(OSCCONbits.COSC != 0x03);
    
    while(OSCCONbits.LOCK != 1);	

 /*   
    // Initiate Clock Switch to FRC with PLL (NOSC=0b001)

    __builtin_write_OSCCONH(0x01);
    __builtin_write_OSCCONL(0x01);


    while(OSCCONbits.COSC != 0b001);
    
    while(OSCCONbits.LOCK != 1);

 */

    // Turn saturation on to insure that overflows will be handled smoothly.
    CORCONbits.SATA  = 0;

	// ============= Port A ==============
	LATA  = 0;
	TRISA = 0x1F93;			// Reset value (all inputs)
    ANSELA = 0X0003;
	// ============= Port B ==============
    LATB  = 0x0000;
    TRISB = 0x03FF;			// RB10~15 outputs for PWM, rest are inputs
    ANSELB = 0;
	// ============= Port C ==============
	LATC  = 0x0000;
	TRISC = 0xBFFF;			// Reset value (all inputs)
    ANSELC = 0;
    // ============= Port D ==============
	LATD  = 0x0000;
	TRISD = 0x0100;	            // 0000 0000 0000 0000; RD8 - Test Point I/P ,RD6 -  LED1 , RD5 - LED2
    // ============= Port E ==============
    LATE  = 0x0000;
    TRISE = 0x2000;             // 0010 0000 0000 0000  ;RE13 - POT ; 
    ANSELE = 0X2000;            // ANSE13 - POT 
    // ============= Port F ==============
    LATF  = 0x0000;
    TRISF = 0x0000;             // 0000 0000 0000 0000  RF1 - TX  
	// ============= Port G ==============
    LATG  = 0x0000;
    TRISG = 0x03C0;			// Reset value (all inputs)

	/************** Code section for the low pin count devices ******/
	/*Assigning the TX and RX and FAULT pins to ports RP97 , RP53 and RP32 to the dsPIC*/
	__builtin_write_OSCCONL(OSCCON & (~(1<<6))); // clear bit 6 

    RPINR19bits.U2RXR = 53;		// Make Pin RP53 U2RX
    RPOR7bits.RP97R = 3;	    // Make Pin RP97 U2TX
	RPINR12bits.FLT1R = 32;		// Make Pin RP32 FLT I/P

	__builtin_write_OSCCONL(OSCCON | (1<<6)); 	 // Set bit 6 	
	/****************************************************************/

   // ============= Motor PWM ======================

    // Center aligned PWM.
    // Note: The PWM period is set to dLoopInTcy/2 but since it counts up and 
    // and then down => the interrupt flag is set to 1 at zero => actual 
    // interrupt period is dLoopInTcy

	PHASE1 = LOOPTIME_TCY;
	PHASE2 = LOOPTIME_TCY;
	PHASE3 = LOOPTIME_TCY;
	PTPER = 2*LOOPTIME_TCY+1;

	PWMCON1 = 0x0204;	        // Enable PWM output pins and configure them as 
	PWMCON2 = 0x0204;	        // complementary mode
	PWMCON3 = 0x0204;

	//I/O pins controlled by PWM
	IOCON1 = 0xC000;
	IOCON2 = 0xC000;
	IOCON3 = 0xC000;

	//set pin outputs
    
	DTR1 = 0x0000;
	DTR2 = 0x0000;
	DTR3 = 0x0000;

	ALTDTR1 = DDEADTIME;        // 700ns of dead time
	ALTDTR2 = DDEADTIME;	    // 700ns of dead time
	ALTDTR3 = DDEADTIME;	    // 700ns of dead time

	FCLCON1 = 0x03;             //Fault disabled
	FCLCON2 = 0x03;             //Fault disabled
	FCLCON3 = 0x03;             //Fault disabled

	PTCON2 = 0x0000;	        // Divide by 1 to generate PWM

    PDC1 = MIN_DUTY;            // Intialising the duty register 
    PDC2 = MIN_DUTY;            // Intialising the duty register
    PDC3 = MIN_DUTY;            // Intialising the duty register
    
	IPC23bits.PWM1IP = 4;	    // PWM Interrupt Priority 4
	IPC23bits.PWM2IP = 4;	    // PWM Interrupt Priority 4
	IPC24bits.PWM3IP = 4;	    // PWM Interrupt Priority 4

	IFS5bits.PWM1IF = 0;	    //clear PWM interrupt flag
	IEC5bits.PWM1IE = 0;	    //disable PWM interrupts

    PTCON = 0x8000;             // Enable PWM for center aligned operation	

    // SEVTCMP: Special Event Compare Count Register 
    // Phase of ADC capture set relative to PWM cycle: 0 offset and counting up
	SEVTCMP = 0;


	// ============= ADC - Measure Current & Pot ======================
    // ADC setup for simultanous sampling on 
    //      CH0=AN13, CH1=AN0, CH2=AN1, CH3=AN2. 
    // Sampling triggered by Timer3 and stored in signed fractional form.
    // Signed fractional (DOUT = sddd dddd dd00 0000)

    AD1CON1bits.FORM = 3;    
	// Timer3 overflow ends sampling and starts conversion
	AD1CON1bits.SSRC = 3;
	AD1CON1bits.SSRCG = 0;
    // Simultaneous Sample Select bit (only applicable when CHPS = 01 or 1x)
    // Samples CH0, CH1, CH2, CH3 simultaneously (when CHPS = 1x)
    // Samples CH0 and CH1 simultaneously (when CHPS = 01)
    AD1CON1bits.SIMSAM = 1;  
    // Sampling begins immediately after last conversion completes. 
    // SAMP bit is auto set.
    AD1CON1bits.ASAM = 1;  


    AD1CON2 = 0;
    // Samples CH0, CH1, CH2, CH3 simultaneously (when CHPS = 1x)
    AD1CON2bits.CHPS = 2;  


    AD1CON3 = 0;
    // A/D Conversion Clock Select bits = 6 * Tcy
    AD1CON3bits.ADCS = 6;  


     /* ADCHS: ADC Input Channel Select Register */
    AD1CHS0 = 0;
    // CH0 is AN13 for POT
    AD1CHS0bits.CH0SA = 13;

   // CH1 positive input is AN0, CH2 positive input is AN1, CH3 positive input is AN2
    AD1CHS123bits.CH123SA = 0;
  
 /* ADCSSL: ADC Input Scan Select Register */
    AD1CSSL = 0;

    // Turn on A/D module
    AD1CON1bits.ADON = 1;

	DDelay();

    //Fault enabled 
    FCLCON1 = 0x00FD;           //Fault enabled Fault SRC - Fault 32
	FCLCON2 = 0x00FD;           //Fault enabled Fault SRC - Fault 32
	FCLCON3 = 0x00FD;           //Fault enabled Fault SRC - Fault 32

	return;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/******************************************************************************/
/* Function name: DDelay                                                      */
/* Function parameters: None                                                  */
/* Function return: None                                                      */
/* Description: Delay Routine											      */
/******************************************************************************/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void DDelay(void)
{
	long i;
	for (i = 0;i < 100000;i++);
	return;
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/******************************************************************************/
/* Function name: ResetPeriph                                                 */
/* Function parameters: None                                                  */
/* Function return: None                                                      */
/* Description: Reset the peripherals during runtime/reset configurations     */
/******************************************************************************/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void ResetPeriph(void)
{

    // clear and Enable ADC interrupt 
    IFS0bits.AD1IF = 0; 
    IEC0bits.AD1IE = 1;

    /* Initialise duty regs */ 
    PDC1 = MIN_DUTY;
    PDC2 = MIN_DUTY;
    PDC3 = MIN_DUTY;
    return;

}
