7 gpio examples, Ktt20/pitx software guide – Kontron KTT20-pITX User Manual
Page 53
KTD-S0045-D
Page 49 Windows® Embedded Compact 7 (WEC7) BSP
KTT20/pITX Software Guide
11.7 GPIO Examples
Defines PORT_C as output and sets each pin to low level (alternative to high level):
#include
#include
#include
// define IO controls
#define IOCTL_GPIO_CLEAR_OUTPUT \
CTL_CODE
(FILE_DEVICE_USERDRIVER, 3300, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_SET_OUTPUT \
CTL_CODE
(FILE_DEVICE_USERDRIVER, 3301, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_CONFIG_OUTPUT \
CTL_CODE
(FILE_DEVICE_USERDRIVER, 3303, METHOD_BUFFERED, FILE_ANY_ACCESS)
// define ports
#define GPIO_PORT_A
0
#define GPIO_PORT_B
1
#define GPIO_PORT_C
2
#define GPIO_PORT_D
3
#define GPIO_PORT_E
4
#define GPIO_PORT_F
5
// index conversion macros
#define GET_INDEX(port, pin) ((port << 3) | (0x07 & pin))
int _tmain (int argc, TCHAR *argv[])
{
HANDLE
hDev;
UINT i;
UCHAR ucBuffIn, ucBuffOut;
// Create handle
hDev = CreateFile (L"PIO1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hDev != INVALID_HANDLE_VALUE)
{
for (i = 0; i < 8; i++)
{
ucBuffIn = GET_INDEX (GPIO_PORT_C, i);
if (! DeviceIoControl (hDev, IOCTL_GPIO_CONFIG_OUTPUT,
&ucBuffIn,
sizeof
(UCHAR), &ucBuffOut, sizeof (UCHAR),
NULL,
NULL))
wprintf (TEXT ("Error: DeviceIoControl_CONFIG_OUTPUT failed\r\n"));
//
use
IOCTL_GPIO_CLEAR_OUTPUT
or
IOCTL_GPIO_SET_OUTPUT
if (! DeviceIoControl (hDev, IOCTL_GPIO_CLEAR_OUTPUT,
&ucBuffIn,
sizeof
(UCHAR), &ucBuffOut, sizeof (UCHAR),
NULL,
NULL))
wprintf (TEXT ("Error: DeviceIoControl_CLEAR_SET_OUTPUT failed\r\n"));
}
}
}