/*    
	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.
	***************************************************************************
*/

/* includes */
#include "types.h"
#include "sys_config.h"
#include "dio.h"
#include "leds.h"
#include "serial.h"
#include "irq.h"
#include "bus.h"
#include "appimage.h"
#include "LPC23xx.h"
#include "CommListeners.h"  
#include "InternalFlash.h"
#include "Watchdog.h"
 
/* Constants to setup the PLL. */
#define mainPLL_ENABLE		( ( UINT32 ) 0x0001 )
#define mainPLL_CONNECT		( ( ( UINT32 ) 0x0002 ) | mainPLL_ENABLE )
#define mainPLL_FEED_BYTE1	( ( UINT32 ) 0xaa )
#define mainPLL_FEED_BYTE2	( ( UINT32 ) 0x55 )
#define mainPLL_LOCK		( ( UINT32 ) 0x4000000 )
#define mainPLL_CONNECTED	( ( UINT32 ) 0x2000000 )
#define mainOSC_ENABLE		( ( UINT32 ) 0x20 )
#define mainOSC_STAT		( ( UINT32 ) 0x40 )
#define mainOSC_SELECT		( ( UINT32 ) 0x01 )

#define SWI_RAM_ADDR        0x40000008   
#define SWI_RAM_FUNC_ADDR	0x40000028    

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

/* Configure the hardware as required by the demo. */
static inline void prvSetVectors();
static void prvSetupHardware( void );
static void prvSetupDrivers( void );
static void cpu_swi_isr( void );

BOOLEAN StopFlashing = FALSE;   

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


int main( void )               
{ 
	prvSetVectors();  
	prvSetupHardware();
	prvSetupDrivers();
	
	ENABLE_INTERRUPTS(); 
	
	ledSet( LED1, 1 ); // Turn both LED's on to indicate Bootcode
	ledSet( LED0, 1 );  

	//watchdogEnable( 10000 );   
	
	if ((appiValidAppImageAvail() == TRUE) && (appiApplicationRequestsUpdate() == FALSE))
	{
	    // Reset watchdog flag anyway
	    watchdogCausedReset();
	
		ledSet( LED1, 0 ); 
		ledSet( LED0, 0 ); 

		appiJumpToAppImage();   
	}
 
	// Enter program state
	initCom1Listener(); 
	initCom2Listener();   
	initBus1Listener(); 
	initBus2Listener();
	
	// for now stay in bootloader to test different things
	for (;;)
	{
		listen2Com1();
		listen2Com2(); 
		listen2Bus1();
		listen2Bus2();
		
		watchdogFeed();
	}
}

  
inline void prvSetVectors()
{
	unsigned int *ptr;
	// Set vectors of interrupt, software interupt and fiq 
	
	// Set interrupt vectors
	ptr = (unsigned int *)SWI_RAM_ADDR;
	*ptr = 0xE59FF018;    // This is a ldr pc, [pc,#24] instruction
	
	// Put SWI, IRQ & FIQ vectors in RAM
	ptr = (unsigned int *)SWI_RAM_FUNC_ADDR;
	*ptr = (unsigned int)&cpu_swi_isr;
}


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

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;  
	
	init_VIC();  
}

void prvSetupDrivers( void )
{
	iflashInit(); 
	ledInit();
    ledSet( LED1, 1 ); 
    ledSet( LED0, 1 ); 
//for(;;);
	
	// Open both COM-ports
	serInit( COM1, B57600, UART_8N1, UART_FIFO_8);
    serInit( COM2, B57600, UART_8N1, UART_FIFO_8);
    busInit( BUS1 );
    busInit( BUS2 );
}

void cpu_swi_isr()
{
	for (;;);
}

