#include "archinc.h"
#include "nohau.h"

#ifdef CONFIG_BOARD_NOHAU

#define LED_DELAY_RAM (65535)
#define LED_DELAY_FLASH (16384)
#define LED_DELAY_COUNT (LED_DELAY_FLASH)
#define LED_DELAY_LOOP {for(i=0; i<LED_DELAY_COUNT; i++);}
static volatile int i;

/* initialize the Nohau board */
int nohauInit(void)
{
  int j;
  

  // set all LEDS to outputs
  IODIR |= 0x0000FF00;
  // turn them all on
  IOCLR = 0x0000FF00;
  // crude delay
  LED_DELAY_LOOP
  // turn them all off
  IOSET = 0x0000FF00;

  for (j=0;j<8;j++) {
    nohauLedOn(j);
    LED_DELAY_LOOP
    nohauLedOff(j);
  }

  for (j=7;j>=0;j--) {
    nohauLedOn(j);
    LED_DELAY_LOOP
    nohauLedOff(j);
  }

  return 0;
}

void nohauToggleLed(unsigned char toggleLed)
{
  lpc210x_reg gpioToggleBits = (((lpc210x_reg)(0x100 << toggleLed)) & 0xff00);
  lpc210x_reg gpioState = IOPIN;

  gpioState ^= gpioToggleBits;

  IOSET = gpioState;
  IOCLR = ~gpioState;
}

/* LEDs seem to be on P0.8 through P0.15 so leave the other gpio unmolested */
void nohauLedOn(unsigned char ledsOn)
{
  lpc210x_reg gpioClrBits = (((lpc210x_reg)(0x100 << ledsOn)) & 0xff00);
  IOCLR = gpioClrBits;
}

/* LEDs seem to be on P0.8 through P0.15 so leave the other gpio unmolested */
void nohauLedOff(unsigned char ledsOff)
{
  lpc210x_reg gpioSetBits = (((lpc210x_reg)(0x100 << ledsOff)) & 0xff00);
  IOSET = gpioSetBits;
}

#endif /* CONFIG_BOARD_NOHAU */

