⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 update.cpp

📁 coldfire5206芯片的boot程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "bastype.h"
#include "uartdrv.h"
#include "netdrv.h"
#include "graph.h"
#include "commprotocol.h"
#include "flash.h"
#include "mbusman.h"
#include "main.h"
#include "mcf5206.h"
#include "update.h"

#define MAX_RECIEVE_LENGTH	2000
#define MAX_FILE_BUFFER		0x300000

#define DOING_COLOR		BLINKYELLOW
#define DONE_COLOR		YELLOW
#define FAIL_COLOR		BLINKRED
//#define	SUCCESS_COLOR	YELLOW
// 将所有升级文件信息放在这个缓冲,文件连续存放,由g_stUpdateInfo确定每个文件的大小
SYS_UpdateInfo g_stUpdateInfo;
UCHAR g_acFileBuffer[MAX_FILE_BUFFER];
UINT32 g_lFileBufPos;		

UINT8 g_acSendBuffer[MAX_RECIEVE_LENGTH];
UINT32	g_lSendSize;
UINT32  g_lSendPos;

UINT8 g_acReceiveBuffer[MAX_RECIEVE_LENGTH];

CCommProtocol::COMMINFO g_stCurrentSendInfo;
CCommProtocol::COMMINFO g_stCurrentRecieveInfo;
CCommProtocol g_clCommProtocol;

static INT16 SendToSelfPort(UCHAR* pSendInfo, UINT16 nSendLength);
static INT16 ReceiveFromSelfPort(UCHAR* pReceiveInfo, UINT16 nReceiveLength);
static INT16 SendToTargetPort(UCHAR* pSendInfo, UINT16 nSendLength);
static INT16 ReceiveFromTargetPort(UCHAR* pReceiveInfo, UINT16 nReceiveLength);
static void DisplayUpateInfo(UINT32 nNormalID, UINT32 nResultID, BOOL bIsFail = FALSE);

#ifdef BIOS_DEBUG
// for test, use TIME1, 13500 = 1s,
extern UINT32 g_nOneTransferTimeForTest1;
extern UINT32 g_nOneTransferTimeForTest2;
extern UINT32 g_nOneTransferTimeForTest3;
extern UINT32 g_nOneTransferTimeForTest4;
extern UINT32 g_nOneTransferTimeForTest5;
extern UINT32 g_nOneTransferTimeForTest6;
static UINT32 g_nResendSta;
static UINT32 g_nResendTimer;
#endif

enum 
{
	UDI_START_MODULE,
	UDI_START_SYS,
	UDI_INIT_NET,
	UDI_CONNECTING,
	UDI_TRANSFER_DATA,
	UDI_WRITE_FLASH,
	UDI_OVER_SUCCEED,
	UDI_OVER_FAIL,
	UDI_OVER_FAIL_FILE_SIZE,
	UDI_EEPROM_FAIL,
	UDI_SYS_RESTART,
	
	UDI_PASS,
	UDI_SUCCEED,
	UDI_FAIL
};
static char* s_aacDisplayInfo[] = 
{
	"Upgrading module soft",
	"Upgrading monitor soft",
	"Initializing upgrade port",
	"Connecting with PC",
	"Transfering data with PC",
	"Writing flash",
	"Upgrade is success",
	"Upgrade is failure, try again after restarting",
	"The selecting contents are too many, please download at twice",
	"EEPROM is not operated now, please reboot and retry",
	"Disconnect the net wire, and reboot the monitor",

	"Pass",
	"Succeed",
	"Fail"
};
// posx = 40
static UINT16 s_nDisplayPosY[] = 
{
	100,
	100,
	140,
	180,
	220,
	260,
	300,
	300,
	300,
	320,
	340
};

/*********************************************************************
* 函数名称:		Update
* 说	明:		update and renew old system or others in flash
* 入口参数:
*				void  -- 无
* 返 回 值:
*				BOOL  -- 1, success; 0, failure
* 作	者:		Shi Liangcai 
* 时	间:	    2002-09-31 12:07:31 
*********************************************************************/
BOOL Update()
{
	int i, nLen;
	INT16 nTempFlag;
	int nCommSuccessFlag;
	UINT32 nTempLen111;
	char acDisplayString[200];
	UCHAR* pTempReceiveStr  =  g_acReceiveBuffer + NET_FRAME_EXTRA_ADD_HEAD;
	UCHAR* pTempSendStr	= g_acSendBuffer + NET_FRAME_EXTRA_ADD_HEAD;
	
	BOOL m_bIsConnected = FALSE;

	g_stUpdateInfo.nFileCount = 0;
	nCommSuccessFlag = 1;
#ifdef BIOS_DEBUG
	g_nResendSta = 0;
#endif
	if (g_stSysFlag.stESysFlag.ucMainSysIsValid != 1
		|| g_stSysFlag.stFSysFlag.ucSelfMachineID == g_stSysFlag.stESysFlag.ucUpdateMachineID )		// whether main system is valid
	{
		TextOut(40, s_nDisplayPosY[UDI_START_SYS], DONE_COLOR, 0, s_aacDisplayInfo[UDI_START_SYS]);
	}
	else
	{
		TextOut(40, s_nDisplayPosY[UDI_START_MODULE], DONE_COLOR, 0, s_aacDisplayInfo[UDI_START_MODULE]);
	}

	DisplayUpateInfo(UDI_INIT_NET, 0);
	if (!InitUpdatePort())
	{
		DisplayUpateInfo(UDI_INIT_NET, UDI_FAIL, TRUE);
		DisplayUpateInfo(UDI_SYS_RESTART, 0);
		CloseUpdatePort();

		for (int j = 0; j < 100; j ++)
		{
			Sleep(40);	// all delay 4 s
		}
//		DisableSysInterrupt(MCF5206::IM_TIMER1);

		return FALSE;
	}
	DisplayUpateInfo(UDI_INIT_NET, UDI_SUCCEED);


	DisplayUpateInfo(UDI_CONNECTING, 0);

	if (g_stSysFlag.stESysFlag.ucMainSysIsValid != 1)		// whether main system is valid
	{
		// main system is invalid, and boot system need to confirm version with PC
		nCommSuccessFlag = 0;
		g_stUpdateInfo.nOldSoftNo = g_stSysFlag.stFSysFlag.ucSelfMachineID;
		memset(g_stUpdateInfo.acOldVerNo, 0x00, 3);
		g_stUpdateInfo.acOldVerNo[3] = '0';
		g_stCurrentSendInfo.commData.verData.soft_no = g_stUpdateInfo.nOldSoftNo;
		memcpy(g_stCurrentSendInfo.commData.verData.ver_info, g_stUpdateInfo.acOldVerNo, 4);
		g_stCurrentSendInfo.commData.verData.flash_no =  g_stSysFlag.stFSysFlag.ucFlashSizeType;
		g_stCurrentSendInfo.commData.verData.module_no = 0x00;
		StartupComm(CRT_VERSION);
	
		while (1)
		{
			nLen = ReceiveFromSelfPort(pTempReceiveStr, MAX_RECIEVE_LENGTH);
			if (nLen > 0)
			{          
				if (m_bIsConnected == FALSE)
				{
					DisplayUpateInfo(UDI_CONNECTING, UDI_SUCCEED);
					DisplayUpateInfo(UDI_TRANSFER_DATA, 0);
					m_bIsConnected = TRUE;
				}

				// 桢结束,目前不处理错误数据,即TransferAndAnalyse返回-1的情况
				if (g_clCommProtocol.TransferAndAnalyse(nLen, pTempReceiveStr) == 1)
				{
					g_lSendSize = g_clCommProtocol.GetCommSendLength();
					if (g_lSendSize)	// 发送应答或其他数据
					{
						g_clCommProtocol.GetSendData(pTempSendStr);
						while (SendToSelfPort(pTempSendStr, g_lSendSize) != g_lSendSize);
					}
					nTempFlag = AnalyseCommResult();
					if (nTempFlag == -1)
						break;
					if (nTempFlag == 0)
						continue;

					if (nTempFlag == 1)
					{
						//	接受方更加傻瓜化,不再判断机器类型,将版本的返回值中的机器类型置为发送方的机器类型
						//	版本处理也由发送方处理,不再由接受方处理
//						if (g_stUpdateInfo.nNewSoftNo == g_stUpdateInfo.nOldSoftNo)
//						{
						nCommSuccessFlag = 1;
						break;
//						}
					}
				}
			}
		}
	}
	else
	{
		if (g_stSysFlag.stFSysFlag.ucSelfMachineID != g_stSysFlag.stESysFlag.ucUpdateMachineID)
		{
			UINT32 nTempFlag20 = 0;
			UINT32 nTempFlag10 = 0;
			CCommProtocol clTempProtocol;	// 用于检查包的完整性
			UINT32 lPos = 0;
			INT16  nTempValue;
			/* for debug bios 
			clTempProtocol.SetCpuIsSame(1);
			*/
			clTempProtocol.SetCpuIsSame(0);
			g_stCurrentSendInfo.nCommType = CST_VERSION;
			clTempProtocol.StartupComm(g_stCurrentSendInfo);

			g_clCommProtocol.SetCpuIsSame(0);
			g_stCurrentSendInfo.nCommType = CRT_BREAK;

			// clear tramismiting's buffer
			g_clCommProtocol.StartupComm(g_stCurrentSendInfo);

			g_lSendSize = g_clCommProtocol.GetCommSendLength();

			while(1)
			{
				nLen = ReceiveFromSelfPort(pTempReceiveStr, MAX_RECIEVE_LENGTH);
				if (nLen > 0)
				{
				if (m_bIsConnected == FALSE)
				{
					DisplayUpateInfo(UDI_CONNECTING, UDI_SUCCEED);
					DisplayUpateInfo(UDI_TRANSFER_DATA, 0);
					m_bIsConnected = TRUE;
				}
					while (SendToTargetPort( pTempReceiveStr, nLen)	!= nLen);
//					Sleep(nLen + 1);
					if ( nTempFlag20 == 0 )
					{
						if (g_clCommProtocol.TransferAndAnalyse(nLen, pTempReceiveStr) == 1)
						{
							if (g_clCommProtocol.GetEndCommFlag()  == CCommProtocol::CET_NORMAL)
							{
								nTempFlag20 = 1;
								DisplayUpateInfo(UDI_TRANSFER_DATA, UDI_SUCCEED);
								DisplayUpateInfo(UDI_WRITE_FLASH, 0);

								g_stCurrentSendInfo.nCommType = CRT_RENEW;
								// clear tramismiting's buffer
								g_clCommProtocol.StartupComm(g_stCurrentSendInfo);
								g_lSendSize = g_clCommProtocol.GetCommSendLength();

							}
							else
							{
								if (g_clCommProtocol.GetEndCommFlag()  != CCommProtocol::CET_NOT)
								{
									// clear tramismiting's buffer
									g_stCurrentSendInfo.nCommType = CRT_BREAK;
									g_clCommProtocol.StartupComm(g_stCurrentSendInfo);		
									g_lSendSize = g_clCommProtocol.GetCommSendLength();
								}
							}
						}
					}
				}

				nLen = ReceiveFromTargetPort(pTempReceiveStr, MAX_RECIEVE_LENGTH);
				if (nLen > 0)
				{
					// 保证接收到模块的包是完整时,再发会PC
					memcpy(pTempSendStr + lPos, pTempReceiveStr, nLen);
					lPos += nLen;
					nTempValue = clTempProtocol.TransferAndAnalyse(nLen, pTempReceiveStr, 0);
					if (nTempValue == 1)
					{
						while (SendToSelfPort(pTempSendStr, lPos) != lPos);
						nTempLen111 = lPos;
						lPos = 0;
						// 虚拟启动
						if (g_clCommProtocol.GetEndCommFlag()  != CCommProtocol::CET_NOT)
						{
							g_stCurrentSendInfo.nCommType = CST_VERSION;
							clTempProtocol.StartupComm(g_stCurrentSendInfo);
						}
					}
					if (nTempValue == -1)
					{
						lPos = 0;
						// 虚拟启动
						g_stCurrentSendInfo.nCommType = CST_VERSION;
						clTempProtocol.StartupComm(g_stCurrentSendInfo);
					}

					if (nTempFlag20 == 1)
					{
						if (g_clCommProtocol.TransferAndAnalyse(nLen, pTempReceiveStr) == 1)
						{
							if (g_clCommProtocol.GetEndCommFlag()  == CCommProtocol::CET_NORMAL)
							{
								nTempFlag20 = 2;
							}
							else
							{
								if (g_clCommProtocol.GetEndCommFlag()  != CCommProtocol::CET_NOT)
								{				
									// clear tramismiting's buffer
									g_stCurrentSendInfo.nCommType = CRT_RENEW;
									g_clCommProtocol.StartupComm(g_stCurrentSendInfo);
	
									g_lSendSize = g_clCommProtocol.GetCommSendLength();
								}
							}
						}
					}
				}
				if (nTempFlag20 == 2)
				{
					g_clCommProtocol.GetRecCommInfo(&g_stCurrentRecieveInfo);
					break;
				}
			}

			if (g_stCurrentRecieveInfo.commData.renewData.cResultFlag)
			{
				DisplayUpateInfo(UDI_WRITE_FLASH, UDI_SUCCEED);
				TextOut(40, s_nDisplayPosY[UDI_OVER_SUCCEED], DONE_COLOR, 0, s_aacDisplayInfo[UDI_OVER_SUCCEED]);
			}
			else
			{
				DisplayUpateInfo(UDI_WRITE_FLASH, UDI_FAIL, TRUE);
				TextOut(40, s_nDisplayPosY[UDI_OVER_FAIL], DOING_COLOR, 0, s_aacDisplayInfo[UDI_OVER_FAIL]);
#ifdef BIOS_DEBUG
				while (1);
#endif
			}
			
			DisplayUpateInfo(UDI_SYS_RESTART, 0);
			CloseUpdatePort();

			for (i = 0; i < 10; i ++)
			{
				while (SendToSelfPort(pTempSendStr, nTempLen111) != nTempLen111);
				Sleep(400);	// delay 40 ms
			}

			// 升级失败时自动重启
			if (!g_stCurrentRecieveInfo.commData.renewData.cResultFlag)
				return FALSE;

			return TRUE;
		}
	}

	// start file's communication
	if (nCommSuccessFlag == 1)		// normally receiving 
	{
		StartupComm(CRT_START);

		while (1)
		{
			nLen = ReceiveFromSelfPort(pTempReceiveStr, MAX_RECIEVE_LENGTH);
			if (nLen > 0)
			{             
				if (m_bIsConnected == FALSE)
				{
					DisplayUpateInfo(UDI_CONNECTING, UDI_SUCCEED);
					DisplayUpateInfo(UDI_TRANSFER_DATA, 0);
					m_bIsConnected = TRUE;
				}
#ifdef BIOS_DEBUG
				g_nOneTransferTimeForTest3 = *(UINT16*)MCF5206::TCN1;
#endif
				// 桢结束,目前不处理错误数据,即TransferAndAnalyse返回-1的情况
				if (g_clCommProtocol.TransferAndAnalyse(nLen, pTempReceiveStr) == 1)
				{
					g_lSendSize = g_clCommProtocol.GetCommSendLength();
					if (g_lSendSize)	// 发送应答或其他数据
					{
						g_clCommProtocol.GetSendData(pTempSendStr);
						while (SendToSelfPort(pTempSendStr, g_lSendSize) != g_lSendSize);
					}

					nTempFlag = AnalyseCommResult();
#ifdef BIOS_DEBUG
			g_nOneTransferTimeForTest5 = *(UINT16*)MCF5206::TCN1;
			g_nOneTransferTimeForTest3 = *(UINT16*)MCF5206::TCN1;
#endif	
					if (nTempFlag == 1)
					{
						GetFilesNextComm();

						if (g_stCurrentSendInfo.nCommType == CT_NULL)
						{
							nCommSuccessFlag = 1;
							break;
						}
						else
						{
							StartupComm(g_stCurrentSendInfo.nCommType);
							continue;
						}
					}
					if (nTempFlag == -1)
					{
						nCommSuccessFlag = 0;
						break;
					}
					if (nTempFlag == 0)
						continue;
				}
			}
			else
			{
			}
		}
	}

	// 判断升级是否成功
	if (nCommSuccessFlag == 1)
	{
		DisplayUpateInfo(UDI_TRANSFER_DATA, UDI_SUCCEED);
		DisplayUpateInfo(UDI_WRITE_FLASH, 0);

		// 按索引更新每个文件
		g_lFileBufPos = 0;
		int j = 0;
		UINT32 lPos, lLen;
		UCHAR acMainSysIsValid[1];
		// 对于8000主控板D版本以前的主控板(都为2M),由于最后FLASH参数BLOCK没有区分,所以会影响系统程序
		// 6000主板则是2M的需要,4M的不需要
//		if (g_stSysFlag.stFSysFlag.ucFlashCircuitType == FLASH_CIRCUIT_VER_8000_D_BEFORE
//			|| (g_stSysFlag.stFSysFlag.ucFlashCircuitType == FLASH_CIRCUIT_VER_6000 && g_nFlashSizeType == FLASH_SIZE_2M))
//		{
//			acMainSysIsValid[0] = 0x00;
//			if (FALSE == WriteEeprom(EEPROM_SYSTEM_FLAG_POS, 1, acMainSysIsValid))
//			{
//				TextOut(40, s_nDisplayPosY[UDI_EEPROM_FAIL], DOING_COLOR, 0, s_aacDisplayInfo[UDI_EEPROM_FAIL]);
//				return FALSE;
//			}
//		}

		for (i = 0; i < g_stUpdateInfo.nFileCount; i ++)
		{
			lPos = g_stUpdateInfo.stUpdateItemTable[i].nPos;
			lLen = g_stUpdateInfo.stUpdateItemTable[i].nLength;
			// 对于D版本以前的主控板,由于最后FLASH参数BLOCK没有区分,所以会影响系统程序
//			if (g_stUpdateInfo.stUpdateItemTable[i].nType == SYS_FILE_TYPE)
//			{
//				// 系统首先无效,EEPROM

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -