当前位置:网站首页>On nanopi M1 (Quanzhi H3) kernel driver programming HelloWorld (compilation mode I)
On nanopi M1 (Quanzhi H3) kernel driver programming HelloWorld (compilation mode I)
2022-04-23 22:03:00 【July meteor】
Method 1 :
development environment :VM+Ubuntu
Compile environment :linux3.4( Quanzhi official BSP)
Cross compiler tool :arm-linux-gcc 4.4.3
1. To write helloworld Device source code
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/cdev.h>
#include<linux/miscdevice.h>
#include<linux/init.h>
#include<linux/device.h>
#include<linux/string.h>
#include<linux/types.h>
#include<linux/moduleparam.h>
#include<linux/errno.h>
#define DEVICE_NAME "helloworld"
#define DEVICE_MAJOR 237
int device_open(struct inode *inode,struct file *file)
{
printk("<0>the helloword device open.. \n");
return 0;
}
static ssize_t device_read(struct file *file,char __user *buf,size_t count,loff_t *f_pos)
{
printk("<0>the helloword device reading.. \n");
return 1;
}
static ssize_t device_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos)
{
printk("<0>the helloword device writing.. \n");
return 1;
}
int device_release(struct inode *inode,struct file *file)
{
printk("<0>the helloword device release \n");
return 0;
}
struct file_operations helloworld_ops=
{
.owner = THIS_MODULE,
.open = device_open,
.read = device_read,
.write = device_write,
.release = device_release,
};
//init
static int __init Helloworld_init(void)
{
int ret;
printk("<0>module init \n");
ret = register_chrdev(DEVICE_MAJOR,DEVICE_NAME,&helloworld_ops);
if(ret<0)
{
printk("<0>%s can't initialized! \n",DEVICE_NAME);
return ret;
}
printk("<0>%s initialized! \n",DEVICE_NAME);
return 0;
}
//exit
static void __exit Helloworld_exit(void)
{
printk("<0>module exit.. \n");
unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
printk("<0>exited \n");
}
module_init(Helloworld_init);
module_exit(Helloworld_exit);
MODULE_AUTHOR("xxx");
MODULE_DESCRIPTION("Nanopi NEO Helloworld Driver");
MODULE_LICENSE("GPL");
take hellowrold.c Copied to the lichee/linux-3.4/drivers/char/ Under the folder , And in Makefile Add... At the end :
obj-m += helloworld.oobj-m Compiled into modules , And the corresponding obj-y Means compiled into the kernel ,$( Variable ) The value of can be y or m.
then , Return to /lichee Catalog , Recompile the kernel
./build.sh -p sun8iw7p1 -b nanopi-h3 -m kernelstay lichee/linux-3.4/drivers/char/ The directory will generate .ko file

take .ko File copy to Nanopi m1 Any directory
2. stay nanopi m1 Write applications on the client side app.c
Simulate the use of the device :
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
char buf[2] = {0,1}; //Âú×ã¸ñʽ
size_t n = 1;
int fd;
fd = open("/dev/helloworld",O_RDWR); //É豸Ãû£¬´ò¿ª·½Ê½£¬È¨ÏÞ
if(fd < 0)
{
printf("Can't open the helloworld device.\n");
return -1;
}
else
printf("Open the helloworld device successfully.\n");
write(fd,buf,n);
read(fd,buf,n);
close(fd);
printf("app over\n");
}
3. Using kernel modules :
Mount the kernel module :
insmod helloworld.koView the list of kernel modules :
lsmodCheck whether there is a device node :
ls /dev/hello*Create device nodes :
mknod /dev/helloworld c 237 0c Represents that the device is a character device ,237 Is the master device number of the device ,0 This is the equipment number . After success ,/dev Generate under directory helloworld Device file .
Compiling the application :
gcc -o app app.cExecute the application :
./appThe display module is opened successfully
Uninstall the kernel module :
rmmod helloworldDelete device files :
rm /dev/helloworldComplete the loading device of kernel module , Build device node , Operating equipment , Unload the device , The function of deleting device files .
版权声明
本文为[July meteor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200609125644.html
边栏推荐
- OpenFeign之响应处理
- [leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]
- Introduction to tensorrt
- JUnit unit test ---- a good tool for testing a single method
- Hystrix断路器开启条件和流程以及默认备选处理
- CVPR2022 | 基于知识蒸馏的高效预训练
- Pytorch deep learning practice (2)
- 2022 - 04 - 24 Daily: Current Progress and Open Challenges of Applied Deep Learning in Biological Sciences
- ROS learning notes - tutorial on the use of ROS
- Introduction to hystrix and implementation of server fuse
猜你喜欢
![[leetcode refers to offer 25. Merge two sorted linked lists (simple)]](/img/7b/b31b1a128e8b48c56493131e0f26c5.png)
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]

Database experiment VI integrity language experiment

在线时序流程图制作工具

Correction of date conversion format error after Oracle adds a row total

OpenFeign调用详细日志展示

SSH in win10

OpenFeign的参数传递之数组和集合类型

修改服务的默认负载均衡策略

Based on jsplumb JS to achieve multi list one to many connection effect

服务熔断的实现
随机推荐
Swift import third-party library reports an error no such module““
A series of problems of C DataGridView binding list
OpenFeign的细节展示
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
LabVIEW modify the appearance of the application window
How to use the project that created SVN for the first time
Pycharm Chinese plug-in
Plato Farm元宇宙IEO上线四大,链上交易颇高
Database Experiment 2 data query
Normalized transforms in pytorch The real calculation process of normalize
Cvpr2022 | efficient pre training based on knowledge distillation
Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
JS to get the browser and screen height
Oracle updates the data of different table structures and fields to another table, and then inserts it into the new table
服务间通信方式
When pycharm debugs, view the tensor image of pytorch in the console
服务降级的实现
A solution of C batch query
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
Database experiment VI integrity language experiment