华清远见上海中心22071班

这篇博客介绍了如何在Linux内核中编写一个字符设备驱动,该驱动与设备树结合,用于控制多个GPIO口。首先,通过设备树节点获取GPIO编号,然后动态或静态申请设备号,初始化cdev结构体并注册设备。驱动提供了打开、关闭和ioctl操作,通过ioctl命令控制GPIO状态。此外,还包含了一个用户空间的应用程序示例,用于打开设备节点并通过ioctl接口控制GPIO的开和关。
摘要由CSDN通过智能技术生成
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include <linux/slab.h>
#include "led.h"
#define CNAME "myled"


//定义指针指向获取的设备树节点信息空间
 struct device_node *node;
 
 int gpiono1;//用于接收gpio编号
 int gpiono2;//用于接收gpio编号
 int gpiono3;//用于接收gpio编号
 int gpiono4;//用于接收gpio编号
 int gpiono5;//用于接收gpio编号
 int gpiono6;//用于接收gpio编号
 int ret;



struct cdev *cdev;
struct class * cls;
struct device * dev;
#if 1
unsigned int major = 0;//动态申请设备号
#else
unsigned int major = 500; //静态指定设备号
#endif
int minor = 0;
const int count = 6;
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)
{
    int ret;
    char image[2];   
    ret = copy_from_user(image,(void*)arg,sizeof(image));
    if(image[0]=='1'){
        if(image[1]=='1'){
            gpio_set_value(gpiono1,1);
        }
        else{
            gpio_set_value(gpiono1,0);
        }
    }
    else if(image[0]=='2'){
         if(image[1]=='1'){
            gpio_set_value(gpiono2,1);
        }
        else{
            gpio_set_value(gpiono2,0);
        }
    }
    else if(image[0]=='3'){
         if(image[1]=='1'){
            gpio_set_value(gpiono3,1);
        }
        else{
            gpio_set_value(gpiono3,0);
        }
    }
     else if(image[0]=='4'){
         if(image[1]=='1'){
            gpio_set_value(gpiono4,1);
        }
        else{
            gpio_set_value(gpiono4,0);
        }
    }
     else if(image[0]=='5'){
         if(image[1]=='1'){
            gpio_set_value(gpiono5,1);
        }
        else{
            gpio_set_value(gpiono5,0);
        }
    }
     else if(image[0]=='6'){
         if(image[1]=='1'){
            gpio_set_value(gpiono6,1);
        }
        else{
            gpio_set_value(gpiono6,0);
        }
    }
    return 0;
}
int  mycdev_close (struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
const struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
//入口
static int __init mycdev_init(void)
{
    int ret;
    dev_t devno;
    int i;
        //通过名字获取设备树节点信息
    node=of_find_node_by_name(NULL,"myleds");
    if(node==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");
    //获取gpio编号
    gpiono1=of_get_named_gpio(node,"myled1",0);
    if(gpiono1<0)
    {
        printk("获取gpio1编号失败\n");
        return gpiono1;
    }
    printk("获取gpio1编号成功\n");
        gpiono2=of_get_named_gpio(node,"myled2",0);
    if(gpiono2<0)
    {
        printk("获取gpio2编号失败\n");
        return gpiono2;
    }
    printk("获取gpio2编号成功\n");
     //获取gpio编号
    gpiono3=of_get_named_gpio(node,"myled3",0);
    if(gpiono3<0)
    {
        printk("获取gpio3编号失败\n");
        return gpiono3;
    }
    printk("获取gpio3编号成功\n");
        gpiono4=of_get_named_gpio(node,"myled4",0);
    if(gpiono4<0)
    {
        printk("获取gpio1编号失败\n");
        return gpiono4;
    }
    printk("获取gpio4编号成功\n");
        gpiono5=of_get_named_gpio(node,"myled5",0);
    if(gpiono5<0)
    {
        printk("获取gpio5编号失败\n");
        return gpiono5;
    }
    printk("获取gpio5编号成功\n");
        gpiono6=of_get_named_gpio(node,"myled6",0);
    if(gpiono6<0)
    {
        printk("获取gpio5编号失败\n");
        return gpiono3;
    }
    printk("获取gpio6编号成功\n");
    //申请GPIO编号
   ret=gpio_request(gpiono1,NULL);
   if(ret)
   {
         printk("申请gpio1编号失败\n");
        return ret;
   }
   printk("申请gpio1编号成功\n");
    ret=gpio_request(gpiono2,NULL);
   if(ret)
   {
         printk("申请gpio2编号失败\n");
        return ret;
   }
   printk("申请gpio2编号成功\n");
     ret=gpio_request(gpiono3,NULL);
   if(ret)
   {
         printk("申请gpio3编号失败\n");
        return ret;
   }
   printk("申请gpio3编号成功\n");
      ret=gpio_request(gpiono4,NULL);
   if(ret)
   {
         printk("申请gpio4编号失败\n");
        return ret;
   }
   printk("申请gpio4编号成功\n");
      ret=gpio_request(gpiono5,NULL);
   if(ret)
   {
         printk("申请gpio5编号失败\n");
        return ret;
   }
   printk("申请gpio5编号成功\n");
      ret=gpio_request(gpiono6,NULL);
   if(ret)
   {
         printk("申请gpio6编号失败\n");
        return ret;
   }
   printk("申请gpio6编号成功\n");

    //设置管脚为输出
   gpio_direction_output(gpiono1,0);
   gpio_direction_output(gpiono2,0);
   gpio_direction_output(gpiono3,0);
   gpio_direction_output(gpiono4,0);
   gpio_direction_output(gpiono5,0);
   gpio_direction_output(gpiono6,0);

    //1.分配cdev结构体
    cdev = cdev_alloc();
    if(NULL == cdev)
    {
        printk("cdev alloc is error\n");
        ret = -EIO;
        goto ERR1;
    }
    //2.初始化结构体
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major > 0)
    {
        //静态指定设备号
        ret = register_chrdev_region(MKDEV(major,minor),count,CNAME);
        if(ret)
        {
            printk("register chrdev region is error\n");
            ret = -ENOMEM;
            goto ERR2;
        }
    }
    else{
        //动态申请设备号
        ret = alloc_chrdev_region(&devno,0,count,CNAME);
        if(ret)
        {
            printk("alloc chrdev region is error\n");
            ret = -ENOMEM;
            goto ERR2;
        }
        //获取主设备号
        major = MAJOR(devno);
        //获取次设备号
        minor = MINOR(devno);
    }
    //4.驱动的注册
    ret = cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("cdev add is error\n");
        ret = -EIO;
        goto ERR3;
    }
    //5.自动创建设备节点
        //1)向上层提交目录信息
        cls = class_create(THIS_MODULE,CNAME);
        if(IS_ERR(cls))
        {
            printk("class create is error\n");
            ret = PTR_ERR(cls);
            goto ERR4;
        }
        //2)向上层提交设备节点信息
        for(i=0;i<count;i++)
        {
            dev = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
            if(IS_ERR(dev))
            {
                printk("device create is error\n");
                ret = PTR_ERR(dev);
                goto ERR5;
            }
        }
    return 0;

ERR5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:  
    unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
    kfree(cdev);
ERR1:
    return -EIO;
}
//出口
static void __exit mycdev_exit(void)
{
    int i;
    //1。销毁设备节点信息
    for(i=0;i<count;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //2.销毁目录信息
    class_destroy(cls);
    //3.驱动的注销
    cdev_del(cdev);
    //4.销毁设备号
    unregister_chrdev_region(MKDEV(major,minor),count);
    //5.释放cdev结构体
    kfree(cdev);
}
//指定入口地址
module_init(mycdev_init);
module_exit(mycdev_exit);
//指定出口地址
//许可证
MODULE_LICENSE("GPL");
#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/ioctl.h>
#include "led.h"
char buf[2] ;

int main(int argc, char const *argv[])
{
    //打开三个节点
    int fd1,fd2,fd3,fd4,fd5,fd6;
    fd1 = open("/dev/myled0",O_RDWR);
    if(-1 == fd1)
    {
        perror("open is error");
        exit(1);
    }
        fd2 = open("/dev/myled1",O_RDWR);
    if(-1 == fd2)
    {
        perror("open is error");
        exit(1);
    }
        fd3 = open("/dev/myled2",O_RDWR);
    if(-1 == fd3)
    {
        perror("open is error");
        exit(1);
    }
        fd4 = open("/dev/myled3",O_RDWR);
    if(-1 == fd4)
    {
        perror("open is error");
        exit(1);
    }
        fd5 = open("/dev/myled4",O_RDWR);
    if(-1 == fd5)
    {
        perror("open is error");
        exit(1);
    }
        fd6 = open("/dev/myled5",O_RDWR);
    if(-1 == fd6)
    {
        perror("open is error");
        exit(1);
    }
    
    while(1){
        buf[0]='1';
        buf[1]='1';
        ioctl(fd1,UACCESS_BUF,buf);
        
        buf[0]='2';
        buf[1]='1';
        ioctl(fd2,UACCESS_BUF,buf);
        buf[0]='3';
        buf[1]='1';
        ioctl(fd3,UACCESS_BUF,buf);
        buf[0]='4';
        buf[1]='1';
        ioctl(fd4,UACCESS_BUF,buf);
        buf[0]='5';
        buf[1]='1';
        ioctl(fd5,UACCESS_BUF,buf);
        buf[0]='6';
        buf[1]='1';
        ioctl(fd6,UACCESS_BUF,buf);
    }
    //printf("copy to user %s\n",ubuf);
    close(fd1);
    close(fd2);
    close(fd3);
     close(fd4);
    close(fd5);
    close(fd6);
    return 0;
}
#ifndef __LED_H__
#define __LED_H__
//命令码封装
#define UACCESS_BUF _IOW('a',0,char [2])
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值