//-----------------------------------
// File:          main.c
// Function:      Boot test program 1
// Supported chip(s):
//    - AT91SAM7XC128
//    - AT91SAM7XC256
// Supported toolchain(s):
//    - IAR Embedded Workbench
// Date created:  04 May 2006
// Created by:    JJo
//-----------------------------------

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

#include "board.h"
#include <AT91SAM7X256.h>
#include <lib_AT91SAM7X256.h>

//-----------------------------------
// Defines

#define SPEED     (MCK / 80)
#define NB_LEDS   4
#define LED1      AT91C_PIO_PB19
#define LED2      AT91C_PIO_PB20
#define LED3      AT91C_PIO_PB21
#define LED4      AT91C_PIO_PB22
#define LED_MASK  (LED1 | LED2 | LED3 | LED4)

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

/**
 * Name:      wait
 * Effect:    Waits for a specified period of time.
 * Input:
 *  - Time to wait for
 */
void wait(unsigned int time) {

    for (unsigned int i=0; i < time; i++);
}

//-----------------------------------
// Main

int main() {

  // Local variables
  const int leds[] = {LED1, LED2, LED3, LED4};

  // Configure leds
  AT91F_PIOB_CfgPMC();
  AT91F_PIO_CfgOutput(AT91C_BASE_PIOB, LED_MASK);
  AT91F_PIO_SetOutput(AT91C_BASE_PIOB, LED_MASK);

  // Led swing loop
  while (1) {

    for (unsigned int i=0; i < NB_LEDS; i++) {

      AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, leds[i]);
      wait(SPEED);
      AT91F_PIO_SetOutput(AT91C_BASE_PIOB, leds[i]);  
    }
  }
}
