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

📄 syscon.cpp

📁 EP9315开发板的Wince6.0的BSP包文件
💻 CPP
字号:

#include "windows.h"
#include "stdio.h"
#include "memorymap.h"
#include "hwdefs.h"

#include <pkfuncs.h>
//#include <mkfuncs.h>
#include "haluser.h"

static int DoSerialTest(int argc, char *argv[], char *envp[]);

__inline BOOL RebootSystem
(    
    void 
)
{
    //
    // Send the IOCTL
    //
    return KernelIoControl
    (
        IOCTL_HAL_REBOOT,
        NULL,
        0,
        NULL,
        0,
        NULL
    );
}




int main(int argc, CHAR *argv[], CHAR *envp[])
{
	DWORD dwPort=-1;
	BOOL  bReboot=FALSE;

    while(1)
    {
        if((argc == 1) || (argv[1][0] != '-'))
        {
            break;
        }
        if(argv[1][1] =='p')
        {
            dwPort = atoi(argv[1] + 2);
        }
        else if(argv[1][1] =='r')
        {
            bReboot=TRUE;
		}
        else if(argv[1][1] == 'h')
        {
            //Usage();
			wprintf(L"Usage:\r\n");
			wprintf(L"\t-p [which port is used for debuging]\r\n");
			wprintf(L"\t-r reboot the system\r\n");
			wprintf(L"\t-s to do serial test\r\n");
            return(1);
        }
        else if(argv[1][1] =='s')
        {
           	return DoSerialTest(--argc, ++argv, envp);
		}
        //
        // Skip to the next argument on the command line.
        //
        argv++;
        argc--;
    }

	if( dwPort !=-1 ){

		wprintf(L"Debug port will be redirected to COM%d\r\n",dwPort);
		if( RedirectionDebugPort( dwPort ) ){
			int num=10;

			wprintf(L"Redirected succeeded\r\n");
			while(num-- ){

				RETAILMSG(1,(L"This is the test message form COM%d\r\n",dwPort));
				Sleep(2000);
			}
		}else
			wprintf(L"Redirected failed\r\n");

	}else if( bReboot ){
		
		RebootSystem( );
	}


    return 0;
}


//****************************************************************************
// following codes are for serial testing.
//
//****************************************************************************


#define BUFFER_SIZE     0x200

HANDLE hSerialPort;


//****************************************************************************
//
// OpenPort opens the specified serial port.
//
//****************************************************************************
int
InitPort(long lPort, long lRate, BOOL bIRMode)
{
    WCHAR pcName[16];

    //
    // Create the device name for the given serial port.
    //
    wsprintf(pcName, L"COM%d:", lPort);

    //
    // Open the serial port.
    //
    hSerialPort = CreateFile(pcName, GENERIC_READ | GENERIC_WRITE, 0, 0,
                             OPEN_EXISTING, 0, 0);
    if(hSerialPort == INVALID_HANDLE_VALUE)
    {
        wprintf(L"Could not open serial port %s.\n", pcName);
		RETAILMSG(1, (L"Could not open serial port %s.\n", pcName));
        return(0);
    }

    DCB dcb;

    dcb.DCBlength        = sizeof(DCB);
    GetCommState(hSerialPort, &dcb);
    //
    // Fill in the device control block.
    //
    dcb.BaudRate = lRate;
    dcb.fBinary = TRUE;
    dcb.fParity = FALSE;
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fDtrControl = DTR_CONTROL_DISABLE;
    dcb.fDsrSensitivity = FALSE;
    dcb.fTXContinueOnXoff = TRUE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    dcb.fErrorChar = FALSE;
    dcb.fNull = FALSE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
    dcb.fAbortOnError = FALSE;
    dcb.XonLim = 0;
    dcb.XoffLim = 0;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.XonChar = 17;
    dcb.XoffChar = 19;
    dcb.ErrorChar = 63;
    dcb.EofChar = 26;
    dcb.EvtChar = 0;
    dcb.wReserved = 0;

    //
    // Set the new serial port configuration.
    //
    SetCommState(hSerialPort, &dcb);


    COMMTIMEOUTS sTimeouts;
    //
    // Fill in the timeout structure based on the timeout requested for this
    // read.
    //
    sTimeouts.ReadIntervalTimeout = 50;
    sTimeouts.ReadTotalTimeoutMultiplier = 0;
    sTimeouts.ReadTotalTimeoutConstant = 5000;
    sTimeouts.WriteTotalTimeoutMultiplier = 0;
    sTimeouts.WriteTotalTimeoutConstant = 0;

    //
    // Set the timeout for this read.
    //

	SetCommTimeouts(hSerialPort, &sTimeouts);

	if( bIRMode )
		EscapeCommFunction(hSerialPort, SETIR);
	
	EscapeCommFunction(hSerialPort, SETRTS);
	EscapeCommFunction(hSerialPort, SETDTR);

    //
    // Purge any pending characters in the serial port.
    //
    PurgeComm(hSerialPort, (PURGE_TXABORT | PURGE_RXABORT |
                            PURGE_TXCLEAR | PURGE_RXCLEAR));

    //
    // Success.
    //
    return(1);
}

//****************************************************************************
//
// SendChar sends a character to the serial port.
//
//****************************************************************************
void
SendData(BYTE* buffer, DWORD dwSize)
{
    DWORD dwLen;

    //
    // Send this character to the serial port.
    //
    WriteFile(hSerialPort, buffer, dwSize, &dwLen, NULL);
}

//****************************************************************************
//
// ReceiveChar reads a character from the serial port.
//
//****************************************************************************
DWORD 
RecieveData(BYTE * buffer, DWORD dwSize)
{
    DWORD dwLen;

    //
    // Read a character.
    //
    if(!ReadFile(hSerialPort, buffer, dwSize, &dwLen, NULL))
    {
        //
        // The read failed, so set the read character to a NULL.
        //
        dwLen = 0;
    }
    return(dwLen);
}

//****************************************************************************
// main
//****************************************************************************
// 
// 
//
int DoSerialTest(int argc, char *argv[], char *envp[])
{
	DWORD dwDev=1;
	DWORD dwRate=9600;
	DWORD dwNum=100;
	BOOL  bClientMode=FALSE;
	BOOL  bBMode=FALSE;
	BOOL  bIRMode=FALSE;
    //
    // Print out the sign on.
    //
    wprintf(L"Serialtest.exe: Serial Port Test Program\r\n");
    wprintf(L"Copyright(c) Cirrus Logic, All Rights Reserved\r\n");

    // Open the serial port and set the Baud rate.
    while(1)
    {
        //
        if((argc == 1) || (argv[1][0] != '-'))
        {
            break;
        }
        if(argv[1][1] =='p')
        {
            dwDev = atoi(argv[1] + 2);
        }
        else if(argv[1][1] =='c')
        {
            bClientMode=TRUE;
		}
        else if(argv[1][1] =='n')
        {
            dwNum= atoi(argv[1] + 2);
		}
        else if(argv[1][1] =='r')
        {
            dwRate = atoi(argv[1] + 2);
		}
        else if(argv[1][1] =='b')
        {
            bBMode=TRUE;
		}
        else if(argv[1][1] =='i')
        {
            bIRMode=TRUE;
		}
        else if(argv[1][1] == 'h')
        {
            //Usage();
			wprintf(L"Usage:\r\n");
			wprintf(L"\t-p [which port is used]\r\n");
			wprintf(L"\t-r [baud rate] default is 9600:\r\n");
			wprintf(L"\t-c Set it if using is as client\r\n");
			wprintf(L"\t-b use binary mode\r\n");
			wprintf(L"\t-i IR mode\r\n");
            return(1);
        }
        //
        // Skip to the next argument on the command line.
        //
        argv++;
        argc--;
    }

	wprintf(L"Serial test Program open port %d, baud rate %d\r\n",dwDev, dwRate);

	if( !InitPort(dwDev,dwRate, bIRMode) ){

		wprintf(L"can't open serial port\r\n");
		return -1;
	}

	if( bClientMode ){

		DWORD i;
		BYTE buffer[200];
		DWORD dwGet;

		for(i=0; i<dwNum; i++ ){

			if( dwGet=RecieveData( buffer, sizeof(buffer) ) ){
				buffer[dwGet]=0;

				if(bBMode){
					DWORD i;

					printf("client get: ");

					for(i=0; i< dwGet; i++ ){

						printf(" %x ", buffer[i]);
					}
					printf("\r\n");
				}
				else
					printf("client get:%s\r\n", buffer);

				if( dwGet )
					SendData( buffer ,dwGet );
			}
		}
	}else{

		DWORD i;
		char sendbuf[200]="Hello,This EDB93XX board,please call back\r\n";
		char sendBinary[15];
		BYTE buffer[200];
		DWORD dwGet;
		DWORD dwLen=strlen(sendbuf);
		char  *pBuf;
		DWORD dwSize;

		if( bBMode ){

			pBuf=sendBinary;

			for(i=0; i<2; i++ ){

				*pBuf++=0x12;
				*pBuf++=0x34;
				*pBuf++=0x56;
				*pBuf++=0x78;
				*pBuf++=0x90;
			}
			*pBuf=0;

			pBuf=sendBinary;
			dwSize=strlen(sendBinary);
		}else{

			pBuf=sendbuf;
			dwSize=strlen(sendbuf);
		}

		for(i=0; i<dwNum; i++ ){

			SendData( (BYTE*)pBuf ,dwSize );

			dwGet=RecieveData( buffer, sizeof(buffer) );
			buffer[dwGet]=0;

			if(bBMode){

				DWORD i;

				printf("master get: ");

				for(i=0; i< dwGet; i++ ){

					printf(" %x ", buffer[i]);
				}
				printf("\r\n");
			}
			else
				printf("master get:%s\r\n", buffer);
		}
	}
    
    CloseHandle(hSerialPort);
    return 0;
}

⌨️ 快捷键说明

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