/***************************************************************************//**
 *   @file   system.c
 *   @brief  Contient les fonctions d'initialisation des oscillateurs
 *   @author Kévin MARCHAND(Kevin.marchand@intelligence-electronique.com)
********************************************************************************
 * Copyright 2014(c) PLCD, Inc.
 *
 * Fonctions d'initialisation des oscillateurs et autres composant non
 * péripherique du microcontroller
 * Définition des QUARTZ :
 *  - OSC1 : 8MHz
 *  - OSC2 : 32,768KHz
 *
********************************************************************************
 *   SVN Revision: 1
*******************************************************************************/

/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
/* Device header file */
#if defined(__XC16__)
    #include <xc.h>
#elif defined(__C30__)
    #if defined(__dsPIC33E__)
    	#include <p33Exxxx.h>
    #elif defined(__dsPIC33F__)
    	#include <p33Fxxxx.h>
    #endif
#endif

#include <stdint.h>          /* For uint16_t definition                       */
#include <stdbool.h>         /* For true/false definition                     */
#include "system.h"          /* variables/params used by system.c             */

/***************************************************************************//**
 * @brief Initialise l'oscillateur principal du microcontroller.
 * - Refer to the device Family Reference Manual Oscillator section for
 * information about available oscillator configurations.
 * @detail Writes to OSCCON register require an unlock sequence
 *
 * Formule :
 * FOSC = FINx(M/(N1xN2))
 * N1 = PLLPRE + 2
 * N2 = 2 x (PLLPOST + 1)
 * M = PLLDIV + 2
 *
 * @param none.
 *
 * @return none.
*******************************************************************************/
void ConfigureOscillator(void)
{
    //Intializes the controller to operate at 60MIPS.
    // Setup configuration
    PLLFBD = 58;                    //58 => M=60       38 => M=40

    CLKDIVbits.PLLPOST = 0;         // N1=2
    CLKDIVbits.PLLPRE = 0;          // N2=2
    OSCTUN = 0;                     // Tune FRC oscillator, if FRC is used

    // Disable Watch Dog Timer
    RCONbits.SWDTEN = 0;

    // Clock switching to incorporate PLL
    __builtin_write_OSCCONH(0x03);  // Initiate Clock Switch to Primary

    // Oscillator with PLL (NOSC=0b011)
    __builtin_write_OSCCONL(0x01);  // Start clock switching
    while(OSCCONbits.COSC != 0b011);

    // Wait for Clock switch to occur
    // Wait for PLL to lock
    while(OSCCONbits.LOCK != 1)
    { };

    // Configuring the auxiliary oscillator for RTC
    //ACLKCON3bits.ENAPLL = 0;
    //ACLKCON3bits.SELACLK = 1;           // Auxiliary PLL or oscillator provides the source clock for auxiliary clock divider
    //ACLKCON3bits.AOSCMD = 0b10;         // XT (Crystal) Oscillator mode select

    //OSCCONbits.LPOSCEN = 1;
    __builtin_write_OSCCONL(OSCCON | 0x0002);   // Secondary (LP) Oscillator Enable bit


    // Configuring the auxiliary PLL, since the primary
    // oscillator provides the source clock to the auxiliary
    // PLL, the auxiliary oscillator is disabled. Note that
    // the AUX PLL is enabled. The input 8MHz clock is divided
    // by 2, multiplied by 24 and then divided by 2. Wait till
    // the AUX PLL locks.
    ACLKCON3 = 0x24C1;
    ACLKDIV3 = 0x7;
    ACLKCON3bits.ENAPLL = 1;
    while(ACLKCON3bits.APLLCK != 1);

}

