日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux字符驱动之点亮LED

發(fā)布時(shí)間:2023/11/30 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux字符驱动之点亮LED 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一節(jié)中,我們講解了如何自動(dòng)創(chuàng)建設(shè)備節(jié)點(diǎn),這一節(jié)我們在上一節(jié)的基礎(chǔ)上,實(shí)現(xiàn)點(diǎn)亮LED。

上一節(jié)文章鏈接:https://blog.csdn.net/qq_37659294/article/details/104308284

?

驅(qū)動(dòng)里面能夠用很多種方法實(shí)現(xiàn)LED驅(qū)動(dòng),其中有本節(jié)的字符驅(qū)動(dòng)(最笨的方法)、混雜設(shè)備驅(qū)動(dòng)、使用內(nèi)核GPIO函數(shù)接口、使用通用的平臺(tái)設(shè)備驅(qū)動(dòng)的方法等。但是,不要因?yàn)楸竟?jié)是最笨的方法,就不學(xué)習(xí)了,對(duì)于初學(xué)者來說,循序漸進(jìn)的學(xué)習(xí)是一種好習(xí)慣,好了,廢話不多說,直奔主題。

?

問:怎么寫LED驅(qū)動(dòng)程序?

1.搭建一個(gè)字符驅(qū)動(dòng)的框架(上一節(jié)已經(jīng)完成)

2.完善硬件的操作

問:驅(qū)動(dòng)里操作硬件寄存器與單片機(jī)操作硬件寄存器有什么不一樣的地方?

答:單片機(jī)操作的寄存器地址是物理地址,驅(qū)動(dòng)里面操作的必須是虛擬地址,因?yàn)轵?qū)動(dòng)是內(nèi)核的一部分,內(nèi)核里的地址都是虛擬地址。

問:怎么讓物理地址轉(zhuǎn)換為虛擬地址?

答:使用ioremap函數(shù),它的功能就是將物理地址映射為虛擬地址,具體怎么映射需要去看linux內(nèi)存管理等內(nèi)容。

問:應(yīng)用程序如果要傳數(shù)據(jù)給內(nèi)核怎么辦?

答:使用copy_from_user函數(shù),同理如果內(nèi)核要傳數(shù)據(jù)給應(yīng)用空間的應(yīng)用程序則使用copy_to_user函數(shù)。
?

?

詳細(xì)請參考驅(qū)動(dòng)源碼:

#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對(duì)應(yīng)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");//拷貝用戶空間的數(shù)據(jù)到內(nèi)核空間copy_from_user(&val, buffer, count);if (val == 1){// 點(diǎn)燈*gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));//跟單片機(jī)操作寄存器一樣}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, };/* 驅(qū)動(dòng)入口函數(shù) */ static int first_drv_init(void) {/* 主設(shè)備號(hào)設(shè)置為0表示由系統(tǒng)自動(dòng)分配主設(shè)備號(hào) */major = register_chrdev(0, "first_drv", &first_drv_fops);//創(chuàng)建一個(gè)“類”firstdrv_class = class_create(THIS_MODULE, "firstdrv");/* 在“類”里面創(chuàng)建設(shè)備* MKDEV(major, 0)指定主設(shè)備號(hào)為major,次設(shè)備號(hào)為0(這里的major必須和register_chrdev返回的一致,不然會(huì)出錯(cuò))*/firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");/* 將物理地址映射為虛擬地址 *///物理地址的起始地址0x56000050,長度16字節(jié)gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//gpfdat的物理地址和gpfcon相差4字節(jié),前面我們總共映射了16字節(jié),所以它們的虛擬地址也相差4字節(jié),long型指針+1相當(dāng)于加四字節(jié)gpbdat = gpbcon + 1;return 0; }/* 驅(qū)動(dòng)出口函數(shù) */ static void first_drv_exit(void) {unregister_chrdev(major, "first_drv");device_unregister(firstdrv_device); //卸載類下的設(shè)備class_destroy(firstdrv_class); //卸載類iounmap(gpbcon); //解除映射 }module_init(first_drv_init); //用于修飾入口函數(shù) module_exit(first_drv_exit); //用于修飾出口函數(shù) MODULE_AUTHOR("LWJ"); MODULE_DESCRIPTION("Just for Demon"); MODULE_LICENSE("GPL"); //遵循GPL協(xié)議

應(yīng)用測試程序源碼:

#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個(gè)字節(jié) */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 //還沒有設(shè)備節(jié)點(diǎn) 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 //裝上驅(qū)動(dòng)程序后自動(dòng)生成了設(shè)備節(jié)點(diǎn)/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]#

可發(fā)現(xiàn),當(dāng)執(zhí)行下面語句時(shí),開發(fā)板上的4個(gè)LED同時(shí)被熄滅:

[WJ2440]# ./first_test off

可發(fā)現(xiàn),當(dāng)執(zhí)行下面語句時(shí),開發(fā)板上的4個(gè)LED同時(shí)被點(diǎn)亮:

[WJ2440]# ./first_test on

若是要單獨(dú)控制某個(gè)LED燈,當(dāng)然可以自定義傳入某些數(shù)據(jù)格式,根據(jù)輸入的參數(shù)不同來設(shè)置我們的寄存器;也可以通過生成多個(gè)設(shè)備節(jié)點(diǎn),通過次設(shè)備號(hào)來分別控制,本質(zhì)上相當(dāng)于把4個(gè)LED看成一個(gè)整體,或是把LED1、LED2(都是LED,主設(shè)備號(hào)相同,次設(shè)備號(hào)用來區(qū)分不同個(gè)體)分別看成一個(gè)設(shè)備。其代碼如下:

//生成4個(gè)設(shè)備節(jié)點(diǎn),用的是同一個(gè)驅(qū)動(dòng)程序,通過次設(shè)備號(hào)來決定控制哪個(gè)燈 #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" /* 加載模式后,執(zhí)行”cat /proc/devices”命令看到的設(shè)備名稱 */ #define LED_MAJOR 231 /* 主設(shè)備號(hào) */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)))/* 應(yīng)用程序?qū)υO(shè)備文件/dev/leds執(zhí)行open(...)時(shí),* 就會(huì)調(diào)用s3c24xx_leds_open函數(shù)*/ 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; }/* 這個(gè)結(jié)構(gòu)是字符設(shè)備驅(qū)動(dòng)程序的核心* 當(dāng)應(yīng)用程序操作設(shè)備文件時(shí)所調(diào)用的open、read、write等函數(shù),* 最終會(huì)調(diào)用這個(gè)結(jié)構(gòu)中指定的對(duì)應(yīng)函數(shù)*/ static struct file_operations s3c24xx_leds_fops = {.owner = THIS_MODULE, /* 這是一個(gè)宏,推向編譯模塊時(shí)自動(dòng)創(chuàng)建的__this_module變量 */.open = s3c24xx_leds_open, .read = s3c24xx_leds_read, .write = s3c24xx_leds_write, };/** 執(zhí)行insmod命令時(shí)就會(huì)調(diào)用這個(gè)函數(shù) */ 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;}/* 注冊字符設(shè)備* 參數(shù)為主設(shè)備號(hào)、設(shè)備名字、file_operations結(jié)構(gòu);* 這樣,主設(shè)備號(hào)就和具體的file_operations結(jié)構(gòu)聯(lián)系起來了,* 操作主設(shè)備為LED_MAJOR的設(shè)備文件時(shí),就會(huì)調(diào)用s3c24xx_leds_fops中的相關(guān)成員函數(shù)* LED_MAJOR可以設(shè)為0,表示由內(nèi)核自動(dòng)分配主設(shè)備號(hào)*/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);/* 在一個(gè)“類”里面創(chuàng)建多個(gè)設(shè)備,說白了就是創(chuàng)建多個(gè)設(shè)備節(jié)點(diǎn) */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; }/** 執(zhí)行rmmod命令時(shí)就會(huì)調(diào)用這個(gè)函數(shù) */ static void __exit s3c24xx_leds_exit(void) {int minor;/* 卸載驅(qū)動(dòng)程序 */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); }/* 這兩行指定驅(qū)動(dòng)程序的初始化函數(shù)和卸載函數(shù) */ module_init(s3c24xx_leds_init); module_exit(s3c24xx_leds_exit);/* 描述驅(qū)動(dòng)程序的一些信息,不是必須的 */ 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

?

總結(jié)

以上是生活随笔為你收集整理的linux字符驱动之点亮LED的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。