免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 918 | 回复: 0
打印 上一主题 下一主题

简单的LED驱动模块 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-13 21:34 |只看该作者 |倒序浏览
  自从买了mini2440的板子后没怎么用过,这两天写了一个简单的LED驱动程序模块,并且通过ftp上传到板子上,调式通过,虽然程序简单,但是却熟悉了整个流程,感觉还是学到了些知识。
因为我的板子用的Linux系统的内核是2.6.13的,并且内核中已经带有LED的驱动,所以我首先将/drivers/char目录下的qq2440_leds.c(原先的LED驱动程序)删掉,最好就是做好备份,然后修改/drivers/char目录下的Makefile文件,将此文件中的
obj-$(CONFIG_QQ2440_LEDS) += qq2440_leds.o行注释掉,因为你已经没有这个文件了,所以不能编译进内核。然后交叉编译此内核代码,在编译之前要修改此内核代码中的原配置文件config_n35 找到CONFIG_QQ2440_LEDS行,然后将其修改为CONFIG_QQ2440_LEDS=m ,表明内核支持LED模块加载,这样我们在下面写好驱动模块后可以加载进内核。接下来就是编译内核,使用命令:
Sudo make menuconfig
出现选择配置界面,然后找到下面的选项:
Load an Alternate Configuration File   
选择此选项,输入配置文件,就是上面改的config_n35。
等待。。。
编译成功,将在arch/arm/boot目录下发现新的内核映像文件,将此文件下载到开发板。
    做好上面的工作之后就可以写LED驱动模块了,代码如下:
               
               
                1 #include linux/config.h>
  2 #include linux/module.h>
  3 #include linux/kernel.h>
  4 #include linux/fs.h>
  5 #include linux/init.h>
  6 #include linux/devfs_fs_kernel.h>
  7 #include linux/miscdevice.h>
  8 #include linux/delay.h>
  9 #include asm/irq.h>
10 #include asm/arch/regs-gpio.h>
11 #include asm/hardware.h>
12 #include asm/uaccess.h>
13 #include linux/errno.h>
14
15 #define DEVICE_NAME     "leds"
16 #define LED_MAJOR 231
17 /* GPIO作为LED的引脚 ,通过此可以知道地址*/
18 static unsigned long led_table [] = {
19         S3C2410_GPB5,   /* (32*1) + 5 */
20         S3C2410_GPB6,   /* (32*1) + 6 */
21         S3C2410_GPB7,   /* (32*1) + 7 */
22         S3C2410_GPB8,   /* (32*1) + 8 */
23 };
24 /* GPIO引脚的工作方式,01为输出 */
25 static unsigned int led_cfg_table [] = {
26         S3C2410_GPB5_OUTP,      /* (0x01
27         S3C2410_GPB6_OUTP,      /* (0x01
28         S3C2410_GPB7_OUTP,      /* (0x01
29         S3C2410_GPB8_OUTP,      /* (0x01                    
  30 };
31
32
33
34 static int s3c2440_leds_open(struct inode *inode,struct file *file)
35 {
36         int i;
37         /* 循环设置4个LED引脚,将其设为输出方式*/
38         for (i = 0; i  4; i++) {
39                 s3c2410_gpio_cfgpin(led_table, led_cfg_table);
40         }
41
42         return 0;
43 }
44
45 static int s3c2440_leds_ioctl(
46                 struct inode *inode,
47                 struct file *file,
48                 unsigned int cmd,
49                 unsigned long arg)
50 {
51         switch(cmd) {
52                 case 0:
53                 case 1:
54                         if (arg > 4) {
55                                 return -EINVAL;
56                         }
57                         s3c2410_gpio_setpin(led_table[arg], !cmd);
58                         return 0;
59                 default:
60                         return -EINVAL;
61         }
62 }
63
64 static int s3c2440_leds_read(
65                 struct file *filp,
66                 char __user *buff,
67                 size_t count,
68                 loff_t *offp)
69 {
70         /* 读取寄存器中的值返回 */
71         char temp[4];
72         int i;
73
74         for (i = 0; i  4; i++) {
75                 if (s3c2410_gpio_getpin(led_table)) /*读取引脚的状态*/
76                         temp = '1';
77                 else
78                         temp = '0';
79         }
80         
81         if (copy_to_user(buff, (void *)temp, 4))
82                 return -EFAULT;
83
84         return 4;
85 }
86
87 static int s3c2440_leds_write(
88                 struct file *filp,
89                 const char __user *buff,
90                 size_t count,
91                 loff_t *offp)
92 {
93         /* 向寄存器中写入传入的值 */
94         char temp[5];
95         int i;
96
97         if (copy_from_user(temp, buff, 5))
98                 return -EFAULT;
99         for (i = 0; i  4; i++) {    /* 设置引脚的状态 */
100                 if (temp != '0')
101                         s3c2410_gpio_setpin(led_table, 1);
102                 else
103                         s3c2410_gpio_setpin(led_table, 0);
104         }
105         return count;
106 }
107
108 static struct file_operations s3c2440_leds_fops = {
109         .owner  =       THIS_MODULE,
110         .ioctl  =       s3c2440_leds_ioctl,
111         .open   =       s3c2440_leds_open,
112         .read   =       s3c2440_leds_read,
113         .write  =       s3c2440_leds_write
114 };
115
116 static int __init s3c2440_leds_init(void)
117 {
118         int ret;
119         
120         ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c2440_leds_fops);
121         if (ret  0) {
122                 printk(DEVICE_NAME " can't register major number\n");
123                 return ret;
124         }
125         
126         devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
127         printk(DEVICE_NAME " initialized\n");
128
129         return 0;
130 }        
131
132 static void __exit s3c2440_leds_exit(void)
133 {
134         devfs_remove(DEVICE_NAME);
135         unregister_chrdev(LED_MAJOR, DEVICE_NAME);
136 }
137
138 module_init(s3c2440_leds_init);
139 module_exit(s3c2440_leds_exit);
140 MODULE_LICENSE("Dual BSD/GPL");      
编译模块的Makefile和测试程序上传至此

       
        文件:led.zip
        大小:2KB
        下载:
下载
       
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/74234/showart_1963874.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP