星期五, 十二月 28, 2007

以用户空间权限访问GPIO

在将Linux系统移植到相关板子上后,我们面临的就是一个编程问题了。开发板上有很多资源是可以和内核中的驱动对应的,比如我们今天要介绍的,最简单的,gpio。

首先你需要用在/dev下mknod xgpio:

crw-rw-rw- 1 root root 10, 185 Jun 1 13:59 /dev/xgpio

下面就是具体访问gpio的示例:


To write to the channel change the call:
ioctl(fgpio,XGPIO_IN,&sGpioData); to ioctl(fgpio,XGPIO_OUT,&sGpioData);

#include
#include
#include
#include

#include

main(){

int fgpio =0;
struct xgpio_ioctl_data sGpioData;
/* {
__u32 device;
__u32 mask;
__u32 data;
}*/

sGpioData.device=0; /* N=0,1,2,3-> GPIO DEV NR 2*N (Lower 32 bit part)
2*N+1 (Upper 32 bit part)*/
sGpioData.mask=0xffffffff;
sGpioData.data=0×55555555;

fgpio = open (”/dev/xgpio”,O_RDWR);

if( fgpio != -1){
ioctl(fgpio,XGPIO_IN,&sGpioData);//pointer here to xgpio_ioctl_data
printf(”Dip Swich readout 0x%X\n”,sGpioData.data);
}
else
printf(”Can not open GPIO\n”);

close(fgpio);
}// end main

0 意見: