当前位置:网站首页>Compilation and testing of raspberry pie driver code
Compilation and testing of raspberry pie driver code
2022-07-27 17:54:00 【LJX】
First, find a simplified driver code
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <asm/io.h>
static struct class *pin4_class;
static struct device *pin4_class_dev;
static dev_t devno; // Device number
static int major = 231; // The main equipment,
static int minor = 0; // Secondary device number
static char *module_name = "pin4"; // Module name
static int pin4_read (struct file *file1,char __user *buf,size_t size,loff_t *ppos)
{
printk("pin4_read\n");
return 0;
}
static int pin4_open(struct inode *inode,struct file *file)
{
printk("pin4_open\n");
return 0;
}
static ssize_t pin4_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
printk("pin4_write\n");
return 0;
}
static struct file_operations pin4_fops = {
.owner = THIS_MODULE,
.open = pin4_open,
.write = pin4_write,
.read = pin4_read,
};
int __init pin4_drv_init(void)
{
int ret;
devno = MKDEV(major,minor);
ret = register_chrdev(major,module_name,&pin4_fops); // Registration drive Tell the kernel , Add this driver to the linked list of kernel drivers
pin4_class = class_create(THIS_MODULE,"myfirstdemo");
pin4_class_dev = device_create(pin4_class,NULL,devno,NULL,module_name); // Create device files
return 0;
}
void __exit pin4_drv_exit(void)
{
device_destroy(pin4_class,devno);
class_destroy(pin4_class);
unregister_chrdev(major,module_name); // Unload the drive
}
module_init(pin4_drv_init);
module_exit(pin4_drv_exit);
MODULE_LICENSE("GPL v2");Write another piece of code for test drive
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("/dev/pin4",O_RDWR);
if(fd < 0){
printf("open failed\n");
perror("reason:");
}else{
printf("open success\n");
}
fd = write(fd,'1',1);
}
Then you can start compiling the driver code
1. open Linux Kernel source tree directory , Get into linux-rpi-4.14.y/drivers/char, Copy the driver code .

2. Get into Makefile , And modify

3. Go back to the kernel and compile the module driver , Will generate a .ko file .
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make modules
4. Put this .ko Upload the file to raspberry pie ( It can also be used here FileZilla Upload )
scp pin4test.ko [email protected]:/home/pi5. Compile test code
arm-linux-gnueabihf-gcc pin4demo.c -o pin4demoUpload the generated executable file to raspberry pie .

When raspberry pie gets these two documents , Next, load the kernel driver .

6. Load the kernel driver
sudo insmod pin4text.koAfter loading successfully, you can see the device number

Or use lsmod Look at the driver , Found that the driver was loaded successfully .

7. Add a permission to the driver that everyone can view
sudo chmod 666 /dev/pin4![]()
8. Call the upper test program

9. At this time, we can't see any information on the top , Because we use printk Print information ,printk Print the data in kernel mode . So we use dmesg Check the contents in kernel mode , Find out pin4_open And pin4_write All called , Successful test !

边栏推荐
- 20 years ago, he was Ma Yun's biggest enemy
- 【数据库系统概论(王珊)】第1章——绪论
- High cost, difficult to implement, slow to take effect, what about open source security?
- Run loam_ Velodyne real-time mapping
- Hidden iframe design details of SAP ui5 fileuploader
- 微软默默给 curl 捐赠一万美元,半年后才通知
- 一文理解分布式开发中的服务治理
- Tencent cloud upload
- 可口可乐的首要挑战,不是元气森林
- 每条你收藏的资讯背后,都离不开TA
猜你喜欢

Microsoft silently donated $10000 to curl, which was not notified until half a year later

Uncle's nephew and his students

How to resolve the public domain name to the intranet IP server -- quickly resolve the domain name and map the Internet access

面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)

Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro

Lichuang EDA - PCB layout (IV)

20 years ago, he was Ma Yun's biggest enemy

细数国产接口协作平台的六把武器!

微软默默给 curl 捐赠一万美元,半年后才通知

Zhengzhou University database course resource description
随机推荐
Use @ in the project created by CRA, let it recognize the @ path and give path tips
【Codeforces】 A. Computer Game
Switch and router technology-03-basic configuration of switch
ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model
Explain the pile of binary trees in detail
Summer Challenge [FFH] real time chat room websocket practice
详解分布式系统的幂等
ES6 array method and pseudo array to array method
记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
Talk about the new trend of machine translation, and the second maverick translation forum is about to open
Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
密集光流提取dense_flow理解
Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book
The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots
How far can invisible orthodontics go under the tuyere?
numpy数组矩阵操作
Yyds dry inventory interview must brush top101: specified interval reversal in the linked list
Windows and network foundation-15-local security policy
Dense optical flow extraction dense_ Flow understanding
Lichuang EDA - layout and inspection of schematic diagram (III)