?? burnflash.cpp
字號:
// NAME: BurnFlash.cpp
//--------------------------------------------------------------------------------------------------
//
// GENERAL DESCRIPTION
//
//--------------------------------------------------------------------------------------------------
//
// Revision History:
// Modification Tracking
// Author Date Number Description of Changes
// ------------------------- ------------ ---------- ----------------------------------------
//
//
// Portability:
// This module can be portable to other C++ compilers or Win32 platforms.
//
/*
when who what, where, why
-------- -------- ---------------------------------------------------------------
01/27/05 nony.wu Change to communicate with dlram to support higher UART
01/20/05 nony.wu fixed bugs to support baudrate change
============================================================================ */
//--------------------------------------------------------------------------------------------------
// INCLUDE FILES
//--------------------------------------------------------------------------------------------------
#include "stdafx.h"
using namespace std;
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "common.h"
#include "UART.h"
#include "BurnFlash.h"
#include "ProcDiag.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//--------------------------------------------------------------------------------------------------
// CONSTANT
//--------------------------------------------------------------------------------------------------
#define RAM_BASE 0x1000000
//--------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------------------------------------
CBurnFlash theFlash; //the unique instance for the application
//--------------------------------------------------------------------------------------------------
// CLASS IMPLEMENTATION
//--------------------------------------------------------------------------------------------------
//
// Construction/Destruction
//--------------------------------------------------------------------------------------------------
CBurnFlash::CBurnFlash()
{
m_pDiag = new CProcDiag();
m_packet_snd = new pkt_buffer_type;
if(m_packet_snd == NULL)
{
cout << "memory new error." << endl;
exit(1);
}
}
CBurnFlash::~CBurnFlash()
{
delete m_packet_snd;
m_packet_snd = NULL;
delete m_pDiag;
m_pDiag = NULL;
}
//--------------------------------------------------------------------------------------------------
//External Member Fucntions
//--------------------------------------------------------------------------------------------------
//
// FUNCTION: CBurnFlash::LoadData
//
// DESCRIPTION:
// This function writes the phone S/W to the specific Flash address.
//
// ARGUMENTS PASSED:
// strFile the name of the file in which contains data to be written;
// nDstAddr the original address from which to write the data;
// nSpaceSize the space size allowed to be written;
// bCode the flag indicating which Flash that the data to be written to.
// If true, the CodeFlash is to be written, else
// the DataFlash is to be written.
//
// RETURN VALUE:
// SUCCESS
// ERR_NO_VALID_RAM_FILE
// ERR_NO_VALID_TARGET_FILE
// ERR_SND_DLOAD_CMD
// ERR_COMM_FAIL
// ERR_SND_RAM_FILE
// ERR_ERASE_FLASH_FAIL
// PRE-CONDITIONS:
// (1) The phone must work on a normal state.
// (2) The phone and the PC connect with a cable through COM1/COM2
//
// POST-CONDITIONS:
// The new phone S/W are written to the Flash and the phone will restarted
// automatically.
//
// IMPORTANT NOTES:
// None
//
//--------------------------------------------------------------------------------------------------
int
CBurnFlash::LoadData(char* strFile, DWORD dwDstAddr,DWORD dwSpaceSize, BOOL bCode, BOOL bInitial)
{
BYTE buf[MAX_PACKET_LEN];
DWORD dwMaxReadSize = 5*1024;
DWORD dwFileLen;
int hFile;
hFile = _open(strFile, _O_BINARY | _O_RDONLY,_S_IREAD);
dwFileLen = _filelength(hFile);
if( hFile == -1 || dwFileLen == 0)
return ERR_NO_VALID_TARGET_FILE;
if(bInitial)
{
if(!RAMFileExist())
return ERR_NO_VALID_RAM_FILE;
BOOL diag_download;
/* Send Hello to see if it is in diag download mode */
if(m_pDiag->SayHello()==SUCCESS)
{
diag_download = TRUE;
}
else{
diag_download = FALSE;
}
printf("diag_download =%d ", diag_download);
if(diag_download){
cout << TAB_SUB_OPR << "Change the phone state to offline." << endl;
if(m_pDiag->SendDloadCmd()!= SUCCESS)
return ERR_SND_DLOAD_CMD;
}
//////cout << TAB_SUB_OPR << "Change the phone state to offline." << endl;
///////if(m_pDiag->SendDloadCmd()!= SUCCESS)
/////// return ERR_SND_DLOAD_CMD;
/* cout << TAB_SUB_OPR << "Change the UART BaudRate to 115200." << endl;
if(!theComm.SetBaudRateTo115200())*/
//wait the phone ready to download
Sleep(1000);
cout << TAB_SUB_OPR << "Load the ram downloader." << endl;
if(LoadRAMFile("dlram.bin") != SUCCESS)
return ERR_SND_RAM_FILE;
}
baud_rate_type bt;
extern baudrate;
switch(baudrate)
{
case 0:
bt=UART_576K_BPS;
cout << TAB_SUB_OPR << "Change the UART BaudRate to 57600." << endl;
break;
case 1:
bt=UART_1152K_BPS;
cout << TAB_SUB_OPR << "Change the UART BaudRate to 115200." << endl;
break;
case 2:
bt=UART_2304K_BPS;
cout << TAB_SUB_OPR << "Change the UART BaudRate to 230400." << endl;
break;
case 4:
bt=UART_4608K_BPS;
cout << TAB_SUB_OPR << "Change the UART BaudRate to 460800." << endl;
break;
default:
bt=UART_1152K_BPS;
cout << TAB_SUB_OPR << "Change the UART BaudRate to 115200." << endl;
}
if(bt!=UART_1152K_BPS && bt!=UART_576K_BPS)
{
FormChangeBaudRateCMD(bt);
if( !SendAndWaitRsp())
{
TRACE0("\r\nsend Change Baud Rate Cmd fail.\r\n");
_close(hFile);
return ERR_COMM_FAIL;
}
if(!theComm.SetBaudRate(bt))
return ERR_COMM_FAIL;
}
cout << TAB_SUB_OPR << "Erase the Flash......"<< endl;
FormEraseFlashCMD(dwDstAddr, dwSpaceSize, bCode);
if( !SendAndWaitRsp())
return ERR_ERASE_FLASH_FAIL;
cout << TAB_SUB_OPR << "Erase successfully"<< endl;
cout << TAB_SUB_OPR << "Begin to download"<< endl;
cout << TAB_SUB_OPR;
DWORD dwRead = 0;
do{
cout << "*";
dwRead = _read(hFile, buf, dwMaxReadSize);
if(dwRead == 0)
break;
FormWriteFlashCMD(dwDstAddr, buf, dwRead, bCode);
if( !SendAndWaitRsp())
{
cout << endl;
return ERR_WRITE_FLASH_FAIL;
}
dwDstAddr += dwRead;
}while(dwRead != 0);
cout << endl;
cout << TAB_SUB_OPR << "Download Successfully"<< endl;
return SUCCESS;
}
//--------------------------------------------------------------------------------------------------
//
// FUNCTION: CBurnFlash::EraseFlash
//
// DESCRIPTION:
// This function erases the spaces of the DataFlash or CodeFlash from the specified address.
//
// ARGUMENTS PASSED:
// nDstAddr the original address to be erased
// nSpaceSize the size to be erased from the specified original address
// bCode the flag indicating which Flash to be erased. If true, the CodeFlash is erased,
// else the DataFlash is erased.
//
// RETURN VALUE:
// SUCCESS
// ERR_NO_VALID_RAM_FILE
// ERR_COMM_FAIL
// ERR_SND_RAM_FILE
// ERR_ERASE_FLASH_FAIL
//
// PRE-CONDITIONS:
// (1) The phone must work on a normal state.
// (2) The phone and the PC connect with a cable through COM1/COM2
//
// POST-CONDITIONS:
// The relevant area in the phone Flash memory is filled with 0xFF and the phone will restart automatically.
//
// IMPORTANT NOTES:
// None
//
//--------------------------------------------------------------------------------------------------
int
CBurnFlash::EraseFlash(DWORD dwDstAddr, DWORD dwSpaceSize, BOOL bCode, BOOL bInitial)
{
if (bInitial)
{
if(!RAMFileExist())
return ERR_NO_VALID_RAM_FILE;
cout << TAB_SUB_OPR << "send Dload Cmd to phone." << endl;
if(m_pDiag->SendDloadCmd() != SUCCESS)
return ERR_SND_DLOAD_CMD;
cout << TAB_SUB_OPR << "change PC BaudRate to 115200." << endl;
if(!theComm.SetBaudRateTo115200())
return ERR_COMM_FAIL;
//wait the phone ready to download
Sleep(1000);
cout << TAB_SUB_OPR << "prepare for download." << endl;
if(LoadRAMFile("dlram.bin") != SUCCESS)
return ERR_SND_RAM_FILE;
}
cout << TAB_SUB_OPR << "start to erase......"<< endl;
FormEraseFlashCMD(dwDstAddr, dwSpaceSize, bCode);
if( !SendAndWaitRsp())
return ERR_ERASE_FLASH_FAIL;
return SUCCESS;
}
//--------------------------------------------------------------------------------------------------
//
// FUNCTION: CBurnFlash::ResetPhone
//
// DESCRIPTION:
// This function sends a Reset CMD to phone and restores the PC serial
// port BaudRate to 38400.
//
// ARGUMENTS PASSED:
// None
//
// RETURN VALUE:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -