linux字符驱动之点亮LED
上一節中,我們講解了如何自動創建設備節點,這一節我們在上一節的基礎上,實現點亮LED。
上一節文章鏈接:https://blog.csdn.net/qq_37659294/article/details/104308284
?
驅動里面能夠用很多種方法實現LED驅動,其中有本節的字符驅動(最笨的方法)、混雜設備驅動、使用內核GPIO函數接口、使用通用的平臺設備驅動的方法等。但是,不要因為本節是最笨的方法,就不學習了,對于初學者來說,循序漸進的學習是一種好習慣,好了,廢話不多說,直奔主題。
?
問:怎么寫LED驅動程序?
1.搭建一個字符驅動的框架(上一節已經完成)
2.完善硬件的操作
問:驅動里操作硬件寄存器與單片機操作硬件寄存器有什么不一樣的地方?
答:單片機操作的寄存器地址是物理地址,驅動里面操作的必須是虛擬地址,因為驅動是內核的一部分,內核里的地址都是虛擬地址。
問:怎么讓物理地址轉換為虛擬地址?
答:使用ioremap函數,它的功能就是將物理地址映射為虛擬地址,具體怎么映射需要去看linux內存管理等內容。
問:應用程序如果要傳數據給內核怎么辦?
答:使用copy_from_user函數,同理如果內核要傳數據給應用空間的應用程序則使用copy_to_user函數。
?
?
詳細請參考驅動源碼:
#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_createstatic 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");/* LED1,LED2,LED3,LED4對應GPB5、GPB6、GPB7、GPB8* 配置GPB5,6,7,8為輸出*/*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; }/* File operations struct for character device */ 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) {/* 主設備號設置為0表示由系統自動分配主設備號 */major = register_chrdev(0, "first_drv", &first_drv_fops);//創建一個“類”firstdrv_class = class_create(THIS_MODULE, "firstdrv");/* 在“類”里面創建設備* MKDEV(major, 0)指定主設備號為major,次設備號為0(這里的major必須和register_chrdev返回的一致,不然會出錯)*/firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");/* 將物理地址映射為虛擬地址 *///物理地址的起始地址0x56000050,長度16字節gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//gpfdat的物理地址和gpfcon相差4字節,前面我們總共映射了16字節,所以它們的虛擬地址也相差4字節,long型指針+1相當于加四字節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協議應用測試程序源碼:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>/* first_test on* first_test off*/ 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;} /* val是int類型,所以寫入4個字節 */write(fd,&val,4);return 0; }測試步驟:
[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 //裝上驅動程序后自動生成了設備節點/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
若是要單獨控制某個LED燈,當然可以自定義傳入某些數據格式,根據輸入的參數不同來設置我們的寄存器;也可以通過生成多個設備節點,通過次設備號來分別控制,本質上相當于把4個LED看成一個整體,或是把LED1、LED2(都是LED,主設備號相同,次設備號用來區分不同個體)分別看成一個設備。其代碼如下:
//生成4個設備節點,用的是同一個驅動程序,通過次設備號來決定控制哪個燈 #include <linux/module.h> #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 <asm/arch/regs-gpio.h> #include <asm/hardware.h>#define DEVICE_NAME "leds" /* 加載模式后,執行”cat /proc/devices”命令看到的設備名稱 */ #define LED_MAJOR 231 /* 主設備號 */static struct class *leds_class; static struct class_device *leds_class_devs[4];/* bit0<=>D10, 0:亮, 1:滅 * bit1<=>D11, 0:亮, 1:滅 * bit2<=>D12, 0:亮, 1:滅 */ static char leds_status = 0x0; static DECLARE_MUTEX(leds_lock); // 定義賦值//static int minor; static unsigned long gpio_va;#define GPIO_OFT(x) ((x) - 0x56000000) #define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050))) #define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))/* 應用程序對設備文件/dev/leds執行open(...)時,* 就會調用s3c24xx_leds_open函數*/ static int s3c24xx_leds_open(struct inode *inode, struct file *file) {int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);switch(minor){case 0: /* /dev/leds */{// 配置3引腳為輸出//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);GPFCON &= ~(0x3<<(4*2));GPFCON |= (1<<(4*2));//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);GPFCON &= ~(0x3<<(5*2));GPFCON |= (1<<(5*2));//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);GPFCON &= ~(0x3<<(6*2));GPFCON |= (1<<(6*2));// 都輸出0//s3c2410_gpio_setpin(S3C2410_GPF4, 0);GPFDAT &= ~(1<<4);//s3c2410_gpio_setpin(S3C2410_GPF5, 0);GPFDAT &= ~(1<<5);//s3c2410_gpio_setpin(S3C2410_GPF6, 0);GPFDAT &= ~(1<<6);down(&leds_lock);leds_status = 0x0;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);s3c2410_gpio_setpin(S3C2410_GPF4, 0);down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);break;}case 2: /* /dev/led2 */{s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);s3c2410_gpio_setpin(S3C2410_GPF5, 0);leds_status &= ~(1<<1);break;}case 3: /* /dev/led3 */{s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);s3c2410_gpio_setpin(S3C2410_GPF6, 0);down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);break;}}return 0; }static int s3c24xx_leds_read(struct file *filp, char __user *buff, size_t count, loff_t *offp) {int minor = MINOR(filp->f_dentry->d_inode->i_rdev);char val;switch (minor){case 0: /* /dev/leds */{copy_to_user(buff, (const void *)&leds_status, 1); break;}case 1: /* /dev/led1 */{down(&leds_lock);val = leds_status & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 2: /* /dev/led2 */{down(&leds_lock);val = (leds_status>>1) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 3: /* /dev/led3 */{down(&leds_lock);val = (leds_status>>2) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}}return 1; }static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {//int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);int minor = MINOR(file->f_dentry->d_inode->i_rdev);char val;copy_from_user(&val, buf, 1);switch (minor){case 0: /* /dev/leds */{ s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));down(&leds_lock);leds_status = val;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_setpin(S3C2410_GPF4, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<0); up(&leds_lock);}break;}case 2: /* /dev/led2 */{s3c2410_gpio_setpin(S3C2410_GPF5, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<1);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<1); up(&leds_lock);}break;}case 3: /* /dev/led3 */{s3c2410_gpio_setpin(S3C2410_GPF6, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<2); up(&leds_lock);}break;}}return 1; }/* 這個結構是字符設備驅動程序的核心* 當應用程序操作設備文件時所調用的open、read、write等函數,* 最終會調用這個結構中指定的對應函數*/ static struct file_operations s3c24xx_leds_fops = {.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */.open = s3c24xx_leds_open, .read = s3c24xx_leds_read, .write = s3c24xx_leds_write, };/** 執行insmod命令時就會調用這個函數 */ static int __init s3c24xx_leds_init(void) //static int __init init_module(void){int ret;int minor = 0;gpio_va = ioremap(0x56000000, 0x100000);if (!gpio_va) {return -EIO;}/* 注冊字符設備* 參數為主設備號、設備名字、file_operations結構;* 這樣,主設備號就和具體的file_operations結構聯系起來了,* 操作主設備為LED_MAJOR的設備文件時,就會調用s3c24xx_leds_fops中的相關成員函數* LED_MAJOR可以設為0,表示由內核自動分配主設備號*/ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);if (ret < 0) {printk(DEVICE_NAME " can't register major number\n");return ret;}leds_class = class_create(THIS_MODULE, "leds");if (IS_ERR(leds_class))return PTR_ERR(leds_class);/* 在一個“類”里面創建多個設備,說白了就是創建多個設備節點 */leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "leds"); /* /dev/leds */for (minor = 1; minor < 4; minor++) /* /dev/led1,2,3 */{leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);if (unlikely(IS_ERR(leds_class_devs[minor])))return PTR_ERR(leds_class_devs[minor]);}printk(DEVICE_NAME " initialized\n");return 0; }/** 執行rmmod命令時就會調用這個函數 */ static void __exit s3c24xx_leds_exit(void) {int minor;/* 卸載驅動程序 */unregister_chrdev(LED_MAJOR, DEVICE_NAME);for (minor = 0; minor < 4; minor++){class_device_unregister(leds_class_devs[minor]);}class_destroy(leds_class);iounmap(gpio_va); }/* 這兩行指定驅動程序的初始化函數和卸載函數 */ module_init(s3c24xx_leds_init); module_exit(s3c24xx_leds_exit);/* 描述驅動程序的一些信息,不是必須的 */ MODULE_AUTHOR("http://www.100ask.net"); MODULE_VERSION("0.1.0"); MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver"); MODULE_LICENSE("GPL"); #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>/** ledtest <dev> <on|off>*/void print_usage(char *file) {printf("Usage:\n");printf("%s <dev> <on|off>\n",file);printf("eg. \n");printf("%s /dev/leds on\n", file);printf("%s /dev/leds off\n", file);printf("%s /dev/led1 on\n", file);printf("%s /dev/led1 off\n", file); }int main(int argc, char **argv) {int fd;char* filename;char val;if (argc != 3){print_usage(argv[0]);return 0;}filename = argv[1];fd = open(filename, O_RDWR);if (fd < 0){printf("error, can't open %s\n", filename);return 0;}if (!strcmp("on", argv[2])){// 亮燈val = 0;write(fd, &val, 1);}else if (!strcmp("off", argv[2])){// 滅燈val = 1;write(fd, &val, 1);}else{print_usage(argv[0]);return 0;}return 0; }?
本文參考于:https://blog.csdn.net/lwj103862095/article/details/17472455
?
總結
以上是生活随笔為你收集整理的linux字符驱动之点亮LED的全部內容,希望文章能夠幫你解決所遇到的問題。