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

个人博客网站模板素材数据分析网站html模板下载

个人博客网站模板素材,数据分析网站html模板下载,想学做网站学什么编程语言,网站建设的平台一、驱动程序分离的思想 【IMX6ULL驱动开发学习】05.字符设备驱动开发模板#xff08;包括读写函数、poll机制、异步通知、定时器、中断、自动创建设备节点和环形缓冲区#xff09;_阿龙还在写代码的博客-CSDN博客 之前编写驱动程序的代码存在不少弊端#xff1a;移植性差…一、驱动程序分离的思想 【IMX6ULL驱动开发学习】05.字符设备驱动开发模板包括读写函数、poll机制、异步通知、定时器、中断、自动创建设备节点和环形缓冲区_阿龙还在写代码的博客-CSDN博客 之前编写驱动程序的代码存在不少弊端移植性差驱动程序移植到别的板子上时开发者需要修改引脚。并且还要重新编译驱动程序或内核。为提高移植和开发效率驱动程序分离编程的思想尤为重要。 首先我们要知道 内核里有个结构体platform_bus_type虚拟的总线总线上抽象出两个链表设备链表和驱动链表。 我们在写驱动程序时可以构造platfrom_device结构体然后把它添加进内核里platform_device_register函数就是放入platform_bus_type结构体的设备链表中。 驱动程序会调用两个函数注册platform_device结构体和platform_driver结构体。platform_device结构体里含有硬件资源包括寄存器地址、内存地址、中断号platform_driver结构体里有通用的代码。 以前写驱动程序时只写成一个.c文件在入口函数里注册字符设备驱动程序现在需要故意拆分成两个文件gpio_drv.c和gpio_dev.c。 在gpio_drv.c的入口函数里注册platform_driver结构体用到platform_driver_register函数在gpio_dev.c的入口函数里注册platform_device结构体用到platform_device_register函数该函数会把要注册的platform_device结构体放入内核中platform_bus_type结构体虚拟总线的设备链表并且会遍历platform_bus_type结构体虚拟总线的驱动链表将platform_device结构体和每一个platform_driver结构体进行比较为硬件设备找驱动程序匹配成功后就不会往后比较了。 如果匹配成功会调用platform_driver结构体中的probe函数。在probe函数中完成①从platform_device结构体中得到引脚编号 ②注册字符设备驱动程序。 如果事先添加了设备platform_device结构体但并没找到与之匹配的驱动程序platform_driver结构体。之后添加驱动时会遍历设备链表若匹配成功则调用驱动的probe函数。且一个驱动可能支持多个设备。 二、设备树 gpio_dev.c和设备树的目的都是为了构造platform_device结构体。如果用gpio_dev.c时需要每次都修改引脚重新编译和安装导致内核里有很多冗余的gpio_dev.c文件和platform_device结构体。 使用设备树在设备树文件中添加节点信息根据节点信息内核会构造出platform_device结构体。 板子启动时有个ubootuboot会做两件事①板子上如果有SD卡SD卡中存放有设备树dtb文件uboot会把设备树文件读入到内存中②SD卡中还有内核uboot会把内核读入内存③启动内核uboot会把设备树的地址传入内核内核会这个地址上把设备树文件解析成各种platform_device结构体。 以后产品修改了引脚我们只需要修改设备树dtb文件就可以了。内核不变变设备树文件。 2.1 使用设备树 修改设备树arch/arm/boot/dts/100ask_imx6ull-14x14.dts 在设备树文件中添加节点信息注意compatible与驱动的compatible匹配 motor {compatible 100ask,gpiodemo;gpios gpio4 19 GPIO_ACTIVE_HIGH, gpio4 20 GPIO_ACTIVE_HIGH,gpio4 21 GPIO_ACTIVE_HIGH,gpio4 22 GPIO_ACTIVE_HIGH; }; 编译make dtbs复制到板子上  PC: cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/开发板: mount -t nfs -o nolock,vers3 192.168.5.11:/home/book/nfs_rootfs /mnt cp /mnt/100ask_imx6ull-14x14.dtb /boot reboot 测试 insmod gpio_drv.ko ./button_test /dev/gpio ... 三、平台总线设备驱动模板 支持platfrom_device来自自己写的.c文件和更改的设备树文件包括中断、定时器、读写、poll机制、异步通知。驱动程序如下 #include linux/module.h #include linux/poll.h #include linux/fs.h #include linux/errno.h #include linux/miscdevice.h #include linux/kernel.h #include linux/major.h #include linux/mutex.h #include linux/proc_fs.h #include linux/seq_file.h #include linux/stat.h #include linux/init.h #include linux/device.h #include linux/tty.h #include linux/kmod.h #include linux/gfp.h #include linux/gpio/consumer.h #include linux/platform_device.h #include linux/of_gpio.h #include linux/of_irq.h #include linux/interrupt.h #include linux/irq.h #include linux/slab.h #include linux/fcntl.h #include linux/timer.hstruct gpio_desc{int gpio;int irq;char name[128];int key;struct timer_list key_timer; } ;static struct gpio_desc *gpios; static int count;/* 主设备号 */ static int major 0; static struct class *gpio_class;/* 环形缓冲区 */ #define BUF_LEN 128 static int g_keys[BUF_LEN]; static int r, w;struct fasync_struct *button_fasync;#define NEXT_POS(x) ((x1) % BUF_LEN)static int is_key_buf_empty(void) {return (r w); }static int is_key_buf_full(void) {return (r NEXT_POS(w)); }static void put_key(int key) {if (!is_key_buf_full()){g_keys[w] key;w NEXT_POS(w);} }static int get_key(void) {int key 0;if (!is_key_buf_empty()){key g_keys[r];r NEXT_POS(r);}return key; }static DECLARE_WAIT_QUEUE_HEAD(gpio_wait);// static void key_timer_expire(struct timer_list *t) static void key_timer_expire(unsigned long data) {/* data gpio */// struct gpio_desc *gpio_desc from_timer(gpio_desc, t, key_timer);struct gpio_desc *gpio_desc (struct gpio_desc *)data;int val;int key;val gpio_get_value(gpio_desc-gpio);//printk(key_timer_expire key %d %d\n, gpio_desc-gpio, val);key (gpio_desc-key) | (val8);put_key(key);wake_up_interruptible(gpio_wait);kill_fasync(button_fasync, SIGIO, POLL_IN); }/* 实现对应的open/read/write等函数填入file_operations结构体 */ static ssize_t gpio_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset) {//printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);int err;int key;if (is_key_buf_empty() (file-f_flags O_NONBLOCK))return -EAGAIN;wait_event_interruptible(gpio_wait, !is_key_buf_empty());key get_key();err copy_to_user(buf, key, 4);return 4; }static ssize_t gpio_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset) {unsigned char ker_buf[2];int err;if (size ! 2)return -EINVAL;err copy_from_user(ker_buf, buf, size);if (ker_buf[0] sizeof(gpios)/sizeof(gpios[0]))return -EINVAL;gpio_set_value(gpios[ker_buf[0]].gpio, ker_buf[1]);return 2; }static unsigned int gpio_drv_poll(struct file *fp, poll_table * wait) {//printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, gpio_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM; }static int gpio_drv_fasync(int fd, struct file *file, int on) {if (fasync_helper(fd, file, on, button_fasync) 0)return 0;elsereturn -EIO; }/* 定义自己的file_operations结构体 */ static struct file_operations gpio_key_drv {.owner THIS_MODULE,.read gpio_drv_read,.write gpio_drv_write,.poll gpio_drv_poll,.fasync gpio_drv_fasync, };static irqreturn_t gpio_key_isr(int irq, void *dev_id) {struct gpio_desc *gpio_desc dev_id;printk(gpio_key_isr key %d irq happened\n, gpio_desc-gpio);mod_timer(gpio_desc-key_timer, jiffies HZ/5);return IRQ_HANDLED; }/* 在入口函数 */ static int gpio_drv_probe(struct platform_device *pdev) {int err 0;int i;//平台设备里面有设备树节点信息//如果平台设备platform_device来自设备树的话np就不是NULLstruct device_node *np pdev-dev.of_node;//资源指针 struct resource *res;printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);/* 从platfrom_device获得引脚信息 * 1. pdev来自自己写的c文件* 2. pdev来自设备树在设备树文件中添加硬件的节点信息*/if (np){/* pdev来自设备树 设备树节点信息示例:reg_usb_ltemodule: regulator1 {compatible 100ask,gpiodemo;gpios gpio5 5 GPIO_ACTIVE_HIGH, gpio5 3 GPIO_ACTIVE_HIGH;};*/count of_gpio_count(np);//获得这个设备信息多少个引脚if (!count)return -EINVAL;gpios kmalloc(count * sizeof(struct gpio_desc), GFP_KERNEL);for (i 0; i count; i){gpios[i].gpio of_get_gpio(np, i);//取出这个设备的第i个引脚的引脚编号sprintf(gpios[i].name, %s_pin_%d, np-name, i);//给对应引脚取名字 申请gpio时需要用到名字}}else{/* pdev来自c文件 static struct resource omap16xx_gpio3_resources[] {{.start 115,.end 115,.flags IORESOURCE_IRQ,},{.start 118,.end 118,.flags IORESOURCE_IRQ,}, }; */count 0;while (1){ //获得平台设备里面的这种IORESOURCE_IRQ类型的资源//dev:platform_device type:resource type num:resource indexres platform_get_resource(pdev, IORESOURCE_IRQ, count);if (res){count;}else{break;}}if (!count)return -EINVAL;gpios kmalloc(count * sizeof(struct gpio_desc), GFP_KERNEL);for (i 0; i count; i){res platform_get_resource(pdev, IORESOURCE_IRQ, i);gpios[i].gpio res-start;//取出这个设备的第i个引脚的引脚编号sprintf(gpios[i].name, %s_pin_%d, pdev-name, i);//给对应引脚取名字 申请gpio时需要用到名字}}for (i 0; i count; i){ gpios[i].irq gpio_to_irq(gpios[i].gpio);setup_timer(gpios[i].key_timer, key_timer_expire, (unsigned long)gpios[i]);//timer_setup(gpios[i].key_timer, key_timer_expire, 0);gpios[i].key_timer.expires ~0;add_timer(gpios[i].key_timer);err request_irq(gpios[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 100ask_gpio_key, gpios[i]);}/* 注册file_operations */major register_chrdev(0, 100ask_gpio_key, gpio_key_drv); /* /dev/gpio_desc */gpio_class class_create(THIS_MODULE, 100ask_gpio_key_class);if (IS_ERR(gpio_class)) {printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, 100ask_gpio_key);return PTR_ERR(gpio_class);}device_create(gpio_class, NULL, MKDEV(major, 0), NULL, 100ask_gpio); /* /dev/100ask_gpio */return err; }/* 有入口函数就应该有出口函数卸载驱动程序时就会去调用这个出口函数*/ //这里应该free gpio这个数组 但是没加上不知道为啥 static int gpio_drv_remove(struct platform_device *pdev) {int i;printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);device_destroy(gpio_class, MKDEV(major, 0));class_destroy(gpio_class);unregister_chrdev(major, 100ask_gpio_key);for (i 0; i count; i){free_irq(gpios[i].irq, gpios[i]);del_timer(gpios[i].key_timer);}return 0; }//支持的设备 //只要设备树节点的信息它的compatible与下面的compatible相同 //即platfrom_device和platfrom_driver匹配成功成功后probe函数就被调用 static const struct of_device_id gpio_dt_ids[] {{ .compatible 100ask,gpiodemo, },{ /* sentinel */ } };static struct platform_driver gpio_platform_driver {.driver {.name 100ask_gpio_plat_drv,.of_match_table gpio_dt_ids,},.probe gpio_drv_probe,.remove gpio_drv_remove, };static int __init gpio_drv_init(void) {/* 注册platform_driver */return platform_driver_register(gpio_platform_driver); }static void __exit gpio_drv_exit(void) {/* 反注册platform_driver */platform_driver_unregister(gpio_platform_driver); }/* 7. 其他完善提供设备信息自动创建设备节点 */module_init(gpio_drv_init); module_exit(gpio_drv_exit);MODULE_LICENSE(GPL);
http://www.dnsts.com.cn/news/70178.html

相关文章:

  • 挣钱做任务的网站做网站能改吗
  • 中小企业网站构建设计中国建设银行的网站
  • 网站做管理员消息推送html网站首页图片切换
  • 做网站哪里买空间好优秀简历模板
  • 企业网站建设的开发方式省心的专业建设网站公司
  • 用来做微网站的有趣的个人网站
  • 国外做免费网站的网站建设语
  • 体现网站特色精品资源共享课程网站建设
  • 衡水网站建设格公司北京软件外包公司排行榜
  • 优化网站专题网站都可以做哪些主题
  • 湖州 外贸网站建设wordpress错误怎么解决方法
  • 织梦做招聘网站qq网站登录
  • 广州建设网站公司哪个济南兴田德润有活动吗学校网站建设需求分析
  • 国外做免费的视频网站有哪些泰安58同城二手房
  • 织梦网站更新Html番禺商城网站建设
  • 旅行社网站制作长沙企业建站方案
  • 哈尔滨做企业网站.net网站方案
  • 做网站付款方式电子商务主要做什么工作
  • 网站推广计划书范文做网站接单
  • 做 爱 网站小视频在线观看erp企业生产管理系统
  • 合肥市做网站的公司有哪些中南集团中南建设网站
  • 大连seo网站推广公司部门解散
  • 网站建设活动策划方案wordpress目录分类与菜单
  • 邢台市做网站电话贵州建设工程招投标协会网站
  • 营销型网站建设大千关于网站建设知识
  • 珠海手机微信网站建设小程序开发奇网企业网站管理系统
  • 网站建设网站软件有哪些方面乌市网络营销
  • 定制手机壳网站平面设计培训需要学什么
  • 洛阳建设工程信息网站博创网站建设团队
  • 阿里云虚拟主机多网站wordpress 小视频