// Copyright (c) 2010 Rowley Associates Limited.
//
// This file may be distributed under the terms of the License Agreement
// provided with this software.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#include <ctl_api.h>
#include <targets/LPC2478.h>

#define EINT0_INT 14

void
ctl_board_init(void)
{
  IO0DIR |= 1 << 13;
  IO0SET = 1 << 13;
}

static CTL_ISR_FN_t userButtonISR;
static void
buttonISR(void)
{
  userButtonISR();
  EXTINT = 1;
}

void
ctl_board_on_button_pressed(CTL_ISR_FN_t isr)
{
  unsigned long v;
  userButtonISR = isr;

  EXTMODE |= 1; /* EXT0 is edge sensitive */
  EXTPOLAR &= ~1; /* EXT0 is falling edge sensitive */
  PINSEL4 |= 1 << 20; /* Configure P2.10 for alternate function EINT0 */

  ctl_set_isr(EINT0_INT, 1, CTL_ISR_TRIGGER_FIXED, buttonISR, 0);
  ctl_unmask_isr(EINT0_INT);
}

void
ctl_board_set_leds(unsigned v)
{
  if (v)
    IO0CLR = 1 << 13;
  else
    IO0SET = 1 << 13;
}

