/*
 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.   

 This file is part of the FreeRTOS.org distribution. 

 FreeRTOS.org is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version. 
 
 FreeRTOS.org is distributed in the hope that it will be useful,  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with FreeRTOS.org; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 A special exception to the GPL can be applied should you wish to distribute
 a combined work that includes FreeRTOS.org, without being obliged to provide
 the source code for any proprietary components.  See the licensing section 
 of http://www.FreeRTOS.org for full details of how and when the exception
 can be applied.  

 ***************************************************************************
 See http://www.FreeRTOS.org for documentation, latest information, license 
 and contact details.  Please ensure to read the configuration and relevant 
 port sections of the online documentation.

 Also see http://www.SafeRTOS.com a version that has been certified for use
 in safety critical systems, plus commercial licensing, development and
 support options.
 ***************************************************************************
 */

/* ---------------------------------------------------------------------------
 * Description: MAIN File to Application Project
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Jul 07, 2008, MMi
 *                 
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include "types.h"  
#include "sys_config.h"

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"  
#include "semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
/* Drivers */
#include "adc.h"
#include "bus.h"
#include "BusProtocol.h"
#include "calibrateaio.h"
#include "can.h"
#include "dac.h"
#include "dio.h"
#include "eeprom.h"
#include "ElecStatusCache.h"
#include "ethernet.h"
#include "irq.h"
#include "leds.h"
#include "menuargs.h"
#include "mmc.h"
#include "power.h"
#include "relay.h"
#include "rtc.h" 
#include "serial.h"
#include "SerOut.h"
#include "ssp0.h" 
#include "ssp1.h"

/* testfiles */
#include "topoftest.h" 

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
/* Demo application definitions. */
#define mainQUEUE_SIZE              ( 3 )
#define mainCHECK_DELAY             (( portTickType ) 5000 / portTICK_RATE_MS )
#define mainBASIC_WEB_STACK_SIZE    ( configMINIMAL_STACK_SIZE * 6 )

/* Task priorities. */  
#define mainQUEUE_POLL_PRIORITY     ( tskIDLE_PRIORITY + 2 )
#define mainCHECK_TASK_PRIORITY     ( tskIDLE_PRIORITY + 3 )
#define mainBLOCK_Q_PRIORITY        ( tskIDLE_PRIORITY + 2 )
#define mainFLASH_PRIORITY          ( tskIDLE_PRIORITY + 2 )
#define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )
#define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY ) 

/* Constants to setup the PLL. */
#define mainPLL_ENABLE              (( unsigned portLONG ) 0x0001 )
#define mainPLL_CONNECT             ((( unsigned portLONG ) 0x0002 ) | mainPLL_ENABLE )
#define mainPLL_FEED_BYTE1          (( unsigned portLONG ) 0xaa )
#define mainPLL_FEED_BYTE2          (( unsigned portLONG ) 0x55 )
#define mainPLL_LOCK                (( unsigned portLONG ) 0x4000000 )
#define mainPLL_CONNECTED           (( unsigned portLONG ) 0x2000000 )
#define mainOSC_ENABLE              (( unsigned portLONG ) 0x20 )
#define mainOSC_STAT                (( unsigned portLONG ) 0x40 )
#define mainOSC_SELECT              (( unsigned portLONG ) 0x01 )

/* Constants to setup the MAM. */
#define mainMAM_TIM_3               (( unsigned portCHAR ) 0x03 )
#define mainMAM_MODE_FULL           (( unsigned portCHAR ) 0x02 )

#define SWI_RAM_ADDR                0x40000008 
#define SWI_RAM_FUNC_ADDR           0x40000028
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */


/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
BOOLEAN StopFlashing= FALSE;
/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */
extern void vPortYieldProcessor (void);

/* Configure Hardware */
static inline void prvSetVectors (void);
static void prvSetupHardware (void);
static void prvSetupDrivers (void);

/* Main Task */
static void ExecuteTestsTask (void *pvParameters);

/*-----------------------------------------------------------*/

extern void *pvPortMalloc(size_t xWantedSize);

int main(void)
{
    static portBASE_TYPE taskCreationResult = -20;
    
    prvSetVectors(); 
    prvSetupHardware();
    

    /* Start the tasks defined within this file/specific to this demo.      */
    taskCreationResult = xTaskCreate(ExecuteTestsTask,
           ( signed portCHAR * ) "Tests", configMINIMAL_STACK_SIZE + 400,
            NULL, mainCHECK_TASK_PRIORITY - 1, NULL);

    
    /* Start the scheduler.                                                 */
    vTaskStartScheduler();
    
    /* Will only get here if there was insufficient memory to create 
     * the idle task. 
     */
    return 0; 
} 

/*-----------------------------------------------------------*/
void vApplicationTickHook(void)
{ 
}

/*-----------------------------------------------------------*/

void ExecuteTestsTask(void *pvParameters) 
{
    prvSetupDrivers();  
    
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)    
    ledSet(LED1, 1);
    ledSet(LED0, 1);
#endif
      
    mainInit(); 
    
    /* IDLE MODE */   
    for (;;)
        {
// \MARK NEW PINSETTINGS FOR TESTER (2)
#if (PINSET_TESTER == 2)
        vTaskDelay (1000);
        ledSet (LED0, FALSE);
        vTaskDelay (1000);
        ledSet (LED1, TRUE);
#else
        vTaskDelay (100);
#endif
                
        }
} 

/*-----------------------------------------------------------*/
 
inline void prvSetVectors() 
{
    unsigned int *ptr;
    // Set vectors of interrupt and software interupt

    // Set interrupt vectors 
    ptr = (unsigned int *)SWI_RAM_ADDR;
    *ptr = 0xE59FF018; // This is a ldr pc, [pc,#24] instruction

    // Put SWI, IRQ vectors in RAM
    ptr = (unsigned int *)SWI_RAM_FUNC_ADDR;
    *ptr = (unsigned int)&vPortYieldProcessor; // Reserved for FreeRTOS
}

/*-----------------------------------------------------------*/

void prvSetupHardware(void)  
{
    //UINT32 i = 0;  
    //volatile UINT32 *vect_addr, *vect_prio;

#ifdef RUN_FROM_RAM
    /* Remap the interrupt vectors to RAM if we are are running from RAM. */
    SCB_MEMMAP = 2;
#endif
    
    /* Disable the PLL. */
    PLLCON = 0;
    PLLFEED = mainPLL_FEED_BYTE1;     
    PLLFEED = mainPLL_FEED_BYTE2;
    /* Configure clock source. */
    SCS |= mainOSC_ENABLE;
    while ( !( SCS & mainOSC_STAT ))
        ;
    CLKSRCSEL = mainOSC_SELECT;
    
    /* Setup the PLL to multiply the XTAL input by 4. */
    PLLCFG = ( PLL_MUL | PLL_DIV );
    PLLFEED = mainPLL_FEED_BYTE1;
    PLLFEED = mainPLL_FEED_BYTE2;
    
    /* Turn on and wait for the PLL to lock... */
    PLLCON = mainPLL_ENABLE;
    PLLFEED = mainPLL_FEED_BYTE1;
    PLLFEED = mainPLL_FEED_BYTE2;
    CCLKCFG = CCLK_DIV;
    while ( !( PLLSTAT & mainPLL_LOCK ))
        ;
    
    /* Connecting the clock. */
    PLLCON = mainPLL_CONNECT;
    PLLFEED = mainPLL_FEED_BYTE1;
    PLLFEED = mainPLL_FEED_BYTE2;
    while ( !( PLLSTAT & mainPLL_CONNECTED ))
        ;
    
    /* Setup and turn on the MAM.  Three cycle access is used due to the fast
     PLL used.  It is possible faster overall performance could be obtained by
     tuning the MAM and PLL settings. */
    MAMCR = 0;
    MAMTIM = mainMAM_TIM_3;
    MAMCR = mainMAM_MODE_FULL;
    
    // Enable power on ethernet chip for using its RAM
    PCONP |= (1 << 30);
    
    init_VIC();
}

void prvSetupDrivers(void)
{

#if (PINSET_TESTER == 0)
    /* Load Inits necessary for the Olimex DemoBoard                        */
    ssp0Init();
    ssp0Enable();
    ssp1Init();
    ssp1Enable();
    rtcInit();
    MmcInit();
    CANInit(CANBitrate125k_12MHz);
    CANSetFilter (0x00000101);
//    ethInit();

#endif
#if (PINSET_TESTER == 1)
    /* Load Inits necessary and available on IO Controller --> REV_C <--    */
    bpecInit();
    dioInit();
    ledInit();
    ssp0Init();
    ssp0Enable();
    ssp1Init();
    ssp1Enable();
    eepromInit();
    adcInit();
    dacInit();
    powerInit();
    rtcInit();
    MmcInit();
    calibrationInit();
#endif

#if (PINSET_TESTER == 2)
    /* Load Inits necessary and available on QUA_2475_TESTER                */
    bpecInit();
    dioInit();
    rlyInit();
    ledInit();
    ssp0Init();
    ssp0Enable();
    ssp1Init();
    ssp1Enable();
    eepromInit();
    adcInit();
    dacInit();
    powerInit();
    rtcInit();
    MmcInit();
    calibrationInit();
    CANInit(CANBitrate125k_12MHz);
//    ethInit();

#endif
    
    menuInit();                             /* Init the Menu in every Case  */

    // Open both COM-ports
    serInit(COM1, B115200, UART_8N1, UART_FIFO_8);
    serInit(COM2, B115200, UART_8N1, UART_FIFO_8);

#if ((PINSET_TESTER == 1) || (PINSET_TESTER == 2))   
    /* BusInit only necessary on IO_CTRL or TESTER, DemoBoard got no Busses */
    busInit(BUS1);
    busInit(BUS2);
#endif
}

