#ifdef __USE_CMSIS
#include "LPC8xx.h"
#endif
#include "type.h"

#include "lpc8xx_clkconfig.h"
#include "lpc8xx_gpio.h"
#include "lpc8xx_mrt.h"

extern uint32_t mrt_counter;

#define mLed 1

void SwitchMatrix_Init()
{
    /* Enable SWM clock */
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);

    /* Pin Assign 8 bit Configuration */
    /* none */

    /* Pin Assign 1 bit Configuration */
    /* SWCLK */
    /* SWDIO */
    /* RESET */
    LPC_SWM->PINENABLE0 = 0xffffffb3UL;
}
/* Main Program */

int main (void) {
  uint32_t regVal;
  //SwitchMatrix_Init();

  SystemCoreClockUpdate();

  /* Config CLKOUT, mostly used for debugging. */
  regVal = LPC_SWM->PINASSIGN8 & ~( 0xFF << 16 );
  LPC_SWM->PINASSIGN8 = regVal | ( 12 << 16 );	/* P0.12 is CLKOUT, ASSIGN(23:16). */
  CLKOUT_Setup( CLKOUTCLK_SRC_MAIN_CLK );

	#if 0
	regVal = LPC_SWM->PINASSIGN0 & ~( (0xFF << 0) | (0xFF << 8) );
	LPC_SWM->PINASSIGN0 = regVal | ( (2 << 0) | (3 << 8) );	/* P0.2 is UART0 TX, ASSIGN(7:0); P0.3 is UART0 RX. ASSIGN(15:8). */
#endif

  /* Enable AHB clock to the GPIO domain. */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);

  /* Set port p0.7 to output */
//  GPIOSetDir( 0, 7, 1 );
  GPIOSetDir( 0, mLed, 1 );

	init_mrt(0x8000);
	
  while (1)                                /* Loop forever */
  {
		/* I/O configuration and LED setting pending. */
		if ( (mrt_counter > 0) && (mrt_counter <= 200) )
		{
//			GPIOSetBitValue( 0, 7, 0 );
			GPIOSetBitValue( 0, mLed, 0 );
		}
		if ( (mrt_counter > 200) && (mrt_counter <= 400) )
		{
//			GPIOSetBitValue( 0, 7, 1 );
			GPIOSetBitValue( 0, mLed, 1 );
			//GPIOSetBitValue( 0, 0, 1 );
		}
		else if ( mrt_counter > 1200 )
		{
			mrt_counter = 0;
		}
  }
}