嵌入式linux字元裝置驅動

2021-05-23 07:33:36 字數 1952 閱讀 6164

arm  linux  驅動  抵岸科技

1. 我們需要先呼叫register_chrdev_region()或 alloc_chrdev_region()來向系統申請裝置號

int register_chrdev_region( dev_t first, unsigned int count, char *name );  //函式通過已知的裝置號first來註冊字元裝置區域。

int alloc_chrdev_region( dev_t *dev, unsigned int firstminor,  unsigned int count, char *name );  //若需要動態分配裝置號,則使用該函式,此處dev作為指標,為僅用於輸出的引數,分配成功後儲存已分配範圍的第乙個編號。firstminor通常為0,表示第乙個次裝置號。

2.釋放裝置號

void unregister_chrdev_region( dev_t first, unsigned int count );

3.註冊完字元裝置區域之後

需要使用cdev_init函式初始化cdev結構和 file_operations結構

cdev->owner = this_module;

void cdev_init( struct cdev *cdev, struct file_operations *fops);  //為cdev結構指標建立記憶體,並將cdev->ops=fops;(抵岸科技注:源**中並未見建立記憶體。cdev_init依次做了:cdev填充0,初始化表頭,kobject成員初始化,將cdev->ops=fops)

接著講cdev加入到核心中

int cdev_add( struct cdev *dev, dev_t num, unsigned int count);

完成字元裝置的註冊過程

以上函式需要#include 

4. 自動建立字元裝置節點

下面的函式需要  #include .

struct class *myclass;  //宣告乙個class結構用於建立字元裝置節點

myclass = class_create(this_module,"myclass");  //"myclass"為類名

device_create(myclass,null,devno,null,"leds%d",0);  //通過myclass結構建立字元裝置,第二個引數是裝置的parent,第三個參

數是裝置號,第四個引數是傳入核心的驅動資料void *型別指標,如果沒有可以設定成null,最後乙個引數是const char* fmt, ...,就是

格式化引數,即leds0

完成上述過程後會在/dev中找到leds0裝置

貌似以前的老版本會用到

devfs_mk_cdev來建立裝置節點,但是最後發現linux-2.6.33並沒有這個函式,應該是被上述函式取代 了吧

5. 當然,還包括驅動makefile的編寫

以及應用程式

幾處比較有用的參考文章:

//比較全面

//自動生成字元裝置節點

//小型總結  

6.tips

printk(kern_warning" this is the test ");

kern_alert

kern_waning

kern_info

kern_err

等等不同優先順序 巨集,具體可以查閱linux裝置驅動程式三,字元裝置驅動一章。

嵌入式linux字元裝置驅動

1.我們需要先呼叫register chrdev region 或 alloc chrdev region 來向系統申請裝置號 int register chrdev region dev t first,unsigned int count,char name 函式通過已知的裝置號first來註冊...

嵌入式linux字元裝置註冊裝置驅動

包含初始化巨集定義的標頭檔案,中的module init和module exit在此檔案中 include 包含初始化載入模組的標頭檔案,中的module license在此標頭檔案中 include 定義module param module param array的標頭檔案 include 定義...

嵌入式Linux字元裝置驅動模型詳解

在linux系統中,裝置的型別非常多。比如 字元裝置,塊裝置,網路裝置介面裝置,pci裝置,usb裝置,平台裝置,混雜裝置。裝置型別不同,對應的驅動模型也不同。linux下開發裝置驅動程式要遵循核心模組的編寫規範,在編寫字元裝置驅動程式時,有乙個統一的框架,也就是字元裝置驅動模型。下面我們來看下整個...