📄 download.cpp
字号:
}
if(m_SerPort.Send2Back_tm("\r",1,1000*50)!=TRUE)
{
// MessageBox("Move block failed6","Error",MB_OK);
return FALSE;
}
Sleep(100);
Answer = 0;
if(m_SerPort.ReadData_tm(&Answer,1,1000*500)!=TRUE)
{
// MessageBox("Move block failed7","Error",MB_OK);
return FALSE;
}
if(Answer != 'O')
{
// MessageBox("Move block failed8","Error",MB_OK);
return FALSE;
}
trans_size += slave_blocks[index].len+BLOCK_BASIC_HEADER_SIZE;
progress = trans_size*100/slave_reorg_size;
if(progress > 100)
{
// MessageBox("progress info error9","Error",MB_OK);
progress = 0;
}
if(progress > prog)
{
prog = progress;
pPrgrss->SetPos(prog);
per.Format("%d",prog);
per += _T("%");
pWnd->SetWindowText(per);
}
return TRUE;
}
#ifdef TRANSFER_FORMAT2_SUPPORT
unsigned int SwapLong(int l)
{
char tmp[4];
tmp[0] = ((char *)&l)[3];
tmp[1] = ((char *)&l)[2];
tmp[2] = ((char *)&l)[1];
tmp[3] = ((char *)&l)[0];
return *((UINT32 *)tmp);
}
BOOL CDownLoad::Command_transfer(int host_block_index)
{
char *TransCommd = "transfer";
char Len[10];
unsigned int id,transLen,l;
char TmpBuff[50];
unsigned char Answer;
// unsigned int package_num ;
unsigned char *Buff, *buffer;
// unsigned count,p1,p2;
unsigned int crc1,crc2;
char CRCbuff[4];
int bRead = 0;
int len;
// int Step;
int progress;
int i;
int err_count;
CString per;
unsigned char head_buf[9] = {'H',0};//"H";
CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_SDRAM);
CWnd *pWnd = GetDlgItem(IDC_STATIC_SDRAM_PERCENT);
id = blocks[host_block_index].id;
transLen = blocks[host_block_index].len+BLOCK_BASIC_HEADER_SIZE;
//offset= blocks[host_block_index].offset;
sprintf(Len, "%u", transLen);
while(m_SerPort.ReadData_tm((unsigned char *)TmpBuff,50,50*1000));
if(m_SerPort.Send2Back_tm(TransCommd,strlen(TransCommd),1000*50)!=TRUE)
{
// MessageBox("Transfer block failed1","Error",MB_OK);
return FALSE;
}
Sleep(10);
if(m_SerPort.Send2Back_tm(" ",1,1000*50)!=TRUE)
{
// MessageBox("Transfer block failed2","Error",MB_OK);
return FALSE;
}
Sleep(10);
if(m_SerPort.Send2Back_tm(Len,strlen(Len),1000*50)!=TRUE)
{
// MessageBox("Transfer block failed3","Error",MB_OK);
return FALSE;
}
Sleep(10);
if(m_SerPort.Send2Back_tm("\r",1,1000*50)!=TRUE)
{
// MessageBox("Transfer block failed4","Error",MB_OK);
return FALSE;
}
Buff = (unsigned char *)malloc(transLen);
if(Buff == NULL)
{
MessageBox("Can not read file, not enough memory!","Error",MB_OK);
return FALSE;
}
/* open file */
if(!m_File.Open(m_Path,CFile::shareDenyRead,NULL))
{
MessageBox("Can not open file!","Error",MB_ICONERROR|MB_OK);
free(Buff);
return FALSE;
}
m_File.Seek(blocks[host_block_index].addr, CFile::begin);
memset(Buff,0,transLen);
if(transLen != m_File.Read(Buff,transLen))
{
MessageBox("Read file error!","Error",MB_ICONERROR|MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
// m_Page2.bDL = TRUE;
Sleep(100);
err_count = 0;
l = 0;
buffer = Buff;
UINT32 PacketNum = 0;
while(l<transLen)
{
len = (l+PACKAGESIZE) <= transLen? PACKAGESIZE : transLen-l;
crc1 = MG_Table_Driven_CRC(0xFFFFFFFF, (unsigned char *)buffer, len);
for(i=0; i<4; i++)
{
CRCbuff[i] = (crc1>>((3-i)*8))&0xFF;
head_buf[i+1] = (PacketNum>>((3-i)*8))&0xFF;
}
crc2 = MG_Table_Driven_CRC(0xFFFFFFFF, &head_buf[1] , 4);
for(i=0; i<4; i++)
head_buf[i+5] = (crc2>>((3-i)*8))&0xFF;
while(1)
{
/* wait line slient */
while(m_SerPort.ReadData_tm((unsigned char *)TmpBuff,50,150) != 0);
/* send packet header */
if(m_SerPort.SendData((const char *)head_buf,9) != 9)
{
MessageBox("Send Packet Header Failed!\n");
free(Buff);
m_File.Close();
return FALSE;
}
Answer = 0;
m_SerPort.ReadData_tm(&Answer,1,100*1000);
if(Answer != 'C')
continue;
/* send one packet */
if(len != m_SerPort.SendData((char *)buffer, len))
{
free(Buff);
m_File.Close();
return FALSE;
}
/* send 4 byte CRC*/
if(4 != m_SerPort.SendData(CRCbuff, 4))
{
free(Buff);
m_File.Close();
return FALSE;
}
Sleep(20);
Answer = 0;
m_SerPort.ReadData_tm(&Answer,1,100*1000);
if(Answer == 'O')
{
if(!m_SerPort.ReadData_tm((unsigned char *)TmpBuff,8,1500*1000))
continue;
if(MG_Table_Driven_CRC(0xFFFFFFFF, (unsigned char *)TmpBuff, 4) != SwapLong((UINT32)(*(UINT32 *)(TmpBuff+4))))
continue;
if(SwapLong((*(UINT32 *)TmpBuff)) != PacketNum)
continue;
PacketNum++;
l += len;
buffer += len;
trans_size += len;
progress = trans_size*100/slave_reorg_size;
if(progress > 100)
{
// MessageBox("progress info error","Error",MB_OK);
progress = 0;
}
if(progress > prog)
{
prog = progress;
pPrgrss->SetPos(prog);
per.Format("%d",prog);
per += _T("%");
pWnd->SetWindowText(per);
}
break;
}
}
}
free(Buff);
m_File.Close();
return TRUE;
}
#else
BOOL CDownLoad::Command_transfer(int host_block_index)
{
char *TransCommd = "transfer";
char Len[10];
unsigned int id,transLen,l;
char TmpBuff[50];
unsigned char Answer;
// unsigned int package_num ;
unsigned char *Buff, *buffer;
// unsigned count,p1,p2;
unsigned int crc;
char CRCbuff[4];
int bRead = 0;
int len;
// int Step;
int progress;
int i;
int err_count;
CString per;
CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_SDRAM);
CWnd *pWnd = GetDlgItem(IDC_STATIC_SDRAM_PERCENT);
id = blocks[host_block_index].id;
transLen = blocks[host_block_index].len+BLOCK_BASIC_HEADER_SIZE;
bool bOverWriteUsrdbHeader = false;
if( id == DEFAULT_DATABASE_ID )//for default db
{
for(int nUdbBlock = 1; nUdbBlock < block_number; nUdbBlock++)
{
if( blocks[nUdbBlock].id == USER_DATABASE_ID )
break;
}
ASSERT( nUdbBlock != block_number );
if( blocks[nUdbBlock].upg_flag != 1 )//if not update user database
{
if( transLen >= blocks[host_block_index].offset - 64 * 1024 )//if last sector need burned
{
//read user block header
bOverWriteUsrdbHeader = true;
transLen = blocks[host_block_index].offset + 0x80;//include userdatabase block header
}
}
}
//offset= blocks[host_block_index].offset;
sprintf(Len, "%u", transLen);
/* make read buffer empty */
while(m_SerPort.ReadData(TmpBuff,50) != 0);
Sleep(20);
if(m_SerPort.Send2Back_tm(TransCommd,strlen(TransCommd),1000*50)!=TRUE)
{
// MessageBox("Transfer block failed1","Error",MB_OK);
return FALSE;
}
Sleep(20);
if(m_SerPort.Send2Back_tm(" ",1,1000*50)!=TRUE)
{
// MessageBox("Transfer block failed2","Error",MB_OK);
return FALSE;
}
Sleep(20);
if(m_SerPort.Send2Back_tm(Len,strlen(Len),1000*50)!=TRUE)
{
// MessageBox("Transfer block failed3","Error",MB_OK);
return FALSE;
}
Sleep(20);
if(m_SerPort.Send2Back_tm("\r",1,1000*50)!=TRUE)
{
// MessageBox("Transfer block failed4","Error",MB_OK);
return FALSE;
}
Sleep(20);
Answer = 0;
if(m_SerPort.ReadData_tm(&Answer,1,1000*500)!=TRUE)
{
// MessageBox("Transfer block failed5","Error",MB_OK);
return FALSE;
}
if(Answer != 'O')
{
// MessageBox("Transfer block failed6","Error",MB_OK);
return FALSE;
}
Buff = (unsigned char *)malloc(transLen + 1);
if(Buff == NULL)
{
MessageBox("Can not read file, not enough memory!","Error",MB_OK);
return FALSE;
}
/* open file */
if(!m_File.Open(m_Path,CFile::shareDenyRead,NULL))
{
MessageBox("Can not open file!","Error",MB_ICONERROR|MB_OK);
free(Buff);
return FALSE;
}
m_File.Seek(blocks[host_block_index].addr, CFile::begin);
memset(Buff,0,transLen);
if(transLen != m_File.ReadHuge(Buff,transLen))
{
MessageBox("Read file error!","Error",MB_ICONERROR|MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
if( bOverWriteUsrdbHeader )
{
memcpy(Buff+blocks[host_block_index].offset, m_usr_block_header, BLOCK_HEADER_SIZE);
}
unsigned char* pBufSb1Start = Buff + 0x80;
unsigned char* pBufSb2Start = pBufSb1Start + 0xD4D0;
// m_Page2.bDL = TRUE;
Sleep(100);
err_count = 0;
l = 0;
buffer = Buff;
while(l<transLen && err_count<TRANSFER_RETRY_TIMES)
{
len = (l+PACKAGESIZE) <= transLen? PACKAGESIZE : transLen-l;
crc = MG_Table_Driven_CRC(0xFFFFFFFF, (unsigned char *)buffer, len);
for(i=0; i<4; i++)
{
CRCbuff[i] = (crc>>((3-i)*8))&0xFF;
}
/* send one package*/
if(len != m_SerPort.SendData((char *)buffer, len))
{
// MessageBox("Data download error!7","Error",MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
Sleep(5);
/* send 4 byte CRC*/
if(4 != m_SerPort.SendData(CRCbuff, 4))
{
// MessageBox("Data download error!8","Error",MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
Sleep(5);
Answer = 0;
if(m_SerPort.ReadData_tm(&Answer,1,1000*500)!=TRUE)
{
// MessageBox("Waiting for STB reply overtime","Error",MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
if(Answer == 'O')
{
l += len;
buffer += len;
err_count = 0;
trans_size += len;
progress = trans_size*100/slave_reorg_size;
if(progress > 100)
{
// MessageBox("progress info error","Error",MB_OK);
progress = 0;
}
if(progress > prog)
{
prog = progress;
pPrgrss->SetPos(prog);
per.Format("%d",prog);
per += _T("%");
pWnd->SetWindowText(per);
}
}
else if(Answer == 'E')
{
err_count++;
continue;
}
else
{
// MessageBox("STB reply invalid1","Error",MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
// Sleep(50);
}
if(err_count >= TRANSFER_RETRY_TIMES)
{
// MessageBox("STB reply invalid2","Error",MB_OK);
free(Buff);
m_File.Close();
return FALSE;
}
// m_Page2.bDL = FALSE;
/* show progress*/
free(Buff);
m_File.Close();
pPrgrss->SetPos(100);
per.Format("%d",100);
per += _T("%");
pWnd->SetWindowText(per);
return TRUE;
}
#endif
BOOL CDownLoad::Command_burn()
{
char *BurnCommd = "burn\r";
unsigned char TmpBuff[50];
unsigned char Answer;
CString per;
CProgressCtrl *pPrgrss = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_FLASH);
CWnd *pWnd = GetDlgItem(IDC_STATIC_FLASH_PERCENT);
/* make read buffer empty */
while(m_SerPort.ReadData(TmpBuff,50) != 0);
if(m_SerPort.Send2Back_tm(BurnCommd,strlen(BurnCommd),1000*50)!=TRUE)
{
//MessageBox("Send burn Flash command failed!","Error",MB_OK);
return FALSE;
}
Answer = 0;
if(m_SerPort.ReadData_tm(&Answer,1,1000*500)!=TRUE)
{
//MessageBox("Burn Flash failed!","Error",MB_OK);
return FALSE;
}
if(Answer != 'O')
{
//MessageBox("Burn Flash failed!","Error",MB_OK);
return FALSE;
}
Sleep(100);
/* read burn progress */
int Wait = 0;
int Finished = 0;
int len = blocks[m_Type].len;
int Progress;
int burn_len;
while(1)
{
memset(TmpBuff, 0, 50);
if(m_SerPort.ReadData_tm(TmpBuff, 1, 1000*2000)!=TRUE)
return FALSE;
if(TmpBuff[0] == 'B')//Burning
{
if(m_SerPort.ReadData_tm(TmpBuff, 6, 1000*100)!=TRUE)
return FALSE;
TmpBuff[4]='\0';
burn_len = (atoi((char *)TmpBuff)*1024);
}
else if(TmpBuff[0] == 'F')//Finished
{
pPrgrss->SetPos(100);
per.Format("%d",100);
per += _T("%");
pWnd->SetWindowText(per);
break;
}
else if(TmpBuff[0] == 'E')//Error
{
if(m_SerPort.ReadData_tm(TmpBuff,8,1000*100)!=TRUE)
return FALSE;
TmpBuff[7]='\0';
per.Format("Burn Flash error, error address:%s",TmpBuff+1);
MessageBox(per,"Error",MB_OK);
return FALSE;
}
else
{
// MessageBox("slave info error","Error",MB_OK);
}
Progress = burn_len*100 / slave_burn_size;
if(Progress > 100)
{
// MessageBox("progress info error","Error",MB_OK);
Progress = 0;
}
pPrgrss->SetPos(Progress);
per.Format("%d",Progress);
per += _T("%");
pWnd->SetWindowText(per);
}
return TRUE;
}
BOOL CDownLoad::Command_reboot()
{
char *RebootCommd = "reboot\r";
int TestLen = strlen(RebootCommd);
if(m_SerPort.IsOpened == FALSE)
return FALSE;
char TmpBuff[50];
/* make read buffer empty */
while(m_SerPort.ReadData(TmpBuff, 50) != 0);
Sleep(100);
/* send test info*/
if(!m_SerPort.Send2Back_tm(RebootCommd,TestLen,1000*100))
return FALSE;
m_SerPort.Close();
return TRUE;
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
BOOL CDownLoad::CheckSerialPort()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -