/*************************************************************************
 * (c) Copyright 2009 Actel Corporation.  All rights reserved.
 *
 *  Application demo for Fusion
 *
 *
 * Author : Actel Application Team
 * Rev     : 1.0.0.3
 *
 **************************************************************************/

/**************************************************************************/
/* Standard Includes */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/**************************************************************************/
/* Driver Includes */
/**************************************************************************/

#include "core_gpio.h"

/**************************************************************************/
/* RTOS Includes */
/**************************************************************************/

#include "FreeRTOS.h"
#include "semphr.h"

#define LEDS_ON   0x00000000

/**************************************************************************/
/* Extern Declarations */
/**************************************************************************/

extern volatile unsigned char inLedTask;
extern xSemaphoreHandle         ledSemHndl;
extern void delay ( volatile uint32_t n);
extern gpio_instance_t g_gpio;

/**************************************************************************/
/* Function LED initialization */
/**************************************************************************/

void led_initialization()
{
    /* Configuration of GPIO's */
	GPIO_config( &g_gpio, GPIO_0, GPIO_OUTPUT_MODE );
	GPIO_config( &g_gpio, GPIO_1, GPIO_OUTPUT_MODE );
	GPIO_config( &g_gpio, GPIO_2, GPIO_OUTPUT_MODE );
	GPIO_config( &g_gpio, GPIO_3, GPIO_OUTPUT_MODE );

	/* Set GPIO to initial value */
    GPIO_set_outputs( &g_gpio, LEDS_ON );
    return;
}

/**************************************************************************/
/* Function LED test */
/* This function blinks the LEDs */
/**************************************************************************/

void led_test()
{
    uint32_t gpio_pattern;
    gpio_pattern = GPIO_get_outputs( &g_gpio );
    gpio_pattern ^= 0x0000000F;
    GPIO_set_outputs( &g_gpio, gpio_pattern );
    /*This delay is required to distinguish the ON and OFF of LEDs */
    delay(1000000);
    return;
}

/**************************************************************************/
/* Creating LED test as task for RTOS */
/**************************************************************************/

void led_task(void *para)
{
    while(1)
    {
        /* This Semaphore waits for the user input for LED test */
        xSemaphoreTake( ledSemHndl, portMAX_DELAY) ;
        while(1)
        {
            led_test();
            if(!inLedTask)
            break;
        }
    }
}
