#include<lpc17xx.h>

#include "sdcard.h"
#include "uart.h" 
#include "fat32.h"
#include "stdutils.h"
#include "delay.h"	 //User defined library which contains the delay routines
#include "spi.h" 
#include "lcd.h"    //User defined LCD library which contains the lcd routines 
#include "rtc.h"    
#include "gpio.h"
  
#define LED1 P1_29
#define SWITCH1 P2_11
#define SBIT_TIMER0 1

#define CapturePulse P1_26
#define PCLK_TIMER0  2

uint16_t Pm25,Pm10;
uint32_t TimeOccupancy;

float finalPm25,finalPm10;

extern unsigned int SystemCoreClock;
unsigned int getPrescalarForUs(uint8_t timerPclkBit);

/************************** PRIVATE FUNCTIONS *************************/
/* Interrupt service routines */
void TIMER0_IRQHandler(void);

/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
 * @brief		TIMER0 interrupt handler sub-routine
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void TIMER0_IRQHandler(void)
{

 	if((LPC_TIM0->IR & (1<<4)) == 0x01) // if MR0 interrupt
	{
        LPC_TIM0->IR |= 1 << 4;         // Clear Capture interrupt flag	
		UART0_Printf("Time capture:");
		TimeOccupancy = LPC_TIM0->CR0; // Read Capture register 0 value
		UART0_Printf("%d\n\r",TimeOccupancy);

	}
	GPIO_PinToggle(LED1) ;

}


int main() 
{
    GPIO_PinDirection(LED1,OUTPUT);               /* Configure LED pins as output */

    GPIO_PinDirection(SWITCH1,INPUT);             /* Configure Switch pins as input */

	GPIO_PinFunction(CapturePulse, PINSEL_FUNC_3);	   /* Configure Pin port p1.26 as Capture function*/

	GPIO_PinDirection(CapturePulse,INPUT); 		 /* Configure Pin port p1.26 as Input */


	LPC_SC->PCONP |= (1<<SBIT_TIMER0);			/* Power ON Timer0 */

	/* Capture Control Register. The CCR controls which edges of the
	   capture inputs are used to load the Capture Registers and whether
	   or not an interrupt is generated when a capture takes place.
	   T0CCR - 0x4000 4028
	   T1CCR - 0x4000 8028 */

	LPC_TIM0->CCR &= ~(0x3);	/*	T0/1/2/3CCR (Table 431) for capture events. Interrupts are enabled in the NVIC using the appropriate
    							Interrupt Set Enable register.  */

	LPC_TIM0->CCR |= 0;

	 /* Timer Counter. The 32-bit TC is incremented every PR+1 cycles of PCLK. The TC is controlled through the TCR.  */

   	LPC_TIM0->TC = 0;	  // RESET Timer Counter 

	 /* Prescale Counter. The 32-bit PC is a counter which is incremented
        to the value stored in PR. When the value in PR is reached, the TC
        is incremented and the PC is cleared. The PC is observable and
        controllable through the bus interface. */

	LPC_TIM0->PC = 0;	   // RESET Prescale Counter 
	
	/* Prescale Register. When the Prescale Counter (below) is equal to
       this value, the next clock increments the TC and clears the PC. */

	LPC_TIM0->PR = 0;	  // RESET Prescale Register

	/* TCR Timer Control Register. The TCR is used to control the Timer
	   Counter functions. The Timer Counter can be disabled or reset
	   through the TCR. */
	LPC_TIM0->TCR |= ((uint32_t)(1<<1)); // Hold counter reset
	LPC_TIM0->TCR &= ~((uint32_t)(1<<1)); // Release counter reset

	LPC_TIM0->PR   = getPrescalarForUs(PCLK_TIMER0);      /* Prescalar for 1us -- sysclk/4 */

	  LPC_TIM0->IR = 0xFFFFFFFF; // Reset all interrupts
//	LPC_TIM0->IR = 0xFFFFFFFF //Clear Interrupt.	Writing a logic one to the corresponding IR bit will reset the interrupt. Writing a zero has no effect.

	LPC_TIM0 -> CCR &= ~((uint32_t)(7<<(0x03)));

	// Enable Rising Edge
	LPC_TIM0->CCR |= (1<<0); 

   // Enable Falling Edge
	LPC_TIM0->CCR |= (1<<1);
	
	// Enable Interrupt on caption (Interrupt on CAPn.0 event: a CR0 load due to a CAPn.0 event will generate an interrupt.) 
	LPC_TIM0->CCR |= (1<<2); 

		/* preemption = 1, sub-priority = 1 */
	NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));

	NVIC_EnableIRQ(TIMER0_IRQn);                          /* Enable Timer0 Interrupt */

	// Start TIMER0 by setting the counter Enable
	LPC_TIM0->TCR |= ((uint32_t)(1<<0)); // Enable counter

    while(1)
    {
     //   GPIO_PinWrite(LED1,GPIO_PinRead(SWITCH1)); /* Read switch status and turn ON/OFF Leds accordingly */
           
    }
}


unsigned int getPrescalarForUs(uint8_t timerPclkBit)
{
    unsigned int pclk,prescalarForUs;
    pclk = (LPC_SC->PCLKSEL0 >> timerPclkBit) & 0x03;  /* get the pclk info for required timer */

    switch ( pclk )                                    /* Decode the bits to determine the pclk*/
    {
    case 0x00:
        pclk = SystemCoreClock/4;
        break;

    case 0x01:
        pclk = SystemCoreClock;
        break; 

    case 0x02:
        pclk = SystemCoreClock/2;
        break; 

    case 0x03:
        pclk = SystemCoreClock/8;
        break;

    default:
        pclk = SystemCoreClock/4;
        break;  
    }

    prescalarForUs =pclk/1000000;                      /* Prescalar for 1us (1000000Counts/sec) */

    return prescalarForUs;
}






