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

📄 dgbtool.cpp

📁 ks8995m5口交换机启动eeprom配置源码
💻 CPP
字号:
// DgbTool.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include <conio.h>

#include "DgbTool.h"

#include "ks95mspi.h"
#include "vlan.h"
#include "RateLimit.h"
#include "Mirroring.h"
#include "MibDialog.h"
#include "StaticMAC.h"

//#include <DbgTerm.h>

//RLQ, 
//#include <winddi.h>
#include <setupapi.h>

CVlan           *pVlan = NULL;
CRateLimit      *pRateLimit = NULL;
CMirroring      *pMirroring = NULL;
CMib            *pMib = NULL;
CStaticMAC		*pStaticMAC = NULL;


static GUID MyDevInterfaceClassGuid = 
{0x6994ad04, 0x93ef, 0x11d0, {0xa3, 0xcc, 0x0, 0xa0, 0xc9, 0x22, 0x31, 0x96}};

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

/////////////////////////////////////////////////////////////////////////////
// CDgbToolApp

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

/////////////////////////////////////////////////////////////////////////////
// CDgbToolApp construction

CDgbToolApp::CDgbToolApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	m_pPropertySheet = NULL;
    m_wParallelBase = 0x378;  // Default parallel port base    
    m_wSPICPort = m_wParallelBase + 2;
    m_wSPIDPort = m_wParallelBase;
    m_wSPIQPort = m_wParallelBase + 1;
    m_wSPISPort = m_wParallelBase + 2;

	m_bHWSemaphore = ~NOMIBREAD;

}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDgbToolApp object

CDgbToolApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDgbToolApp initialization

BOOL CDgbToolApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	if (FALSE == AllocPages()) {
		TRACE("Allocate pages failed\n");
		return FALSE;
	}

    m_pPropertySheet->AddPage(pVlan);
    m_pPropertySheet->AddPage(pRateLimit);
    m_pPropertySheet->AddPage(pMirroring);    
    m_pPropertySheet->AddPage(pMib);
	m_pPropertySheet->AddPage(pStaticMAC);

    // Start switch
	WriteData ( KS8995M_CHIP_ID1, 1 );


//	InitPages();
	// flag indicates final updates are made or not

	if (IDCANCEL == m_pPropertySheet->DoModal()) {
		TRACE("Cancelled by the user\n");
	}

//	if (INVALID_HANDLE_VALUE != m_hDevice) {
//		CloseHandle(m_hDevice);
//		m_hDevice = INVALID_HANDLE_VALUE;
//	}

	FreePages();
	delete m_pPropertySheet;

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

BOOL CDgbToolApp::AllocPages()
{
	TRY
	{
		////////////////////
	//	pAutoSensing = new CAutoSensing;
	//	pRegisterIO = new CRegisterIO;
	//	pOemSettings = new COemSettings;
	//	pMisc = new CMisc;

		m_pPropertySheet = (CMyPropertySheet *)new CMyPropertySheet(IDS_TITLE);

        pVlan = new CVlan;
        pRateLimit = new CRateLimit;
        pMirroring = new CMirroring;
        pMib = new CMib;
		pStaticMAC = new CStaticMAC;


		//pFinished = new CFinished;
	}
	CATCH(CMemoryException, e)
	{
		FreePages();
		return FALSE;
	}
	END_CATCH

	return TRUE;
}

BOOL CDgbToolApp::FreePages()
{

    if (pVlan) {
		delete pVlan;
		pVlan = NULL;
	}

    if (pRateLimit) {
		delete pRateLimit;
		pRateLimit = NULL;
	}

    if (pMirroring) {
		delete pMirroring;
		pMirroring = NULL;
	}

    if (pMib) {
		delete pMib;
		pMib = NULL;
	}
        
    if (pStaticMAC) {
		delete pStaticMAC;
		pStaticMAC = NULL;
	}
        
    
	return TRUE;
}


DWORD CDgbToolApp::Hextol(TCHAR * pStr)
{
   int	i, j;
   TCHAR	ch;
   DWORD	dwValue;

   j = lstrlen(pStr);
   if (0 == j)
      return 0;

    //RLQ, 6/28/99, filter 0x if any
	if ('0' == *pStr && ('x' == *(pStr + 1) || 'X' == *(pStr + 1))) {
		j -= 2;
		for (i = 0; i < j; i++) {
			*(pStr + i) = *(pStr + i + 2);
		}
	}

   // Convert it to hex in a cheap sort of way.
   dwValue = 0 ;
   for (i = 0; i < j; i++)
   {
      ch = *(pStr + i);
      if ((ch >= TEXT('0')) && (ch <= TEXT('9')))
         dwValue = (dwValue << 4) + (ch - TEXT('0')) ;
      else if ((ch >= TEXT('A')) && (ch <= TEXT('F')))
         dwValue = (dwValue << 4) + (10 + ch - TEXT('A')) ;
      else if ((ch >= TEXT('a')) && (ch <= TEXT('f')))
         dwValue = (dwValue << 4) + (10 + ch - TEXT('a')) ;
      else
         break;
   }
   return dwValue;
}



//--------------------------------------------------------------------------
//
//
//
//  This module contains routines for SPI emulation with PC parallel port.
//
//
//
//--------------------------------------------------------------------------

//
//    KS8995M             Parallel Port
//  +----------+
//  | SPIS_N   +---------+ (SS) pin [1]  (Output)
//  |          |
//  | SPID (Rx)+---------+ (Tx) pin [9]  (Output)
//  |          |
//  | SPIQ (Tx)+---------+ (Rx) pin [10] (Input)
//  |          |
//  | SPIC     +---------+ (CLK) pin [16] (Output)
//  +----------+
//
//
//   Parallel Port I/O
//
//   Output to address 278h/378h/3BCh 
//
//   Bit    7    6    5    4    3    2    1    0
//
//   Pin   [9]   8    7    6    5    4    3    2
//
//
//   Output to address 27Ah/37Ah/3BEh 
//
//   Bit    7    6    5    4   ~3    2   ~1   ~0
//
//   Pin    -    -    -   IRQ   17  [16]  14  [1]
//
//
//   Input from address 279h/379h/3BDh 
//
//   Bit    7    6    5    4    3    2    1    0
//
//   Pin    11  [10]  12   13   15   -    -    -



//--------------------------------------------------------------------------
//  
//  BYTE SPIMWrite
//
//  Description:
//      This is the core routine of SPI operation emulation. 
//
//  Parameters:
//      BYTE    cData       Data written to the port.
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
void CDgbToolApp::SPIMWrite ( BYTE bData )
{
    int i = 0;

    for ( i = 7; i >= 0; i-- )
    {
        ( bData & ( 0x01 << i ) ) ? _outp ( m_wSPIDPort, SPIDMASK ) : _outp ( m_wSPIDPort, ~SPIDMASK );
  
       
        // Toggle the clock without effecting SS signal
        _outp( m_wSPICPort, SPICMASK | SPISMASK );

		_outp( m_wSPICPort, SPISMASK );

    }
  
}

//--------------------------------------------------------------------------
//  
//  BYTE SPIMRead
//
//  Description:
//      This is the core routine of SPI operation emulation. 
//
//  Parameters:
//      None
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
BYTE CDgbToolApp::SPIMRead ()
{

    int i;
    BYTE    bTemp = 0;

    for ( i = 7; i >= 0; i-- )
    {
        bTemp |= ( _inp ( m_wSPIQPort ) & SPIQMASK ) ? 1 << i : 0;   
        
        // Toggle the clock without effecting SS signal
        _outp( m_wSPICPort, SPICMASK | SPISMASK );

        _outp( m_wSPICPort, SPISMASK );
    }   

    return bTemp;
}


//--------------------------------------------------------------------------
//  
//  void WriteData
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to write
//      the data to SPI port.
//
//  Parameters:
//      BYTE    cReg    Register number
//      BYTE    cData   Data to write
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
void CDgbToolApp::WriteData ( BYTE cReg, BYTE cData )
{
    _outp ( m_wSPISPort, SPISMASK );

	SPIMWrite ( SPI_WRITE );

	SPIMWrite ( cReg );

	SPIMWrite ( cData );
    
    _outp ( m_wSPISPort, _inp ( m_wSPICPort ) & ~SPISMASK );

	return;
}

//--------------------------------------------------------------------------
//  
//  void WriteDataBegin
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to write
//      the data to SPI port.
//
//      It leaves the SS signal unchanged after the data write so the follwoing
//      calls to WriteDataContinue() or WriteDataEnd() can write data to cReg + 1 
//         without issuing SPI_WRITE command first.
//
//      There is no disable SPIM module at the end of the rouine.
//
//
//  Parameters:
//      BYTE    cReg    Register number
//      BYTE    cData   Data to write
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
void CDgbToolApp::WriteDataBegin ( BYTE cReg, BYTE cData )
{

    _outp ( m_wSPISPort, SPISMASK );

	SPIMWrite ( SPI_WRITE );

	SPIMWrite ( cReg );

	SPIMWrite ( cData );

	return;
}


//--------------------------------------------------------------------------
//  
//  void WriteDataContinue
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to write
//      the data to SPI port.
//
//      This is a companion routine of WriteDataBegin(), it writes data to the register
//      next to cReg of WriteDataCotinue().
//
//  Parameters:
//      BYTE    cData   Data to write
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
void CDgbToolApp::WriteDataContinue ( BYTE cData )
{

	SPIMWrite ( cData );

	return;
}

//--------------------------------------------------------------------------
//  
//  void WriteDataEnd
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to write
//      the data to SPI port.
//
//      This is a companion routine of WriteDataBegin() and WriteDataContinue(), it writes data 
//      to the register next to cReg of WriteDataBegin() or WriteDataContinue().
//
//      It disables the SPIM after the write operation.
//
//  Parameters:
//      BYTE    cData   Data to write
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
void CDgbToolApp::WriteDataEnd ( BYTE cData )
{
	SPIMWrite ( cData );

    _outp ( m_wSPISPort, _inp ( m_wSPICPort ) & ~SPISMASK );

	return;
}


//--------------------------------------------------------------------------
//  
//  void ReadData
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to read
//      data from SPI port.
//
//  Parameters:
//      BYTE    cReg    Register number
//  
//  Return Vlaue:
//      Byte read from the SPI port
//  
//--------------------------------------------------------------------------
BYTE CDgbToolApp::ReadData ( BYTE cReg )
{
	BYTE bData;

    _outp ( m_wSPISPort, SPISMASK );

	SPIMWrite ( SPI_READ );

	SPIMWrite ( cReg );
	
	bData = SPIMRead ();

    _outp ( m_wSPISPort, _inp ( m_wSPICPort ) & ~SPISMASK );
    
	return ( bData );
}

//--------------------------------------------------------------------------
//  
//  void ReadDataBegin
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to read
//      data from SPI port.
//
//      It leaves the SS signal unchanged after the data read so the follwoing
//      calls to ReadDataContinue() or ReadDataEnd() can read data from cReg + 1 without 
//      issuing SPI_READ command first.
//
//      There is no disable SPIM module at the end of the rouine.
//
//
//  Parameters:
//      BYTE    cReg    Register number
//  
//  Return Vlaue:
//      Byte read from SPI port.
//  
//--------------------------------------------------------------------------
BYTE CDgbToolApp::ReadDataBegin ( BYTE cReg )
{
	BYTE bData;

    _outp ( m_wSPISPort, SPISMASK );

	SPIMWrite ( SPI_READ );

	SPIMWrite ( cReg );
	
	bData = SPIMRead ();
	
	return ( bData );
}


//--------------------------------------------------------------------------
//  
//  void ReadDataContinue
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to read
//      data from SPI port.
//
//      This is a companion routine of ReadDataBegin(), it reads data from the register
//      next to cReg of ReadDataCotinue().
//
//  Parameters:
//      BYTE    cReg    Register number
//  
//  Return Vlaue:
//      Byte read from the port
//  
//--------------------------------------------------------------------------
BYTE CDgbToolApp::ReadDataContinue ()
{

	return ( SPIMRead () );

}

//--------------------------------------------------------------------------
//  
//  void ReadDataEnd
//
//  Description:
//      This is the high level SPIM routine. It calls SPIMExchangeData to read
//      the data from SPI port.
//
//      This is a companion routine of ReadDataBegin() and ReadDataContinue(), it writes data 
//      to the register next to cReg of ReadDataBegin() or ReadDataContinue().
//
//      It disables the SPIM after the write operation.
//
//  Parameters:
//      BYTE    cData   Data to write
//  
//  Return Vlaue:
//      None
//  
//--------------------------------------------------------------------------
BYTE CDgbToolApp::ReadDataEnd ()
{
	BYTE bData;

	bData = SPIMRead ();

    _outp ( m_wSPISPort, _inp ( m_wSPICPort ) & ~SPISMASK );

	return ( bData );
}


⌨️ 快捷键说明

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