問:怎麼寫LED驅動程式?
1.搭建一個字元驅動的架構(上一節已經完成)
2.完善硬體的操作
問:驅動裡操作硬體寄存器與單片機操作硬體寄存器有什麼不一樣的地方?
答:單片機操作的寄存器位址是實體位址,驅動裡面操作的必須是虛拟位址,因為驅動是核心的一部分,核心裡的位址都是虛拟位址。
問:怎麼讓實體位址轉換為虛拟位址?
答:使用ioremap函數,它的功能就是将實體位址映射為虛拟位址,具體怎麼映射需要去看linux記憶體管理等内容。
問:應用程式如果要傳資料給核心怎麼辦?
答:使用copy_from_user函數,同理如果核心要傳資料給應用空間的應用程式則使用copy_to_user函數。
詳細請參考驅動源碼:
[cpp] view plain copy print?

- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- #include <asm/uaccess.h>
- #include <asm/irq.h>
- #include <asm/io.h>
- #include <linux/module.h>
- #include <linux/device.h> //class_create
- static struct class *firstdrv_class;
- static struct device *firstdrv_device;
- volatile unsigned long *gpbcon = NULL;
- volatile unsigned long *gpbdat = NULL;
- int major;
- static int first_drv_open(struct inode * inode, struct file * filp)
- {
- printk("first_drv_open\n");
- *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2)));
- *gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2)));
- return 0;
- }
- static int first_drv_write(struct file * file, const char __user * buffer, size_t count, loff_t * ppos)
- {
- int val;
- printk("first_drv_write\n");
- copy_from_user(&val, buffer, count);
- if (val == 1)
- {
- // 點燈
- *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));
- }
- else
- {
- // 滅燈
- *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);
- }
- return 0;
- }
- static const struct file_operations first_drv_fops = {
- .owner = THIS_MODULE,
- .open = first_drv_open,
- .write = first_drv_write,
- };
- static int first_drv_init(void)
- {
- major = register_chrdev(0, "first_drv", &first_drv_fops);
- firstdrv_class = class_create(THIS_MODULE, "firstdrv");
- firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");
- gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);
- gpbdat = gpbcon + 1;
- return 0;
- }
- static void first_drv_exit(void)
- {
- unregister_chrdev(major, "first_drv");
- device_unregister(firstdrv_device); //解除安裝類下的裝置
- class_destroy(firstdrv_class); //解除安裝類
- iounmap(gpbcon); //解除映射
- }
- module_init(first_drv_init); //用于修飾入口函數
- module_exit(first_drv_exit); //用于修飾出口函數
- MODULE_AUTHOR("LWJ");
- MODULE_DESCRIPTION("Just for Demon");
- MODULE_LICENSE("GPL"); //遵循GPL協定
應用測試程式源碼:
[cpp] view plain copy print?

- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- int main(int argc ,char *argv[])
- {
- int fd;
- int val = 0;
- fd = open("/dev/xxx",O_RDWR);
- if (fd < 0)
- {
- printf("open error\n");
- }
- if (argc != 2)
- {
- printf("Usage:\n");
- printf("%s <on|off>\n",argv[0]);
- return 0;
- }
- if(strncmp(argv[1],"on",2) == 0)
- {
- val = 1;
- }
- else if (strncmp(argv[1],"off",3) == 0)
- {
- val = 0;
- }
- write(fd,&val,4);
- return 0;
- }
測試步驟:
[cpp] view plain copy print?

- [WJ2440]# ls
- Qt driver_test lib root udisk
- TQLedtest etc linuxrc sbin usr
- app_test first_drv.ko mnt sddisk var
- bin first_test opt sys web
- dev home proc tmp
- [WJ2440]# ls -l /dev/xxx
- ls: /dev/xxx: No such file or directory
- [WJ2440]# insmod first_drv.ko
- [WJ2440]# lsmod
- first_drv 2300 0 - Live 0xbf003000
- [WJ2440]# ls -l /dev/xxx
- crw-rw---- 1 root root 252, 0 Jan 2 00:23 /dev/xxx
- [WJ2440]# ./first_test
- first_drv_open
- Usage:
- ./first_test <on|off>
- [WJ2440]# ./first_test off
- first_drv_open
- first_drv_write
- [WJ2440]# ./first_test on
- first_drv_open
- first_drv_write
- [WJ2440]#
可發現,當執行下面語句時,開發闆上的4個LED同時被熄滅:
[WJ2440]# ./first_test off
可發現,當執行下面語句時,開發闆上的4個LED同時被點亮:
[WJ2440]# ./first_test on
轉載于:https://www.cnblogs.com/LiuDaohui0805/p/5270862.html