linux c gpio读驱动程序,嵌入式Linux设备驱动开发之:GPIO驱动程序实例

本文引用地址:http://www.eepw.com.cn/article/257110.htm

staticvoidgpio_cleanup(void)

{

cdev_del(gpio_devs);/*字符设备的注销*/

unregister_chrdev_region(MKDEV(major,0),1);/*设备号的注销*/

printk(Gpiodeviceuninstalled\n);

}

module_init(gpio_init);

module_exit(gpio_cleanup);

MODULE_AUTHOR(David);

MODULE_LICENSE(DualBSD/GPL);

下面列出GPIO驱动程序的测试用例:

/*gpio_test.c*/

#includestdio.h>

#includestdlib.h>

#includeunistd.h>

#includefcntl.h>

#includestring.h>

#includesys/types.h>

#includesys/stat.h>

#includegpio_drv.h

intled_timer(intdev_fd,intled_no,unsignedinttime)

{/*指定LED发亮一段时间之后熄灭它*/

led_no%=4;

ioctl(dev_fd,LED_D09_SWT+led_no,LED_SWT_ON);/*发亮*/

sleep(time);

ioctl(dev_fd,LED_D09_SWT+led_no,LED_SWT_OFF);/*熄灭*/

}

intbeep_timer(intdev_fd,unsignedinttime)

{/*开蜂鸣器一段时间之后关闭*/

ioctl(dev_fd,BEEP_SWT,BEEP_SWT_ON);/*发声*/

sleep(time);

ioctl(dev_fd,BEEP_SWT,BEEP_SWT_OFF);/*关闭*/

}

intmain()

{

inti=0;

intdev_fd;

/*打开gpio设备*/

dev_fd=open(GPIO_DEVICE_FILENAME,O_RDWR|O_NONBLOCK);

if(dev_fd==-1)

{

printf(Canntopengpiodevicefile\n);

exit(1);

}

while(1)

{

i=(i+1)%4;

led_timer(dev_fd,i,1);

beep_timer(dev_fd,1);

}

close(dev_fd);

return0;

}

具体运行过程如下所示。首先编译并加载驱动程序:

$makeclean;make/*驱动程序的编译*/

$insmodgpio_drv.ko/*加载gpio驱动*/

$cat/proc/devices/*通过这个命令可以查到gpio设备的主设备号*/

$mknod/dev/gpioc2520/*假设主设备号为252,创建设备文件节点*/

然后编译并运行驱动测试程序:

$arm-linux-gcc–ogpio_testgpio_test.c

$./gpio_test

运行结果为4个LED轮流闪烁,同时蜂鸣器以一定周期发出声响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值