首页 > 学院 > 开发设计 > 正文

小小的led驱动和测试程序

2019-11-06 09:23:46
字体:
来源:转载
供稿:网友

使用的板子为gec210的板子

#include <linux/kernel.h>

#include <linux/module.h>#include <linux/init.h>#include <linux/device.h>#include <linux/cdev.h>#include <linux/fs.h>#include <mach/gpio.h>#include <mach/regs-gpio.h>#include <plat/gpio-cfg.h>#include <linux/delay.h>

#include <linux/io.h>

#define LEDCON 0xe0200280  //led控制寄存器的物理地址#define LEDDAT 0xe0200284  //led数据寄存器的物理地址#define LED_MAGIC 'm'#define LED_ON _IO(LED_MAGIC,0)#define LED_OFF _IO(LED_MAGIC,1)unsigned int *led_config;unsigned int *led_data;struct cdev cdev;dev_t devno;#define DRIVER_NAME "my_led_driver"struct class *my_class;static int led_open(struct inode *inode,struct file *file){led_config=ioremap(LEDCON,4);writel(0x00001111,led_config);led_data=ioremap(LEDDAT,4);return 0;}static int led_close(struct inode *inode,struct file *file){iounmap(led_config);iounmap(led_data);return 0;}long led_ioctl (struct file *file, unsigned int cmd, unsigned long arg){switch(cmd){case LED_ON:writel(0x00,led_data);break;case LED_OFF:writel(0x0f,led_data);break;default:return -EINVAL;}return 0;}static struct file_Operations led_fops ={.owner = THIS_MODULE,.open = led_open,.release = led_close,.unlocked_ioctl =led_ioctl,};static int led_init(void){cdev_init(&cdev,&led_fops);alloc_chrdev_region(&devno, 0 , 1 , "myled");cdev_add(&cdev, devno, 1);my_class = class_create(THIS_MODULE, "led_class");if(IS_ERR(my_class)){PRintk("Err: failed in creating class./n");return -1;}device_create(my_class, NULL, devno,NULL,"led_driver");return 0;}static void led_exit(void){device_destroy(my_class,devno);class_destroy(my_class);cdev_del(&cdev);unregister_chrdev_region(devno,1);}module_init(led_init);module_exit(led_exit);MODULE_LICENSE("GPL");

MODULE_AUTHOR("EIGHT&FIVE");

测试程序如下

#include<stdio.h>#include<sys/ioctl.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#define LED_MAGIC 'm'#define LED_ON _IO(LED_MAGIC,0)#define LED_OFF _IO(LED_MAGIC,1)int main(int argv,char **argc){int fd,i;fd=open("/dev/led_driver",0x666);while(1){scanf("%d",&i);switch(i){case 1 :ioctl(fd,LED_ON,NULL);break;case 2:ioctl(fd,LED_OFF,NULL);break;default:break;}}close(fd);return 0;}

测试已成功。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表