/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use TPU channel 4 as interrupt input.
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/m68k.h>
#include <target.h>

static long icnt;				/* interrupt counter */

/*
 * TP4 interrupt service
 */
#pragma interrupt				/* handle as ISR! */
static void
tp4isr(void)
{
	icnt++;					/* up count */
	INTERN.tpu.cisr &= ~0x10;		/* reset int flag */
}
#pragma endinterrupt

/*
 * TP4 is initialized as interrupt input. The
 * interrupt count is printed once per second.
 */
int
main(void)
{
	INTERN.tpu.tmcr	     = ARB_TPU;		/* set arbitration, enable TPU */
	INTERN.tpu.ticr	     = 0x600|VEC_TPU;	/* set base vector, IRQ level 6 */
	INTERN.tpu.c[4].p[0] = 8;		/* TP4 detect falling edge */
	INTERN.tpu.cfsr2    |= 8;		/*     DIO function   (CFS =  8) */
	INTERN.tpu.hsrr1    |= 0x300;		/*     initialize     (HSR =%11) */
	INTERN.tpu.cpr1	    |= 0x300;		/*     high priority  (CPR =%11) */
	while (INTERN.tpu.hsrr1&0x300) ;	/*     wait for ready (HSR =%00) */
	INTERN.tpu.cisr &= ~0x10;		/*     reset int flag */
	INTERN.tpu.cier |=  0x10;		/*     enable int */

	EXCEPT_VEC(VEC_TPU+4) = tp4isr;		/* set ISR addr */
	ENABLE_CPU_INTERRUPTS;

	while (1) {
		printf("TP4 interrupts: %ld\n", icnt);
		_delay(1000000);
	}
}

