首先,下面是控制输入输出的三条指令:
GPIO_PAD_EN_SET(GPIO_GROUP(BSPLCD_SHP_VCC_GPIO_GROUP),GPIO_MASK(BSPLCD_SHP_VCC_GPIO_INDEX));——先设置在GPIO口
GPIO_OUTPUT_ENABLE(GPIO,GPIO_GROUP(BSPLCD_SHP_VCC_GPIO_GROUP),GPIO_INDEX(BSPLCD_SHP_VCC_GPIO_INDEX));—识能——控制输出还是输入,这里是输出
PIO_OUTPUT_ONE(GPIO,GPIO_GROUP(BSPLCD_SHP_VCC_GPIO_GROUP), GPIO_INDEX(BSPLCD_SHP_VCC_GPIO_INDEX));——高输出,低不输出!!
(A4 板)GPIO0_7 是控制 LCD 屏幕亮度的,当输出高时,亮度为最亮,当输出低时,屏幕暗——同理,可通过调整它的输出高低来控制屏幕亮度和 LED 灯亮度,当然可在中断服务程序中控制。
实践:
1)新建与 GPIO 驱动相对应的 MFC 窗口应用程序,在它的 init 函数中添加代码:
H_hUSBOtg= CreateFile(TEXT("GIO1:"),//M11 的版本出现问题
GENERIC_READ| GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (H_hUSBOtg == INVALID_HANDLE_VALUE)
{
::MessageBox(NULL,TEXT("CreateFile CAM1: failed.\r\n"),NULL,0);
return 1;
}
DWORDgroup=0;
DWORDindex=1;
DWORDoutput=0;
DWORDoutputP=0;
DWORDinputPara=(group<<8)|(index<<1)|(output);
if(!DeviceIoControl( H_hUSBOtg,
IOCTL_UIM_SERVICE_SET_GPIO,
&inputPara,//(LPVOID)&uuidSet,
sizeof(DWORD),
NULL,
0,
NULL,
NULL)== FALSE)
{
RETAILMSG(1,(TEXT(" GPIO Setting OK!\r\n")));
// return 0;
}
for(group=0;group<5;group++){
for(index=0;index<32;index++)
{
inputPara=(group<<8)|(index<<1)|(output);
if(!DeviceIoControl( H_hUSBOtg,
IOCTL_UIM_SERVICE_GET_GPIO,
&inputPara,//(LPVOID)&uuidSet,
sizeof(DWORD),
&outputP,
sizeof(DWORD),
NULL,
NULL)== FALSE)
{
RETAILMSG(1,(TEXT(" %d,%d Setting OK!%x\r\n"),group,index,outputP));
// return 0;
}
}
}
CloseHandle(H_hUSBOtg);
当然,为了能够运行,要为其中的两个标志性常数定义,定义如下:
#defineIOCTL_UIM_SERVICE_SET_GPIOCTL_CODE(FILE_DEVICE_UNKNOWN, 60, METHOD_BUFFERED, FILE_ANY_ACCESS)
#defineIOCTL_UIM_SERVICE_GET_GPIOCTL_CODE(FILE_DEVICE_UNKNOWN, 61, METHOD_BUFFERED, FILE_ANY_ACCESS)
为了能够应用上面的define,我们需要添加头文件——#include "winioctl.h" 。
这样对于应用程序的设置就完成了。
2)GPIO 驱动那边的设置如下:
首先,为了与应用程序那边联系起来,需要对那两个常数进行define:
#include "winioctl.h"
#defineIOCTL_UIM_SERVICE_SET_GPIOCTL_CODE(FILE_DEVICE_UNKNOWN, 60, METHOD_BUFFERED, FILE_ANY_ACCESS)
#defineIOCTL_UIM_SERVICE_GET_GPIOCTL_CODE(FILE_DEVICE_UNKNOWN, 61, METHOD_BUFFERED, FILE_ANY_ACCESS)
然后,在函数GIO_IOControl中进行编辑。
BOOLGIO_IOControl(DWORD p1, DWORD p2, PBYTE pInBuf, DWORD InBufLen, PBYTEpOutBuf, DWORD OutBufLen, PDWORD p7)
{
DWORDGroup;DWORD Index;
switch (p2)
{
case IOCTL_UIM_SERVICE_SET_GPIO:
if ( NULL != pInBuf && sizeof(DWORD) == InBufLen ){
DWORDvalue = (*(DWORD *)pInBuf);
value&= 0xfff;
Group= value >> 8;
Index= (value & 0xfe) >> 1;
if ( Group <= 4 && Index <= 31 ){
GPIO_PAD_EN_SET(Group,GPIO_MASK(Index) );
if ( value & 1 ){
PIO_OUTPUT_ONE(GPIO, GPIO_GROUP(Group), GPIO_INDEX(Index) );
RETAILMSG(1,(TEXT("-------------------------------------------------high!\r\n")));
}else{
PIO_OUTPUT_ZERO(GPIO, GPIO_GROUP(Group), GPIO_INDEX(Index) );
RETAILMSG(1,(TEXT("--------------------------------------------------low!\r\n")));
}
PIO_OUTPUT_ENABLE(GPIO, GPIO_GROUP(Group), GPIO_INDEX(Index) );
}
}
break;
case IOCTL_UIM_SERVICE_GET_GPIO:
if (NULL != pInBuf && sizeof(DWORD) == InBufLen && NULL != pOutBuf && sizeof(DWORD) == OutBufLen ){
DWORDvalue = (*(DWORD *)pInBuf);
value&= 0xfff;
Group= value >> 8;
Index= (value & 0xfe) >> 1;
if ( Group <= 4 && Index <= 31 ){
RETAILMSG(1,(TEXT("--------------IOCTL_UIM_SERVICE_GET_GPIO!\r\n")));
GPIO_PAD_EN_SET(Group, GPIO_MASK(Index) );
PIO_OUTPUT_DISABLE(GPIO, GPIO_GROUP(Group), GPIO_INDEX(Index) );
*((DWORD*)pOutBuf) = PIO_READ_INPUT( GPIO, GPIO_GROUP(Group), GPIO_INDEX(Index) );
}
}
break;
default:
{
DWORDdwRet = 1;
return dwRet;
}
}
return (TRUE);
}