//*****************************************************************************
//
// bl_uart.c : Functions to transfer data via the UART port.
// Author    : QJ Wang. qjwang@ti.com
// Date      : 9-19-2012
//
// 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 <stdio.h>
#include "bl_config.h"
//#include "UART_Task.h"
#if defined(UART_ENABLE_UPDATE)
#include "HL_sci.h"
#include "bl_uart.h"
#include "sci_common.h"
#include "bl_ymodem.h"
#include "HL_sys_common.h"
#include "bl_flash.h"

uint32_t JumpAddress;
void get_software_Version(void);
void get_hardware_Info(void);

//extern uint32_t g_pulUpdateSuccess[8];
//extern uint32_t g_ulUpdateStatusAddr;
//extern uint32_t g_ulUpdateBufferSize;    //32 bytes or 8 32-bit words
//extern char fileName[FILENAME_LEN];
//char tab_1024[1024] = {    0  };

//*****************************************************************************
//
//  bl_uart_api
//
//
//*****************************************************************************
/**
  * @brief  Download a file via serial port
  * @param  None
  * @retval None
  */
//void UART_Download()
//{
//	char Numberstr[10] = {0};
//	int Size = 0;
//
//	UART_putString(UART, "\n\r Waiting for the file to be sent ... ");
//	UART_putString(UART, "\n\r Use Transfer->Send File and Ymodem Protocol from HyperTerminal \n\r");
//
//	Size = Ymodem_Receive(UART, &tab_1024[0]);
//	if (Size > 0)
//	{
//		UART_putString(UART, "\n\r The application image has been programmed successfully!\n\r---------------------------\n\r Name: ");
//		UART_putString(UART, fileName);
//		sprintf(Numberstr, "%d", Size);
//		UART_putString(UART, "\n\n\r Size:     ");
//		UART_putString(UART, Numberstr);
//		UART_putString(UART, "  Bytes\n\r");
//		UART_putString(UART, "---------------------------\n\n\n\n\r");
//	}
//	else
//	{
//		UART_putString(UART, "\n\rFailed to receive the file!\n\r");
//	}
//}

/**
  * @brief  Upload a file via serial port.
  * @param  None
  * @retval None
  */
//void UART_Upload(void)
//{
//	uint32_t status = 0;
//	uint32_t *updateInfo;
//	uint32_t imageSize;
//	uint32_t imageAddress;
//
//	updateInfo = (uint32_t *)g_ulUpdateStatusAddr;
//
//	imageAddress = updateInfo[1];
//	imageSize = updateInfo[2];
//
//
//	UART_putString(UART, "\n\n\rSelect Receive File and Ymodel protocol in the drop-down menu... (press any key to abort)\n\r");
//
//	if (UART_getKey(UART) == CRC)
//	{
//		/* Transmit the flash image through ymodem protocol */
//		status = Ymodem_Transmit(UART, (uint8_t*)imageAddress, (char*)"UploadedApplicationImage.bin", imageSize);
//
//		if (status != 0)
//		{
//			UART_putString(UART, "\n\rError occured while transmitting\n\r");
//		}
//		else
//		{
//			UART_putString(UART, "\n\rApplication image has been transmitted successfully \n\n\n\r");
//		}
//	}
//	else
//	{
//		UART_putString(UART, "\r\n\nAborted by user.\n\r");
//	}
//
//}

//*****************************************************************************
//
//! This function performs the update on the selected port.
//!
//! This function is called directly by the boot loader or it is called as a
//! result of an update request from the application.
//!
//! \return Never returns.
//
//*****************************************************************************
extern void delay(unsigned int delayval);
//char buf[1000],buf2[16*1024];
void UpdaterUART(void)
{
    while(1);
//	char key = 0;
//	unsigned int FlashDestination = APP_START_ADDRESS ;            /* Flash user program offset */
//	uint32_t check = 0;
//	 uint32_t i = 0;
//	 for(i = 0;i<1000;i++)
//	 {
//	     buf[i] = i+10;
//	 }
//	while (1)
//	{
////		LITE_TOPRIGHT_LED;
//		UART_putString(UART, "\r\n================== Main Menu ==========================\r\n");
//		UART_putString(UART, "  1. Download Application Image To the Internal Flash \r\n");
//		UART_putString(UART, "  2. Upload The Application Image From the Internal Flash \r\n");
//		UART_putString(UART, "  3. Execute The Application Code \r\n");
//		UART_putString(UART, "  4. Get Bootloader Version \r\n");
//		UART_putString(UART, "  5. Get Device Information \r\n");
//
//		UART_putString(UART, "=======================================================\r\n\n");
//
//		key = UART_getKey(UART);
//
//		if (key == 0x31)
//		{
//
//
//             Fapi_BlockErase(FlashDestination+100, 1000);
//
//             delay(5000000);
//
//             Fapi_BlockProgram(FlashDestination, (unsigned long)buf, 1000);
//
//             delay(5000000);
////extern uint32_t Fapi_BlockRead( uint32_t Flash_Start_Address, uint32_t Data_Start_Address, uint32_t Size_In_Bytes);
////check =   Fapi_BlockRead( FlashDestination, (unsigned long)buf2, 32);
//if(check)
//{
//    UART_putString(UART, " read error! \r\n");
//}
//             for(i = 0;i<1000;i++)
//             {
////                 UART_putString(UART, "buf2 = %d  i=%d \r\n",buf2[i],i);
//                 delay(5000);
//             }
////             UART_putString(UART, " 5. Get Device Information \r\n");
//			/* Download user application in the Flash */
////			UART_Download();
//		}
//		else if (key == 0x32)
//		{
//			/* Upload user application from the Flash */
//			UART_Upload();
//		}
//		else if (key == 0x33)
//		{
//			JumpAddress = (uint32_t)APP_START_ADDRESS;
//			//JumpAddress = 0;
//			((void (*)(void))JumpAddress)();
//			UART_putString(UART, "The application is running now !! \n\r\n\r");
//		}
//		else if (key == 0x34)
//		{
//			get_software_Version();
//		}
//		else if (key == 0x35)
//		{
//			get_hardware_Info();
//		}
//		else
//		{
//			UART_putString(UART, "Invalid Number !! \n\r\n\r");
//		}
//	}
}

#endif


