当前位置:网站首页>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.o
obj-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 kernel
stay 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.ko
View the list of kernel modules :
lsmod
Check whether there is a device node :
ls /dev/hello*
Create device nodes :
mknod /dev/helloworld c 237 0
c 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.c
Execute the application :
./app
The display module is opened successfully
Uninstall the kernel module :
rmmod helloworld
Delete device files :
rm /dev/helloworld
Complete 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
边栏推荐
- 服务雪崩效应
- JUnit unit test ---- a good tool for testing a single method
- MySQL 回表
- How Axure installs a catalog
- Cvpr2022 | efficient pre training based on knowledge distillation
- 阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
- 2022-04-24日报:在生物科学领域应用深度学习的当前进展和开放挑战
- Database Experiment 8 trigger experiment
- Ribbon负载均衡策略
- OpenFeign组件的使用
猜你喜欢
服务雪崩、服务熔断、服务降级
OpenFeign之响应处理
Ribbon负载均衡策略
Correction of date conversion format error after Oracle adds a row total
[leetcode refers to offer 52. The first common node of two linked lists (simple)]
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
Based on jsplumb JS to achieve multi list one to many connection effect
Echerts add pie chart random color
服务熔断的实现
[leetcode refers to offer 18. Delete the node of the linked list (simple)]
随机推荐
Echerts add pie chart random color
JS prototype and prototype chain
NVM introduction, NVM download, installation and use (node version management)
consul 关闭健康监控检查
Correction of date conversion format error after Oracle adds a row total
OpenFeign 组件说明
Hystrix components
Online Excel to CSV tool
ERP function_ Financial management_ The difference between red and blue words in invoices
通过OpenFeign传递对象类型参数
五个拿来就能用的炫酷登录页面
清华大学 | WebFace260M:百万级深度人脸识别的基准(TPAMI2022)
延时消息常见实现方案
Daily operation and maintenance knowledge -- 1
C list data paging
How to make Jenkins job run automatically after startup
Database Experiment four View experiment
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
A solution of C batch query
Ribbon组件基本介绍和使用