GPIO驱动实例:操作LED开关
2012-09-15 20:14
711 查看
应用程序调用ioctl():
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
#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; }
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
相关文章推荐
- Linux简单设备驱动(1):使用IO内存操作GPIO--LED
- 实例操作led驱动
- LED驱动中s3c2410_gpio_setpin s3c2410_gpio_cfgpin函数的定义
- Android GPIO LED 驱动与HAL分析
- 基于TX2440开发板在ADS1.2中编写LED的驱动(GPIO的使用)裸机程序
- cubieboard2使用GPIO驱动LED
- 通用GPIO驱动和apk(含jni),支持所有GPIO,支持LED控制,支持友善6410/210开发板,支持安卓2.3/4.0,内核2.6/3.0
- 字符设备驱动另一种写法—mmap方法操作LED
- ESP8266系列 远程LED开关操作-综述
- Linux 下操作gpio(两种方法,驱动和mmap)
- java 通过jdbc驱动连接hive操作实例
- 2-4 LED驱动实例
- 流水灯实例,点亮发光管LED并闪烁(查表操作)
- 达芬奇DM6446的LED驱动及GPIO驱动分析
- MongoDB入门教程之C#驱动操作实例
- 编写LED混杂设备驱动:静态映射,如何用Linux内核里的gpio_request(),gpio_set_value()等函数,ioctl函数
- java 通过jdbc驱动连接hive操作实例
- [RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED
- STM32学习笔记——控制GPIO输出点亮LED灯(直接操作存储器)
- 第一课:GPIO操作——LED & 按键