当前位置:网站首页>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 !

边栏推荐
- 【单片机】2.1 AT89S52单片机的硬件组成
- anaconda重新卸载
- Windows与网络基础-15-本地安全策略
- Lichuang EDA - layout and inspection of schematic diagram (III)
- 初识多态
- Run loam_ Velodyne real-time mapping
- 每条你收藏的资讯背后,都离不开TA
- Explain the idempotence of distributed system in detail
- 7 岁男孩被 AI 机器人折断手指,仅因下棋太快?
- Yyds dry inventory interview must brush top101: specified interval reversal in the linked list
猜你喜欢

An end-to-end file upload program based on form, including client and server

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
![[MCU] 2.2 pin function of AT89S52](/img/d0/b204efb80ec5f0b7bc2d83d0250eb9.png)
[MCU] 2.2 pin function of AT89S52

The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots

Because the employee set the password to "123456", amd stolen 450gb data?

成本高、落地难、见效慢,开源安全怎么办?

Cow! His secret is to reproduce the paper in 2 hours——

微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists

Windows and network foundation-15-local security policy

信号量保护之位带操作
随机推荐
数据库超话(一)
ES6 array method and pseudo array to array method
信号量保护之位带操作
【数据库系统概论(王珊)】第4章——数据库安全性
可口可乐的首要挑战,不是元气森林
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
树莓派驱动代码的编译和测试
Lichuang EDA - PCB layout (IV)
Use @ in the project created by CRA, let it recognize the @ path and give path tips
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
From digitalization to intelligent operation and maintenance: what are the values and challenges?
二舅的外甥和他的学生们
一文理解分布式开发中的服务治理
【数据库系统概论(王珊)】第1章——绪论
选择体育场馆的LED显示屏时应该注重哪些方面
【单片机】2.2 AT89S52的引脚功能
Dense optical flow extraction dense_ Flow understanding
交换机和路由器技术-02-以太网交换机工作原理
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