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

This program is used to test that the debugging works.

It just counts and prints the value of the counter
 on the LEDs and the UART.

***************************************************************

This program will work with the STR7x and the STR9x devices.
Just make sure that the correct derivative is selected
 in the target options, the "#if"s will do the rest.
 
***************************************************************/

#include "STRx.h"

int delaycnt;
int k;

//this is the counter
volatile unsigned short i=0x0550;

//uncomment this line if you want the main function to be located
// in the FLASH BLOCK 1 (by default, it goes in BLOCK 0)
//int main() __attribute__ ((section (".b1text")));

int main()
{

	//GPIO6 goes to the LEDs
	SCU->PRR1  |= 0x00100000;  // release P6 reset
	SCU->PCGR1 |= 0x00100000; // clock gating P6
	SCU->GPIOOUT[6] = 0x5555; // enable GPIO6 OUTPUT for all pins
   GPIO6->DIR = 0x0FF;       // Dir output on GPIO6 for all pins
	
	//GPIO3 goes to the buttons
	SCU->PRR1  |= 0x00020000;  // release P3 reset
	SCU->PCGR1 |= 0x00020000; // clock gating P3
	SCU->GPIOIN[3] |= 0x60; // enable GPIO3 INPUT for pins 5-6
   GPIO3->DIR &= 0x09F;       // Dir input on GPIO3 for pins 5-6
	
	//GPIO4 goes to the switches
	SCU->PRR1  |= 0x00040000;  // release P4 reset
	SCU->PCGR1 |= 0x00040000; // clock gating P4
	SCU->GPIOIN[3] |= 0x0F; // enable GPIO4 INPUT for pins 0-3
   GPIO4->DIR &= 0x0F0;       // Dir input on GPIO4 for pins 0-3
	
	while(1)
	{
		//increment the counter
		i++;
		
		//read the value of the buttons
		k=(GPIO_READDATA(GPIO3)&0x60)+(GPIO_READDATA(GPIO4)&0x0F);
		
		//print value of the counter and buttons on the LEDs
		GPIO_WRITEDATA(GPIO6, k);
		
		//delay
      for(delaycnt=0;delaycnt<10000;delaycnt++);
	}

	//this will never be executed. (and is probably optimized out)	
	// but it avoids a warning when compiling
	return 0;
}


