驱动开发day8

基于GPIO子系统编写LED驱动,编写应用程序进行测试

设置定时器,5秒打印一次hello world

mycdev.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>
/*
myled                                                                                               
      {
          led1-gpio=<&gpioe 10 0>;
          led2-gpio=<&gpiof 10 0>;
          led3-gpio=<&gpioe 8 0>;
      };

*/
struct device_node *dnode; //解析得到的设备树节点对象指针
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct timer_list mytimer; 

struct class *cls;
struct device *dev;
int major;
char kbuf[128]={0};

//定时器处理函数
void mytimer_function(struct timer_list *timer)
{
    //LED1状态取反
    //gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
    //LED2状态取反
    //gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
    //LED3状态取反
    //gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));

    printk("hello world\n");
    //再次启用定时器
    mod_timer(timer,jiffies+5*HZ);
}
int mycdev_open(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    return 0;
}
 
ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    //设定该GPIO管脚的输出值
    if(kbuf[0]=='0')
    {
        gpiod_set_value(gpiono1,0);
        gpiod_set_value(gpiono2,0);
        gpiod_set_value(gpiono3,0);       
    }
    else if(kbuf[0]=='1')
    {
        gpiod_set_value(gpiono1,1);
        gpiod_set_value(gpiono2,1);
        gpiod_set_value(gpiono3,1); 
    }
    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,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
};

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");

    //向上提交设备节点信息
    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;
    }
    //获取LED1 GPIO编号
    gpiono1 = gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("获取GPIO编号失败\n");
        return -PTR_ERR(gpiono1);
    }
    //获取LED1 GPIO编号
    gpiono2 = gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono2))
    {
        printk("获取GPIO编号失败\n");
        return -PTR_ERR(gpiono2);
    }
    //获取LED1 GPIO编号
    gpiono3 = gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono3))
    {
        printk("获取GPIO编号失败\n");
        return -PTR_ERR(gpiono3);
    }
    //初始化定时器对象
    timer_setup(&mytimer,mytimer_function,0);
    mytimer.expires = jiffies+5*HZ;//定时1s

    //注册定时器
    add_timer(&mytimer);
    return 0;
}
static void __exit mycdev_exit(void)
{
    //注销定时器
    del_timer(&mytimer);
    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    gpiod_set_value(gpiono3,0);
    //释放GPIO编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);

    //销毁设备节点信息
    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");

main.c

复制代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/epoll.h>
/* According to earlier standards */
#include <sys/time.h>
int fd1,fd2,fd3;
char buf[128] ={0};
int main(int argc, char const *argv[])
{
    fd1 = open("/dev/myled0", O_RDWR);
    if (fd1 < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        bzero(buf, sizeof(buf));
        printf("请输入0(关灯),1(开灯):"); 
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        write(fd1,buf,sizeof(buf));
    }
    close(fd1);
 
    return 0;
}
相关推荐
Ghost Face...8 小时前
LS2K PCIe驱动开发全链路解析
驱动开发
Joseph Cooper9 小时前
Linux Power Management 子系统:从 suspend/resume 到 Runtime PM、PM QoS
linux·驱动开发·linux kernel·嵌入式linux·电源管理
d111111111d15 小时前
移植MQTT到STM32
驱动开发
码踏樱花15 小时前
navicat17安装包和破解
驱动开发
senijusene2 天前
基于 MX6UL 的 DHT11 温湿度传感器 驱动开发
驱动开发
charlie1145141912 天前
嵌入式Linux驱动开发(8)——内存映射 I/O - 别拿物理地址当指针用
linux·开发语言·驱动开发·c·imx6ull
Wallace Zhang2 天前
SimpleFOC源码学习09(v2.3.2) - 磁编码器MagneticSensorSPI.cpp与MagneticSensorSPI.h
驱动开发·stm32·simplefoc·foc电机控制
Freak嵌入式2 天前
亲测可用!可本地部署的 MicroPython 开源仿真器
ide·驱动开发·嵌入式·仿真·micropython·upypi
进击的小头2 天前
20_第20篇:嵌入式外设驱动开发基础:寄存器级开发与库函数开发对比实战
arm开发·驱动开发·单片机
低调小一2 天前
BDD(行为驱动开发)入门:把“测试”写成“行为”,把“需求”写成“场景”
驱动开发·tdd·bdd