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

通过platform总线驱动框架编写LED灯的驱动,编写应用程序测试

mydev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>// 创建功能码
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)int major;
struct class *cls;
struct device *devi;struct gpio_desc *gpiono1; // led1设备号
struct gpio_desc *gpiono2; // led2设备号
struct gpio_desc *gpiono3; // led3设备号long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case LED_ON:gpiod_set_value(gpiono1, 1);gpiod_set_value(gpiono2, 1);gpiod_set_value(gpiono3, 1);break;case LED_OFF:gpiod_set_value(gpiono1, 0); gpiod_set_value(gpiono2, 0);gpiod_set_value(gpiono3, 0);break;}return 0;
}// 定义操作方法结构体变量并赋值
struct file_operations fops = {.unlocked_ioctl = mycdev_ioctl,};
//封装probe函数,当设备和驱动匹配成功之后执行
int pdrv_probe(struct platform_device *dev)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);// 字符设备驱动注册major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("字符设备驱动注册失败\n");return major;}printk("字符设备驱动注册成功:major=%d\n", major);// 向上提交目录cls = class_create(THIS_MODULE, "mychrdev");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录成功\n");// 向上提交设备节点信息int i; for (i = 0; i < 3; i++){devi = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);if (IS_ERR(dev)){printk("向上提交设备节点失败\n");return -PTR_ERR(dev);}}printk("向上提交设备节点成功\n");// 解析LED1的gpio编号gpiono1 = gpiod_get_from_of_node(dev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono1 == NULL){printk("解析led1对应gpio编号失败\n");return -ENXIO;}printk("解析led1对应gpio编号成功\n");// 解析LED1的gpio编号gpiono2 = gpiod_get_from_of_node(dev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono2 == NULL){printk("解析led2对应gpio编号失败\n");return -ENXIO;}printk("解析led2对应gpio编号成功\n");// 解析LED1的gpio编号gpiono3 = gpiod_get_from_of_node(dev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono3 == NULL){printk("解析led3对应gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");return 0;
}// 封装remove函数,用于驱动和设备卸载时执行
int pdrv_remove(struct platform_device *dev)
{// 销毁设备节点信息device_destroy(cls, MKDEV(major, 0));// 销毁设备节点信息int i;for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}// 释放gpio编号gpiod_put(gpiono1);gpiod_put(gpiono2);gpiod_put(gpiono3);// 销毁目录class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mychrdev");printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
// 构建用于匹配的设备树表
struct of_device_id oftable[] = {{.compatible = "hqyj,myplatform",},{/* end node */}, // 防止数组越界
};
// 分配驱动对象并初始化
struct platform_driver pdrv = {.probe = pdrv_probe,.remove = pdrv_remove,.driver = {.name = "bbbbb",.of_match_table = oftable, // 设置设备树匹配},};
// 一键注册宏
module_platform_driver(pdrv);MODULE_LICENSE("GPL");

test.c

#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>
//创建功能码
#define LED_ON _IO('l',1)  
#define LED_OFF _IO('l',0)int main(int argc, char const *argv[])
{int a;int fd=open("/dev/myled0",O_RDWR);if(fd<0){printf("打开设备文件失败\n");exit(-1);}while(1){//从终端读取printf("请输入要实现的功能\n");printf("0(关灯) 1(开灯)\n");printf("请输入>");scanf("%d",&a);switch(a){case 1:ioctl(fd,LED_ON);break;case 0:ioctl(fd,LED_OFF);break;}}close(fd);return 0;
}

http://www.lryc.cn/news/304723.html

相关文章:

  • 费舍尔FISHER金属探测器探测仪维修F70
  • Airtest-Selenium实操小课③:下载可爱猫猫图片
  • Druid无法登录监控页面
  • 【Linux系统化学习】深入理解匿名管道(pipe)和命名管道(fifo)
  • 信息学奥赛一本通1209:分数求和
  • LabVIEW储氢材料循环寿命测试系统
  • Unity3D 框架如何搭建基于纯Lua的U框架与开发模式详解
  • Linux常见指令(2)
  • 【C++】封装
  • Maxwell安装部署
  • 说一下JVM类加载机制?
  • 解决SpringAMQP工作队列模型程序报错:WARN 48068:Failed to declare queue: simple.queue
  • mysql在服务器中的主从复制Linux下
  • QT-Day2
  • 流量分析——陇剑杯 2021【签到、jwt】
  • Java并发基础:原子类之AtomicIntegerFieldUpdater全面解析
  • 普中51单片机学习(串口通信)
  • 【ArcGIS】利用高程进行坡度分析
  • 递归读取文件夹下的所有文件
  • phpspreadsheet导出数据和图片到excel
  • Seata的 TCC 模式
  • Vue全局指令防止重复点击(等待请求)
  • 数据库索引面试的相关问题
  • Spring启动生命周期
  • 瑞芯微RK3568芯片介绍
  • 15.一种坍缩式的简单——组合模式详解
  • Node.js的debug模块源码分析及在harmonyOS平台移植
  • 【Crypto | CTF】BUUCTF RSA2
  • 单片机学习笔记---红外遥控红外遥控电机调速(完结篇)
  • Linux第62步_备份移植好的所有的文件和文件夹