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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符设备驱动笔记(一)

發(fā)布時間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符设备驱动笔记(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.調(diào)用關(guān)系
?? ??? ???? 應(yīng)用程序:open ?? ??? ?read ?? ??? ?write?? ?
應(yīng)用層?? ?-----------------------------------------
?? ??? ??? ? ??? ??? ??? ??? ?c庫?? ??? ?(swi val)
=================================================?? ?
內(nèi)核:?? ?system call interface?? ?
?? ??? ???? (在異常處理函數(shù)里面,根據(jù)發(fā)生中斷的原因,調(diào)用
?? ??? ????? 不同的處理函數(shù))
?? ??? ?-----------------------------------------
?? ??? ???? sys_open sys_read sys_write
?? ??? ???? (VFS Virtual File System)
?? ??? ?-----------------------------------------
?? ??? ???? 驅(qū)動: led_open ?? ??? ?led_read ?? ?led_write?? ?
?? ??? ?-----------------------------------------
?? ??? ??? 硬件: led ?? ?flash ....?? ?
?? ??? ?
? ????
2.驅(qū)動程序框架:
1)寫出led_on led_read
2)通知內(nèi)核
? 1>構(gòu)造file_operations
? ?? ??? ?.open
? ?? ??? ?.write
? ?? ??? ?...
? 2>入口函數(shù)中:register_chrdev
? ?? ??? ??? ????????? unregister_chrdev
? 3>first_drv_init(自定義的入口函數(shù))
? ?? ?first_drv_exit
? 4>module_init修飾入口函數(shù)
? ?? ?module_exit
?
3.?? ?
用戶空間
?? ??? ?APP:?? ?open("/dev/xxx")?? ??? ?read?? ??? ?write
?? ? ??? ?????????? 屬性:c(設(shè)備類型),111(主設(shè)備號)
?? ? ??? ?-----------------------------------------------------
? ?? ??? ??? ??? ??? ??? ?C庫
==============================================================
內(nèi)核?? ??? ??? ??? ??? ?system call interface
?? ??? ?------------------------------------------------------
?? ??? ? ??? ??? ? VFS?? ??? ? ?? ??? ? major作為索引
?? ??? ??? ??? ?————————————————————————
?? ??? ?   | 數(shù)組?? ???? |?? ?|?? ?|...?? ?|file_operations|?? ??? |
?? ??? ??? ??? ?————————————————————————
?? ??? ?------------------------------------------------------
?? ??? ??? ??? ? led_open?? ??? ??? ??? ?led_read?? ?led_write
?? ??? ?------------------------------------------------------
?
VFS根據(jù)打開的文件的屬性:設(shè)備類型、主設(shè)備號,這兩個屬性就能找到
register_chrdev函數(shù)注冊進內(nèi)核的file_operations結(jié)構(gòu)體

4.Makefile

KERN_DIR = /work/system/linux-2.6.22.6all:make -C $(KERN_DIR) M=`pwd` modules clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderobj-m += first_drv.o

5.字符設(shè)備驅(qū)動程序:

#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>static struct class *firstdrv_class; static struct class_device *firstdrv_class_dev;volatile unsigned long *gpfcon = NULL; volatile unsigned long *gpfdat = NULL;static int first_drv_open(struct inode *inode, struct file *file) {//printk("first_drv_open\n");/* 配置GPF4,5,6為輸出 */*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));return 0; }static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {int val;//printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user();if (val == 1){// 點燈*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));}else{// 滅燈*gpfdat |= (1<<4) | (1<<5) | (1<<6);}return 0; }static struct file_operations first_drv_fops = {.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量 */.open = first_drv_open, .write = first_drv_write, };//驅(qū)動的入口函數(shù) int major; static int first_drv_init(void) {
   //0系統(tǒng)自動分配主設(shè)備號major
= register_chrdev(0, "first_drv", &first_drv_fops); // 注冊, 告訴內(nèi)核 firstdrv_class = class_create(THIS_MODULE, "firstdrv");firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
   //0x56000050為GPFCON的物理地址gpfcon
= (volatile unsigned long *)ioremap(0x56000050, 16);gpfdat = gpfcon + 1;return 0; }static void first_drv_exit(void) {unregister_chrdev(major, "first_drv"); // 卸載 class_device_unregister(firstdrv_class_dev);class_destroy(firstdrv_class);iounmap(gpfcon); }module_init(first_drv_init); module_exit(first_drv_exit);//不添加,則不能夠識別 MODULE_LICENSE("GPL");

6.測試程序:

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>/* firstdrvtest on* firstdrvtest off*/ int main(int argc, char **argv) {int fd;int val = 1;fd = open("/dev/xyz", O_RDWR);if (fd < 0){printf("can't open!\n");}if (argc != 2){printf("Usage :\n");printf("%s <on|off>\n", argv[0]);return 0;}if (strcmp(argv[1], "on") == 0){val = 1;}else{val = 0;}write(fd, &val, 4);return 0; }

?

7.運行

? 1>cat /proc/devices
? //加載驅(qū)動
? 2>insmod first_drv.ko
? //創(chuàng)建設(shè)備節(jié)點
? 3>mknod /dev/xxx? c? 111? 0
? 4>運行
?
? 5>lsmod
? 6>rmmod first_drv
? 7>rm /dev/xxx


? 8.? 自動分配主設(shè)備號,自動創(chuàng)建設(shè)備
?? ?1)驅(qū)動:①自動分配主設(shè)備號
?? ??????????? ②手工指定
?? ?2)應(yīng)用:
?? ???????? open("/dev/xxx")
?? ???????? 1>mknod /dev/xxx c 主設(shè)備號???? 次設(shè)備號
?? ???????? 2>自動創(chuàng)建設(shè)備? udev? mdev(根據(jù)系統(tǒng)信息,創(chuàng)建設(shè)備節(jié)點)?
?? ?     class_create()
?? ?    class_device_create()
?? ?3)查看系統(tǒng)信息
?? ???? cd /sys/class
?? ???? cd firstdrv
?? ???? cd xyz
?? ???? ls
?? ?4) ?
?? ???? /etc/init.d/rc.S
?? ???? echo /sbin/mdev > /proc/sys/kernel/hotplus

總結(jié)

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

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