基于GPIO子系统编写LED驱动,编写应用程序进行测试设置定时器,5秒钟打印一次hello world

应用程序 test.c

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

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int a, b;
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        // 从终端读取
        scanf("%d", &a);
        scanf("%d", &b);
        switch (a)
        {
        case 1:
            ioctl(fd, 1, b);
            break;
        case 0:
            ioctl(fd, 0, b);
            break;
        }
    }

    close(fd);

    return 0;
}

驱动文件demo.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
/*       myled
    {
        led1-gpio=<&gpioe 10 0>;
        led2-gpio=<&gpiof 10 0>;
        led3-gpio=<&gpioe 8 0>;
    };
*/
struct device_node *dnode;
// unsigned int gpiono;
struct gpio_desc *gpiono[3];
char *str[3] = {"led1-gpio", "led2-gpio", "led3-gpio"};
struct timer_list mytimer;
// 封装操作方法
int major;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    // arg-1 : 0=led1 1=led2 2=led3
    // cmd = 0 关灯,cmd = 1 开灯
    gpiod_set_value(gpiono[arg - 1], cmd);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
void mytimer_function(struct timer_list *timer)
{
    // 每5s打印一次hello world!
    printk("hello world!\n");
    printk("请输入要实现的功能 0(关灯) 1(开灯) 请输入>\n");
    printk("请选择要控制的灯:1(LED1)2(LED2) 3(LED3) 请输入>\n");
    mod_timer(timer, jiffies + 5 * HZ);
}
static int __init mycdev_init(void)
{
    // 字符设备驱动注册
    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");
    // 向上提交设备节点信息
    // 向上提交三次设备节点信息
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled%d", 0);
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");

    // 解析设备树节点信息
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    // 获取LED GPIO编号
    int i = 0;
    for (i = 0; i < 3; i++)
    {
        gpiono[i] = gpiod_get_from_of_node(dnode, str[i], 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(gpiono[i]))
        {
            printk("申请gpio信息失败\n");
            return -PTR_ERR(gpiono[i]);
        }
    }
    // 初始化定时器对象
    timer_setup(&mytimer, mytimer_function, 0);
    mytimer.expires = jiffies + 5 * HZ; // 定时5s
    // 注册定时器
    add_timer(&mytimer);

    return 0;
}
static void __exit mycdev_exit(void)
{
    // 注销定时器
    del_timer(&mytimer);
    int i = 0;
    for (i = 0; i < 3; i++)
    {
        // gpio_set_value(gpiono,0);
        gpiod_set_value(gpiono[i], 0);
        // 释放GPIO编号
        // gpio_free(gpiono);
        gpiod_put(gpiono[i]);
    }
    // 销毁设备节点信息
    device_destroy(cls, MKDEV(major, 0));

    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

 效果:

在终端输入对应1 0 / 123 就可以控制开发板上led1,led2,led3的亮灭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ck钉钉钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值