//*****************************************************************************
//
// bl_main.c      : The file holds the main control loop of the boot loader.
// Author         : QJ Wang. qjwang@ti.com
// Date           : 5-9-2013
//
// Copyright (c) 2006-2011 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************


#include "bl_config.h"
#include "system.h"
#include "bl_check.h"
#include "reg_can.h"
#include "hw_sci.h"
#include "bl_can.h"

//*****************************************************************************
//
// bl_main_api
//
//*****************************************************************************
#if defined (CAN_ENABLE_UPDATE)


//*****************************************************************************
//
// This holds the current remaining size in bytes to be downloaded.
//
//*****************************************************************************
uint32 g_ulTransferSize;
uint32 g_ulAppImageSize;

//*****************************************************************************
//
// This holds the stack top at the first word of APP_START_ADDRESS
// This value should be 0x0800xxxx. I will write 0xFFFFFFFF at the begining indicating
// failed update, when the update complete successfully, it will be updated to the
// actual value
//
//*****************************************************************************
uint32 g_ulStackTopData;

//*****************************************************************************
//
// This holds the current address that is being written to during a download
// command.
//
//*****************************************************************************
uint32 g_ulTransferAddress;
uint32 g_ulAppImageAddress;

//*****************************************************************************
//
// This is the data buffer used during transfers to the boot loader.
//
//*****************************************************************************
uint32 g_pulDataBuffer[BUFFER_SIZE];

#define E_PASS     		0
#define E_FAIL     		0x1U
#define E_TIMEOUT  		0x2U

//*****************************************************************************
//
// This is an specially aligned buffer pointer to g_pulDataBuffer to make
// copying to the buffer simpler.  It must be offset to end on an address that
// ends with 3.
//
//*****************************************************************************
uint8 *g_pucDataBuffer[BUFFER_SIZE];

//*****************************************************************************
//
// This holds the current address that is being written to during a download
// command.
//
//*****************************************************************************
//extern uint32 g_ulTransferAddress;

void delay(void) {
	  static volatile uint32 delayval;
	  delayval = 10000;   // 100000 are about 1ms
	  while(delayval--);
}
void delay_long(void) {
	  static volatile uint32 delayval;
	  delayval = 10000;   // 100000 are about 10ms
	  while(delayval--);
}

void main(void)
{
    /** - Initialize SCI Routines to receive Command and transmit data */
	sciInit();

#if defined (CAN_ENABLE_UPDATE)
	UART_putString(UART, "\r Hercules MCU CAN BootLoader ");
#endif
	UART_putString(UART, "\r TI Safety MCU Application Team, qjwang@ti.com \r\r");
}

#endif
