📄 flashhelper.cpp
字号:
}
break;
case rtLINEAR_ADDRESS:
if (pFileItem->eType == ftHex80)
{
// File must be in Intel80 hex format (not extended format).
return feFLASHFILE_EXPECTED_INTEL80_ERROR;
}
if (2 != cLineDataSize)
{
eRet = feFLASHFILE_FORMAT_ERROR;
}
else
{
// Tell the embedded side that this data is 24bit addressed
pFileItem->cFlashFlags |= LINEAR_ADDRESS_MODE;
*pcFlashFlags = pFileItem->cFlashFlags;
// First byte is high byte of segment.
wSegment = (cLineDataArray[0] << 8) | cLineDataArray[1];
bLinearAddress = TRUE;
bAddressSet = FALSE;
}
break;
case rtSTART_ADDRESS:
if (4 != cLineDataSize)
{
eRet = feFLASHFILE_FORMAT_ERROR;
}
// Setup IP portion of start address
*((WORD*)(pcBuffer + 1)) = (cLineDataArray[2] << 8) | cLineDataArray[3];
// Setup CS portion of start address
*((WORD*)(pcBuffer + 3)) = (cLineDataArray[0] << 8) | cLineDataArray[1];
break;
default:
TRACE("Unknown record type %d in HEX file\n", eRecordType);
}
}
if (feOK == eRet)
{
if (pdwCurrentSize)
{
// TRACE("Size= %d\n", *pdwCurrentSize);
}
//--------------------------------------------------------------------
// If the source is not an Intel Hex 80 format, then we need to setup
// a header with zero for the block size as an end of file marker,
// since we will be using the X-Modem protocol to download the data
// and we don't want to parse any invalid data near the end of the
// last X-Modem block.
//--------------------------------------------------------------------
if (pFileItem->eType == ftHexEx)
{
*((WORD*)pcDest) = 0; // use zero for destination offset
*((WORD*)(pcDest + 2)) = 0; // use zero for destination segment
pcDest += 4;
*((DWORD*)(pcDest)) = 0; // use zero for block size (EOF marker)
pcDest += 4;
}
*ppBinary = pcBuffer; // set returned start address
*pnSize = pcDest - pcBuffer; // set returned size
}
else
{
// An error occured. If we created a buffer for the binary data, delete
// it here.
if (pcBuffer)
{
delete pcBuffer;
pcBuffer = NULL;
}
}
return eRet; // return status
}
//----------------------------------------------------------------------------
// Parse one line of Intel Hex ASCII data and convert to binary, extracting
// the record type.
//
// Params:
// ppSourceText : Pointer to pointer to source data. On return, points to
// start of next source line.
// pnTextSize : Pointer to source text size. On return, size is decremented
// by size of text parsed.
// pwOffset : Pointer to returned offset stored in record.
// peCode : Pointer to returned record type.
// pDataArray : Pointer to returned binary data.
// pcDataSize : Pointer to size of binary bytes returned.
//
// Return: TRUE= Successfully parsed line, FALSE= error in text line.
//
// Notes:
//----------------------------------------------------------------------------
BOOL CFlashHelper::ParseIntelExtendedHexLine(char **ppSourceText, int *pnTextSize,
WORD *pwOffset, eRECORDTYPE *peCode,
BYTE *pDataArray, BYTE *pcDataSize)
{
//--------------------------------------------------------------------
// Determine line string length.
//--------------------------------------------------------------------
char *pText = *ppSourceText;
char *pNextLine = *ppSourceText;
while (0x0d < *pNextLine++ && (*pnTextSize)--); // look for CR (end of line)
// If any source data remains, check for LF characters.
if (*pnTextSize)
{
if (0x0a == *pNextLine)
{
pNextLine++; // LF found. Bump source text pointer.
(*pnTextSize)--; // decrement source text size parsed.
}
}
// Setup returned pointer to start of next line.
*ppSourceText = pNextLine;
// Check if text line length is valid.
int nLineLength = pNextLine - pText;
if (nLineLength < 11)
{
return FALSE;
}
// Check if first character is a colon.
if (':' != *pText++)
{
return FALSE;
}
// Get binary data length.
if (FALSE == AsciiHexToByte(pText, pcDataSize))
{
return FALSE;
}
pText += 2;
// Check if length given is valid.
if (nLineLength < (11 + (*pcDataSize * 2)))
{
return FALSE;
}
// Get offset.
if (FALSE == AsciiHexToWord(pText, pwOffset))
{
return FALSE;
}
pText += 4;
// Get record type.
*peCode = (eRECORDTYPE)0; // clear upper bits of enum value
if (FALSE == AsciiHexToByte(pText, (BYTE*)peCode))
{
return FALSE;
}
pText += 2;
// Get checksum of binary data size, offset and record type.
int nSum = (*pcDataSize) + ((*pwOffset >> 8) & 0xff) + (*pwOffset & 0xff) +
(*peCode);
// Add binary data to checksum.
for (int i = 0; i < *pcDataSize; i++)
{
if (FALSE == AsciiHexToByte(pText, &pDataArray[i]))
{
return FALSE;
}
pText += 2;
nSum += pDataArray[i];
}
// Get stored checksum value.
BYTE cChecksum;
if (FALSE == AsciiHexToByte(pText, &cChecksum))
{
return FALSE;
}
// Check if stored checksum plus calculated checksum equal zero.
if ((nSum + cChecksum) & 0xff)
{
return FALSE; // Checksum doesn't equal zero
}
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CFlashHelper::AsciiHexToByte(char *pText, BYTE *pcRet)
{
BYTE cData;
if (*pText >= '0' && *pText <= '9')
{
cData = (*pText - '0') << 4;
}
else
{
if (*pText >= 'A' && *pText <= 'F')
{
cData = (*pText - 'A' + 10) << 4;
}
else
{
return FALSE;
}
}
pText++;
if (*pText >= '0' && *pText <= '9')
{
cData |= *pText - '0';
}
else
{
if (*pText >= 'A' && *pText <= 'F')
{
cData |= *pText - 'A' + 10;
}
else
{
return FALSE;
}
}
*pcRet = cData;
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CFlashHelper::AsciiHexToWord(char *pText, WORD *pwRet)
{
BYTE cLow;
BYTE cHigh;
if (FALSE == AsciiHexToByte(pText, &cHigh))
{
return FALSE;
}
if (FALSE == AsciiHexToByte(pText + 2, &cLow))
{
return FALSE;
}
*pwRet = cLow | (cHigh << 8);
return TRUE;
}
//----------------------------------------------------------------------------
// Static thread procedure used to download binary data to target system using
// a serial port.
//
// Params:
// pParam : Pointer to instance of this object that created the new thread.
//
// Return: Thread exit code.
//
// Notes:
//----------------------------------------------------------------------------
DWORD WINAPI CFlashHelper::ThreadProc(LPVOID pParam)
{
ASSERT(pParam); // ensure that pointer to this object's instance exist
CFlashHelper *pThis = (CFlashHelper*)pParam;
pThis->DownloadFlashCode(); // download flash code
pThis->m_hThread = 0; // Thread is exiting. Clear handle value.
return 0; // exit code not currently used
}
//----------------------------------------------------------------------------
// Download binary data to target system.
//
// Params: none
//
// Return: TRUE= Binary data successfully downloaded to target.
//
// Notes:
//----------------------------------------------------------------------------
BOOL CFlashHelper::DownloadFlashCode()
{
eSENDCMDRETVAL eRet = scOK;
int nBytes = 0;
//------------------------------------------------------------------------
// Initiate transfer. Wait for CHECKSUM_ID_CHAR to be received on serial
// port, but only if we're not using USB...
//------------------------------------------------------------------------
BOOL bWait = TRUE;
while (bWait && (m_pComPortCmd->m_eComm != ccUSB))
{
CByteArray byteArray;
// Get data received from COM port.
eRet = m_pComPortCmd->GetData(byteArray, m_hCancelEvent, 1, INFINITE);
nBytes++;
if (scOK == eRet)
{
BYTE cData = byteArray.GetAt(0); // get first character received
if (CHECKSUM_ID_CHAR == cData) // Check for start character.
{
m_nBlockSize = 128;
TRACE("Got start character (128 byte blocks)\n");
bWait = FALSE; // Got start character, break loop.
}
else if (CHECKSUM_ID_CHAR_1K == cData) // Check for start character.
{
m_nBlockSize = 1024;
TRACE("Got start character (1024 byte blocks)\n");
bWait = FALSE; // Got start character, break loop.
}
}
else if (scCANCEL == eRet)
{
return FALSE; // Received cancel. Abort download.
}
}
if (m_pComPortCmd->m_eComm == ccUSB)
{
m_pComPortCmd->SendData(SOH_CHAR);
Sleep(100);
m_pComPortCmd->SendData(SOH_CHAR);
Sleep(100);
m_pComPortCmd->SendData(SOH_CHAR);
Sleep(100);
m_pComPortCmd->SendData(SOH_CHAR);
Sleep(100);
m_pComPortCmd->SendData(SOH_CHAR);
Sleep(100);
m_pComPortCmd->SendData(CHECKSUM_ID_CHAR_1K);
Sleep(100);
if (!g_bHaveBlockSize)
{
CByteArray byteArray;
eRet = m_pComPortCmd->GetData(byteArray, m_hCancelEvent, 1, 0);
if (byteArray.GetSize() != 0)
{
m_nBlockSize = byteArray.GetAt(0);
g_bHaveBlockSize = TRUE;
}
else
{
return FALSE;
}
}
}
// Download data using X-Modem protocol to target.
eRet = DownloadDataXModem(m_pFlashData, m_nFlashDataSize, m_strReturnedMsg);
if (m_bCanceled)
{
// Operation canceled. Attempt to send a CAN character to target.
m_pComPortCmd->SendData(CAN_CHAR);
return FALSE;
}
switch (eRet)
{
case scOK:
//----------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -