/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... enter idle mode.
 *
 *  ... use the RTC for wakeup once a minute.
 */

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

#define LED 0x10000				// LED =P0.16

/*
 * RTC interrupt service
 */
static void __attribute__((interrupt))		// handle as ISR!
rtc_isr(void)
{
	Intern_ilr = 1;				// clear int flag
	Intern_vicvectaddr = 0;			// reset VIC priority logic
	puts("... wakeup\7");
	Intern_iodir |= LED;			// LED on
	while (!Intern_sec) ;			// wait 1s
	Intern_iodir &= ~LED;			// LED off
}

/*
 * The RTC is setup to generate an interrupt when the
 * minutes counter changes. Then idle mode is entered.
 *
 * Every minute, the RTC interrupt wakes up the core
 * and lights the LED for one second. Then idle mode
 * is re-entered.
 */
int
main(void)
{
	_settime(0, 0);				// start clock

	Intern_vicvectaddr0 = (long)rtc_isr;	// set ISR addr
	Intern_vicintenable = 0x2000;		// enable rtc int
	Intern_vicvectcntl0 = 0x2d;		//  vector

	Intern_ciir = 2;			// int on minutes inc
	Intern_amr  = 0xff;			// alarm int off
	Intern_ilr  = 3;			// clear pending ints

	ENABLE_INTERRUPTS;			// enable core ints

	/*
	 * Enter idle mode again and again...
	 */
	while (1) {
		puts("Going to sleep for 1 minute...");
		Intern_pcon = 1;
	}
}

