天天看點

嵌入式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