手把手教你在 Linux 中創(chuàng)建節(jié)點(diǎn),使其可以進(jìn)行 cat 和 echo 。
我們測試驅(qū)動(dòng)加載是否正常工作,一般都會(huì)寫應(yīng)用程序去測試,這樣驅(qū)動(dòng)程序中需要實(shí)現(xiàn) open、read 函數(shù)和 write 函數(shù),然后寫一個(gè)應(yīng)用程序通過 open 打開節(jié)點(diǎn),獲取 fb 文件描述符,進(jìn)而對文件進(jìn)行讀寫操作。
這里我介紹另外一種方法,我們可以在驅(qū)動(dòng)中實(shí)現(xiàn) show_xxx 和 set_xxx 函數(shù),使這個(gè)節(jié)點(diǎn)可以進(jìn)行 cat 和 echo 操作,源碼如下:
test.c
#include < linux/module.h >
#include < linux/init.h >
#include < linux/platform_device.h >
#include < linux/gpio.h >
#include < linux/delay.h >
#include < linux/regulator/consumer.h >
#include < sound/soc.h >
#include < sound/jack.h >
static char mybuf[100]="123";
//cat命令時(shí),將會(huì)調(diào)用該函數(shù)
static ssize_t show_my_device(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%sn", mybuf);
}
//echo命令時(shí),將會(huì)調(diào)用該函數(shù)
static ssize_t set_my_device(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
sprintf(mybuf, "%s", buf);
return len;
}
//定義一個(gè)名字為my_device_test的設(shè)備屬性文件
static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device);
struct file_operations mytest_ops={
.owner = THIS_MODULE,
};
static int major;
static struct class *cls;
static int mytest_init(void)
{
struct device *mydev;
major=register_chrdev(0,"mytest", &mytest_ops);
cls=class_create(THIS_MODULE, "mytest_class");
//創(chuàng)建mytest_device設(shè)備
mydev = device_create(cls, 0, MKDEV(major,0),NULL,"mytest_device");
//在mytest_device設(shè)備目錄下創(chuàng)建一個(gè)my_device_test屬性文件
if(sysfs_create_file(&(mydev- >kobj), &dev_attr_my_device_test.attr)) {
return -1;
}
return 0;
}
static void mytest_exit(void)
{
device_destroy(cls, MKDEV(major,0));
class_destroy(cls);
unregister_chrdev(major, "mytest");
}
module_init(mytest_init);
module_exit(mytest_exit);
MODULE_LICENSE("GPL");
Makefile
KERNELDIR := /home/book/linux/tool/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)
obj-m := test.o
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
在 Linux 中新建文件夾,將 test.c 和 Makefile 放在一個(gè)文件夾中,進(jìn)行編譯,編譯之前記得準(zhǔn)備好你的 Linux 內(nèi)核源碼,因?yàn)榫幾g需要引用頭文件,所以我們在 Makefile 中寫明 Linux 內(nèi)核源碼目錄(源碼必須是編譯過的源碼,編譯 Linux 大概需要半個(gè)多小時(shí))。另外需要注意,你編譯驅(qū)動(dòng)所引用的內(nèi)核和你板子中真正運(yùn)行的 Linux 內(nèi)核要需要是同一個(gè)版本,否則掛載不上去。
編譯過程:
然后把 test.ko 傳輸過去,不管是使用 scp 命令還是使用 ftp 協(xié)議都可以。
加載驅(qū)動(dòng)后cat:
echo
-
測試
+關(guān)注
關(guān)注
8文章
5627瀏覽量
128291 -
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1899瀏覽量
86524 -
Linux
+關(guān)注
關(guān)注
87文章
11457瀏覽量
212772 -
程序
+關(guān)注
關(guān)注
117文章
3820瀏覽量
82396 -
系統(tǒng)
+關(guān)注
關(guān)注
1文章
1029瀏覽量
21705
發(fā)布評論請先 登錄
Linux驅(qū)動(dòng)中創(chuàng)建procfs接口的方法

如何在LoRaWAN網(wǎng)關(guān)的內(nèi)置NS創(chuàng)建應(yīng)用與節(jié)點(diǎn)設(shè)備

如何在單個(gè)或兩個(gè)節(jié)點(diǎn)環(huán)境下去創(chuàng)建索引呢
如何在沒有udev的情況下啟動(dòng)之前創(chuàng)建靜態(tài)/dev/節(jié)點(diǎn)?
AN1246中文手冊之如何在Microchip圖形庫中創(chuàng)建控件

如何在IP Integrator中創(chuàng)建MicroBlaze設(shè)計(jì)

如何在Linux上創(chuàng)建tarball
如何在PADS中創(chuàng)建差分對
如何在 Vitis 中使用 UIO 驅(qū)動(dòng)框架創(chuàng)建簡單的 Linux 用戶應(yīng)用
如何在Petalinux創(chuàng)建Linux內(nèi)核模塊?
如何在Linux系統(tǒng)下自動(dòng)創(chuàng)建設(shè)備節(jié)點(diǎn)
如何在Linux中查找斷開的符號鏈接
Linux中如何如何為現(xiàn)有用戶創(chuàng)建主目錄?

評論