使用GPIO子系统实现led灯亮灭(2月14日)

该代码实现了一个Linux内核模块,用于管理GPIO引脚。它定义了读取和写入操作,根据设备的子设备号控制GPIO状态,并在打开和释放设备时进行初始化和清理。用户空间程序通过打开设备节点交互,切换GPIO的高电平和低电平状态。
摘要由CSDN通过智能技术生成

驱动部分代码

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/of_gpio.h>
#define LEN 3
int i;
dev_t dev;
int major, minor = 0;
struct inode *inode;
static struct cdev *mycdev;
static struct class *myclass;
static struct gpio_desc *des;
static struct device *my_device;
static struct device_node *gpio_node;
struct chrdev_operations *board_status;
static ssize_t read_opr(struct file *file, char __user *buf, size_t size, loff_t *offect)
{
    /* 获取引脚的状态 */
    int status;
    switch (minor)
    {
    case 0:
        status = gpiod_get_value(des);
        break;
    case 1:
        status = gpiod_get_value(des);
        break;
    case 2:
        status = gpiod_get_value(des);
        break;
    default:
        break;
    }
    copy_to_user(buf,(char *)&status,sizeof(status));
    return 0;
}
static ssize_t write_opr(struct file *file, const char __user *buf, size_t size, loff_t *offect)
{
    char status;
    /* 根据子设备号和写入的值来控制设备 */
    inode = file_inode(file);
    minor = iminor(inode);
    copy_from_user(&status, buf, 1);
    switch (minor)
    {
    case 0:
        if (!status)
        {
            gpiod_set_value(des, 0);
        }
        else
        {
            gpiod_set_value(des, 1);
        }
        break;
    case 1:
        if (!status)
        {
            gpiod_set_value(des, 0);
        }
        else
        {
            gpiod_set_value(des, 1);
        }
        break;
    case 2:
        if (!status)
        {
            gpiod_set_value(des, 0);
        }
        else
        {
            gpiod_set_value(des, 1);
        }
        break;
    default:
        break;
    }
    return 0;
}
static int open_opr(struct inode *inode, struct file *file)
{
    /* 初始化设备,根据子设备号来初始化 */
    minor = iminor(inode);
    switch (minor)
    {
    case 0:
    {
        /* 根据子设备号来确定初始化哪个灯,一开始初始化位低电平 */
        des = gpiod_get_from_of_node(gpio_node, "led1", 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(des))
        {
            return PTR_ERR(des);
        }
    }
    break;
    case 1:
    {
        des = gpiod_get_from_of_node(gpio_node, "led2", 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(des))
        {
            return PTR_ERR(des);
        }
    }
    break;
    case 2:
    {
        des = gpiod_get_from_of_node(gpio_node, "led3", 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(des))
        {
            return PTR_ERR(des);
        }
    }
    break;
    default:
        break;
    }
    return 0;
}
static int release_opr(struct inode *inode, struct file *file)
{
    gpiod_put(des);
    return 0;
}
static struct file_operations chrdev = {
    .owner = THIS_MODULE,
    .open = open_opr,
    .read = read_opr,
    .write = write_opr,
    .release = release_opr};

static int __init chrdev_init(void)
{
    mycdev = cdev_alloc();
    if (!mycdev)
    {
        printk("cdev alloc failed!\n");
        goto LABEL_A;
    }
    cdev_init(mycdev, &chrdev);
    if (alloc_chrdev_region(&dev, 0, 3, "mydrv"))
    {
        printk("alloc chrdev failed!\n");
        goto LABEL_B;
    }
    major = MAJOR(dev);
    minor = MINOR(dev);
    if (cdev_add(mycdev, dev, 3))
    {
        printk("cdev add failed!\n");
        goto LABEL_C;
    }
    myclass = class_create(THIS_MODULE, "myclass");
    if (IS_ERR(myclass))
    {
        goto LABEL_D;
    }
    for (i = 0; i < LEN; i++)
    {
        my_device = device_create(myclass, NULL, MKDEV(major, i), NULL, "led%d", i);
        if (IS_ERR(my_device))
        {
            goto LABEL_E;
        }
    }
    gpio_node = of_find_node_by_path("/fsmp1a_leds");
    if (!gpio_node)
    {
        printk("ERRLINE:%d;find node is failed!\n", __LINE__);
        goto LABEL_E;
    }
    return 0;
LABEL_E:
    for (--i; i >= 0; i--)
    {
        device_destroy(myclass, dev);
    }
    class_destroy(myclass);
LABEL_D:
    cdev_del(mycdev);
LABEL_C:
    unregister_chrdev_region(dev, 3);
LABEL_B:
    kfree(mycdev);
LABEL_A:
    return -1;
}
static void __exit chrdev_exit(void)
{
    kfree(mycdev);
    for (--i; i >= 0; i++)
    {
        device_destroy(myclass, dev);
    }
    class_destroy(myclass);
    cdev_del(mycdev);
    unregister_chrdev_region(dev, 3);
}
module_init(chrdev_init);
module_exit(chrdev_exit);
MODULE_LICENSE("GPL");

测试部分代码

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

#define ERR_MSG(err)                                \
    do                                              \
    {                                               \
        fprintf(stderr, "ERR_LINE:%d\n", __LINE__); \
        perror(err);                                \
    } while (0)
int main(int argc, const char **argv)
{
    if (argc != 3)
    {
        printf("try: <%s /dev/ledx on|off>\n", argv[0]);
        return -1;
    }
    int fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        ERR_MSG("open");
        return -1;
    }
    if (!strcmp("on", argv[2]))
    {
        char val = 1;
        int num;
        write(fd, &val, 1);
        read(fd,&num,sizeof(num));
        printf("读取到的引脚状态为:%d\n",num);
    }
    else
    {
        char val = 0;
        write(fd, &val, 1);
    }
    close(fd);
    return 0;
}

实验现象
在这里插入图片描述
请添加图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值