📄 flashwritedlg.cpp
字号:
// FlashWriteDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FlashWrite.h"
#include "FlashWriteDlg.h"
#include "TestSelectDlg.h"
#include <afxdlgs.h>
#include "S29GL256N.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFlashWriteDlg dialog
CFlashWriteDlg::CFlashWriteDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFlashWriteDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFlashWriteDlg)
m_strBurnOperates = _T("编程进度");
m_WriteBinToFlash = 1;
m_FileDirectory = _T("");
m_FlashStartOffset = _T("");
m_csFlashProgSize = _T("");
BurnStatus = -1;
bFlashInitialized = FALSE;
m_FlashStartOffset = _T("0");
m_csFlashProgSize = _T("100000");;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFlashWriteDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFlashWriteDlg)
DDX_Control(pDX, IDC_BURN_PROGRESS, m_CtlBurnProgress);
DDX_Control(pDX, IDC_FLASH_TYPE, m_FlashType);
DDX_Control(pDX, IDC_CPU_TYPE, m_CPUType);
DDX_Radio(pDX, IDC_WRITE, m_WriteBinToFlash);
DDX_Text(pDX, IDC_FILE_DIRECTORY, m_FileDirectory);
DDX_Text(pDX, IDC_FLASHOFFSET, m_FlashStartOffset);
DDX_Text(pDX, IDC_FLASH_SIZE, m_csFlashProgSize);
DDX_Text(pDX, IDC_BURN_OPERATES, m_strBurnOperates);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFlashWriteDlg, CDialog)
//{{AFX_MSG_MAP(CFlashWriteDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_TESTDLG, OnTestdlg)
ON_BN_CLICKED(ID_START_OPERATE, OnStartOperate)
ON_BN_CLICKED(IDC_BTN_FILESELECT, OnBtnFileselect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFlashWriteDlg message handlers
BOOL CFlashWriteDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_FlashType.AddString("S29GL256N");
m_FlashType.AddString("SST39VF160");
m_CPUType.AddString("LFXP2-5E-PQFP208");
m_CPUType.AddString("MPC8347NE");
return TRUE; // return TRUE unless you set the focus to a control
}
void CFlashWriteDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFlashWriteDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFlashWriteDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFlashWriteDlg::OnTestdlg()
{
// TODO: Add your control notification handler code here
CTestSelectDlg Dialog;
if(Dialog.DoModal()!=IDOK)
return;
}
void CFlashWriteDlg::OnStartOperate()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!bFlashInitialized)
{
if(ERROR == S29GL128MInitialization())
{
//AfxMessageBox("JTAG& Flash Initialized failed!");
return;
}
}
if( (0 != m_WriteBinToFlash) && (1 != m_WriteBinToFlash))
{
AfxMessageBox("Some Necessary Filed shold be filled");
return;
}
if(m_FileDirectory.IsEmpty())
{
AfxMessageBox("Some Necessary Filed shold be filled");
return;
}
iFlashStartOffset = CString2IntValidate(m_FlashStartOffset);
if(!iFlashStartOffset)
{
AfxMessageBox("Some Necessary Filed shold be filled");
return;
}
iFlashBurnSize = CString2IntValidate(m_csFlashProgSize);
if(!iFlashBurnSize)
{
AfxMessageBox("Some Necessary Filed shold be filled");
return;
}
int iFlashType = m_FlashType.GetCurSel();
if(-1 == iFlashType)
{
AfxMessageBox("Some Necessary Filed shold be filled or validated");
return;
}
int iCPUType = m_CPUType.GetCurSel();
if(-1 == iCPUType)
{
AfxMessageBox("Some Necessary Filed shold be filled");
return;
}
ReallyExcuteBurnRead();
}
void CFlashWriteDlg::ReallyExcuteBurnRead()
{
FILE *pgmfile;
if( m_WriteBinToFlash )
pgmfile = fopen(m_FileDirectory, "wb");
else
pgmfile = fopen(m_FileDirectory, "rb");
if( m_WriteBinToFlash )
{
if(ERROR == EraseFlash(iFlashStartOffset, iFlashBurnSize))
{
fclose(pgmfile);
AfxMessageBox("Erase the FLASH error!");
// Log to the log files
return;
}
if(ERROR == WriteFlash(pgmfile, iFlashStartOffset, iFlashBurnSize))
{
fclose(pgmfile);
AfxMessageBox("Write the FLASH error!");
// Log to the log files
return;
}
if(ERROR ==CheckFlash(iFlashStartOffset, pgmfile, iFlashBurnSize))
{
fclose(pgmfile);
AfxMessageBox("Check the FLASH error!");
// Log to the log files
return;
}
}
else
{
ReadFlash(pgmfile, iFlashStartOffset, iFlashBurnSize);
}
fclose(pgmfile);
}
int CFlashWriteDlg::CString2IntValidate(CString OffsetStart)
{
int iLength = OffsetStart.GetLength();
int returnValue = 0;
if(iLength>8)
{
return returnValue;
}
OffsetStart.MakeLower();
for(int ii=iLength; ii>0; ii--)
{
int tempValue = 0;
if((47<OffsetStart[ii-1])&&(OffsetStart[ii-1]<58))
{
tempValue = OffsetStart[ii-1] - 48;
}
else if((96<OffsetStart[ii-1])&&(OffsetStart[ii-1]<1038))
{
tempValue = OffsetStart[ii-1] - 97 + 10;
}
else
{
returnValue = 0;
break;
}
returnValue |= tempValue<<((iLength-ii)*4);
}
//不允许写1M后面内容
if(returnValue>0x100000)
{
returnValue = 0;
}
return returnValue;
}
void CFlashWriteDlg::OnBtnFileselect()
{
UpdateData(TRUE);
// TODO: Add your control notification handler code here
BOOL bOpen = TRUE;
if(1 == m_WriteBinToFlash)
{
bOpen = FALSE;
}
CFileDialog FileSelectDlg(bOpen);
FileSelectDlg.DoModal();
m_FileDirectory = FileSelectDlg.GetPathName();
UpdateData(FALSE);
}
void CFlashWriteDlg::TestTempFuncs()
{
// TODO: Add extra validation here
for(int i=0; i<100000001; i++)
{
if(!(i%1000000))
{
int percent = i/1000000;
int bStatus = 0;
if(percent<33)
{
bStatus = 0;
}
else if(percent>66)
{
bStatus = 2;
}
else
{
bStatus = 1;
}
UpdateProgress(bStatus, percent);
}
}
}
void CFlashWriteDlg::UpdateProgress(int iWrite, int percent)
{
if(iWrite != BurnStatus)
{
switch(iWrite)
{
case 0: m_strBurnOperates = "FLASH擦除"; break;
case 1: m_strBurnOperates = "FLASH编程"; break;
case 2: m_strBurnOperates = "FLASH检测"; break;
case 3: m_strBurnOperates = "FLASH读取"; break;
}
UpdateData(FALSE);
}
m_CtlBurnProgress.SetPos(percent);
}
int CFlashWriteDlg::ReadFlash(FILE *stream, int Src, int size)
{
int SrcReadAddress = Src;
int TotalLen = 0;
int RemainLen = TotalLen = size/sizeof(WORD);
WORD buffer[512];
int ReallyReadLen = 0;
int ReallyReadByte = 0;
if( NULL == stream )
return -1;
fseek( stream,0,SEEK_SET );
while(RemainLen)
{
ReallyReadLen = (512>RemainLen)? RemainLen:512;
S29GL128MRead(SrcReadAddress, buffer, ReallyReadLen);
fwrite((char*)buffer,sizeof(WORD),ReallyReadLen,stream );
RemainLen = RemainLen - ReallyReadLen;
SrcReadAddress = SrcReadAddress + 2*ReallyReadLen;
UpdateProgress(4,(int)(100 - 100*((float)RemainLen/TotalLen)));
}
return 0;
}
int CFlashWriteDlg::WriteFlash(FILE *stream, int Src, int size)
{
int SrcReadAddress = Src;
int TotalLen = size/sizeof(WORD);
int TotalyWrited = 0;
WORD buffer[4096];
int ReallyReadLen = 0;
int ReallyReadByte = 0;
BOOL EndOfFile = FALSE;
while(TRUE) //以4k word为单位进行编程
{
memset(buffer, 0xff, 4096*2); // 填充指定长度的字节
ReallyReadByte = fread(buffer, sizeof(WORD), 4096, stream);
if (4096*2 > ReallyReadByte)
EndOfFile = TRUE;
if (OK == S29gl256nWrite(SrcReadAddress, buffer,ReallyReadByte))
{
printf("Write success from [0x%08x] to [0x%08x]\n", SrcReadAddress,SrcReadAddress+0x2000-1);
}
else
{
printf("Write fail[%08x]\n", SrcReadAddress);
return ERROR;
}
SrcReadAddress = SrcReadAddress + ReallyReadByte;
TotalyWrited = TotalyWrited + ReallyReadByte;
UpdateProgress(1,(int)(100*((float)TotalyWrited/TotalLen)));
if (EndOfFile == TRUE)
break;
}
return OK;
}
int CFlashWriteDlg::EraseFlash(int offset, int dataLen)
{
int retValue = 0;
int FlashEraseBaseAdr =offset & FLASH_MASK;
int numBlocks = (dataLen + offset%FLASH_SECTOR_SIZE)/FLASH_SECTOR_SIZE;
/* For each block..
*/
for(int i = 0; i<numBlocks ; i++)
{
if(OK == S29GL256NErase(FlashEraseBaseAdr,1))
{
retValue = 0;
}
else
{
retValue = -1;
return ERROR;
}
UpdateProgress(0,(int)(100*((float)i+1)/numBlocks));
}
return retValue;
}
int CFlashWriteDlg::CheckFlash(int offset, FILE *stream, int dataLen)
{
int SrcReadAddress = offset;
int TotalLen = dataLen/sizeof(WORD);
int TotalyRead = 0;
WORD buffer_flash[1024];
WORD buffer_file[1024];
int ReallyReadLen = 0;
int ReallyReadByte = 0;
BOOL EndOfFile = FALSE;
while(TRUE) //以4k word为单位进行编程
{
memset(buffer_flash, 0xff, 1024*2); // 填充指定长度的字节
memset(buffer_file, 0xff, 1024*2); // 填充指定长度的字节
ReallyReadByte = fread(buffer_file, sizeof(WORD), 1024, stream);
if (4096*2 > ReallyReadByte)
EndOfFile = TRUE;
S29GL128MRead(SrcReadAddress, buffer_flash, ReallyReadByte);
if(!memcmp((void *)buffer_flash,(void *)buffer_file,ReallyReadByte*2))
{
//logs to the log files
return ERROR;
}
SrcReadAddress = SrcReadAddress + ReallyReadByte*2;
TotalyRead = TotalyRead + ReallyReadByte;
UpdateProgress(2,(int)(100*((float)TotalyRead/TotalLen)));
if (EndOfFile == TRUE)
break;
}
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -