嵌入式Linux LED小灯点亮实验

问:怎么写LED驱动程序?

1.搭建一个字符驱动的框架(上一节已经完成)

2.完善硬件的操作

问:驱动里操作硬件寄存器与单片机操作硬件寄存器有什么不一样的地方?

答:单片机操作的寄存器地址是物理地址,驱动里面操作的必须是虚拟地址,因为驱动是内核的一部分,内核里的地址都是虚拟地址。

问:怎么让物理地址转换为虚拟地址?

答:使用ioremap函数,它的功能就是将物理地址映射为虚拟地址,具体怎么映射需要去看linux内存管理等内容。

问:应用程序如果要传数据给内核怎么办?

答:使用copy_from_user函数,同理如果内核要传数据给应用空间的应用程序则使用copy_to_user函数。

详细请参考驱动源码:

[cpp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. #include <linux/kernel.h>  
  2. #include <linux/fs.h>  
  3. #include <linux/init.h>  
  4. #include <linux/delay.h>  
  5. #include <asm/uaccess.h>  
  6. #include <asm/irq.h>  
  7. #include <asm/io.h>  
  8. #include <linux/module.h>  
  9. #include <linux/device.h>     //class_create  
  10.   
  11. static struct class *firstdrv_class;  
  12. static struct device *firstdrv_device;  
  13.   
  14. volatile unsigned long *gpbcon = NULL;  
  15. volatile unsigned long *gpbdat = NULL;  
  16.   
  17. int major;  
  18. static int first_drv_open(struct inode * inode, struct file * filp)  
  19. {  
  20.     printk("first_drv_open ");  
  21.   
  22.     /*  LED1,LED2,LED3,LED4对应GPB5、GPB6、GPB7、GPB8 
  23.      *  配置GPB5,6,7,8为输出 
  24.      */  
  25.     *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2)));  
  26.     *gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2)));  
  27.     return 0;  
  28. }  
  29. static int first_drv_write(struct file * file, const char __user * buffer, size_t count, loff_t * ppos)  
  30. {  
  31.     int val;  
  32.     printk("first_drv_write ");  
  33.   
  34.     copy_from_user(&val, buffer, count);  
  35.   
  36.     if (val == 1)  
  37.     {  
  38.         // 点灯  
  39.         *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));  
  40.     }  
  41.     else  
  42.     {  
  43.         // 灭灯  
  44.         *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);  
  45.     }  
  46.     return 0;  
  47. }  
  48.   
  49. /* File operations struct for character device */  
  50. static const struct file_operations first_drv_fops = {  
  51.     .owner      = THIS_MODULE,  
  52.     .open       = first_drv_open,  
  53.     .write      = first_drv_write,  
  54. };  
  55.   
  56. /* 驱动入口函数 */  
  57. static int first_drv_init(void)  
  58. {  
  59.     /* 主设备号设置为0表示由系统自动分配主设备号 */  
  60.     major = register_chrdev(0, "first_drv", &first_drv_fops);  
  61.   
  62.     /* 创建firstdrv类 */  
  63.     firstdrv_class = class_create(THIS_MODULE, "firstdrv");  
  64.   
  65.     /* 在firstdrv类下创建xxx设备,供应用程序打开设备*/  
  66.     firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");  
  67.   
  68.     /* 将物理地址映射为虚拟地址 */  
  69.     gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);  
  70.     gpbdat = gpbcon + 1;  
  71.       
  72.     return 0;  
  73. }  
  74.   
  75. /* 驱动出口函数 */  
  76. static void first_drv_exit(void)  
  77. {  
  78.     unregister_chrdev(major, "first_drv");  
  79.     device_unregister(firstdrv_device);  //卸载类下的设备  
  80.     class_destroy(firstdrv_class);      //卸载类  
  81.     iounmap(gpbcon);                    //解除映射  
  82. }  
  83.   
  84. module_init(first_drv_init);  //用于修饰入口函数  
  85. module_exit(first_drv_exit);  //用于修饰出口函数      
  86.   
  87. MODULE_AUTHOR("LWJ");  
  88. MODULE_DESCRIPTION("Just for Demon");  
  89. MODULE_LICENSE("GPL");  //遵循GPL协议  


应用测试程序源码:

[cpp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #include <unistd.h>  
  6. #include <string.h>  
  7.   
  8. /* first_test on 
  9.  * first_test off 
  10.  */   
  11. int main(int argc ,char *argv[])  
  12.   
  13. {  
  14.     int fd;  
  15.     int val = 0;  
  16.     fd = open("/dev/xxx",O_RDWR);  
  17.     if (fd < 0)  
  18.     {  
  19.         printf("open error ");  
  20.     }  
  21.       
  22.     if (argc != 2)  
  23.     {  
  24.         printf("Usage: ");  
  25.         printf("%s <on|off> ",argv[0]);  
  26.         return 0;  
  27.     }  
  28.     if(strncmp(argv[1],"on",2) == 0)  
  29.     {  
  30.         val = 1;  
  31.     }  
  32.     else if (strncmp(argv[1],"off",3) == 0)  
  33.     {  
  34.         val = 0;  
  35.     }   
  36.     write(fd,&val,4);  
  37.     return 0;  
  38. }  

测试步骤:

[cpp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. [WJ2440]# ls  
  2. Qt            driver_test   lib           root          udisk  
  3. TQLedtest     etc           linuxrc       sbin          usr  
  4. app_test      first_drv.ko  mnt           sddisk        var  
  5. bin           first_test    opt           sys           web  
  6. dev           home          proc          tmp  
  7. [WJ2440]# ls -l /dev/xxx  
  8. ls: /dev/xxx: No such file or directory  
  9. [WJ2440]# insmod first_drv.ko   
  10. [WJ2440]# lsmod   
  11. first_drv 2300 0 - Live 0xbf003000  
  12. [WJ2440]# ls -l /dev/xxx  
  13. crw-rw----    1 root     root      252,   0 Jan  2 00:23 /dev/xxx  
  14. [WJ2440]# ./first_test   
  15. first_drv_open  
  16. Usage:  
  17. ./first_test <on|off>  
  18. [WJ2440]# ./first_test off  
  19. first_drv_open  
  20. first_drv_write  
  21. [WJ2440]# ./first_test on   
  22. first_drv_open  
  23. first_drv_write  
  24. [WJ2440]#   


可发现,当执行下面语句时,开发板上的4个LED同时被熄灭:

[WJ2440]# ./first_test off

可发现,当执行下面语句时,开发板上的4个LED同时被点亮:

[WJ2440]# ./first_test on

原文地址:https://www.cnblogs.com/LiuDaohui0805/p/5270862.html