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

#include <stdio.h>
#include <target.h>

/*
 * Return TP4 level.
 */
static int
tp4get(void)
{
	INTERN.tpu.hsrr1 |= 0x300;		/* update now     (HSR =%11) */
	while (INTERN.tpu.hsrr1&0x300) ;	/* wait for ready (HSR =%00) */

	/*
	 * Parameter RAM[1] now contains the last
	 * 16 TP4 levels. Bit0 is the oldest, and
	 * Bit15 is the current level.
	 */
	return !!(INTERN.tpu.c[4].p[1]&0x8000);	/* return current level */
}

/*
 * TP4 is setup as digital input. When hitting
 * RETURN the TP4 level is sampled and printed.
 */
int
main(void)
{
	INTERN.tpu.tmcr	  = 0;			/* enable TPU */
	INTERN.tpu.cfsr2 |= 8;			/* TP4 DIO function      (CFS =  8) */
	INTERN.tpu.hsqr1 |= 0x200;		/*     update on HSR %11 (HSQ =%10) */
	INTERN.tpu.cpr1	 |= 0x300;		/*     high priority     (CPR =%11) */

	while (1) {
		puts("Hit RETURN to sample TP4..."), getchar();
		printf("TP4 is %s\n", tp4get()? "high": "low");
	}
}

