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

头文件:

#ifndef __HEAD_H__
#define __HEAD_H__

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

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>
#include "head.h"
int main(int argc, char const *argv[])
{
    char buf[20] = "";
    int a, b;
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    while (1)
    {
        printf("请输入要实现的功能 1(关灯) 0(开灯):");
        scanf("%d", &a);
        switch (a)
        {
        case 0:
            ioctl(fd, LED_ON);
            break;
        case 1:
            ioctl(fd, LED_OFF);
            break;
        }
    }
    close(fd);
    return 0;
}

驱动文件:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include "head.h"
int major;
char kbuf[20] = "";
struct class *cls;
struct device *dev;
struct device_node *dnode;
unsigned int gpionu1;
unsigned int gpionu2;
unsigned int gpionu3;
struct timer_list mytimer;
//int ret;
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)
{

    switch (cmd)
    {
    case LED_ON:
        gpio_set_value(gpionu1, 1);
        gpio_set_value(gpionu2, 1);
        gpio_set_value(gpionu3, 1);
        break;
    case LED_OFF:
        gpio_set_value(gpionu1, 0);
        gpio_set_value(gpionu2, 0);
        gpio_set_value(gpionu3, 0);
        break;
    }
    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)
{
    //打印一次hello world
    prinkt("hellow world\n");
    //再次启用定时器
    mod_timer(timer,jiffies+(5*HZ));
}
static int __init mycdev_init(void)
{
    // 字符驱动设备注册
    major = register_chrdev(0, "myled", &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");
    // 向上提交三个设备节点信息
    int i;
    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");
    // 解析设备树节点信息
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");
    // 获取LED1 GPIO编号
    gpionu1 = of_get_named_gpio(dnode, "led1-gpio", 0);
    if (gpionu1 < 0)
    {
        printk("获取LED1GPIO编号失败\n");
        return -ENXIO;
    }
    // 获取LED2 GPIO编号
    gpionu2 = of_get_named_gpio(dnode, "led2-gpio", 0);
    if (gpionu2 < 0)
    {
        printk("获取LED2GPIO编号失败\n");
        return -ENXIO;
    }
    // 获取LED3 GPIO编号
    gpionu3 = of_get_named_gpio(dnode, "led3-gpio", 0);
    if (gpionu3 < 0)
    {
        printk("获取LED3GPIO编号失败\n");
        return -ENXIO;
    }
    // 申请LED1 GPIO编号
    int ret = gpio_request(gpionu1, NULL);
    if (ret)
    {
        printk("申请LED1 GPIO编号失败");
        return -ENXIO;
    }
    printk("申请LED1 GPIO编号成功:%d\n", gpionu1);
    // 申请LED2 GPIO编号
    ret = gpio_request(gpionu2, NULL);
    if (ret)
    {
        printk("申请LED2 GPIO编号失败");
        return -ENXIO;
    }
    printk("申请LED2 GPIO编号成功:%d\n", gpionu2);
    // 申请LED3 GPIO编号
    ret = gpio_request(gpionu3, NULL);
    if (ret)
    {
        printk("申请LED3 GPIO编号失败");
        return -ENXIO;
    }
    printk("申请LED3 GPIO编号成功:%d\n", gpionu3);
    // 设置LED1 GPIO输出
    ret = gpio_direction_output(gpionu1, 0);
    if (ret)
    {
        printk("LEd1 GPIO输出设置失败\n");
        return -1;
    }
    // 设置LED2 GPIO输出
    ret = gpio_direction_output(gpionu2, 0);
    if (ret)
    {
        printk("LEd2 GPIO输出设置失败\n");
        return -1;
    }
    // 设置LED3 GPIO输出
    ret = gpio_direction_output(gpionu3, 0);
    if (ret)
    {
        printk("LEd3 GPIO输出设置失败\n");
        return -1;
    }
    //初始化定时器对象
    timer_setup(&mytimer,mytimer_function,0);
    mytimer.expires=jiffies+(5*HZ);//定时5s
    //注册定时器
    add_timer(&mytimer);
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 设置灯灭
    gpio_set_value(gpionu1, 0);
    gpio_set_value(gpionu2, 0);
    gpio_set_value(gpionu3, 0);
    //注销定时器
    del_timer(&mytimer);
    // 释放GPIO编号
    gpio_free(gpionu1);
    gpio_free(gpionu2);
    gpio_free(gpionu3);
    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "myled");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值