📄 gpiotest.c
字号:
default :
return FALSE;
}
}
// Write the port, GPIO_OUT_HIGH(1) or GPIO_OUT_LOW(0).
dwWrite = GPIO_OUT_HIGH;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwWrite;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_WRITE_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! [Port Write]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Port Write]\r\n")));
break;
default :
return FALSE;
}
}else{
RETAILMSG(1, (TEXT(" 1 Write. \r\n")));
}
return TRUE;
}
//-----------------------------------------------------------------------------
// Output0Mode
//-----------------------------------------------------------------------------
BOOL Output0Mode(
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;
}
}
//Set filter, on or off
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;
default :
return FALSE;
}
}
// Write the port, GPIO_OUT_HIGH(1) or GPIO_OUT_LOW(0).
dwWrite = GPIO_OUT_LOW;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwWrite;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_WRITE_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! [Port Write]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Port Write]\r\n")));
break;
default :
return FALSE;
}
}else{
RETAILMSG(1, (TEXT(" 0 Write. \r\n")));
}
return TRUE;
}
//-----------------------------------------------------------------------------
// InterruptMode
//-----------------------------------------------------------------------------
BOOL InterruptMode(
HANDLE hGPIOPort
)
{
BOOL RetVal;
DWORD dwPosneg;
DWORD dwEdglev;
DWORD dwFilter;
DWORD dwIoint;
DWORD dwTimeout;
DWORD dwInterrupt;
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;
}
}
//Set port, edge or level
dwEdglev = GPIO_EDGLEV_EDGE;
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwEdglev;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_EDGLEVEL,
&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 edge or level]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Set edge or level]\r\n")));
break;
default :
return FALSE;
}
}
//Set filter, on or off
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_INTERRUPT;
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;
}
}
//Interrupt Clear
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = Port;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_INTCLR,
&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! [Interrupt Clear]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Interrupt Clear]\r\n")));
break;
default :
return FALSE;
}
}
//Mask Clear
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = Port;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_SET_MSKCLR,
&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! [Mask Clear]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Mask Clear]\r\n")));
break;
default :
return FALSE;
}
}
//Get event
dwTimeout = 20000; //Wait time :20000ms
Port_Info.dwBlockNum = Block;
Port_Info.dwPortNum = dwTimeout;
RetVal = DeviceIoControl(
hGPIOPort,
IOCTL_GPIO_GET_EVENT,
&Port_Info,
sizeof(struct _PORT_INFO),
&dwInterrupt,
sizeof(DWORD),
&dwReturnBytes,
NULL
);
if ( !RetVal ){
switch( GetLastError() ){
case GPIO_PORT_ERROR :
RETAILMSG(1, (TEXT(" Port does not exist! [Get Event]\r\n")));
break;
case GPIO_PORT_LOCKED :
RETAILMSG(1, (TEXT(" Port is locked! [Get Event]\r\n")));
break;
default :
return FALSE;
}
}
else if( dwInterrupt == WAIT_OBJECT_0 ){
RETAILMSG(1, (TEXT("Get Event !!\r\n")));
}else if( dwInterrupt == WAIT_TIMEOUT ){
RETAILMSG(1, (TEXT("TIMEOUT !\r\n")));
}else{
RETAILMSG(1, (TEXT("Wait Failed.\r\n")));
}
return TRUE;
}
//-----------------------------------------------------------------------------
// GetFormat
//-----------------------------------------------------------------------------
BOOL
GetFormat(
LPWSTR pStrings,
LPWSTR pFirstStr,
PDWORD pdwBlock,
PDWORD pdwPort
)
{
LPWSTR Seps = L" ";
LPWSTR fs;
LPWSTR ss;
LPWSTR ts;
LPWSTR pStop;
fs = wcstok(pStrings, Seps);
if ( fs == NULL ){
return FALSE;
}
ss = wcstok(NULL, Seps);
if ( ss == NULL ){
return FALSE;
}
ts = wcstok(NULL, Seps);
if ( ts == NULL ){
return FALSE;
}
wcscpy(pFirstStr, _wcslwr(fs));
*pdwBlock = wcstol(ss, &pStop, 10);
*pdwPort = wcstol(ts, &pStop, 10);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -