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;
}

设备树

结果

相关推荐
Ven%4 分钟前
centos查看硬盘资源使用情况命令大全
linux·运维·centos
TeYiToKu1 小时前
笔记整理—linux驱动开发部分(9)framebuffer驱动框架
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件·arm
dsywws1 小时前
Linux学习笔记之时间日期和查找和解压缩指令
linux·笔记·学习
yeyuningzi1 小时前
Debian 12环境里部署nginx步骤记录
linux·运维·服务器
上辈子杀猪这辈子学IT1 小时前
【Zookeeper集群搭建】安装zookeeper、zookeeper集群配置、zookeeper启动与关闭、zookeeper的shell命令操作
linux·hadoop·zookeeper·centos·debian
minihuabei1 小时前
linux centos 安装redis
linux·redis·centos
lldhsds2 小时前
书生大模型实战营第四期-入门岛-1. Linux前置基础
linux
wowocpp3 小时前
ubuntu 22.04 硬件配置 查看 显卡
linux·运维·ubuntu
山河君3 小时前
ubuntu使用DeepSpeech进行语音识别(包含交叉编译)
linux·ubuntu·语音识别
鹏大师运维3 小时前
【功能介绍】信创终端系统上各WPS版本的授权差异
linux·wps·授权·麒麟·国产操作系统·1024程序员节·统信uos