//timer interrupt demo application demo application 

#include "xtmrctr.h"
#include "xil_exception.h"
#include "xil_printf.h"
#include <stdio.h>
#include "xparameters.h"


#include "xinterrupt_wrap.h"

/************************** TIMER Constant Definitions *****************************/
#define XTMRCTR_BASEADDRESS	XPAR_XTMRCTR_0_BASEADDR
#define TIMER_CNTR_0	 0
#define RESET_VALUE	 100000000
 //number of clock cycles for interrupt

int SetupTimerInterrupt(XTmrCtr *InstancePtr, UINTPTR BaseAddr);
static void TmrCtrDisableIntr( u16 IntrId, UINTPTR IntrParent);
static void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber);
XTmrCtr TimerCounterInst;   /* The instance of the Timer Counter */
volatile int TimerExpired;
/************************************************************************************/

volatile u32 * ledptr=(u32 *)XPAR_XGPIO_0_BASEADDR;
u32 ledcounter=0;


int main(void)
{
	int Status=0;

    //let us set up timer interrupt
    SetupTimerInterrupt(&TimerCounterInst,
				   XTMRCTR_BASEADDRESS);
	if (Status != XST_SUCCESS) {
		xil_printf("Tmrctr interrupt Example Failed\r\n");
		return XST_FAILURE;
	}

    while (1==1)
    {
        //do something.....        
    }
	return 0;
    TmrCtrDisableIntr((&TimerCounterInst)->Config.IntrId, (&TimerCounterInst)->Config.IntrParent);

}


int SetupTimerInterrupt(XTmrCtr *TmrCtrInstancePtr,
		      UINTPTR BaseAddr)
{
	int Status;
	u8 TmrCtrNumber = TIMER_CNTR_0;

	/*
	 * Initialize the timer counter so that it's ready to use,
	 * specify the device ID that is generated in xparameters.h
	 */
	Status = XTmrCtr_Initialize(TmrCtrInstancePtr, BaseAddr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Perform a self-test to ensure that the hardware was built
	 * correctly, use the 1st timer in the device (0)
	 */
	Status = XTmrCtr_SelfTest(TmrCtrInstancePtr, TmrCtrNumber);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Connect the timer counter to the interrupt subsystem such that
	 * interrupts can occur.  This function is application specific.
	 */
	Status = XSetupInterruptSystem(TmrCtrInstancePtr, (XInterruptHandler)XTmrCtr_InterruptHandler, \
				       TmrCtrInstancePtr->Config.IntrId, TmrCtrInstancePtr->Config.IntrParent, \
				       XINTERRUPT_DEFAULT_PRIORITY);

	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Setup the handler for the timer counter that will be called from the
	 * interrupt context when the timer expires, specify a pointer to the
	 * timer counter driver instance as the callback reference so the
	 * handler is able to access the instance data
	 */
	XTmrCtr_SetHandler(TmrCtrInstancePtr, TimerCounterHandler,
			   TmrCtrInstancePtr);

	/*
	 * Enable the interrupt of the timer counter so interrupts will occur
	 * and use auto reload mode such that the timer counter will reload
	 * itself automatically and continue repeatedly, without this option
	 * it would expire once only
	 */
	XTmrCtr_SetOptions(TmrCtrInstancePtr, TmrCtrNumber,
			   XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION);

	/*
	 * Set a reset value for the timer counter such that it will expire
	 * eariler than letting it roll over from 0, the reset value is loaded
	 * into the timer counter when it is started
	 */
	XTmrCtr_SetResetValue(TmrCtrInstancePtr, TmrCtrNumber, RESET_VALUE);

	/*
	 * Start the timer counter such that it's incrementing by default,
	 * then wait for it to timeout a number of times
	 */
	XTmrCtr_Start(TmrCtrInstancePtr, TmrCtrNumber);

	TimerExpired = 0;
    
	return XST_SUCCESS;
}

/*****************************************************************************/
/**
* This function is the handler which performs processing for the timer counter.
* It is called from an interrupt context such that the amount of processing
* performed should be minimized.  It is called when the timer counter expires
* if interrupts are enabled.
*
* This handler provides an example of how to handle timer counter interrupts
* but is application specific.
*
* @param	CallBackRef is a pointer to the callback function
* @param	TmrCtrNumber is the number of the timer to which this
*		handler is associated with.
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber)
{
	
    xil_printf(".");
    *ledptr=ledcounter;
    ledcounter++;
    return;  
}


void TmrCtrDisableIntr( u16 IntrId, UINTPTR IntrParent)
{
	XDisableIntrId( IntrId, IntrParent);
}
