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

📄 peridialog.cpp

📁 为keil C 软件模拟方式运行编写的一个闪存模块。
💻 CPP
字号:
// PeriDialog.cpp : implementation file
// Peripheral Dialog: A/D Converter for ADuC812

#include "stdafx.h"
#include "Agsi.h"
#include "SPeriDLL.h"
#include "PeriDialog.h"
#include "SettinDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// Protoyptes for forward references
static void PeriUpdate (void);
static void PeriKill   (AGSIDLGD *pM);
static void PeriDisp   (AGSIMENU *pM);
extern DWORD g_FlashMemBase,g_flashMemSize;
CString g_flashDataFile;

/////////////////////////////////////////////////////////////////////////////
// CPeriDialog dialog
//#define CPeriDialog CSettinDlg
static CPeriDialog  * pCPeriDialog;
BYTE initflag = 0;

// must not use 'const' here !
//                     iOpen  Hwnd   Dlg Proc.  Rect: -1 := default    Update       Kill
AGSIDLGD PeriDlg =  {    0,   NULL,      NULL,  { -1, -1, -1, -1, },  PeriUpdate,  PeriKill };

//                    nDelim   *szText            *fp      nID   nDlgId         *pDlg;
AGSIMENU PeriMenu =  {  1,   "AMD Flash Memory(by victor)"  , PeriDisp,  0,  IDD_ADCON,     &PeriDlg   };  // Peripheral Dialog


static DWORD  adccon1,  adccon2,  adccon3,  adcdatal,  adcdatah,  dmal,  dmah,  dmap;   // Current values
static DWORD  adccon1p, adccon2p, adccon3p, adcdatalp, adcdatahp, dmalp, dmahp, dmapp;  // Previous values


// Output 2-digit Hexnumber to Text-Control of given Dialog
void StringHex2 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%02X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output 4-digit Hexnumber to Text-Control of given Dialog
void StringHex4 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%04X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output 6-digit Hexnumber to Text-Control of given Dialog
void StringHex6 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%06X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output Float number to Text-Control of given Dialog
void StringFloat (CWnd * pCWnd, float val)  {
  char locbuffer[20];
  sprintf (locbuffer, "%1.4f", val);
  pCWnd->SetWindowText (locbuffer);
}

static const char INPUT_ERR_TITLE [] = "Invalid number";
static const char INPUT_ERRMSG[]     = "You have entered an invalid number!\n"
                                       "The previous value will be restored.\n"
                                       "Examples: 0x12\n";

static const char INPUT_OVR_TITLE [] = "Out of range";
static const char INPUT_OVRMSG[]     = "You have entered a number that is out of range!\n";

long GetDlg8BNumber (CWnd * pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(&INPUT_ERRMSG[0], &INPUT_ERR_TITLE[0], MB_OK|MB_ICONSTOP); 
    StringHex2 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex2 (pCWnd, oldval);
    return(-1);
  }
  StringHex2 (pCWnd, temp);
  return(temp);
}

long GetDlg16BNumber (CWnd *pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(&INPUT_ERRMSG[0],&INPUT_ERR_TITLE[0],MB_OK|MB_ICONSTOP); 
    StringHex4 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFFFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex4 (pCWnd, oldval);
    return(-1);
  }
  StringHex4 (pCWnd, temp);
  return(temp);
}

long GetDlg24BNumber (CWnd *pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_ERRMSG, INPUT_ERR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex6 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFFFFFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex6 (pCWnd, oldval);
    return(-1);
  }
  StringHex6 (pCWnd, temp);
  return(temp);
}


static const char INPUT_F_ERR_TITLE [] = "Invalid float number";
static const char INPUT_F_ERRMSG[]     = "You have entered an invalid float number!\n"
                                         "The previous value will be restored.\n"
                                         "Example: 1.234\n";

float GetDlgFloat (CWnd * pCWnd, float oldval) {
  WORD n;
  float f;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%f", &f);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_F_ERRMSG, INPUT_F_ERR_TITLE, MB_OK | MB_ICONSTOP); 
    sprintf (lbuf, "%1.4f", oldval);
    pCWnd->SetWindowText (lbuf);
    return((float)-1.9876e-36);
  }
  sprintf (lbuf, "%1.4f", f);
  pCWnd->SetWindowText (lbuf);
  return(f);
}


// This function handles a byte input in a dialog
void HandleByteInput(CWnd * pCWnd, AGSIADDR sfr) {
  DWORD oldval, oldvalp, tmp;

  Agsi.ReadSFR(sfr, &oldval, &oldvalp, 0xFF);
  tmp = GetDlg8BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfr, tmp, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a word input in a dialog
void HandleWordInput(CWnd * pCWnd, AGSIADDR sfrl, AGSIADDR sfrh) {
  DWORD oldval, tmp, tmpp;

  Agsi.ReadSFR(sfrl, &tmp, &tmpp, 0xFF);
  oldval = tmp;
  Agsi.ReadSFR(sfrh, &tmp, &tmpp, 0xFF);
  oldval += tmp << 8;
  tmp = GetDlg16BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfrl, tmp, 0xFF);
    Agsi.WriteSFR(sfrh, tmp >> 8, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a 3 byte input in a dialog
void Handle3ByteInput(CWnd * pCWnd, AGSIADDR sfrl, AGSIADDR sfrh, AGSIADDR sfrp) {
  DWORD oldval, tmp, tmpp;

  Agsi.ReadSFR(sfrl, &tmp, &tmpp, 0xFF);
  oldval = tmp;
  Agsi.ReadSFR(sfrh, &tmp, &tmpp, 0xFF);
  oldval += tmp << 8;
  Agsi.ReadSFR(sfrp, &tmp, &tmpp, 0xFF);
  oldval += tmp << 16;
  tmp = GetDlg24BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfrl, tmp, 0xFF);
    Agsi.WriteSFR(sfrh, tmp >> 8, 0xFF);
    Agsi.WriteSFR(sfrp, tmp >> 16, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a float number input in a dialog
void HandleFloatInput(CWnd * pCWnd, AGSIVTR vtr) {
  union fv tmp;
  float result;

  Agsi.ReadVTR(vtr, &tmp.DW);
  result = GetDlgFloat (pCWnd, tmp.f);
  if (result != -1.9876e-36) {
	tmp.f = result;
    Agsi.WriteVTR(vtr, tmp.DW);
  }
}

// This function is intended to be called when a checkbox is clicked (SFR bit)
void WriteBit(AGSIADDR sfr, DWORD mask, DWORD set) {
  DWORD value;

  value = (set == 0) ? 0 : 0xFF;
  Agsi.WriteSFR(sfr, value, mask);
  Agsi.UpdateWindows();
}

// This function is intended to be called when a checkbox is clicked (VTR bit)
void WriteBitVTR(AGSIVTR vtr, DWORD mask, DWORD set) {
  DWORD value;	

  Agsi.ReadVTR(vtr, &value);
  if (set) value |= mask;
  else value &= ~mask;
  Agsi.WriteVTR(vtr, value);
  Agsi.UpdateWindows();
}


static void PeriUpdate (void)  {            // Update Function
  if (pCPeriDialog) pCPeriDialog->Update();
}

static void PeriKill (AGSIDLGD *pM)  {      // Kill Function
  if (pCPeriDialog == NULL) return;
  pCPeriDialog->SendMessage (WM_CLOSE);
  pCPeriDialog  = NULL;
  pM->iOpen = 0;
  pM->hw    = NULL;
}

static void PeriDisp (AGSIMENU *pM)  {
  if (pM->pDlg->hw != NULL)  {              // created
    PeriKill (pM->pDlg);                    // close
  } else  {
//    AFX_MANAGE_STATE(AfxGetStaticModuleState());// -- not necessary.
    pCPeriDialog = new CPeriDialog (pM, NULL);     // modeless construction
    if (pCPeriDialog != NULL)  {                   // construction was Ok.
      pM->pDlg->hw = pCPeriDialog->m_hWnd;         // Dialog handle
    }
  }
}

CPeriDialog::CPeriDialog (AGSIMENU *pMen, CWnd *pWnd)  {
  pM = pMen;                     // save DYM-Descriptor locally.
  Create (IDD_ADCON, pWnd);
  pCPeriDialog = this;
}


void CPeriDialog::Update (void)  
{          /* Update Dialog Contents */

}


BEGIN_MESSAGE_MAP(CPeriDialog, CDialog)
	//{{AFX_MSG_MAP(CPeriDialog)
	ON_WM_ACTIVATE()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPeriDialog message handlers

void CPeriDialog::PostNcDestroy() {

  delete this;              // delete the new'ed object
  pCPeriDialog = NULL;      // clear external Object pointer here.
}

extern CString g_strComm;
extern int g_scanInterval;

BOOL CPeriDialog::OnInitDialog() {
  CDialog::OnInitDialog();

  // TODO: Add extra initialization here

  // Restore Position (Only moving without resizing)
  if (PeriDlg.rc.left != -1) {
 	SetWindowPos(NULL,                        /* placement order - not used */
                 PeriDlg.rc.left,             /* left */
                 PeriDlg.rc.top,              /* top  */
                 0,                           /* width - not used */
                 0,                           /* height - not used */
                 SWP_NOSIZE | SWP_NOZORDER);  /* flags */
  }
	
  initflag =1;
	
	CString txt;

	txt.Format("%x",g_FlashMemBase);
	m_flashAddrBase.SetWindowText(txt);
	txt.Format("%x",g_flashMemSize);
	m_flashMemSize.SetWindowText(txt);
	m_fileName.SetWindowText(g_flashDataFile);
	m_commPort.SetWindowText(g_strComm);
	SetDlgItemInt(IDC_SCANINTERVAL,g_scanInterval);
  return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPeriDialog::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) {
  CDialog::OnActivate(nState, pWndOther, bMinimized);
  
  switch (nState)  {
    case WA_INACTIVE:
      Agsi.HandleFocus(NULL);      // Clear Modeless Handle
      break;
    case WA_ACTIVE:
    case WA_CLICKACTIVE:
      Agsi.HandleFocus(m_hWnd);    // Set Modeless Handle
      break;
  }
}

void CPeriDialog::OnClose() {

  GetWindowRect (&pM->pDlg->rc);  // save Window position
  pM->pDlg->hw = NULL;            // clear m_hWnd
  DestroyWindow();                //--- modeless
}


// The following two functions and the corresponding invisible buttons are necessary 
// to handle the behavior of the ESC and Enter Keys 
void CPeriDialog::OnOK() {
	
	CWinApp *theApp=AfxGetApp();
	CString addrBase,size;

	m_flashAddrBase.GetWindowText(addrBase);
	m_flashMemSize.GetWindowText(size);
	addrBase="0x0"+addrBase;
	size="0x0"+size;
	sscanf(addrBase, "%x", &g_FlashMemBase );
	sscanf(size, "%x", &g_flashMemSize );
	m_commPort.GetWindowText(g_strComm);
	g_scanInterval=GetDlgItemInt(IDC_SCANINTERVAL);
	//SendMessage(WM_CLOSE );
 // OnClose();            // Close Dialog when <ESC> is pressed 
//	CDialog::OnOK();  // Do nothing when <Enter> is pressed
}

void CPeriDialog::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPeriDialog)
	DDX_Control(pDX, IDC_SCANINTERVAL, m_scanInterval);
	DDX_Control(pDX, IDC_COMMPORT, m_commPort);
	DDX_Control(pDX, IDC_FILENAME, m_fileName);
	DDX_Control(pDX, IDC_MEMSIZE, m_flashMemSize);
	DDX_Control(pDX, IDC_ADDRBASE, m_flashAddrBase);
	//}}AFX_DATA_MAP
}

void CPeriDialog::OnBrowse() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(false,"dat","am29f800",OFN_OVERWRITEPROMPT,"data Files (*.dat)|*.dat");
	dlg.DoModal();
	g_flashDataFile=dlg.GetPathName();
	m_fileName.SetWindowText(g_flashDataFile);
}

void CPeriDialog::OnSave() 
{
	// TODO: Add your control notification handler code here
	char *pBuf=new char[g_flashMemSize+10];
	try
	{
		CString addrBase,size;
		
		m_flashAddrBase.GetWindowText(addrBase);
		m_flashMemSize.GetWindowText(size);
		addrBase="0x0"+addrBase;
		size="0x0"+size;
		sscanf(addrBase, "%x", &g_FlashMemBase );
		sscanf(size, "%x", &g_flashMemSize );
		m_fileName.GetWindowText(g_flashDataFile);
		Agsi.ReadMemory(g_FlashMemBase,g_flashMemSize,(byte*)pBuf);
		CFile dataFile(g_flashDataFile,CFile::typeBinary|CFile::modeWrite|CFile::modeCreate);
		dataFile.Write(pBuf,g_flashMemSize);
	}
	catch(CFileException *e)
	{
		e->ReportError();
	}
	delete[] pBuf;
}

⌨️ 快捷键说明

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