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

📄 device.cpp

📁 基于Nuleus操作系统和s3c4510的编写的EFC。已经包含了该EFC的设计说明。这是个实际产品的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2003, Ulink Telecom Equipment Co., Ltd. All rights reserved.
//
// File:
//
//    Device.cpp
//
// Abstract:
//
//    implementation of the CDevice class.
//
// History:
//
//    V1.0	2003-03-19	Alex Duan	Original version.
//    V1.1	2003-11-12	Alex Duan	Replace >>/<< with OnSaveData/OnLoadData
//	  V1.3  2006-09-12  Bozhong Xu	Add ACO select
//									Rename OnInitTimer to OnWatchdogTimer
//									Modified OnInitDevice, OnTimer
//									Modified OnLoadData & OnSaveData
///////////////////////////////////////////////////////////////////////////////

#include "Device.h"
#include "APPCORE.H"
#include "mac_4510.h"
#include "rtcDriver.h"

#define MAX_ACO_COUNT		3
#define BDMA_RX_MAX_IDEL	20
#define IP_RX_MAX_IDEL		600

///////////////////////////////////////////////////////////////////////////////
// CDevice

IMPLEMENT_DYNAMIC(CDevice, CRTObject)

extern "C" 
{
	// the following two functions are used to reset (defined in init.s)
	void INT_Initialize(void); // restart from current code of DRAM
}

//extern UINT32 g_dwIpCnt;
//extern UINT32 g_dwBdmaCnt;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DEVICEINFO::DEVICEINFO()
{
	Default();	
}

DEVICEINFO::~DEVICEINFO()
{
}

// Initialize default value
void DEVICEINFO::Default()
{
	aAddr.uAddrH = 0;
	aAddr.uAddrL = 1;
	aAddr.uGroup = 0;

	dMadeDate.uDay = 1;
	dMadeDate.uMonth = 1;
	dMadeDate.wYear = 2006;

	strName = "Unit 1";
	strProducer = "Ulink Telecom Equipment Co., Ltd.";
	strSerial = " ";
}

DEVICEINFO& DEVICEINFO::operator =(const DEVICEINFO &a)
{
	if (this == &a)
		return *this;

	this->aAddr = a.aAddr;
	this->dMadeDate = a.dMadeDate;
	this->strName = a.strName;
	this->strProducer = a.strProducer;
	this->strSerial = a.strSerial;

	return *this;
}

NETDEVICE::NETDEVICE()
{
	Default();	
}

NETDEVICE::~NETDEVICE()
{
}

// Initialize default value
void NETDEVICE::Default()
{
	strName = "";

	// Ip address
	auIP[0] = 192;
	auIP[1] = 168;
	auIP[2] = 0;
	auIP[3] = 201;

	// Subnet mask
	auSubnet[0] = 255;
	auSubnet[1] = 255;
	auSubnet[2] = 255;
	auSubnet[3] = 0;

	// Gateway
	auGateway[0] = 192;
	auGateway[1] = 168;
	auGateway[2] = 0;
	auGateway[3] = 1;

	// Mac address
	auMac[0] = 0x00;
	auMac[1] = 0x0B;
	auMac[2] = 0x9A;
	auMac[3] = 0x00;
	auMac[4] = 0x00;
	auMac[5] = 0x06;

	// Port
	nPort = 5000;
}

// Override operator=
NETDEVICE& NETDEVICE::operator =(const NETDEVICE &a)
{
	if (this == &a)
		return *this;
	
	this->strName = a.strName;
	this->nPort = a.nPort;
	for (BYTE i = 0; i < 6; i++)
	{
		if (i < 4)
		{
			this->auIP[i] = a.auIP[i];
			this->auSubnet[i] = a.auSubnet[i];
			this->auGateway[i] = a.auGateway[i];
		}
		this->auMac[i] = a.auMac[i];		
	}
	
	return *this;
}

CDevice::CDevice()
{
	m_bACO = TRUE; // ACO

	m_dwMinorUpgrade = 0;
	m_almLevel.dwAll = -1;
//	m_ndDevice.nPort = 0;

	ROMCON rcRom; // Store value of the ROMCON register of the S3C4510B
	DWORD dwAddr; // Address in the flash
	
	// Init flash data member
	rcRom.dwReg = ROMCON0;
	dwAddr = ((DWORD)rcRom.bits.base) << 16;
	m_cFlash.SetType(AM29LV160D, dwAddr);
	
	SetSectionString("Device");

	// Add
	m_bRedLed = FALSE;
	m_bReset = FALSE;
	m_uLoaddef = 0;

	// Set default master station, added by xbz
	m_addrMaster.uAddrH = 0;
	m_addrMaster.uAddrL = 1;
	m_addrMaster.uGroup = 0;

	// Net Status
	BDMA_RX_Idel	= 0;
	IP_RX_Idel		= 0;
	MacInit_Cnt		= 0;
	StackInit_cnt	= 0;
	Test_Cnt		= 0;
	m_byLinkStatus  = 0;	//	link down
}

CDevice::~CDevice()
{
	WORD key;
	CBoard *pcbBoard = NULL;
	POSITION pos = m_mapBoards.GetStartPosition();
	while (pos != NULL) 
	{
		m_mapBoards.GetNextAssoc(pos, key, pcbBoard);
		if (pcbBoard != NULL) 
		{
			delete pcbBoard;
			pcbBoard = NULL;
		}
	}
}

// gets the product identifier
BYTE CDevice::GetProductID() const
{
	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		diInfo	[out]structure for retrieving the device information
// Remarks:
//		Gets the device information
void CDevice::GetDeviceInfo(DEVICEINFO &diInfo) const
{
	diInfo = m_diInfo;
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		diInfo	[in]structure that contains the data to set
// Remarks:
//		Set the device information
void CDevice::SetDeviceInfo(const DEVICEINFO &diInfo)
{
	m_diInfo = diInfo;	
//	GetApp()->SetModifiedFlag();
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		uIndex	Zero-based index of the board in the device
// Return Value:
//		A pointer to the CBoard object if specified board exists, else NULL
// Remarks:
//		Retrieves the board object by index from the device.
CBoard* CDevice::GetBoard(BYTE uIndex) const
{
	CBoard *pcbBoard = NULL;
	m_mapBoards.Lookup(uIndex, pcbBoard);
	return pcbBoard;
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		wBoardIndex		Zero-based index of the board in the device
//		wInterfaceType	The type of the interface.
//		wInterfaceIndex	Zero-based index of the interface in the board
// Return Value:
//		A pointer to the CInterface object if interface exists, else NULL
// Remarks:
//		Gets the interface of the specified board
CInterface* CDevice::GetInterface(WORD wBoardIndex, WORD wInterfaceType, 
								  WORD wInterfaceIndex) const
{
	CInterface *pciInterface = NULL;
	CBoard *pcbBoard = NULL;
	m_mapBoards.Lookup(wBoardIndex, pcbBoard);
	if (pcbBoard != NULL) 
	{
		pciInterface = pcbBoard->GetInterface(wInterfaceType, wInterfaceIndex);
	}
	return pciInterface;
}

// Watchdog reset
void CDevice::OnWatchdog()
{
	SetBitVal(IOPDATA, IO_WATCHDOG, !GetBitVal(IOPDATA, IO_WATCHDOG));
}

BOOL CDevice::OnGetACO()
{
	return GetBitVal(IOPDATA, IO_ACO);
}

///////////////////////////////////////////////////////////////////////////////
// Parameters:
//		nIDEvent	Specifies the identifier of the timer.
// Remarks:
//		The framework calls this member function after each interval specified 
//		in the CreateTimer member function of CRTApp used to install a timer.
void CDevice::OnTimer(UINT nIDEvent)
{
	switch(nIDEvent) {
	case TIMER_DEVICE_300MS: // Watchdog reset
		if (GetApp()->BeWatchdog() ||  // watchdog running
			(!GetApp()->BeWatchdog() && GetApp()->IsModified())) // saving task not completed.
			OnWatchdog();		
		break;

	case TIMER_DEVICE_400MS: // ACO
		{
			// Deal with ACO key and ACO LED 			
			static BYTE uAcoCount = 0;
			
			if (OnGetACO()) // high
			{ 
				uAcoCount = 0;
			}
			else if (++uAcoCount >= MAX_ACO_COUNT)
			{
				if (uAcoCount == MAX_ACO_COUNT)
				{
					OnAco();
				}
				else
				{
					uAcoCount = MAX_ACO_COUNT;
				}
			}
		}
		break;

	case TIMER_DEVICE_1S: // Running led or other
		
		if (m_bReset && !GetApp()->IsModified())
		{
			INT_Initialize(); // restart application			
		}

		OnRunLed();
		
		// Set RTC
		if (GetApp()->BeRTC())
			rtcAdjustLeapYear();

		// Check ethernet link status
		if (!GetApp()->IsSubSystem())
			CheckLinkStatus();
		break;
	default:
		break;
	}	
}

// flash the run LED
void CDevice::OnRunLed()
{	// P3: Red color of RUN LED (1:RED)
	// P4: Green color of RUN LED(1:GREEN)
//	SetBitVal(IOPDATA, IO_RUN_RED, 0);
//	SetBitVal(IOPDATA, IO_RUN_GREEN,!GetBitVal(IOPDATA, IO_RUN_GREEN));
	if (m_bRedLed)
	{
		SetBitVal(IOPDATA, IO_RUN_RED, !GetBitVal(IOPDATA, IO_RUN_GREEN));
	}
	else
	{
		SetBitVal(IOPDATA, IO_RUN_RED, 0);
	}
	SetBitVal(IOPDATA, IO_RUN_GREEN, !GetBitVal(IOPDATA, IO_RUN_GREEN));
}

// Reset run led
void CDevice::ResetRunLed()
{
	// First delete timer
	VERIFY(DeleteTimer("RunTimer"));
	
	// Set run led
	SetBitVal(IOPDATA, IO_RUN_GREEN, 0);
	SetBitVal(IOPDATA, IO_RUN_RED, 0);
	
	// Create timer again
	VERIFY(CreateTimer("RunTimer", TIMER_DEVICE_1S, 1000, 10));	
}

// On alarm cut off button pressed
void CDevice::OnAco()
{	
#ifdef __EFC_MASTER // Added by xbz
	BYTE uPara  = 1;
	GetApp()->AddEvent(PRESS_ACO, &uPara, 1);	
#endif
}

// sets net device parameters
void CDevice::SetNetDevice(const NETDEVICE &ndDevice, BYTE byMode)
{
	m_ndDevice = ndDevice;	

	switch(byMode) {
	case CONFIG_ALL:
		{
			NU_DEVICE device;
			device.dv_name = (LPSTR)"s3c4510";
			device.dv_hw.ether.dv_irq = 0;
			device.dv_hw.ether.dv_io_addr = 0;
			device.dv_hw.ether.dv_shared_addr = 0;
			device.dv_init = s3c4510_Initialize;
			device.dv_flags = 0;
			device.dv_driver_options = (DWORD)ndDevice.auMac;
			
			memcpy(device.dv_ip_addr, ndDevice.auIP, sizeof(ndDevice.auIP));
			memcpy(device.dv_subnet_mask, ndDevice.auSubnet, sizeof(ndDevice.auSubnet));
			memcpy(device.dv_gw, ndDevice.auGateway, sizeof(ndDevice.auGateway));
			VERIFY(NU_Init_Devices(&device, 1) == NU_SUCCESS);
		}
		break;
	case CONFIG_IP:
		{
			// Detach privous ip, attach new ip
			STATUS status;
			status = DEV_Detach_IP_From_Device((CHAR*)"s3c4510");
			ASSERT(status == NU_SUCCESS);
			status = DEV_Attach_IP_To_Device((CHAR*)"s3c4510", (UINT8*)ndDevice.auIP, 
				(UINT8*)ndDevice.auSubnet);	
			ASSERT(status == NU_SUCCESS);
		}
		break;
	case CONFIG_MAC:
		{		
			DV_DEVICE_ENTRY *pDevEntry = DEV_Get_Dev_By_Name((CHAR*)"s3c4510");
			ASSERT(pDevEntry);	 

			pDevEntry->dev_driver_options = (DWORD)ndDevice.auMac;

			// Set mac address to chips
			KS5_SetMacAddr(pDevEntry);
			KS5_MacInit(pDevEntry);
			
			// Reattch ip
			VERIFY(DEV_Attach_IP_To_Device((CHAR*)"s3c4510", (UINT8*)ndDevice.auIP, 
				(UINT8*)ndDevice.auSubnet) == NU_SUCCESS);
		}
		break;
	default: break;
	} 
		
}

// sets alarm level
void CDevice::SetAlarmLevel(DWORD dwLevel)
{ 
	m_almLevel.dwAll = dwLevel;
//	GetApp()->SetModifiedFlag();
}

⌨️ 快捷键说明

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