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

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

/*
 * Set TP2 to level.
 */
static void
tp2set(int level)
{
	while (INTERN.tpu.hsrr1&0x30) ;		/* wait for ready (HSR =%00) */
	INTERN.tpu.hsrr1 |= level? 0x10: 0x20;	/* set high/low   (HSR =%01/10) */
}

/*
 * TP2 is setup as digital output, initially
 * low. When hitting RETURN TP2 gets toggled.
 */
int
main(void)
{
	INTERN.tpu.tmcr	     = 0;		/* enable TPU */
	INTERN.tpu.c[2].p[0] = 0x82;		/* TP2 output ch, force pin low */
	INTERN.tpu.cfsr3    |= 0x800;		/*     DIO function  (CFS =  8) */
	INTERN.tpu.hsrr1    |= 0x30;		/*     initialize    (HSR =%11) */
	INTERN.tpu.cpr1	    |= 0x30;		/*     high priority (CPR =%11) */

	while (1) {
		puts("Hit RETURN to set TP2 high..."), getchar(), tp2set(1);
		puts("Hit RETURN to set TP2 low..." ), getchar(), tp2set(0);
	}
}
