/***************************************************************

   FlashLEDS.c
   Zworld, 2000

   Rabbit 2000 Core Module Demo board example program

   This program flashes the DS2 and DS3 LED's on the Rabbit
   2000 Core Module Prototyping board (PA0 and PA1 ports on
   the Rabbit Core Module).
   It also demonstrates coding with assembly instructions,
   cofunctions and costatements.

****************************************************************/

/***************************************************************
  predefines for the leds and switches
****************************************************************/
#define DS1 0
#define DS2 1

/***************************************************************
  initialize_ports();
  set up the parallel ports so PORTA are outputs and PORTE bit 0
  is and output. 
****************************************************************/
void initialize_ports()
{
#asm
	ld		a,0x84				; port a all outputs
ioi ld	(SPCR),a
	ld		(SPCRShadow), a	; load value into shadow register
	xor	a						; port e bit 1..7 inputs, 0 output
ioi ld	(PEFR),a
	ld		(PEFRShadow), a	; load shadow register
	ld		a,0x01
ioi ld	(PEDDR),a
	ld		(PEDDRShadow), a	; load shadow register
	xor	a
ioi ld	(PECR),a
	ld		(PECRShadow), a
#endasm
}

/***************************************************************
  ledon(int led)
  turn the specified led on.
****************************************************************/
void ledon(int led)
{
	(1<<led)^0xff;
#asm
ioi ld	a,(PADR)
	and	l
ioi ld	(PADR),a
	ld		(PADRShadow), a
#endasm
}

/***************************************************************
  ledoff(int led)
  turn the specified led off.
****************************************************************/
void ledoff(int led)
{
	1<<led;
#asm
ioi ld	a,(PADR)
	or		l
ioi ld	(PADR),a
	ld		(PADRShadow), a
#endasm
}

/***************************************************************
  flashled[4](int,int,int)
  cofunction which flashes the "which" led on for ontime then
  off for offtime
****************************************************************/

cofunc flashled[4](int which, int ontime, int offtime)
{
	for(;;) {
		waitfor(DelayMs(ontime));
		ledoff(which);
		waitfor(DelayMs(offtime));
		ledon(which);
	}
}

main()
{
	initialize_ports();
	
	for(;;) {
				
		costate {
			wfd {
				flashled[0](DS1,500,1000);    // Flashes DS2  PA0
				flashled[1](DS2,1000,500);    // Flashes DS3  PA1
				}
		}
	}
}