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

丝绸之路网站建设报告高清服务器大全

丝绸之路网站建设报告,高清服务器大全,网络推广与推广,wordpress图片存到七牛云编写LED灯的驱动&#xff0c;使用GPIO子系统&#xff0c;里面添加按键的中断处理 1.应用程序发送指令控制LED亮灭 2.按键1 按下&#xff0c;led1电位反转 按键2按下&#xff0c;led2电位反转 按键3 按下&#xff0c;led3电位反转 驱动程序 #include <linux/init.h> #i…

编写LED灯的驱动,使用GPIO子系统,里面添加按键的中断处理

1.应用程序发送指令控制LED亮灭

2.按键1 按下,led1电位反转 按键2按下,led2电位反转 按键3 按下,led3电位反转

驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
struct device_node *dev_key;
unsigned int irqno_key1;
unsigned int irqno_key2;
unsigned int irqno_key3;struct device_node *dev_led;
struct gpio_desc *gpiono_led1;
struct gpio_desc *gpiono_led2;
struct gpio_desc *gpiono_led3;int major;
struct class *cls;
struct device *dev;
// 中断处理函数
irqreturn_t myirq_handler_key1(int irq, void *dev)
{gpiod_set_value(gpiono_led1,!gpiod_get_value(gpiono_led1));return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key2(int irq, void *dev)
{gpiod_set_value(gpiono_led2,!gpiod_get_value(gpiono_led2));return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key3(int irq, void *dev)
{gpiod_set_value(gpiono_led3,!gpiod_get_value(gpiono_led3));return IRQ_HANDLED;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case LED_ON:switch (arg){case 1:                           // LED1gpiod_set_value(gpiono_led1,1);; // LED1开灯break;case 2:                           // LED2gpiod_set_value(gpiono_led2,1); // LED2开灯break;case 3:                          // LED3gpiod_set_value(gpiono_led3,1); // LED3开灯break;}break;case LED_OFF:switch (arg){case 1:gpiod_set_value(gpiono_led1,0);break;case 2:gpiod_set_value(gpiono_led2,0);break;case 3:gpiod_set_value(gpiono_led3,0);break;}break;}return 0;
}
struct file_operations fops = {.unlocked_ioctl = mycdev_ioctl,};
static int __init mycdev_init(void)
{int i;// 字符设备驱动注册major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("字符设备驱动注册失败\n");return major;}printk("字符设备驱动注册成功:major=%d\n", major);// 向上提交目录cls = class_create(THIS_MODULE, "myled");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功\n");// 向上提交设备节点信息for (i = 0; i < 3; i++){dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);if (IS_ERR(dev)){printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev);}}printk("向上提交设备节点成功\n");int ret;// 解析按键的设备树节点dev_key = of_find_node_by_path("/myirq");if (dev_key == NULL){printk("解析设备树节点失败\n");return -EFAULT;}printk("解析设备树节点成功\n");// 根据设备树节点解析出软中断号irqno_key1 = irq_of_parse_and_map(dev_key, 0); // 按键1索引号为0if (!irqno_key1){printk("解析软中断号失败\n");return -ENXIO;}printk("key1解析软中断号成功 irqno=%d\n", irqno_key1);irqno_key2 = irq_of_parse_and_map(dev_key, 1); // 按键1索引号为0if (!irqno_key2){printk("解析软中断号失败\n");return -ENXIO;}printk("key2解析软中断号成功 irqno=%d\n", irqno_key2);irqno_key3 = irq_of_parse_and_map(dev_key, 2); // 按键1索引号为0if (!irqno_key3){printk("解析软中断号失败\n");return -ENXIO;}printk("key3解析软中断号成功 irqno=%d\n", irqno_key3);// 注册中断ret = request_irq(irqno_key1, myirq_handler_key1, IRQF_TRIGGER_FALLING, "key1", NULL);if (ret){printk("注册中断失败\n");return ret;}printk("key1注册中断成功\n");ret = request_irq(irqno_key2, myirq_handler_key2, IRQF_TRIGGER_FALLING, "key2", NULL);if (ret){printk("注册中断失败\n");return ret;}printk("key2注册中断成功\n");ret = request_irq(irqno_key3, myirq_handler_key3, IRQF_TRIGGER_FALLING, "key3", NULL);if (ret){printk("注册中断失败\n");return ret;}printk("key3注册中断成功\n");// 根据设备树节点的路径解析设备树信息dev_led = of_find_node_by_path("/leds");if (dev_led == NULL){printk("解析设备树节点失败\n");return -EFAULT;}printk("解析设备树节点成功\n");// 申请gpio_desc对象并设置输出为低电平gpiono_led1 = gpiod_get_from_of_node(dev_led, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono_led1)){printk("申请gpio对象失败\n");return -PTR_ERR(gpiono_led1);}printk("申请gpio_led1对象成功\n");gpiono_led2 = gpiod_get_from_of_node(dev_led, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono_led2)){printk("申请gpio对象失败\n");return -PTR_ERR(gpiono_led2);}printk("申请gpio_led1对象成功\n");gpiono_led3 = gpiod_get_from_of_node(dev_led, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono_led3)){printk("申请gpio对象失败\n");return -PTR_ERR(gpiono_led3);}printk("申请gpio_led1对象成功\n");return 0;
}
static void __exit mycdev_exit(void)
{// 注销中断free_irq(irqno_key1, NULL);free_irq(irqno_key2, NULL);free_irq(irqno_key3, NULL);// 灭灯gpiod_set_value(gpiono_led1, 0);// 释放gpio编号gpiod_put(gpiono_led1);// 灭灯gpiod_set_value(gpiono_led2, 0);// 释放gpio编号gpiod_put(gpiono_led2);// 灭灯gpiod_set_value(gpiono_led3, 0);// 释放gpio编号gpiod_put(gpiono_led3);int i;for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}// 销毁目录信息class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用程序

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"int main(int argc, char const *argv[])
{int a, b;char buf[128] = {0};int fd0 = open("/dev/mycdev0", O_RDWR);if (fd0 < 0){printf("打开设备文件失败\n");exit(-1);}int fd1 = open("/dev/mycdev1", O_RDWR);if (fd1 < 0){printf("打开设备文件失败\n");exit(-1);}int fd2 = open("/dev/mycdev2", O_RDWR);if (fd2 < 0){printf("打开设备文件失败\n");exit(-1);}while (1){// 从终端读取printf("请输入指令\n");printf("0(关灯) 1(开灯)\n");printf("请输入>");scanf("%d", &a);printf("请输入要控制的灯 1(LED1) 2(LED2) 3(LED3)>");scanf("%d", &b);switch (b){case 1:switch (a){case 1:ioctl(fd0, LED_ON); // 开灯break;case 0:ioctl(fd0, LED_OFF);break;}break;case 2:switch (a){case 1:ioctl(fd1, LED_ON); // 开灯break;case 0:ioctl(fd1, LED_OFF);break;}break;case 3:switch (a){case 1:ioctl(fd2, LED_ON); // 开灯break;case 0:ioctl(fd2, LED_OFF);break;}break;}}close(fd0);close(fd1);close(fd2);return 0;
}

http://www.yayakq.cn/news/503250/

相关文章:

  • 网站的类型是什么意思安徽建设厅官方网站
  • 网站建设策划书范文6篇自媒体视频发布平台
  • 建设手机银行官方网站网站开发总结简写
  • alexa的网站排名主要分为哪两种东莞标志设计公司
  • 湖南网站备案注销石家庄做网站的公司有哪些
  • 西城做网站软件开发需要具备什么条件
  • 移动课程播放网站建设多少钱上海网站原型设计
  • html5 网站布局应用教程通过WordPress开发的主题
  • 团队氛围建设 网站西安企业名录黄页
  • 卖域名的网站要怎么做无锡网络推广公司哪家好
  • 雄安免费网站建设seo引擎优化公司
  • 天河网站 建设seo信科分公司网络广告营销概念
  • 商城网站建设的注意事项济南网站开发xywlcn
  • 手机直接看的网站有哪些自己做网站打不开是怎么回事
  • 网站建设金华国税网站页面建设中
  • 网站备案 内容品牌形象网站建设
  • 自助搭建网站系统网站推广平台搭建
  • 教育技术学网站模版网络营销企业案例
  • 微博带动网站做排名网站建设工作人员有哪些职责
  • 网站开发前后端技术餐饮网站建设
  • 网站建设相关的工作京东做代码的网站吗
  • wordpress支持国内视频的编辑器优化设计答案六年级
  • 晋江免费网站建设长春网站seo外包
  • 免费制作单页的网站产品设计论文
  • 广东微信网站制作价格定制化网站开发
  • 苏州网站建设公司科技城wordpress 插件 手机版
  • 个人建网站吴忠北京网站建设
  • 云浮住房和城乡建设部官方网站想学平面设计哪个网上可以学
  • 12306网站制作企业vi设计包括哪些内容
  • 广东省城乡建设部网站湖北企业商城网站建设