/*********************************************************************
*                SEGGER MICROCONTROLLER GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 2003-2008     SEGGER Microcontroller GmbH & Co KG       *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

----------------------------------------------------------------------
File    : BSP.c
Purpose : BSP for Luminary LM3S6965 eval board
--------  END-OF-HEADER  ---------------------------------------------
*/

#include "BSP.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "sysctl.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/


/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/


/*********************************************************************
*
*       BSP_GetKeyStat
*/
unsigned BSP_GetKeyStat(void) {
  unsigned r;
  unsigned KeyStatHW;
  
  r         = 0;
  KeyStatHW = GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
  if (KeyStatHW & GPIO_PIN_0) {   // PE0/PWM4 Up
    r |= KEY_STAT_UP;
  }
  if (KeyStatHW & GPIO_PIN_1) {   // PE1/PWM5 Down 
    r |= KEY_STAT_DOWN;
  }
  if (KeyStatHW & GPIO_PIN_2) {  // PE2/PhB1 Left
    r |= KEY_STAT_LEFT;
  }
  if (KeyStatHW & GPIO_PIN_3) {  // PE3/PhA1 Right
    r |= KEY_STAT_RIGHT;
  }
  return r;
}

/*********************************************************************
*
*       BSP_Init()
*/
void BSP_Init(void) {
  //
  // Configure the GPIO that drives the on-board LED.
  //
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_DIR_MODE_OUT);
  //
  // Configure the GPIOs used to read the state of the on-board push buttons.
  //
  GPIODirModeSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_IN);
  GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
  GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_DIR_MODE_IN);
}

/*********************************************************************
*
*       LED switching routines
*       LEDs are switched on with low level on port lines
*/
void BSP_SetLED(int Index) {
  GPIOPinWrite(GPIO_PORTF_BASE, 1 << Index, 1 << Index);
}

void BSP_ClrLED(int Index) {
  GPIOPinWrite(GPIO_PORTF_BASE, 1 << Index, 0);
}

void BSP_ToggleLED(int Index) {
  if (GPIOPinRead(GPIO_PORTF_BASE, 1 << Index)) {
    BSP_SetLED(Index);
  } else {
    BSP_ClrLED(Index);
  }
}
