使用platform框架实现led流水灯和按键中断开启风扇

2023-05-16

需求:

使用platform框架实现led流水灯和按键中断开启风扇

实现过程:

设备树文件:

/dts-v1/;
#include "stm32mp157.dtsi"
#include "stm32mp15xa.dtsi"
#include "stm32mp15-pinctrl.dtsi"
#include "stm32mp15xxac-pinctrl.dtsi"
#include "stm32mp15xx-fsmp1x.dtsi" 
/ {
	model = "HQYJ STM32MP157 FSMP1A Discovery Board";
	compatible = "st,stm32mp157a-dk1", "st,stm32mp157";

	aliases {
		serial0 = &uart4;
		serial5 = &usart3;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	reserved-memory {
			gpu_reserved: gpu@d4000000 {
				  reg = <0xd4000000 0x4000000>;
				  no-map;
			  };
		
			optee_memory: optee@0xde000000 {
				  reg = <0xde000000 0x02000000>;
				  no-map;
			  };
	};
	myplatform{        //添加的platform内容
		compatible = "hqyj,platform";
		reg = <0x12345678 0x14>;
		interrupt-parent = <&gpiof>;
		interrupts = <8 0>;
		led1=<&gpioe 10 0>;
		led2=<&gpiof 10 0>;
		led3=<&gpioe 8 0>;
		fan=<&gpioe 9 0>;
	};
};

驱动源文件:

#include "pdrv.h"

//KEY3 PF8 中断处理函数
irqreturn_t irq1_handler(int irqno, void *arg)
{
    gpiod_set_value(gpiono4, !gpiod_get_value(gpiono4));
    return IRQ_HANDLED;
}

//ioctl
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    int whitch;
    int ret;
    switch (cmd)
    {
    case LED_ON:
        ret = copy_from_user(&whitch, (void *)args, sizeof(int));
        if (ret)
        {
            printk("copy_from_user failed\n");
            return -EIO;
        }
        switch (whitch)
        {
        case LED1:
            gpiod_set_value(gpiono1, 1);
            break;
        case LED2:
            gpiod_set_value(gpiono2, 1);
            break;
        case LED3:
            gpiod_set_value(gpiono3, 1);
            break;
        }
        break;
    case LED_OFF:
        ret = copy_from_user(&whitch, (void *)args, sizeof(int));
        if (ret)
        {
            printk("copy from user is error\n");
            return -EIO;
        }
        switch (whitch)
        {
        case LED1:
            gpiod_set_value(gpiono1, 0);
            break;
        case LED2:
            gpiod_set_value(gpiono2, 0);
            break;
        case LED3:
            gpiod_set_value(gpiono3, 0);
            break;
        }
        break;
    }
    return 0;
}

const struct file_operations fops = {
    .unlocked_ioctl = mycdev_ioctl,
};

int pdrv_probe(struct platform_device *pdev)
{
    int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);

    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (NULL == res)
    {
        printk("获取设备信息失败\n");
        return -ENODATA;
    }
    printk("mem类型资源数值为:%x\n", res->start);

    gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono1))
    {
        printk("解析gpio1编号失败\n");
        return PTR_ERR(gpiono1);
    }
    printk("解析gpio1编号成功\n");
    gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("解析gpio2编号失败\n");
        return PTR_ERR(gpiono2);
    }
    printk("解析gpio2编号成功\n");
    gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("解析gpio3编号失败\n");
        return PTR_ERR(gpiono3);
    }
    printk("解析gpio3编号成功\n");
    gpiono4 = gpiod_get_from_of_node(pdev->dev.of_node, "fan", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono4))
    {
        printk("解析gpio4编号失败\n");
        return PTR_ERR(gpiono4);
    }
    printk("解析gpio4编号成功\n");

    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("获取中断类型资源失败\n");
        return -ENODATA;
    }
    printk("中断类型的资源数值为%d\n", irqno);
    request_irq(irqno, irq1_handler, IRQF_TRIGGER_FALLING, "myirq", 0);

    // 1.分配对象
    cdev = cdev_alloc();
    if (NULL == cdev)
    {
        printk("分配对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("分配对象空间成功\n");
    // 2.初始化驱动对象
    cdev_init(cdev, &fops);
    if (0 == major)
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    else if (major > 0)
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "mycdev");
        if (ret)
        {
            printk("静态申请设备号失败\n");
            goto ERR2;
        }
    }
    // 3.注册对象
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("驱动对象注册进内核失败\n");
        goto ERR3;
    }
    printk("驱动对象注册进内核成功\n");
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto ERR4;
    }
    printk("向上提交目录成功\n");

    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev0");
    if (IS_ERR(dev))
    {
        printk("向上提交结点信息失败\n");
        goto ERR5;
    }
    printk("向上提交结点信息成功\n");

    printk("向上提交设备结点成功\n");
    printk("major = %d\n", major);
    return 0;
ERR5:
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
ERR2:
    kfree(cdev);
ERR1:
    return ret;

    return 0;
}

int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major, minor), 3);
    kfree(cdev);
    free_irq(irqno, NULL);
    gpiod_set_value(gpiono1, 0);
    gpiod_put(gpiono1);
    gpiod_set_value(gpiono2, 0);
    gpiod_put(gpiono2);
    gpiod_set_value(gpiono3, 0);
    gpiod_put(gpiono3);
    gpiod_set_value(gpiono4, 0);
    gpiod_put(gpiono4);
    return 0;
}

struct of_device_id oftable[] =
    {
        {.compatible = "hqyj,platform"},
        {},
};

struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "aaaa",
        .of_match_table = oftable,
    },
};

module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

驱动头文件:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>

struct resource* res;
int irqno;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct gpio_desc* gpiono4;
int irqno;

struct cdev* cdev;
dev_t devno;
#if 0
unsigned int major = 0;
#else 
unsigned int major = 500;
#endif
unsigned int minor = 0;
struct class* cls;
struct device* dev;

#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)

enum{
    LED1,
    LED2,
    LED3,
};

测试文件:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>

#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)

enum{
    LED1,
    LED2,
    LED3,
};

int main(int argc,const char * argv[])
{
    int cmd;
    int fd = -1;
    fd = open("/dev/mycdev0",O_RDWR);
    if(fd == -1)
    {
        perror("open is error\n");
        return -1;
    }
    int whitch;
    while(1)
    {
        whitch = LED1;
        ioctl(fd,LED_ON, &whitch);
        sleep(1);
        ioctl(fd,LED_OFF, &whitch);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        sleep(1);

        whitch = LED2;
        ioctl(fd,LED_ON, &whitch);
        sleep(1);
        ioctl(fd,LED_OFF, &whitch);
        sleep(1);

        whitch = LED3;
        ioctl(fd,LED_ON, &whitch);
        sleep(1);
        ioctl(fd,LED_OFF, &whitch);
        sleep(1);
    }
    return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用platform框架实现led流水灯和按键中断开启风扇 的相关文章

  • STM32报错解决:..\OBJ\LED.axf: error: L6002U: Could not open file ..\obj\main.o: No such file/

    出现这个问题的原因主要有MDK的安装路径为中文 xff0c 或电脑的用户名为中文 xff0c 解决方法见如下的链接 xff1a 常规的原因 也有出现语法错误导致的 xff0c 如下面这位博主的情况 语法错误 而我遇到这个问题的原因比较难以确
  • LED高效恒流驱动电源的设计指导书

    参考链接 LED高效恒流驱动电源的设计指导书 LED灯驱动电源设计 LED恒流驱动电路 精 LED恒流驱动电路 led灯驱动电源电路图 led灯的驱动原理电路图方案详解 KIA MOS管 一 LED驱动电源原理 1 由于LED的光特性通常都
  • LED与照明光学基础知识

    LED封装类型 插件封装 xff08 小功率 xff09 xff1b 角度受控COB封装 xff08 超大功率 xff09 xff1b 出光面积大贴片封装 xff08 中功率 xff09 xff1b 出光面积大贴片封装 xff08 大功率
  • ESP32设备驱动-PCA9685 LED控制器驱动

    PCA9685 LED控制器驱动 文章目录 PCA9685 LED控制器驱动 1 PCA9685介绍 2 硬件准备 3 软件准备 4 驱动实现 1 PCA9685介绍 PCA9685 是一款 I C 总线控制的 16 通道 LED 控制器
  • Pytorch框架实现WGAN-GP

    目录 1 WGAN GP产生背景 2 WGAN GP的主要成就 3 权重限制的困难 Difficulties with weight constraints xff08 1 xff09 WGAN GP算法流程 xff08 2 xff09 梯
  • Pytorch框架实现Pix2Pix(Image-to-image)

    目录 1 pix2pix研究背景 2 Pix2Pix基本原理 xff08 1 xff09 原理图 xff08 2 xff09 条件GAN cGAN xff08 3 xff09 公式原理 3 Pix2Pix网络模型 xff08 1 xff09
  • Android手机控制ZigBee板上LED

    环境 xff1a Windows 编译器 xff1a IAREW8051 8 1 硬件 xff1a CC2530 协议栈 xff1a ZStack CC2530 2 3 0 1 4 0 手机 xff1a Android4 1 2 又重新开始
  • LED测试方案及光谱图

  • 驱动框架入门之LED-linux驱动开发第4部分-朱有鹏-专题视频课程

    驱动框架入门之LED linux驱动开发第4部分 5199人已学习 课程介绍 本课程是linux驱动开发的第4个课程 xff0c 主要内容是驱动框架的引入 通过led驱动框架和gpiolib的这两个框架的详细解释 xff0c 让大家学习内核
  • ST-Link的LED指示灯说明

    自ST LINK V2以来的所有ST LINK板都实现了一个标有 COM 的LED 无论是在外壳上还是在PCB上 一般 COM 是由红 绿两个LED组合 xff0c 有常亮 常灭 闪烁等 xff0c 两个LED同时亮呈现橙色 ST Link
  • 点亮LED灯及IAR调试

    点亮LED灯 一开始想先通过简单的点亮一个灯的程序来试一下MCU xff0c 硬件上的原理图如下 xff1a 从这里看出PB4 PB5 PC3 PC4 PC5 PC7都能用 xff0c 还没接外设 xff0c 一开始我选择了 PB5 去外接
  • 十一、Linux驱动之platform总线设备驱动

    1 基本概念 从Linux2 6开始Linux加入了一套驱动管理和注册机制 platform平台总线驱动模型 platform平台总线是一条虚拟总线 platform device为相应的设备 platform driver为相应的驱动 与
  • Visual Studio 在 Windows 7 上将 WINVER/_WIN32_WINNT 设置为 Windows 8?

    我正在使用 Visual Studio 2012 在 Windows 7 x64 上执行一些测试 看起来 Microsoft 的工具链正在设置 WIN32 WINNT to 0x602 WIN32 WINNT WIN8 运行我们的测试程序结
  • VHDL——连接开关和LED

    我有 Xilinx Spartan6 和下一个 VHDL 代码 library ieee use ieee std logic 1164 all use ieee numeric std all entity Switches Leds i
  • Netbeans 中的 Arduino(处理)库和控制

    我正在尝试控制 4 个 LED 并从 4 个触点获取模拟输入 该程序是用java编写的 因此要访问arduino的功能 例如AnalogRead 和将LED设置为高或低 导入处理库可以让程序使用这些功能吗 我还想知道 如果程序会自行传输到a
  • 在 Ruby 中检测 Linux 发行版/平台

    我可以通过多种方式检查运行 Ruby 代码的平台的操作系统 RUBY PLATFORM https stackoverflow com a 171011 462015 https stackoverflow com a 171011 462
  • 将 Arduino RGB LED 从一种颜色渐变为另一种颜色?

    目前 我已成功让 LED 灯循环显示我选择的八种颜色 一切都工作正常 除了我想要一种更自然的感觉 并且想要从一种颜色褪色 过渡到下一种颜色 而不是让它们互相替换 到目前为止 这是我的代码 int redPin 11 int greenPin
  • JavaFX 单实例应用程序

    尝试做到这一点 当用户 关闭 程序时单击所有退出按钮 这样就不再有托盘图标 我调用 Platform setImplicitExit false 所以程序仍然在后台运行 我正在尝试学习如何做到这一点 以便当用户重新单击运行 jar 的 ex
  • 如何使用 Ionic 4 检测平台

    如何使用 Ionic 4 检测浏览器和移动网络平台 因为当我在桌面浏览器中尝试使用以下代码时 它没有落入 core 健康 状况 if this platform is core alert core platform else alert
  • 如何将WCF项目平台目标更改为x86?

    我编写了一个使用第三方项目的项目 只有在 项目属性 下的 构建 下的 平台目标 设置为 X86 而不是 我的项目 中的 任何CPU 时 我才能运行它 但是 当我尝试将此第三方项目与我的 WCF 项目一起使用时 平台目标 功能不会出现在 WC

随机推荐