【转】Linux驱动修炼之道-LCD背光与gpio控制

  下图是LCD的引脚截图,GPG4是复用引脚,可以发送电源使能信号。GPIO的程序很简单,这里不再赘述。

 

【转】Linux驱动修炼之道-LCD背光与gpio控制_第1张图片

 

驱动程序:

#include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <mach/regs-gpio.h> #include <asm/io.h> #include <mach/hardware.h> #include <linux/device.h> #include <linux/cdev.h> #include <linux/delay.h> int MYGPIO_MAJOR = 0; int MYGPIO_MINOR = 0; #define MYGPIO_NAME "lwp-gpio" dev_t dev_num; struct cdev *gpio_cdev; struct class *gpio_class; int i; static unsigned long led_table[]={ S3C2410_GPF3, S3C2410_GPF4, S3C2410_GPF5, S3C2410_GPF6, }; static unsigned long led_cfg_table[]={ S3C2410_GPF3_OUTP, S3C2410_GPF4_OUTP, S3C2410_GPF5_OUTP, S3C2410_GPF6_OUTP, }; int gpio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ switch(cmd){ case -1: s3c2410_gpio_cfgpin(S3C2410_GPG4, S3C2410_GPG4_OUTP); //关闭LCD背光 s3c2410_gpio_setpin(S3C2410_GPG4, 0); break; case 1: //打开背光 s3c2410_gpio_setpin(S3C2410_GPG4, 1); break; case 2: //开启流水灯 while(1){ for(i = 0; i < 4; i++) s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]); for(i = 0; i < 4; i++){ s3c2410_gpio_setpin(led_table[i],0); msleep(1000); s3c2410_gpio_setpin(led_table[i],1); } } break; case -2: //关闭流水灯 for(i = 0; i < 4; i++){ s3c2410_gpio_setpin(led_table[i],1); } break; case 3: //开启蜂鸣器 s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP); s3c2410_gpio_setpin(S3C2410_GPB0, 1); break; case -3: //关闭蜂鸣器 s3c2410_gpio_setpin(S3C2410_GPB0, 0); break; default: break; } return 0; } int gpio_open(struct inode *inode, struct file *file){ printk("gpio is opened success/n"); return 0; } int gpio_close(struct inode *inode, struct file *file){ printk("gpio is closed success/n"); return 0; } struct file_operations gpio_ops = { .owner = THIS_MODULE, .ioctl = gpio_ioctl, .open = gpio_open, .release = gpio_close, }; static int __init my_gpio_init(void){ int ret; ret = alloc_chrdev_region(&dev_num, MYGPIO_MINOR, 1, MYGPIO_NAME); //分配设备号 if(ret < 0) printk("can't get major number/n"); gpio_cdev = kmalloc(sizeof(struct cdev),GFP_KERNEL); //分配字父设备 if(!gpio_cdev){ return -ENOMEM; goto fail_malloc; } memset(gpio_cdev,0,sizeof(struct cdev)); cdev_init(gpio_cdev, &gpio_ops); //初始化字父设备 cdev_add(gpio_cdev, dev_num, 1); //字符设备注册到系统 gpio_class = class_create(THIS_MODULE,MYGPIO_NAME); //在/sys下建立一个类 if(IS_ERR(gpio_class)){ printk("ERROR: Fail to create gpio_class class/n"); return -1; } device_create(gpio_class,NULL,dev_num,NULL,MYGPIO_NAME); //创建设备节点 printk("gpio_cdev is registered success/n"); fail_malloc: unregister_chrdev_region(dev_num, 1); return 0; } static void __exit my_gpio_exit(void){ unregister_chrdev_region(dev_num, 1); cdev_del(gpio_cdev); kfree(gpio_cdev); device_destroy(gpio_class,dev_num); class_destroy(gpio_class); printk("gpio_cdev is dereigstered success/n"); } module_init(my_gpio_init); module_exit(my_gpio_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("liwanpeng");

测试程序:

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/ioctl.h> int main(){ int fd, cmd; cmd = 0; fd = open("/dev/lwp-gpio",O_RDWR); if(fd < 0){ printf("cannot open /dev/lwp-gpio/n"); exit(1); } while(1){ scanf("%d", &cmd); printf("cmd is %d/n",cmd); ioctl(fd, cmd); } close(fd); return 0; }

你可能感兴趣的:(linux,struct,File,Module,table,Class)