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

免费看今天开始做女神的网站百度seo查询收录查询

免费看今天开始做女神的网站,百度seo查询收录查询,昔阳网站建设,阿里云智能建站文章目录 “休眠-唤醒”机制:APP执行过程内核函数休眠函数唤醒函数 休眠与唤醒方式的按键驱动程序(stm32mp157)驱动程序框架button_test.cgpio_key_drv.cMakefile修改设备树文件编译测试 “休眠-唤醒”机制: 当应用程序必须等待某个事件发生&#xff0c…

文章目录

  • “休眠-唤醒”机制:
  • APP执行过程
  • 内核函数
    • 休眠函数
    • 唤醒函数
  • 休眠与唤醒方式的按键驱动程序(stm32mp157)
    • 驱动程序框架
    • button_test.c
    • gpio_key_drv.c
    • Makefile
    • 修改设备树文件
    • 编译测试

“休眠-唤醒”机制:

当应用程序必须等待某个事件发生,比如必须等待按键被按下时,可以使用“休眠-唤醒”机制:

  • ① APP 调用 read 等函数试图读取数据,比如读取按键;
  • ② APP 进入内核态,也就是调用驱动中的对应函数,发现有数据则复制到用户空间并马上返回;
  • ③ 如果 APP 在内核态,也就是在驱动程序中发现没有数据,则 APP 休眠
  • ④ 当有数据时,比如当按下按键时,驱动程序的中断服务程序被调用,它会记录数据、唤醒 APP
  • ⑤ APP 继续运行它的内核态代码,也就是驱动程序中的函数,复制数据到用户空间并马上返回。

APP执行过程

在这里插入图片描述

  • 驱动中没有数据时,APP1 在内核态执行到 drv_read 时会休眠
  • 所谓休眠就是把自己的状态改为非 RUNNING,这样内核的调度器就不会让它运行。
  • 当按下按键,驱动程序中的中断服务程序被调用,它会记录数据,并唤醒 APP1。所以唤醒就是把程序的状态改为 RUNNING,这样内核的调度器有合适的时间就会让它运行。
  • 当 APP1 再次运行时,就会继续执行 drv_read 中剩下的代码,把数据复制回用户空间,返回用户空间。
  • 在 APP的read到内核态的drv_read函数中(进程上下文),也就是在 APP1 的执行过程中,它是可以休眠的
  • 在中断处理函数中(属于中断上下文),不能休眠,也就是不能调用会导致休眠的函数。

内核调度器负责维护该链表,链表里面保存的是线程,如果线程的状态为RUNNING,则会找到合适的时间就会让它运行,如果是非RUNNING,内核的调度器就不会让它运行。

内核函数

参考内核源码: include\linux\wait.h

休眠函数

函数说明
wait_event_interruptible(wq, condition)休眠,直到 condition 为真;休眠期间是可被打断的,可以被信号打断
wait_event(wq, condition)休眠,直到 condition 为真;退出的唯一条件是 condition 为真,信号也不好使
wait_event_interruptible_timeout(wq,condition, timeout)休眠,直到 condition 为真或超时;休眠期间是可被打断的,可以被信号打断
wait_event_timeout(wq, condition,timeout)休眠,直到 condition 为真;退出的唯一条件是 condition 为真,信号也不好使

比较重要的参数就是:

  • ① wq:waitqueue,等待队列
    • 休眠时除了把程序状态改为非 RUNNING 之外,还要把进程/进程放入wq 中,以后中断服务程序要从 wq 中把它取出来唤醒。
    • 没有 wq 的话,茫茫人海中,中断服务程序去哪里找到你?
  • ② condition
    • 这可以是一个变量,也可以是任何表达式。表示“一直等待,直到condition 为真”

唤醒函数

函数说明
wake_up_interruptible(x)唤醒 x 队列中状态为“TASK_INTERRUPTIBLE”的线程,只唤醒其中的一个线程
wake_up_interruptible_nr(x, nr)唤醒 x 队列中状态为“TASK_INTERRUPTIBLE”的线程,只唤醒其中的 nr 个线程
wake_up_interruptible_all(x)唤醒 x 队列中状态为“TASK_INTERRUPTIBLE”的线程,唤醒其中的所有线程
wake_up(x)唤 醒 x 队 列 中 状 态 为 “ TASK_INTERRUPTIBLE ” 或“TASK_UNINTERRUPTIBLE”的线程,只唤醒其中的一个线程
wake_up_nr(x, nr)唤 醒 x 队 列 中 状 态 为 “ TASK_INTERRUPTIBLE ” 或“TASK_UNINTERRUPTIBLE”的线程,只唤醒其中 nr 个线程
wake_up_all(x)唤 醒 x 队 列 中 状 态 为 “ TASK_INTERRUPTIBLE ” 或“TASK_UNINTERRUPTIBLE”的线程,唤醒其中的所有线程

休眠与唤醒方式的按键驱动程序(stm32mp157)

驱动程序框架

在这里插入图片描述

要休眠的线程,放在 wq 队列里,中断处理函数从 wq 队列里把它取出来唤醒。

代码编写内容

  • ① 初始化 wq 队列
  • 在驱动的 read 函数中,调用 wait_event_interruptible:
    • 它本身会判断 event 是否为 FALSE,如果为 FASLE 表示无数据,则休眠。当从 wait_event_interruptible 返回后,把数据复制回用户空间。
  • 在中断服务程序里
    • 设置 event 为 TRUE,并调用 wake_up_interruptible 唤醒线程。

button_test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>/** ./button_test /dev/100ask_button0**/
int main(int argc, char **argv)
{int fd;int val;/* 1. 判断参数 */if (argc != 2) {printf("Usage: %s <dev>\n", argv[0]);return -1;}/* 2. 打开文件 */fd = open(argv[1], O_RDWR);if (fd == -1){printf("can not open file %s\n", argv[1]);return -1;}while (1){/* 3. 读文件 */read(fd, &val, 4);printf("get button : 0x%x\n", val);}close(fd);return 0;
}

gpio_key_drv.c

使用环形缓冲区来保存按键值,相比于全局变量,可以避免被覆盖的问题

#include <linux/module.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>struct gpio_key{int gpio;struct gpio_desc *gpiod;int flag;int irq;
} ;static struct gpio_key *gpio_keys_first;/* 主设备号                                                                 */
static int major = 0;
static struct class *gpio_key_class;/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;//r,w是指针,指向读写的位置#define NEXT_POS(x) ((x+1) % BUF_LEN)static int is_key_buf_empty(void)
{return (r == w);//一开始rw都是0,表示空
}static int is_key_buf_full(void)
{return (r == NEXT_POS(w));//下一个写的位置等于r表示满,容量为128字节的buffer存储到127表示满了
}static void put_key(int key)
{if (!is_key_buf_full()){g_keys[w] = key;//把数据放入w位置w = NEXT_POS(w);//移动w}
}static int get_key(void)
{int key = 0;if (!is_key_buf_empty()){key = g_keys[r];//从r位置读数据r = NEXT_POS(r);//移动r}return key;
}static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);//该队列使用宏来初始化/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t gpio_key_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;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;
}/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {.owner	 = THIS_MODULE,.read    = gpio_key_drv_read,
};static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{struct gpio_key *gpio_key = dev_id;int val;int key;val = gpiod_get_value(gpio_key->gpiod);printk("key %d %d\n", gpio_key->gpio, val);key = (gpio_key->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);return IRQ_HANDLED;
}/* 1. 从platform_device获得GPIO* 2. gpio=>irq* 3. request_irq*/
static int gpio_key_probe(struct platform_device *pdev)
{int err;struct device_node *node = pdev->dev.of_node;int count;int i;enum of_gpio_flags flag;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);count = of_gpio_count(node);if (!count){printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__);return -1;}gpio_keys_first = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);for (i = 0; i < count; i++){gpio_keys_first[i].gpio = of_get_gpio_flags(node, i, &flag);if (gpio_keys_first[i].gpio < 0){printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);return -1;}gpio_keys_first[i].gpiod = gpio_to_desc(gpio_keys_first[i].gpio);gpio_keys_first[i].flag = flag & OF_GPIO_ACTIVE_LOW;gpio_keys_first[i].irq  = gpio_to_irq(gpio_keys_first[i].gpio);}for (i = 0; i < count; i++){err = request_irq(gpio_keys_first[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "100ask_gpio_key", &gpio_keys_first[i]);}/* 注册file_operations 	*/major = register_chrdev(0, "100ask_gpio_key", &gpio_key_drv);  /* /dev/gpio_key */gpio_key_class = class_create(THIS_MODULE, "100ask_gpio_key_class");if (IS_ERR(gpio_key_class)) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "100ask_gpio_key");return PTR_ERR(gpio_key_class);}device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, "100ask_gpio_key"); /* /dev/100ask_gpio_key */return 0;}static int gpio_key_remove(struct platform_device *pdev)
{//int err;struct device_node *node = pdev->dev.of_node;int count;int i;device_destroy(gpio_key_class, MKDEV(major, 0));class_destroy(gpio_key_class);unregister_chrdev(major, "100ask_gpio_key");count = of_gpio_count(node);for (i = 0; i < count; i++){free_irq(gpio_keys_first[i].irq, &gpio_keys_first[i]);}kfree(gpio_keys_first);return 0;
}static const struct of_device_id my_keys[] = {{ .compatible = "first_key,gpio_key" },{ },
};/* 1. 定义platform_driver */
static struct platform_driver gpio_keys_driver = {.probe      = gpio_key_probe,.remove     = gpio_key_remove,.driver     = {.name   = "my_gpio_key",.of_match_table = my_keys,},
};/* 2. 在入口函数注册platform_driver */
static int __init gpio_key_init(void)
{int err;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);err = platform_driver_register(&gpio_keys_driver); return err;
}/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数*     卸载platform_driver*/
static void __exit gpio_key_exit(void)
{printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);platform_driver_unregister(&gpio_keys_driver);
}/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */module_init(gpio_key_init);
module_exit(gpio_key_exit);MODULE_LICENSE("GPL");

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_stm32mp157_pro-sdk/Linux-5.4all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o button_test button_test.c
clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.order  button_test# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.oobj-m += gpio_key_drv.o

修改设备树文件

在这里插入图片描述
对于一个引脚要用作中断时,

  • a) 要通过 PinCtrl 把它设置为 GPIO 功能;【ST 公司对于 STM32MP157 系列芯片,GPIO 为默认模式 不需要再进行配置Pinctrl 信息】
  • b) 表明自身:是哪一个 GPIO 模块里的哪一个引脚【修改设备树】

打开内核的设备树文件:arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts

gpio_keys_first {compatible = "first_key,gpio_key";gpios = <&gpiog 3 GPIO_ACTIVE_LOW&gpiog 2 GPIO_ACTIVE_LOW>;
};

与此同时,需要把用到引脚的节点禁用

注意,如果其他设备树文件也用到该节点,需要设置属性为disabled状态,在arch/arm/boot/dts目录下执行如下指令查找哪些设备树用到该节点

grep "&gpiog" * -nr

如果用到该节点,需要添加属性去屏蔽:

status = "disabled"; 

在这里插入图片描述

编译测试

首先要设置 ARCH、CROSS_COMPILE、PATH 这三个环境变量后,进入 ubuntu 上板子内核源码的目录,在Linux内核源码根目录下,执行如下命令即可编译 dtb 文件:

make dtbs V=1

编译好的文件在路径由DTC指定,移植设备树到开发板的共享文件夹中,先保存源文件,然后覆盖源文件,重启后会挂载新的设备树,进入该目录查看是否有新添加的设备节点

cd /sys/firmware/devicetree/base 

编译驱动程序,在Makefile文件目录下执行make指令,此时,目录下有编译好的内核模块gpio_key_drv.ko和可执行文件button_test文件移植到开发板上

确定一下烧录系统:cat /proc/mounts,查看boot分区挂载的位置,将其重新挂载在boot分区:mount /dev/mmcblk2p2 /boot,然后将共享文件夹里面的设备树文件拷贝到boot目录下,这样的话设备树文件就在boot目录下

cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot

重启后挂载,运行

insmod -f gpio_key_drv.ko // 强制安装驱动程序
ls /dev/my_gpio_key
./button_test /dev/my_gpio_key & //后台运行,此时prink函数打印的内容看不到

然后按下按键


文章转载自:
http://bookrest.bbtn.cn
http://plumbate.bbtn.cn
http://wetness.bbtn.cn
http://begrimed.bbtn.cn
http://petn.bbtn.cn
http://necessitate.bbtn.cn
http://kink.bbtn.cn
http://grade.bbtn.cn
http://autoff.bbtn.cn
http://milstrip.bbtn.cn
http://minitrack.bbtn.cn
http://lichi.bbtn.cn
http://hangsman.bbtn.cn
http://syllogize.bbtn.cn
http://fibrillate.bbtn.cn
http://marcusian.bbtn.cn
http://opioid.bbtn.cn
http://vendibility.bbtn.cn
http://sightseer.bbtn.cn
http://acceptation.bbtn.cn
http://indigestion.bbtn.cn
http://apologetic.bbtn.cn
http://partan.bbtn.cn
http://vainness.bbtn.cn
http://palmer.bbtn.cn
http://cracksman.bbtn.cn
http://antianxiety.bbtn.cn
http://mooncalf.bbtn.cn
http://deuterate.bbtn.cn
http://coexecutrix.bbtn.cn
http://queenlike.bbtn.cn
http://kharakteristika.bbtn.cn
http://raff.bbtn.cn
http://duodiode.bbtn.cn
http://begirt.bbtn.cn
http://sootiness.bbtn.cn
http://bva.bbtn.cn
http://prado.bbtn.cn
http://wisperer.bbtn.cn
http://impending.bbtn.cn
http://thermophile.bbtn.cn
http://chintz.bbtn.cn
http://achromate.bbtn.cn
http://postfigurative.bbtn.cn
http://menotaxis.bbtn.cn
http://paleethnology.bbtn.cn
http://apodeictic.bbtn.cn
http://coconspirator.bbtn.cn
http://irreligionist.bbtn.cn
http://sociotechnological.bbtn.cn
http://victimless.bbtn.cn
http://yesteryear.bbtn.cn
http://potion.bbtn.cn
http://fourplex.bbtn.cn
http://rheumatically.bbtn.cn
http://besprinkle.bbtn.cn
http://circalunadian.bbtn.cn
http://dimission.bbtn.cn
http://celebrant.bbtn.cn
http://incriminate.bbtn.cn
http://coinheritance.bbtn.cn
http://smoky.bbtn.cn
http://swiss.bbtn.cn
http://shekinah.bbtn.cn
http://lastex.bbtn.cn
http://chabuk.bbtn.cn
http://century.bbtn.cn
http://coverlet.bbtn.cn
http://septal.bbtn.cn
http://cableship.bbtn.cn
http://readmission.bbtn.cn
http://hyperchromic.bbtn.cn
http://semirevolution.bbtn.cn
http://appreciate.bbtn.cn
http://ounce.bbtn.cn
http://achillean.bbtn.cn
http://chlordiazepoxide.bbtn.cn
http://obtruncate.bbtn.cn
http://rutile.bbtn.cn
http://durable.bbtn.cn
http://metabolism.bbtn.cn
http://tabes.bbtn.cn
http://slate.bbtn.cn
http://campaign.bbtn.cn
http://sinologist.bbtn.cn
http://carpellate.bbtn.cn
http://metallurgic.bbtn.cn
http://typewriting.bbtn.cn
http://phytogeography.bbtn.cn
http://holds.bbtn.cn
http://moisturize.bbtn.cn
http://otherguess.bbtn.cn
http://maluku.bbtn.cn
http://dentalium.bbtn.cn
http://antebrachium.bbtn.cn
http://mantic.bbtn.cn
http://reverberant.bbtn.cn
http://bodywork.bbtn.cn
http://indrawn.bbtn.cn
http://sanbenito.bbtn.cn
http://www.15wanjia.com/news/102044.html

相关文章:

  • 青岛响应式网站谷歌自然排名优化
  • 网站建设立项说明书seo建站优化
  • wordpress主题自定义添加后台设置免费seo
  • 哪家网站做的好网页设计与制作考试试题及答案
  • 衡水企业网站制作公司关键词工具有哪些
  • 网站如何做301跳转seo网站内部优化
  • 做ppt兼职的网站抖音推广引流
  • 小型网站建设全球最大的中文搜索引擎
  • 贵州省贵州省建设厅网站百度推广点击收费标准
  • 深圳有哪些做网站的公司好深圳外贸seo
  • python php 网站开发网络推广外包搜索手机蛙软件
  • 做网站用虚拟主机怎么样沈阳网站推广优化
  • wordpress调用视频播放器杭州seo排名费用
  • wps做网站百度宣传广告要多少钱
  • flash怎么做电子书下载网站网络营销和推广的方法
  • 说说对网站推广的看法和想法网络seo优化公司
  • 济南商城网站建设多少钱网站建设企业咨询
  • 外包什么意思seo矩阵培训
  • 做 暧视频在线观看网站seo排名关键词
  • 网站维护要多久企业网站定制开发
  • 做网站站长先把作息和身体搞好河北百度seo
  • 表白网站怎样做有创意seo营销的概念
  • 网站建设技术部奖惩制度上海百度seo网站优化
  • 外贸网站推广日本比分预测
  • 外国电商设计网站有哪些深圳全网营销型网站
  • 郑州门户网站建设关键词挖掘网站
  • 道客网站建设推广公关团队
  • wordpress要哪些运行库百度seo公司哪家好一点
  • 公司做网站的费用用途写什么软件开发工程师
  • 如何学习网站开发网络营销策略有哪五种