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

This example copies the buttons on the LEDs

It is referenced in the QuickStart document and serves as basis for it.

Make sure that the enabling jumpers are set for the buttons and the LEDs

Also make sure that the boot mode selected on the board is the same as the mode 
 selected in the target options. (RAM by default)
 
***************************************************************

This program will only work with the STR3x devices.

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


#include "STR73x.h"


 void Init(void)
 {

   unsigned char delay = 0 ;

	//tell io_putchar the board's crystal freq   
   __io_SetMainOscFreq(8000000);

	//configure REva LEDs D2 to D7 as output
   // GPIO2 and GPIO5 Clock Enable
   CFG->PCGR0 |= (0x0001 << 20) + (0x0001 << 23);
   for (delay = 8 ; delay != 0 ; delay--); /* Peripheral Stabilization */
	
	//configure REva LEDs as output
	// bits 10, 9, 8, 6, 5, 4 of P1 as open drain outputs,
	// other P1 bits as HiZ inputs
 	GPIO2->PC0 = 0x0000;
 	GPIO2->PC1 = 0x0000;
 	GPIO2->PC2 = 0xFFFF;
 	
	// P0[14..10] as pull up/down inputs,
	// P0[15, 9..0] as HiZ inputs
 	GPIO5->PC0 = 0xFFFF;
 	GPIO5->PC1 = 0xFFFF;
 	GPIO5->PC2 = 0x0000;
 	GPIO5->PD = 0xFFFF;
 }
 
 
 int main(void)
 {
	Init();

 	while(1)
 	{
			//D1=SW1 ... P2[1]	= P5[12]
			if (GPIO5->PD & 0x1000)
				GPIO2->PD |= 0x01;
			else
				GPIO2->PD &= ~0x01;

			//D2=SW2 ... P2[2]	= P5[13]
			if (GPIO5->PD & 0x2000)
				GPIO2->PD |= 0x02;
			else
				GPIO2->PD &= ~0x02;

			//D3=SW3 ... P2[3]	= P5[14]
			if (GPIO5->PD & 0x4000)
				GPIO2->PD |= 0x04;
			else
				GPIO2->PD &= ~0x04;

			//D4=SW4 ... P2[4]	= P5[15]
			if (GPIO5->PD & 0x8000)
				GPIO2->PD |= 0x08;
			else
				GPIO2->PD &= ~0x08;

			//D5=BT5 ... P2[5]	= P5[8]
			if (GPIO5->PD & 0x0100)
				GPIO2->PD |= 0x010;
			else
				GPIO2->PD &= ~0x010;
 
			//D6=BT6 ... P2[6]	= P5[9]
			if (GPIO5->PD & 0x0200)
				GPIO2->PD |= 0x020;
			else
				GPIO2->PD &= ~0x020;
 
 	}
 }
 
 

