/**********************************************************************
* $Id$		gpio_int.c  				2010-05-21
*//**
* @file		gpio_int.c
* @brief	This example used to test GPIO interrupt function
* @version	2.0
* @date		21. May. 2010
* @author	NXP MCU SW Application Team/Micromint USA Support
*
* Copyright(C) 2010, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/
#include "LPC17xx.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_libcfg.h"
#include "lincoln_board.h"

/* Example group ----------------------------------------------------------- */
/** @defgroup GPIO_Interrupt	GPIO_Interrupt
 * @ingroup GPIO_Examples
 * @{
 */

/************************** PRIVATE DEFINITIONS *************************/

#define POLL_LED	(1<<LED1_PIN)		// User LED
#define POLL_LED_PORT   LED1_PORT
#define INT3_LED	(1<<LED2_PIN)		// User LED
#define INT3_LED_PORT   LED2_PORT
#define GPIO_INT	(1<<USERPB_PIN)		// test GPIO interrupt on USER pushbutton


/************************** PRIVATE FUNCTIONS *************************/
void user_push_button_IRQHandler(void);

void delay (void);

/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
 * @brief		External interrupt 3 handler sub-routine
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void EINT3_IRQHandler(void)
{
  int j;
  if(GPIO_GetIntStatus(USERPB_PORT, USERPB_PIN, 0))
  {
    GPIO_ClearInt(USERPB_PORT,(1<<USERPB_PIN));
    for (j= 0; j<8; j++)
    {
	/* blink LED when EINT3 occur  */
      GPIO_ClearValue(INT3_LED_PORT, INT3_LED);        
      delay();
      GPIO_SetValue(INT3_LED_PORT, INT3_LED);
      delay();
    }
  }
}

/*-------------------------PRIVATE FUNCTIONS------------------------------*/
/*********************************************************************//**
 * @brief		Delay function
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void delay (void) {
  unsigned int i;

  for (i = 0; i < 0x100000; i++) {
  }
}

int main(void)
{
  /*Set the LED ports to outputs */
  GPIO_SetDir(POLL_LED_PORT, POLL_LED, 1);  /* Configure as output */
  GPIO_SetDir(INT3_LED_PORT, INT3_LED, 1);  /* Configure as output */
  /* Turn off all LEDs */
  GPIO_SetValue(POLL_LED_PORT, POLL_LED); 
  GPIO_SetValue(INT3_LED_PORT, INT3_LED);  
  /* Enable GPIO interrupt */
  GPIO_IntCmd(USERPB_PORT,1<<USERPB_PIN,0);
  NVIC_EnableIRQ(EINT3_IRQn);
  while (1)
  {
    //polling led while idle
    GPIO_SetValue(POLL_LED_PORT, POLL_LED);
    delay();
    GPIO_ClearValue(POLL_LED_PORT, POLL_LED);
    delay();
  }
}

#ifdef  DEBUG
/*******************************************************************************
* @brief		Reports the name of the source file and the source line number
* 				where the CHECK_PARAM error has occurred.
* @param[in]	file Pointer to the source file name
* @param[in]    line assert_param error line source number
* @return		None
*******************************************************************************/
void check_failed(uint8_t *file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while(1);
}
#endif

/*
 * @}
 */
