#include <stdio.h>
#include <ADuCM360.H>
#include <string.h>

#include <..\ClkLib.h>
#include <..\IntLib.h>
#include <..\DioLib.h>
#include <..\RstLib.h>
#include <..\PwrLib.h>
#include <..\WdtLib.h>

void delay(long int);
int iGoToSleep = 0;
int main (void)
{
    volatile int iPwrMod = 0;  
        
         WdtCfg(T3CON_PRE_DIV1,T3CON_IRQ_EN,T3CON_PD_DIS); // Disable Watchdog timer resets
   DioOen(pADI_GP1,0x8);                             // Set P1.3 as an output to toggle the LED
          //Disable clock to unused peripherals
   ClkDis(CLKDIS_DISSPI0CLK|CLKDIS_DISSPI1CLK|CLKDIS_DISI2CCLK|CLKDIS_DISUARTCLK|CLKDIS_DISPWMCLK|CLKDIS_DIST0CLK|CLKDIS_DIST1CLK|CLKDIS_DISDACCLK|CLKDIS_DISDMACLK|CLKDIS_DISADCCLK);
   ClkCfg(CLK_CD0,CLK_HF,CLKSYSDIV_DIV2EN_DIS,CLK_UCLKCG);            // Select CD0 for CPU clock - 16Mhz clock
         EiCfg(EXTINT2,INT_EN,INT_FALL);                    
         EiCfg(EXTINT4,INT_EN,INT_FALL);
         iGoToSleep = 0;
   NVIC_EnableIRQ(EINT4_IRQn);                       // Enable external interrupt 4 - used to wake-up MCU
         NVIC_EnableIRQ(EINT2_IRQn);                       // Enable external interrupt 2 - used to put MCU into required sleep mode
   while (1)
   {        
           DioTgl(pADI_GP1,0x8);        // Toggle P1.3
           delay(0x60000);              // Delay routine        
     if (iGoToSleep     !=0)
                 {
                         iGoToSleep = 0;
                         PwrCfg(PWRMOD_MOD_TOTALHALT);
                         //PwrCfg(PWRMOD_MOD_PERHALT);
                 }
   }
}
