天天看点

嵌入式Linux LED小灯点亮实验

问:怎么写LED驱动程序?

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

2.完善硬件的操作

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

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

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

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

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

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

详细请参考驱动源码:

[cpp]  view plain  copy    print?

嵌入式Linux LED小灯点亮实验
嵌入式Linux LED小灯点亮实验
  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. static struct class *firstdrv_class;  
  11. static struct device *firstdrv_device;  
  12. volatile unsigned long *gpbcon = NULL;  
  13. volatile unsigned long *gpbdat = NULL;  
  14. int major;  
  15. static int first_drv_open(struct inode * inode, struct file * filp)  
  16. {  
  17.     printk("first_drv_open\n");  
  18.     *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2)));  
  19.     *gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2)));  
  20.     return 0;  
  21. }  
  22. static int first_drv_write(struct file * file, const char __user * buffer, size_t count, loff_t * ppos)  
  23. {  
  24.     int val;  
  25.     printk("first_drv_write\n");  
  26.     copy_from_user(&val, buffer, count);  
  27.     if (val == 1)  
  28.     {  
  29.         // 点灯  
  30.         *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));  
  31.     }  
  32.     else  
  33.     {  
  34.         // 灭灯  
  35.         *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);  
  36.     }  
  37.     return 0;  
  38. }  
  39. static const struct file_operations first_drv_fops = {  
  40.     .owner      = THIS_MODULE,  
  41.     .open       = first_drv_open,  
  42.     .write      = first_drv_write,  
  43. };  
  44. static int first_drv_init(void)  
  45. {  
  46.     major = register_chrdev(0, "first_drv", &first_drv_fops);  
  47.     firstdrv_class = class_create(THIS_MODULE, "firstdrv");  
  48.     firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");  
  49.     gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);  
  50.     gpbdat = gpbcon + 1;  
  51.     return 0;  
  52. }  
  53. static void first_drv_exit(void)  
  54. {  
  55.     unregister_chrdev(major, "first_drv");  
  56.     device_unregister(firstdrv_device);  //卸载类下的设备  
  57.     class_destroy(firstdrv_class);      //卸载类  
  58.     iounmap(gpbcon);                    //解除映射  
  59. }  
  60. module_init(first_drv_init);  //用于修饰入口函数  
  61. module_exit(first_drv_exit);  //用于修饰出口函数      
  62. MODULE_AUTHOR("LWJ");  
  63. MODULE_DESCRIPTION("Just for Demon");  
  64. MODULE_LICENSE("GPL");  //遵循GPL协议  

应用测试程序源码:

[cpp]  view plain  copy    print?

嵌入式Linux LED小灯点亮实验
嵌入式Linux LED小灯点亮实验
  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. int main(int argc ,char *argv[])  
  8. {  
  9.     int fd;  
  10.     int val = 0;  
  11.     fd = open("/dev/xxx",O_RDWR);  
  12.     if (fd < 0)  
  13.     {  
  14.         printf("open error\n");  
  15.     }  
  16.     if (argc != 2)  
  17.     {  
  18.         printf("Usage:\n");  
  19.         printf("%s <on|off>\n",argv[0]);  
  20.         return 0;  
  21.     }  
  22.     if(strncmp(argv[1],"on",2) == 0)  
  23.     {  
  24.         val = 1;  
  25.     }  
  26.     else if (strncmp(argv[1],"off",3) == 0)  
  27.     {  
  28.         val = 0;  
  29.     }   
  30.     write(fd,&val,4);  
  31.     return 0;  
  32. }  

测试步骤:

[cpp]  view plain  copy    print?

嵌入式Linux LED小灯点亮实验
嵌入式Linux LED小灯点亮实验
  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