应用程序调用ioctl():
[code]
#include
#include
#include
#include
int main(int argc, char **argv)
{
int on;
int led_no;
int fd;
if (argc != 3 || sscanf(argv[1], "%d", led_no) != 1 || sscanf(argv[2],"%d", on) != 1 ||
on < 0 || on > 1 || led_no < 0 || led_no > 3) {
fprintf(stderr, "Usage: leds led_no 0|1\n");
exit(1);
}
fd = open("/dev/led", 0);
if (fd < 0) {
perror("can not open device led");
exit(1);
}
ioctl(fd, on, led_no);
close(fd);
return 0;
}
[/code]
GPIO驱动部分:
[code]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME"led"
#define LED_MAJOR 233
MODULE_AUTHOR("Luofuchong");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("led");
static unsigned long led_table [] = {
S3C2410_GPB7,
S3C2410_GPB8,
S3C2410_GPB9,
S3C2410_GPB10,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
S3C2410_GPB9_OUTP,
S3C2410_GPB10_OUTP,
};
static int leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 3) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg],!cmd);
break;
default:
return -EINVAL;
}
return 0;
}
static struct file_operations leds_fops = {
.owner = THIS_MODULE,
.ioctl = leds_ioctl,
};
static struct class *led_class;
static int __init leds_init(void)
{
int err = 0;
int i;
if(register_chrdev(LED_MAJOR,"led",leds_fops)){
printk("led driver:Unable to register driver\n");
return -ENODEV;
}
led_class = class_create(THIS_MODULE, "led");
if(IS_ERR(led_class)){
err = PTR_ERR(led_class);
goto out_chrdev;
}
class_device_create(led_class,MKDEV(LED_MAJOR, 0),NULL,"led");
err = devfs_mk_cdev(MKDEV(LED_MAJOR,0),
S_IFCHR | S_IRUGO | S_IWUSR,"led");
if(err)
goto out_class;
for(i=0;i<4;i ){
s3c2410_gpio_cfgpin(led_table[i],led_table[i 4]);
s3c2410_gpio_setpin(led_table[i],1);
}
printk("led driver initialized\n");
goto out;
out_class:
class_device_destroy(led_class,MKDEV(LED_MAJOR, 0));
class_destroy(led_class);
out_chrdev:
unregister_chrdev(LED_MAJOR, "led");
out:
return err;
}
static void __exit leds_exit(void)
{
class_device_destroy(led_class,MKDEV(LED_MAJOR,0));
class_destroy(led_class);
unregister_chrdev(LED_MAJOR,"led");
devfs_remove("led");
printk("led driver removed\n");
}
module_init(leds_init);
module_exit(leds_exit);
[/code]
转自:
http://www.hhcn.com/cgi-bin/topic.cgi?forum=3topic=367
《Linux Device Driver》example link:
ftp://ftp.ora.com/pub/examples/
ftp://ftp.ora.com/pub/examples/
GPIO驱动实例:操作LED开关
原创hanxuefei432 ©著作权
©著作权归作者所有:来自51CTO博客作者hanxuefei432的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【驱动篇】龙芯LS2K0300之LED驱动
LED驱动及测试用例
龙芯 LS2K0300 LED 驱动 LoongArch -
Linux驱动开发|LED驱动开发实例
LED驱动开发实例
驱动开发 linux imx6ull driver #include -
266_S32K144 GPIO驱动LED
完整的S32K144的学习汇总如下:://github.com/G
初始化 原理图 github