#include "archinc.h"
#include "medusa.h"

#ifdef CONFIG_BOARD_MEDUSA

/* MBARI Medusa board */

#define ORANGE_LED_MASK 0x00001000
#define GREEN_LED_MASK  0x00002000

static boolean greenLedState  = false;
static boolean orangeLedState = false;

int medusaInit(void)
{
  PINSEL0 = 0x80005555;
  PINSEL1 = 0x55400000;

  IOSET   = 0x00000E00; // negate ZL50409 bus ctrl
  IOCLR   = 0x07003000; // LEDs off to start, clear Zarlink A[2:0] 
  IODIR   = 0x07003E00; // LEDs, '409 bus ctrl are outputs, leave D[7:0] input

  return 0;
}

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
void medusaSetGreenLed(boolean onOff)
#else
void INLINE_KEYWORD medusaSetGreenLed(boolean onOff)
#endif
{
  if (onOff == false)
    IOCLR = GREEN_LED_MASK;
  else
    IOSET = GREEN_LED_MASK;

  greenLedState = onOff;
}

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
void medusaToggleGreenLed(void)
#else
void INLINE_KEYWORD medusaToggleGreenLed(void)
#endif
{
    medusaSetGreenLed(greenLedState == true ? false : true);
}

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
void medusaSetOrangeLed(boolean onOff)
#else
void INLINE_KEYWORD medusaSetOrangeLed(boolean onOff)
#endif
{
  if (onOff == false)
    IOCLR = ORANGE_LED_MASK;
  else
    IOSET = ORANGE_LED_MASK;

  orangeLedState = onOff;
}

#ifdef CONFIG_TOOLCHAIN_IAR
#pragma inline
void medusaToggleOrangeLed(void)
#else
void INLINE_KEYWORD medusaToggleOrangeLed(void)
#endif
{
    medusaSetOrangeLed(orangeLedState == true ? false : true);
}

#endif /* CONFIG_BOARD_MEDUSA */
