linux-2.6.32在mini2440开发板上移植(16)之LED 驱动程序移植
发布日期:2021-06-30 18:58:11 浏览次数:4 分类:技术文章

本文共 4622 字,大约阅读时间需要 15 分钟。

LED 驱动程序移植

编者;对于led的驱动程序,很多文章都有详细的介绍,我的博客里面有一篇专门详解这个的。需要看的,可以找下。led灯的驱动其实就代表了I/O口的驱动。在linux系统下,操作一个I/O口,可以说实在是麻烦至极与裸机操作相比较的话。这里简介移植过程,没写分析。

1 LED 驱动原理

这个就给个图就够了,搞驱动要连这个都搞不懂,那就完了。

 

2、驱动的移植。

在drivers/char 目录下,我们建立一个驱动程序文件mini2440_leds.c,内容如下:

[cpp] 
  1. <span style="font-size:18px;">#include <linux/miscdevice.h>  
  2. #include <linux/delay.h>  
  3. #include <asm/irq.h>  
  4. #include <mach/regs-gpio.h>  
  5. #include <mach/hardware.h>  
  6. #include <linux/kernel.h>  
  7. #include <linux/module.h>  
  8. #include <linux/init.h>  
  9. #include <linux/mm.h>  
  10. #include <linux/fs.h>  
  11. #include <linux/types.h>  
  12. #include <linux/delay.h>  
  13. #include <linux/moduleparam.h>  
  14. #include <linux/slab.h>  
  15. #include <linux/errno.h>  
  16. #include <linux/ioctl.h>  
  17. #include <linux/cdev.h>  
  18. #include <linux/string.h>  
  19. #include <linux/list.h>  
  20. #include <linux/pci.h>  
  21. #include <linux/gpio.h>  
  22. #include <asm/uaccess.h>  
  23. #include <asm/atomic.h>  
  24. #include <asm/unistd.h>  
  25. #define DEVICE_NAME "leds" <span style="color:#3366ff;">//设备名(/dev/leds</span>)  
  26. <span style="color:#3366ff;">//LED 对应的GPIO 端口列表  
  27. </span>static unsigned long led_table [] = {  
  28. S3C2410_GPB(5),  
  29. S3C2410_GPB(6),  
  30. S3C2410_GPB(7),  
  31. S3C2410_GPB(8),  
  32. };  
  33. <span style="color:#3366ff;">//LED 对应端口将要输出的状态列表  
  34. </span>static unsigned int led_cfg_table [] = {  
  35. S3C2410_GPIO_OUTPUT,  
  36. S3C2410_GPIO_OUTPUT,  
  37. S3C2410_GPIO_OUTPUT,  
  38. S3C2410_GPIO_OUTPUT,  
  39. };  
  40. <span style="color:#3366ff;">/*ioctl 函数的实现 
  41. * 在应用/用户层将通过ioctl 函数向内核传递参数,以控制LED 的输出状态 
  42. */  
  43. </span>static int sbc2440_leds_ioctl(  
  44. struct inode *inode,  
  45. struct file *file,  
  46. unsigned int cmd,  
  47. unsigned long arg)  
  48. {  
  49. switch(cmd) {  
  50. case 0:  
  51. case 1:  
  52. if (arg > 4) {  
  53. return -EINVAL;  
  54. }  
  55. <span style="color:#3366ff;">//根据应用/用户层传递来的参数(取反),通过s3c2410_gpio_setpin 函数设置LED 对应的端口寄存  
  56. 器,</span>  
  57. s3c2410_gpio_setpin(led_table[arg], !cmd);  
  58. return 0;  
  59. default:  
  60. return -EINVAL;  
  61. }  
  62. }  
  63. <span style="color:#3366ff;">/* 
  64. * 设备函数操作集,在此只有ioctl 函数,通常还有read, write, open, close 等,因为本LED 驱动在下面已经 
  65. * 注册为misc 设备,因此也可以不用open/close 
  66. */  
  67. </span>static struct file_operations dev_fops = {  
  68. .owner = THIS_MODULE,  
  69. .ioctl = sbc2440_leds_ioctl,  
  70. };  
  71. <span style="color:#3366ff;BACKGROUND-COLOR: #ffffff">/* 
  72. * 把LED 驱动注册为MISC 设备 
  73. */  
  74. </span>static struct miscdevice misc = {  
  75. .minor = MISC_DYNAMIC_MINOR, //动态设备号  
  76. .name = DEVICE_NAME,  
  77. .fops = &dev_fops,  
  78. };  
  79. <span style="color:#3366ff;">/* 
  80. * 设备初始化 
  81. */  
  82. </span>static int __init dev_init(void)  
  83. {  
  84. int ret;  
  85. int i;  
  86. for (i = 0; i < 4; i++) {  
  87. <span style="color:#3366ff;">//设置LED 对应的端口寄存器为输出(OUTPUT)  
  88. </span>s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);  
  89. <span style="color:#3366ff;">//设置LED 对应的端口寄存器为低电平输出,在模块加载结束后,四个LED 应该是全部都是发光  
  90. 状态</span>  
  91. s3c2410_gpio_setpin(led_table[i], 0);  
  92. }  
  93. ret = misc_register(&misc); <span style="color:#3366ff;">//注册设备</span>  
  94. printk (DEVICE_NAME"\tinitialized\n"); <span style="color:#3366ff;">//打印初始化信息</span>  
  95. return ret;  
  96. }  
  97. static void __exit dev_exit(void)  
  98. {  
  99. misc_deregister(&misc);  
  100. }  
  101. module_init(dev_init); <span style="color:#3333ff;">//模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用  
  102. </span>module_exit(dev_exit<span style="BACKGROUND-COLOR: #ffffff">);<span style="color:#3366ff;">//卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将调用此函  
  103. </span></span><span style="BACKGROUND-COLOR: #3366ff">数  
  104. </span>MODULE_LICENSE("GPL"); <span style="color:#3366ff;">//版权信息  
  105. </span>MODULE_AUTHOR("FriendlyARM Inc."); <span style="color:#3366ff;BACKGROUND-COLOR: #ffffff">//开发者信息</span></span>  

接下来,我们添加LED 设备的内核配置选项,打开drivers/char/Kconfig 文件,添加如下红色部分内容:

config DEVKMEM
          bool "/dev/kmem virtual device support"
          default y
          help
             Say Y here if you want to support the /dev/kmem device. The
             /dev/kmem device is rarely used, but can be used for certain
             kind of kernel debugging operations.
             When in doubt, say "N".
config LEDS_MINI2440
           tristate "LED Support for Mini2440 GPIO LEDs"
           depends on MACH_MINI2440
           default y if MACH_MINI2440

           help

                  This option enables support for LEDs connected to GPIO lines
                   on Mini2440 boards.
config MINI2440_ADC
           bool "ADC driver for FriendlyARM Mini2440 development boards"
           depends on MACH_MINI2440
           default y if MACH_MINI2440
           help
                this is ADC driver for FriendlyARM Mini2440 development boards
                Notes: the touch-screen-driver required this option
接下来,再根据该驱动的配置定义,把对应的驱动目标文件加入内核中,打开linux-2.6.32.2/drivers/char/Makefile 文件,添加如下红色部分内容:
obj-$(CONFIG_JS_RTC) += js-rtc.o
js-rtc-y = rtc.o
obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o
obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o
# Files generated that shall be removed upon make clean
clean-files := consolemap_deftbl.c defkeymap.c
这样,我们就在内核中添加做好了LED 驱动

3 配置编译新内核并测试LED
接上面的步骤,在内核源代码目录下执行:make menuconfig 重新配置内核,依次选择进入如下子菜单项:
Device Drivers --->
        Character devices --->
进入LED 驱动配置菜单,进行内核配置。
在内核源代码根目录下执行;make zImage,把生成的新内核烧写到开发板中。

3 测试LED

用自带的文件系统,启动后就会运行一个led程序。测试结果如图。

转载地址:https://linus.blog.csdn.net/article/details/8904453 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LCD驱动移植(一)
下一篇:安霸SPI 剖析

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月25日 21时00分03秒