当前位置: 首页 > news >正文

小众做的好的网站班级优化大师怎么用

小众做的好的网站,班级优化大师怎么用,登陆页面模板,做网站网站建设hello的驱动编写 编写驱动程序的步骤 1.确定主设备号,也可以让内核分配 2.定义自己的 file_operations 结构体 3.实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构 体 4.把 file_operations 结构体告诉内核:regist…

hello的驱动编写

编写驱动程序的步骤

1.确定主设备号,也可以让内核分配
2.定义自己的 file_operations 结构体
3.实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构 体
4.把 file_operations 结构体告诉内核:register_chrdev
5.谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这 个入口函数
6.有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
7.其他完善:提供设备信息,自动创建设备节点:class_create,

代码

驱动代码
hello_drv.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/ctype.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#include <linux/platform_device.h>static ssize_t hello_read(struct file *file, char __user *buf, size_t count,loff_t *ppos);
static ssize_t hello_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos);
static int hello_open(struct inode *inode, struct file *file);
int hello_close(struct inode *inode, struct file *file);char kernel_buf[1024] = "www.ask100.com";
static struct class *hello_class;
#define MIN(a,b) (a<b ? a:b)//1确定主设备号,也可以让内核分配
static unsigned int major = 0 ; //2定义自己的 file_operations 结构体
static const struct file_operations hello_fops = {.owner	 = THIS_MODULE,.open    = hello_open,.read    = hello_read,.write   = hello_write,.release = hello_close,
};//3.实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体
static int hello_open(struct inode *inode, struct file *file)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;}static ssize_t hello_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
{int len;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);len = copy_from_user(kernel_buf, buf, MIN(1024,sizeof(kernel_buf)));kernel_buf[len] = '\0';return MIN(1024,sizeof(kernel_buf));}static ssize_t hello_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
{int err;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);err = copy_to_user(buf, kernel_buf, MIN(1024,sizeof(kernel_buf)));return MIN(1024,sizeof(kernel_buf));
}int hello_close(struct inode *inode, struct file *file)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
}//4.把 file_operations 结构体告诉内核:register_chrdev
//5.谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init hello_init(void)
{int err;major = register_chrdev(0,"hello",&hello_fops);hello_class = class_create(THIS_MODULE, "hello");err = PTR_ERR(hello_class);if (IS_ERR(hello_class))return -1;device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /*/dev/hello*/return 0;}//6.有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdevstatic void __init hello_exit(void)
{device_destroy(hello_class, MKDEV(major, 0));class_destroy(hello_class);unregister_chrdev(major, "hello");}//7.其他完善:提供设备信息,自动创建设备节点:class_create,device_create
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

测试程序
hello_drv_test.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>/** ./hello_drv_test -w abc* ./hello_drv_test -r*/
int main(int argc, char **argv)
{int fd;char buf[1024];int len;/* 1. 判断参数 */if (argc < 2) {printf("Usage: %s -w <string>\n", argv[0]);printf("       %s -r\n", argv[0]);return -1;}/* 2. 打开文件 */fd = open("/dev/hello", O_RDWR);if (fd == -1){printf("can not open file /dev/hello\n");return -1;}/* 3. 写文件或读文件 */if ((0 == strcmp(argv[1], "-w")) && (argc == 3)){len = strlen(argv[2]) + 1;len = len < 1024 ? len : 1024;write(fd, argv[2], len);}else{len = read(fd, buf, 1024);		buf[1023] = '\0';printf("APP read : %s\n", buf);}close(fd);return 0;
}

Makefile


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderrm -f hello_testobj-m	+= hello_drv.oKERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88

用make命令编译后,将编译出的hello_drv.ko 和hello_drv_test传给板子

板子操作

  1. 安装驱动:insmod hello_drv.ko

2.lsmod : 查看已经安装的驱动,可以看到hello_drv已经安装

在这里插入图片描述

3.cat /proc/devices查看设备节点,看到hello节点已经被创建,这是再hello_drv.c文件里创建的节点,具体看hello_int函数
在这里插入图片描述

4.执行程序: 在这里插入图片描述

5.删除驱动程序 rmmod hello_drv

http://www.zhongyajixie.com/news/51688.html

相关文章:

  • 安装wordpress要数据库吗对网站外部的搜索引擎优化
  • 做酒店网站设计服务营销的七个要素
  • 有做a50期货的网站seo网络优化师
  • 自己怎么制作海报图片网站优化策划书
  • 织梦网站上传保存文档站外推广怎么做
  • 佛山网站推广市场免费做网站自助建站
  • 为什么做织梦网站时图片出不来友链购买
  • 脑卒中中心建设网站百度下载免费
  • 网站建设费的会计处理电商网站开发需要多少钱
  • 浙江网站搭建会计培训班初级费用
  • 女和男做搞基视频网站中央新闻
  • php网站的优势西安建站推广
  • 参加网站建设项目人员保障体系湖南网站seo找行者seo
  • 网站建设框架模板下载沧州网站建设公司
  • wordpress怎么去掉谷歌字体seo关键词是怎么优化的
  • 济南做网站百度网站排名查询
  • 有新浪的域名怎么做网站淘宝搜索关键词排名查询工具
  • 网站备案要关站吗湖南长沙最新情况
  • 动态网站开发商城网站网站子域名查询
  • 网站做视频在线观看怎么免费做网站
  • 九江网站建设优化公司seo优化快排
  • 摄影网站怎么备案百度搜索关键词推广
  • cf刷枪网站怎么做的佛山百度seo代理
  • 大良营销网站建设如何我想在百度发布信息
  • 伊犁做网站百度升级最新版本下载安装
  • 网站做项目营销型网站的分类
  • 单页网站怎么做seo衡阳seo快速排名
  • 不是用于制作网页的软件seo同行网站
  • 做网站通常到哪找图片网站推广的目的是什么
  • 网站开发建设合同自己在家做电商