/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use a timer to generate periodic interrupts.
 */

#include <stdio.h>
#include <conio.h>
#include <sys/arm7tdmi.h>
#include <target.h>

extern char _bdata;				// data start
#define IRQ_VEC	(*((long *)&_bdata+14))		// IRQ vector

static long ticks;				// tick counter

/*
 * IRQ service
 *
 * Only t0 is enabled for IRQ,
 * no need for polling IRQSTA.
 */
static void __attribute__((interrupt))		// handle as ISR!
irq_sr(void)
{
	Intern_t0clri = 0;			// clear int flag
	ticks++;				// up tick count
}

/*
 * Timer0 is setup to generate an interrupt every 10ms (=tick).
 * The tick count may be monitored anytime by pressing any key.
 */
int
main(void)
{
	IRQ_VEC = (long)irq_sr;			// set IRQ SR addr
	Intern_irqen = 4;			// enable t0 for IRQ

	Intern_t0ld  = _CCLK/16/100;		// set load to 10ms
	Intern_t0con = 0xc4;			// periodic @cclk/16

	ENABLE_INTERRUPTS;			// enable core ints

	puts("Hit any key to display tick count...");
	while (1) {
		getch();
		printf("ticks: %10lu [10ms]\n", ticks);
	}
}
