/*******************************************************************************
 * (c) Copyright 2009 Actel Corporation.  All rights reserved.
 *
 *  Application demo for Smartfusion
 *
 *
 * Author : Actel's Corporate Application Team
 * Rev    : 1.0.0.4
 *
 *******************************************************************************/

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

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

/**************************************************************************/
/* Firmware Includes */
/**************************************************************************/

#include "a2fxxxm3.h"
#include "./drivers/mss_gpio/mss_gpio.h"
#include "./drivers/mss_watchdog/mss_watchdog.h"
#include "./drivers/mss_uart/mss_uart.h"
#include "../drivers/mss_ace/mss_ace.h"

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

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "cpu_types.h"
#include "conf_eth.h"

/**************************************************************************/
/* Preprocessor Macros                                                    */
/**************************************************************************/

#define SYS_TICK_CTRL_AND_STATUS_REG      0xE000E010
#define SYS_TICK_CONFIG_REG               0xE0042038
#define SYS_TICK_FCLK_DIV_32_NO_REF_CLK   0x31000000
#define ENABLE_SYS_TICK                   0x7
#define mainETH_TASK_PRIORITY            ( configMAX_PRIORITIES - 1)

/**************************************************************************/
/*Declaration of global variables                                         */
/**************************************************************************/

xTaskHandle aceTaskHndl = NULL;

/**************************************************************************/
/* Extern Declarations*/
/**************************************************************************/
extern void ace_task(void *para);
extern void web_task(void *para);

extern void init_mac(void);
extern void ACE_Initialization(void);
extern void vStartEthernetTask( unsigned portBASE_TYPE uxPriority );

/**************************************************************************/
/*Functions for Interrupt Handlers */
/**************************************************************************/


/**************************************************************************/
/* Function to initialization all necessary hardware components for this  */
/* demo                                                                   */
/**************************************************************************/

void init_system()
{
    /* Disable the Watch Dog Timer */
    MSS_WD_disable( );
    /* Initialize the GPIO */
    MSS_GPIO_init();
    /* Initialize and configure UART0. */
    MSS_UART_init
    (
        &g_mss_uart0,
        MSS_UART_57600_BAUD,
        MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
    );
    /* ACE Initialization */
    ACE_init();
} 

/***************************************************************************/
/* Entry to Main form user boot code                                       */
/***************************************************************************/

int main()
{
    /* Initialization all necessary hardware components */
    init_system();
    iprintf("Welcome to SmartFusion lwIP web server Demo\n\r");
    iprintf("using freeRTOS\n\r");
    iprintf("use the following IP to browse the web server\n\r");
    init_mac();

    vStartEthernetTask( mainETH_TASK_PRIORITY );
    /* Enable the SYS TICK Timer and provide the divider and clock source
     * this is required to enable the RTOS tick */

    *(volatile unsigned long *)SYS_TICK_CTRL_AND_STATUS_REG = ENABLE_SYS_TICK;
    *(volatile unsigned long *)SYS_TICK_CONFIG_REG          = SYS_TICK_FCLK_DIV_32_NO_REF_CLK;

    /* Start the scheduler. */
    vTaskStartScheduler();

    /* Will only get here if there was not enough heap space to create the
    idle task. */

    iprintf("\n Test Case End: Should never Come here \n\r");

    return 0;

}
