/*******************************************************************************
*                                                                              *
* Copyright 2010 Rheinmetall Canada Inc.                                  	   *
*                                                                              *
* No part of this document may be reproduced, stored in                        *
* a retrieval system, or transmitted, in any form or by any means,             *
* electronic, mechanical, photocopying, recording, or otherwise,               *
* without the prior written permission of Rheinmetall Canada Inc.              *
*                                                                              *
*******************************************************************************/

#include "define.h"
#include "BoardCfg.h"
#include "InternalUart.h"
#include "FlashInterface.h"
#include "BootloaderProtocol.h"
#include "BootloaderStateMachine.h"
#include "BootloaderInterface.h"
#include "DigitalIO.h"
#include "SPI_Flash.h"
//#include "FatFS/ff.h"

//#pragma config FMIIEN = OFF, FSRSSEL = PRIORITY_7, FETHIO = ON
//#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
//#pragma config POSCMOD = XT, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config ICESEL = ICS_PGx2, BWP = ON



////#pragma config FMIIEN = OFF, FSRSSEL = PRIORITY_7, FETHIO = ON
////#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
//#pragma config FPLLMUL = MUL_19, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
//#pragma config POSCMOD = XT, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config ICESEL = ICS_PGx1, BWP = ON


//#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF, WDTPS = PS128
//#pragma config POSCMOD = EC, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config POSCMOD = XT, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config ICESEL = ICS_PGx2, BWP = OFF

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF, WDTPS = PS4096   //Watchdog timeout = 4,096s
#pragma config POSCMOD = EC, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config POSCMOD = XT, FNOSC = PRIPLL, FPBDIV = DIV_1
#pragma config ICESEL = ICS_PGx2, BWP = OFF
#pragma config FSOSCEN = OFF


void _general_exception_handler(void);
	
int main(void)
{
//	FATFS fs;
//    FRESULT fr;
//    FIL fil;
    char RdBuf[256];
    unsigned int rd,wr;

	

	
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);	//Use peripheral library to optimize configuration
 //   INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);// configure for multi-vectored mode interrupts
    AD1PCFG = 0xFFFF;	//Sart with I/O pins configured as digital I/O
    
	InitBoard();
    InternalUartInit();
    InitFlashInterface();
//    ProtocolInit();
    BootloaderSMInit();
    InitDigitalIO();
    
    OpenInternalPort(0,0,9600,INT_UART_ONE_STOP_BIT,INT_UART_NO_PARITY);
	
	InitSPIFlash();
	
	if(SPIFlashCheckAndConfigure() == RET_OK)
	{
		BootloaderSMSetSPIFlashDetected(RET_OK);
	}
	else
	{
		BootloaderSMSetSPIFlashDetected(RET_ERROR);
	}
	
	
    
//    fr = f_mount(&fs,"",1);
//    fr = f_open(&fil,"JF.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
//    if(fr == FR_OK)
//    {
//        fr = f_read(&fil,RdBuf,100,&rd);
//        fr = f_write(&fil,RdBuf,rd,&wr);
//    }
//    else
//    {
//        int toto = 0xDEADBEEF;
//    }
//    f_close(&fil);
     
//    ProtocolSendCmd(BOOTLOADER_READY_CMD,0,0);
    HEARTBEAT_LED_1_PIN = 0;
    
    char mbuf[30];
	mbuf[0] = 'B';
	mbuf[1] = 'o';
	mbuf[2] = 'o';
	mbuf[3] = 't';
	mbuf[4] = 'l';
	mbuf[5] = 'o';
	mbuf[6] = 'a';
	mbuf[7] = 'd';
	mbuf[8] = 'e';
	mbuf[9] = 'r';
	mbuf[10] = ' ';
	mbuf[11] = 'V';
	mbuf[12] = '0';
	mbuf[13] = '.';
	mbuf[14] = '0';
	mbuf[15] = '1';
	mbuf[16] = '\n';
    mbuf[17] = '\r';
	SendInternalUartData(&mbuf[0],18);
	
	
    
    int i,j;
    i =0;
    j = 0;
    while(1)
    {

	    if(ReadInternalUart() > 0)
	    {
//			New Data arrived.
//			nothing special to do.
//			could flash a led
			HEARTBEAT_LED_2_PIN = !HEARTBEAT_LED_2_PIN;
		} 
//	    
//	    if(e_CommandPending)
//	    {
//			//Valid command received.
//			ProtocolExecCmd();
//			e_CommandPending = false;
//		} 
//		
		BootloaderSM(SM_TICK_EVENT,SM_NULL_DATA);
		
		if(++i >= 30000)
		{
		//	if(++j >= 10000000)
			{
				HEARTBEAT_LED_1_PIN = ~HEARTBEAT_LED_1_PIN;
				i = 0;
				j = 0;
			}	
		}	
		
			
	} 
	
	
    

	return true;
}



void _general_exception_handler(void)
{
	char mbuf[30];
	mbuf[0] = 'E';
	mbuf[1] = 'x';
	mbuf[2] = 'c';
	mbuf[3] = 'e';
	mbuf[4] = 'p';
	mbuf[5] = 't';
	mbuf[6] = 'i';
	mbuf[7] = 'o';
	mbuf[8] = 'n';
	mbuf[9] = ' ';
	mbuf[10] = 'o';
	mbuf[11] = 'c';
	mbuf[12] = 'c';
	mbuf[13] = 'u';
	mbuf[14] = 'r';
	mbuf[15] = 'r';
	mbuf[16] = 'e';
	mbuf[17] = 'd';
	mbuf[18] = '\r';
	SendInternalUartData(&mbuf[0],19);
						
	SoftReset();
    
    while(1);
					
}		

//EOF

