9.20 作业

myplatform.c

cpp 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/cdev.h>

char number = 1;
struct gpio_desc *led1;
unsigned int irqno;
struct timer_list timer;
unsigned int major = 0;
unsigned int minor = 0;
char *devname = "myplatform";
struct cdev *cdev;
struct class *cls;
struct device *dev;

// 封装操作方法
int led_open(struct inode *inode, struct file *file)
{
    return 0;
}
ssize_t led_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    unsigned long res = copy_to_user(ubuf, &number, size < sizeof(number) ? size : sizeof(number));
    return res;
}
int led_close(struct inode *inode, struct file *file)
{
    return 0;
}
// 定义一个操作方法结构体对象并且初始化
struct file_operations fops = {
    .open = led_open,
    .read = led_read,
    .release = led_close,
};

irqreturn_t key_handler(int irq, void *dev)
{
    number = !number;
    gpiod_set_value(led1, !gpiod_get_value(led1));
    return IRQ_HANDLED;
}

int pdrv_probe(struct platform_device *pdev)
{
    unsigned int dnub;
    printk("%s%s%d\n", __FILE__, __func__, __LINE__);

    cdev = cdev_alloc();
    if (!cdev)
    {
        printk("字符设备驱动对象申请空间失败\n");
        return -1;
    }
    printk("字符设备驱动对象申请空间成功\n");
    cdev_init(cdev, &fops);

    if (alloc_chrdev_region(&dnub, minor, 1, devname))
    {
        printk("动态申请设备号失败\n");
        return -1;
    }
    printk("动态申请设备号成功\n");
    major = MAJOR(dnub);

    if (cdev_add(cdev, MKDEV(major, minor), 1))
    {
        printk("注册驱动失败\n");
        return -1;
    }
    printk("注册驱动成功\n");

    cls = class_create(THIS_MODULE, devname);
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -1;
    }
    printk("向上提交目录成功\n");

    dev = device_create(cls, NULL, MKDEV(major, minor), NULL, devname);
    if (IS_ERR(cls))
    {
        printk("向上提交设备节点信息失败\n");
        return -1;
    }
    printk("向上提交设备节点信息成功\n");

    led1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_HIGH, NULL);
    if (IS_ERR(led1))
    {
        printk("解析GPIO失败\n");
        return -1;
    }
    printk("解析GPIO成功\n");

    irqno = irq_of_parse_and_map(pdev->dev.of_node, 0);
    if (!irqno)
    {
        printk("中断号解析失败\n");
        return -1;
    }
    printk("中断号解析成功\n");

    if (request_irq(irqno, key_handler, IRQF_TRIGGER_FALLING, "key", NULL))
    {
        printk("中断注册失败\n");
        return -1;
    }
    printk("中断注册成功\n");

    return 0;
};

int pdrv_remove(struct platform_device *pdev)
{
    printk("%s%s%d\n", __FILE__, __func__, __LINE__);
    gpiod_set_value(led1, 0);
    gpiod_put(led1);
    device_destroy(cls, MKDEV(major, minor));
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major, minor), 1);
    kfree(cdev);
    free_irq(irqno, NULL);
    return 0;
}

struct of_device_id oftable[] = {
    {
        .name = "myplatform",
        //.compatible = "hqyj,myplatform",
    },
    {},
};

struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "abcd",
        .of_match_table = oftable,
    },
};

module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

test.c

cpp 复制代码
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "head.h"

int main()
{
    int fd;
    char number;

    fd = open("/dev/myplatform", O_RDWR);
    if (fd < 0)
    {
        printf("文件打开失败\n");
        return -1;
    }

    read(fd,&number,sizeof(number));

    printf("number=%d\n",number);

    close(fd);
    return 0;
}

设备树

结果

相关推荐
被制作时长两年半的个人练习生9 小时前
如何调试llama.cpp及判断是否支持RVV
linux·服务器·llama
赖small强9 小时前
【音视频开发】Linux V4L2 (Video for Linux 2) 驱动框架深度解析白皮书
linux·音视频·v4l2·设备节点管理·视频缓冲队列·videobuf2
四谎真好看9 小时前
Linux 附录二,实验一
linux·运维·服务器·学习笔记
神秘的土鸡9 小时前
Linux中使用Docker构建Nginx容器完整教程
linux·nginx·docker
Molesidy9 小时前
【Embedded Development】BootROM的详细分析以及Linux开发板的上电启动流程初步分析
linux
wdfk_prog9 小时前
[Linux]学习笔记系列 -- [block]kyber-iosched
linux·笔记·学习
赖small强10 小时前
【Linux驱动开发】Linux dd 命令详解
linux·dd命令
傲世(C/C++,Linux)10 小时前
Linux系统编程——TCP客户端
linux·运维·tcp/ip
Xの哲學10 小时前
C语言内存函数总结
linux·服务器·网络·架构·边缘计算