//------------------------------------------------------------------------------
// File:          switches.c
// Function:      Trigger bootloader using board switches
// Supported chip(s):
//    - AT91SAM7XC128
//    - AT91SAM7XC256
// Supported toolchain(s):
//    - IAR Embedded Workbench
// Date created:  12 May 2006
// Created by:    JJo
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------

#include "switches.h"

#ifdef USE_SWITCHES

//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------

/**
 * Name:      trigger_init
 * Purpose:   Initializes switches
 */
void trigger_init() {

  debug_printf("Switches: Initializing ...\n");
  
  // Configure PMC for PIOA & switches as inputs
  AT91F_PIOA_CfgPMC();
  AT91F_PIO_CfgInput(AT91C_BASE_PIOA, SWITCH_COMBINATION);

  debug_printf("Switches: Initialization done.\n");
}

/**
 * Name:      trigger_cleanup
 * Purpose:   Cleans up the switches
 */
void trigger_cleanup() {

  debug_printf("Switches: Cleaning up ...\n");

  // Shut down PMC for PIOA
  AT91F_PMC_DisablePeriphClock(AT91C_BASE_PMC, AT91C_ID_PIOA);

  debug_printf("Switches: Cleanup done.\n");
}

/**
 * Name:      trigger_poll
 * Purpose:   Returns whether the switches are activated or not
 * Output:    TRUE if all switches are activated, FALSE otherwise
 */
unsigned int trigger_poll() {

  if ((AT91F_PIO_GetInput(AT91C_BASE_PIOA) & SWITCH_COMBINATION) == SWITCH_COMBINATION) {

    return FALSE;
  }
  else {

    return TRUE;
  }
}

#endif // USE_SWITCHES
