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

📄 speridll.cpp

📁 为keil C 软件模拟方式运行编写的一个闪存模块。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SPeriDLL.cpp : Defines the initialization routines for the DLL.
// Sample Peripheral DLL: A/D Converter for ADuC812

#include "stdafx.h"
#include "Agsi.h"
#include "SPeriDLL.h"
#include "PeriDialog.h"
#include "serial\\serialport.h"
#include <process.h>    /* _beginthread, _endthread */

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CSPeriDLLApp

BEGIN_MESSAGE_MAP(CSPeriDLLApp, CWinApp)
	//{{AFX_MSG_MAP(CSPeriDLLApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static AGSICONFIG  AgsiConfig;
struct AGSIFUNCS   Agsi;

DWORD TX_REG,RX_REG;
DWORD g_CpuType;
DWORD g_FlashMemBase=0x400000,g_flashMemSize=0x80000;
extern CString g_flashDataFile;
CSerialPort comPort,listenPort,listenPort1;
CString g_strComm="com4";

DWORD GetFunctionPointers(void) {           // get all function pointers

  Agsi.DefineSFR            = (tAgsiDefineSFR)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineSFR");
  Agsi.DefineVTR            = (tAgsiDefineVTR)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineVTR");
  Agsi.DeclareInterrupt     = (tAgsiDeclareInterrupt)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiDeclareInterrupt");
  Agsi.SetWatchOnSFR        = (tAgsiSetWatchOnSFR)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnSFR");
  Agsi.SetWatchOnVTR        = (tAgsiSetWatchOnVTR)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnVTR");
  Agsi.SetWatchOnMemory     = (tAgsiSetWatchOnMemory)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnMemory");
  Agsi.CreateTimer          = (tAgsiCreateTimer)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiCreateTimer");
  Agsi.DefineMenuItem       = (tAgsiDefineMenuItem)        GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineMenuItem");

  Agsi.WriteSFR             = (tAgsiWriteSFR)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteSFR");
  Agsi.ReadSFR              = (tAgsiReadSFR)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadSFR");
  Agsi.WriteVTR             = (tAgsiWriteVTR)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteVTR");
  Agsi.ReadVTR              = (tAgsiReadVTR)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadVTR");
  Agsi.SetSFRReadValue      = (tAgsiSetSFRReadValue)       GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetSFRReadValue");
  Agsi.ReadMemory           = (tAgsiReadMemory)            GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadMemory");
  Agsi.WriteMemory          = (tAgsiWriteMemory)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteMemory");
  Agsi.GetLastMemoryAddress = (tAgsiGetLastMemoryAddress)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetLastMemoryAddress");
  Agsi.IsSimulatorAccess    = (tAgsiIsSimulatorAccess)     GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsSimulatorAccess");
  Agsi.SetTimer             = (tAgsiSetTimer)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetTimer");
  Agsi.GetStates            = (tAgsiGetStates)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetStates");
  Agsi.GetProgramCounter    = (tAgsiGetProgramCounter)     GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetProgramCounter");
  Agsi.IsInInterrupt        = (tAgsiIsInInterrupt)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsInInterrupt");
  Agsi.IsSleeping           = (tAgsiIsSleeping)            GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsSleeping");
  Agsi.StopSimulator        = (tAgsiStopSimulator)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiStopSimulator");
  Agsi.TriggerReset         = (tAgsiTriggerReset)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiTriggerReset");
  Agsi.UpdateWindows        = (tAgsiUpdateWindows)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiUpdateWindows");
  Agsi.HandleFocus          = (tAgsiHandleFocus)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiHandleFocus");
  Agsi.GetExternalClockRate = (tAgsiGetExternalClockRate)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetExternalClockRate");
  Agsi.GetInternalClockRate = (tAgsiGetInternalClockRate)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetInternalClockRate");
  Agsi.GetClockFactor       = (tAgsiGetClockFactor)        GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetClockFactor");
  Agsi.Message              = (tAgsiMessage)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiMessage");
  Agsi.GetTargetKey         = (tAgsiGetTargetKey)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetTargetKey");
  Agsi.SetTargetKey         = (tAgsiSetTargetKey)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetTargetKey");
  Agsi.GetSymbolByName      = (tAgsiGetSymbolByName)       GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetSymbolByName");
  Agsi.GetSymbolByValue     = (tAgsiGetSymbolByValue)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetSymbolByValue");
  Agsi.ExecuteCommand       = (tAgsiExecuteCommand)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiExecuteCommand");

  if ((Agsi.DefineSFR            == NULL) ||
      (Agsi.DefineVTR            == NULL) ||
      (Agsi.DeclareInterrupt     == NULL) ||
      (Agsi.SetWatchOnSFR        == NULL) ||
      (Agsi.SetWatchOnVTR        == NULL) ||
      (Agsi.SetWatchOnMemory     == NULL) ||
      (Agsi.CreateTimer          == NULL) ||
      (Agsi.DefineMenuItem       == NULL) ||

      (Agsi.WriteSFR             == NULL) ||
      (Agsi.ReadSFR              == NULL) ||
      (Agsi.WriteVTR             == NULL) ||
      (Agsi.ReadVTR              == NULL) ||
      (Agsi.SetSFRReadValue      == NULL) ||
      (Agsi.ReadMemory           == NULL) ||
      (Agsi.WriteMemory          == NULL) ||
      (Agsi.GetLastMemoryAddress == NULL) ||
      (Agsi.IsSimulatorAccess    == NULL) ||
      (Agsi.SetTimer             == NULL) ||
      (Agsi.GetStates            == NULL) ||
      (Agsi.GetProgramCounter    == NULL) ||
      (Agsi.IsInInterrupt        == NULL) ||
      (Agsi.IsSleeping           == NULL) ||
      (Agsi.StopSimulator        == NULL) ||
      (Agsi.TriggerReset         == NULL) ||
      (Agsi.UpdateWindows        == NULL) ||
      (Agsi.HandleFocus          == NULL) ||
      (Agsi.GetExternalClockRate == NULL) ||
      (Agsi.GetInternalClockRate == NULL) ||
      (Agsi.GetClockFactor       == NULL) ||
      (Agsi.Message              == NULL) ||
      (Agsi.GetTargetKey         == NULL) ||
      (Agsi.SetTargetKey         == NULL) ||
      (Agsi.GetSymbolByName      == NULL) ||
      (Agsi.ExecuteCommand       == NULL) ||
      (Agsi.GetSymbolByValue     == NULL)) {

    return(FALSE);
  }

  return(1);
}


DWORD DefineAllSFR(void) {           // declare all special function registers and their bits
  BOOL ret = TRUE;

	/*
  ret &= Agsi.DefineSFR("ADCCON1",  ADCCON1,  AGSIBYTE, 0);  // If more SFR's are defined, do it in a table
  ret &= Agsi.DefineSFR("ADCCON2",  ADCCON2,  AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("ADCCON3",  ADCCON3,  AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("ADCDATAL", ADCDATAL, AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("ADCDATAH", ADCDATAH, AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("DMAL",     DMAL,     AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("DMAH",     DMAH,     AGSIBYTE, 0);
  ret &= Agsi.DefineSFR("DMAP",     DMAP,     AGSIBYTE, 0);

  // It is only allowed to define bits which are bitaddressable.
  ret &= Agsi.DefineSFR("CS0",      ADCCON2,  AGSIBIT,  0);
  ret &= Agsi.DefineSFR("CS1",      ADCCON2,  AGSIBIT,  1);
  ret &= Agsi.DefineSFR("CS2",      ADCCON2,  AGSIBIT,  2);
  ret &= Agsi.DefineSFR("CS3",      ADCCON2,  AGSIBIT,  3);
  ret &= Agsi.DefineSFR("SCONV",    ADCCON2,  AGSIBIT,  4);
  ret &= Agsi.DefineSFR("CCNOV",    ADCCON2,  AGSIBIT,  5);
  ret &= Agsi.DefineSFR("DMA",      ADCCON2,  AGSIBIT,  6);
  ret &= Agsi.DefineSFR("ADCI",     ADCCON2,  AGSIBIT,  7);	
	if(g_CpuType==80166)
	{
		ret &= Agsi.DefineSFR("ASC_RBUF", RX_REG,  AGSIWORD,  AGSIREAD);
		ret &= Agsi.DefineSFR("ASC_TBUF", TX_REG,  AGSIWORD,  AGSIWRITE);
	}
	else if(g_CpuType==8051)
	{
		ret &= Agsi.DefineSFR("ASC_TBUF", TX_REG,  AGSIBYTE,  AGSIWRITE);
	}
	 	*/
 // ret &= Agsi.DefineSFR("ASC1RIR",0xF18A,  AGSIBIT,  7);	
  return(ret);
}														


struct vtrlist VTREG[] = {           // VTR's definition: keep this table consistent with defines in SPeriDLL.h
  { "S0IN",   AGSIVTRCHAR, 0x00000000, NULL },
  { "S1IN",   AGSIVTRCHAR, 0x00000000, NULL },
  { "S0OUT",  AGSIVTRCHAR, 0x00000000, NULL },
  { "S1OUT",  AGSIVTRCHAR, 0x00000000, NULL },
  { "AIN4",   AGSIVTRFLOAT, 0x00000000, NULL },
  { "AIN5",   AGSIVTRFLOAT, 0x00000000, NULL },
  { "AIN6",   AGSIVTRFLOAT, 0x00000000, NULL },
  { "AIN7",   AGSIVTRFLOAT, 0x00000000, NULL },
  { "TEMP",   AGSIVTRFLOAT, 0x41C80000, NULL },  // 25 癈
  { "VREF",   AGSIVTRFLOAT, 0x40200000, NULL },  // 2.5 V
  { "XTAL",   AGSIVTRLONG,  0x00A8C000, NULL },  // 11.0592MHz oscillator frequency
  { "CONVST", AGSIVTRCHAR,  0x00000001, NULL },
};

DWORD DefineAllVTREG(void) {         // declare all virtual registers
  BOOL ret = TRUE;
  int i;

  for (i = 0; i < (sizeof(VTREG) / sizeof (VTREG[0])); i++) {
    VTREG[i].hVTR = Agsi.DefineVTR(VTREG[i].pName, VTREG[i].Type, VTREG[i].Value);
	if (!VTREG[i].hVTR) ret = FALSE;
  }

  return(ret);
}


AGSIINTERRUPT Interrupt = {     // Table of all Interrupts of this module
// vec    *mess           msfr mmask *mname  rsfr     rmask *rname  esfr  emask *ename  p0sfr p0mask *pname p1sfr p1mask pwl auto_reset
   0x33, "A/D Converter",  0,  0,     "",    ADCCON2, 0x80, "ADCI", IE,   0x40, "EADC", IP,   0x40,  "Pri", 0,    0x00,  7,  1   // ADCI
};


DWORD DefineAllInterrupts(void) {   // declare all interrupts

  if (!Agsi.DeclareInterrupt(&Interrupt)) return(FALSE);

  return(TRUE);
}

const DWORD flash_addr0=0x0aaa;  
const DWORD flash_addr1=0x0554;
const DWORD ManuIDAddr=0x0; 
const DWORD DevIDAddr=0x02;  


const WORD flash_comm0=0xaaaa; 	    
const WORD flash_comm1=0x5555;
const WORD flash_readID=0x9090;       
const WORD flash_write=0xA0A0;       
const WORD flash_ready=0x8080;        
const WORD flash_erasechip=0x1010;   
const WORD flash_erasesector=0x3030;  
const WORD flash_ManuID=0x4;
const WORD flash_DevID=0x2258;

int mstate=0;


void stateMachine(DWORD addr,WORD cmd)
{
	static WORD old0,old1,old2;
	WORD tmp;

	if(addr<g_FlashMemBase||addr>=g_FlashMemBase+g_flashMemSize)return;
	addr-=g_FlashMemBase;
	Agsi.Message("current State %i,Address:0x%08X,data:%i\n",mstate,addr,cmd);
	switch(mstate)
	{
	case 0:
		if(cmd==0xf0f0)
			mstate=1;
		break;
	case 1:
		if(cmd==flash_comm0)
			mstate=2;
		else if(cmd!=0xf0f0)
			mstate=0;
		break;
	case 2:
		if(cmd==flash_comm1)
			mstate=3;
		break;
	case 3:
		switch(cmd)
		{
		case 0x9090://flash_readID:
			Agsi.ReadMemory(g_FlashMemBase+ManuIDAddr,2,(BYTE *)&old0);	 
			Agsi.ReadMemory(g_FlashMemBase+DevIDAddr,2,(BYTE *)&old1);	
			Agsi.WriteMemory(g_FlashMemBase+ManuIDAddr,2,(BYTE *)&flash_ManuID);
			Agsi.WriteMemory(g_FlashMemBase+DevIDAddr,2,(BYTE *)&flash_DevID);
			mstate=20;
			break;
		case 0xA0a0://flash_write:
			mstate=21;
			break;
			//			case 0x1010://flash_erasechip:
			//			mstate=12;
			//				break;
			//				case 0x3030://flash_erasesector:
			//					mstate=20;
			//					break;
		case 0x8080://flash_ready:
			mstate=8;
			break;
		default:
			mstate=0;
			break;
		}
		break;
	case 8:
		if(cmd==flash_comm0)
			mstate=9;
		break;
	case 9:
		if(cmd==flash_comm1)
		{
			mstate=10;
	//		memset(FLASHBASE,memEnd-FLASHBASE+1,0xff);
		}
		break;
	case 10:
		if(cmd==0xf0f0)
		{
			mstate=1;
		}
		else
			mstate=0;
		break;
	case 13:
		break;
	case 14:
		break;
	case 20:
		if(cmd==0xf0f0)
		{
			Agsi.WriteMemory(g_FlashMemBase+ManuIDAddr,2,(BYTE *)&old0);	 
			Agsi.WriteMemory(g_FlashMemBase+DevIDAddr,2,(BYTE *)&old1);	
			mstate=1;
		}
		else
			mstate=0;
		break;
	case 21:
		break;
	default:
		mstate=0;
	}
}


 void FlashRead(DWORD adr, AGSICB_REASON r)
{
	 static DWORD order=0;
	 byte value[4]={2,3,4,5};
	// DWORD addr=Agsi.GetLastMemoryAddress();
	//Agsi.WriteMemory(adr,2,value);
	 Agsi.Message("%09u %Address:0x%08X,reason:%i\n",order++,adr,r);
}

void FlashWrite(DWORD adr, AGSICB_REASON r)
{
	stateMachine(adr,1);
}

byte txbuf[8192];
int txindex=0;

void asc1Tx(DWORD adr, AGSICB_REASON r)
{
  DWORD outChar,prev;
	Agsi.ReadSFR(0xfeb8,&outChar,&prev,0xff);
	comPort.Write(&outChar,1);
	listenPort.Write(&outChar,1);
}

void asc0Tx(DWORD adr, AGSICB_REASON r)
{

⌨️ 快捷键说明

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