📄 gpiotest.c
字号:
//
// Copyright(C) Renesas Technology Corp. 2002-2003. All rights reserved.
//
// GPIOTEST driver for ITS-DS7
//
// FILE : GPIOTEST.C
// CREATED : 2002.06.27
// MODIFIED : 2003.06.20
// AUTHOR : Renesas Technology Corp.
// HARDWARE : RENESAS ITS-DS7
// HISTORY :
// 2003.06.20
// - Created release code.
//
#include <windows.h>
#include "ioctl_its_ds7.h"
BOOL GetFormat( LPWSTR, LPWSTR, PDWORD, PDWORD );
BOOL InputMode( HANDLE );
BOOL Output1Mode( HANDLE );
BOOL Output0Mode( HANDLE );
BOOL InterruptMode( HANDLE );
//typedef struct _PORT_INFO{
// DWORD dwBlockNum;
// DWORD dwPortNum;
//}Port_Info,*PPortInfo;
struct _PORT_INFO{
DWORD dwBlockNum;
DWORD dwPortNum;
};
struct _PORT_INFO Port_Info;
DWORD Port;
DWORD Block;
int WINAPI
WinMain (
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow
)
{
HANDLE hGPIOPort;
DWORD dwReturnBytes;
BOOL RetVal;
// DWORD dwPort;
DWORD dwLock;
WCHAR Func[10];
// DWORD Port;
// DWORD Block;
LPWSTR In = TEXT("input");
LPWSTR Out1 = TEXT("output1");
LPWSTR Out0 = TEXT("output0");
LPWSTR Int = TEXT("interrupt");
// open GPIO driver
hGPIOPort = CreateFile (
TEXT ( "GPI1:" ),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if ( hGPIOPort == INVALID_HANDLE_VALUE ){
if ( GetLastError() == ERROR_DEV_NOT_EXIST ){
//register GPIO device driver
hGPIOPort = RegisterDevice(TEXT("GPI"), 1, TEXT("GPIO.DLL"), 0);
if ( !hGPIOPort ){
RETAILMSG(1, (TEXT("Failed to register GPIO device driver.\r\n")));
}
hGPIOPort = CreateFile(TEXT("GPI1:"),
GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
}
if ( hGPIOPort == INVALID_HANDLE_VALUE ){
RETAILMSG(1, (TEXT("Failed to get device handle.\r\n")));
return 0;
}
}
if ( GetFormat(lpCmdLine, Func, &Block, &Port) != TRUE ){
goto return_program;
}
//Set Port
Port_Info.dwBlockNum = (DWORD)Block;
Port_Info.dwPortNum = (DWORD)Port;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_PORT,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Port]\r\n")));
break;
default :
goto return_program;
}
}
//Set port, lock or unlock
dwLock = GPIO_PORT_UNLOCK; //set port unlock
Port_Info.dwBlockNum= Block;
Port_Info.dwPortNum = dwLock;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_LOCK,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Lock]\r\n")));
break;
default :
goto return_program;
}
}
if( !wcscmp(Func,In) ){
if ( InputMode( hGPIOPort ) != TRUE ){
goto return_program;
}
}else if( !wcscmp(Func,Out1) ){
if ( Output1Mode( hGPIOPort ) != TRUE ){
goto return_program;
}
}else if( !wcscmp(Func,Out0) ){
if ( Output0Mode( hGPIOPort ) != TRUE ){
goto return_program;
}
}else if( !wcscmp(Func,Int) ){
if ( InterruptMode( hGPIOPort ) != TRUE ){
goto return_program;
}
}else{
RETAILMSG(1, (TEXT(" Error command ! \r\n")));
}
return_program:
CloseHandle ( hGPIOPort );
return 0;
}
//-----------------------------------------------------------------------------
// InputMode
//-----------------------------------------------------------------------------
BOOL InputMode(
HANDLE hGPIOPort
)
{
BOOL RetVal;
DWORD dwPosneg;
DWORD dwFilter;
DWORD dwIoint;
DWORD dwInout;
DWORD dwRead;
DWORD dwReturnBytes;
//Set port, positive or negative
dwPosneg = GPIO_POSNEG_POSITIVE; //set port positive logic
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwPosneg;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_POSNEG,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Posneg]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set Posneg]\r\n")));
break;
default :
return FALSE;
}
}
//Set filter, on or off
dwFilter = GPIO_FILONOFF_OFF; //set filter off
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwFilter;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_FILTER,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set filter]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set filter]\r\n")));
break;
default :
return FALSE;
}
}
//Set mode, inout or interrupt.
dwIoint = GPIO_IOINT_INOUT; //set mode inout
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwIoint;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_IOINT,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Inout or Interrupt]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set Inout or Interrupt]\r\n")));
break;
default :
return FALSE;
}
}
//Set mode, input or output
dwInout = GPIO_INOUT_IN; //set mode in
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwInout;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_INOUT,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set In or Out]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set In or Out]\r\n")));
break;
default :
return FALSE;
}
}
//Port Read
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = Port;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_READ_PORT,
&Port_Info,
sizeof(struct _PORT_INFO),
&dwRead,
sizeof(struct _PORT_INFO),
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Port Read]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Port Read]\r\n")));
break;
default :
return FALSE;
}
}
if( dwRead == 1 ){
RETAILMSG(1, (TEXT(" 1 Received. \r\n")));
}else if( dwRead == 0 ){
RETAILMSG(1, (TEXT(" 0 Received. \r\n")));
}
return TRUE;
}
//-----------------------------------------------------------------------------
// Output1Mode
//-----------------------------------------------------------------------------
BOOL Output1Mode(
HANDLE hGPIOPort
)
{
BOOL RetVal;
DWORD dwPosneg;
DWORD dwFilter;
DWORD dwIoint;
DWORD dwInout;
DWORD dwWrite;
DWORD dwReturnBytes;
//Set port, positive or negative
dwPosneg = GPIO_POSNEG_POSITIVE;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwPosneg;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_POSNEG,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Posneg]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set Posneg]\r\n")));
break;
default :
return FALSE;
}
}
dwFilter = GPIO_FILONOFF_OFF;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwFilter;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_FILTER,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set filter]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set filter]\r\n")));
break;
default :
return FALSE;
}
}
//Set mode, inout or interrupt.
dwIoint = GPIO_IOINT_INOUT;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwIoint;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_IOINT,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set Inout or Interrupt]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set Inout or Interrupt]\r\n")));
break;
default :
return FALSE;
}
}
//Set mode, input or output
dwInout = GPIO_INOUT_OUT;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwInout;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_INOUT,
&Port_Info,
sizeof(struct _PORT_INFO),
NULL,
0,
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Set In or Out]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set In or Out]\r\n")));
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -