您的位置:首页 > 科技 > IT业 > 游戏ui设计是什么_广州工程有限公司_互联网推广员是做什么_头条搜索站长平台

游戏ui设计是什么_广州工程有限公司_互联网推广员是做什么_头条搜索站长平台

2025/7/14 1:42:57 来源:https://blog.csdn.net/weixin_40933496/article/details/143256458  浏览:    关键词:游戏ui设计是什么_广州工程有限公司_互联网推广员是做什么_头条搜索站长平台
游戏ui设计是什么_广州工程有限公司_互联网推广员是做什么_头条搜索站长平台

1.在上一个博客的基础上,添加一个字符设备

https://blog.csdn.net/weixin_40933496/article/details/143253515?spm=1001.2014.3001.5501

2.在probe函数中注册字符设备

register_chrdev(包含对应的file_operations结构体)
class_create
device_create

3.在中断处理函数中唤醒等待队列,然后在read函数中返回按键值。
在这里插入图片描述
在这里插入图片描述
3.实现的代码
3.1驱动程序

#include <linux/irqreturn.h>
#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>/* 定义结构体来描述gpio */
struct gpio_key{int gpio;struct gpio_desc* gpiod;int flag;int irq;
};/* 定义全局变量来存储设备树中的所有gpio节点信息 */
static struct gpio_key* gpio_keys_100ask;/* 字符设备的主设备号 */
static unsigned int major = 0;
static struct class *gpio_class;
static int g_key = 0;/* 定义等待队列 */
static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);static ssize_t gpio_read(struct file *fp, char __user *buf, size_t size, loff_t * offset)
{int err;wait_event_interruptible(gpio_key_wait, g_key);err = copy_to_user(buf, &g_key, 4);g_key = 0;return 4;
}static const struct file_operations gpio_fops = {.owner = THIS_MODULE,.read  = gpio_read,
};static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{struct gpio_key* gpio_key = dev_id;int val;val = gpio_get_value(gpio_key->gpio);//printk("key %d %d\n", gpio_key->gpio, val);g_key = (gpio_key->gpio << 8) | val;wake_up_interruptible(&gpio_key_wait);return IRQ_HANDLED;
}static int gpio_probe(struct platform_device *pdev)
{int count, i;struct device_node *node;int err;node = pdev->dev.of_node;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_100ask = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);if (!gpio_keys_100ask){printk("kzalloc error\n");return -1;}/* 获得资源 */for (i = 0; i < count; i++){gpio_keys_100ask[i].gpio = of_get_gpio(node, i);if (gpio_keys_100ask[i].gpio < 0){printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);return -1;}gpio_keys_100ask[i].gpiod = gpio_to_desc(gpio_keys_100ask[i].gpio);gpio_keys_100ask[i].irq   = gpio_to_irq(gpio_keys_100ask[i].gpio);/* 申请中断 */err = request_irq(gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "test1_gpio_keys_100ask", &gpio_keys_100ask[i]);if (err) {printk("request_irq err\n");}}/* 注册字符设备 */major = register_chrdev(major, "100ask_key", &gpio_fops);if(major < 0){printk("register_chrdev err'\n");return -1;}/* 注册类 */gpio_class = class_create(THIS_MODULE, "100ask_key_class");/* 注册设备 */device_create(gpio_class, NULL, MKDEV(major,0), NULL, "100ask_key_button");printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}static int gpio_remove(struct platform_device *pdev)
{int count, i;struct device_node *node;node = pdev->dev.of_node;count = of_gpio_count(node);device_destroy(gpio_class, MKDEV(major,0));class_destroy(gpio_class);unregister_chrdev(major, "100ask_key");for (i = 0; i < count; i++){free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]);}kfree(gpio_keys_100ask);printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}/*
* 在设备树中添加的节点的compatible属性为:"test1_100ask,test1_gpio_key"
*/
static const struct of_device_id gpio_key_of_match[] = {{.compatible = "test1_100ask,test1_gpio_key"},{/* 这里必须要有一个空项,表示结束 */}
};static struct platform_driver gpio_driver = {.driver = {.name	= "test1_gpio_keys_100ask",.of_match_table = gpio_key_of_match,},.probe	= gpio_probe,.remove = gpio_remove,
};/* 基于platform总线来实现这个程序 */
static int gpio_init(void)  
{int ret;ret = platform_driver_register(&gpio_driver);if (ret != 0){printk("platform_driver_register err\n");return -1;}printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return ret;
}static void gpio_exit(void)
{platform_driver_unregister(&gpio_driver);printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
}module_init(gpio_init);
module_exit(gpio_exit);MODULE_LICENSE("GPL");

3.2应用程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char** argv)
{int fd;int val;int num;/* 判断输入值是否合法 */if(argc != 2){printf("Usage ./button_app /dev/devname\n");return -1;}fd = open(argv[1], O_RDONLY);if (fd == -1){printf("open err\n");}while(1){read(fd, &val, 4);num  = val >>8;printf("get button: %x val = %x\n", num, val - num *(1 << 8));}close(fd);return 0;
}

3.3Makefile文件

KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88all:make -C $(KERN_DIR) M=`pwd` modules$(CROSS_COMPILE)gcc -o button_app button_app.c
clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderrm -rf button_app
obj-m += button_drv.o

3.4运行方式
在这里插入图片描述
4.实现方式
4.1 gpio_probe中添加字符设备代码
在这里插入图片描述
4.2 gpio_remove函数中添加对应的删除代码
在这里插入图片描述
4.3 定义全局变量,等待队列
在这里插入图片描述
4.4在中断服务程序中,给全局变量赋值,并且唤醒等待队列
在这里插入图片描述
4.5此时,.read函数会将数据拷贝回用户空间
在这里插入图片描述
5.文件路径

/home/book/nfs_rootfs/CSDN/01_gpio_irq/02_read_key_irq

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com