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

设备树

结果

相关推荐
内核程序员kevin2 小时前
TCP Listen 队列详解与优化指南
linux·网络·tcp/ip
朝九晚五ฺ6 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
自由的dream6 小时前
Linux的桌面
linux
xiaozhiwise7 小时前
Makefile 之 自动化变量
linux
意疏9 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
BLEACH-heiqiyihu9 小时前
RedHat7—Linux中kickstart自动安装脚本制作
linux·运维·服务器
一只爱撸猫的程序猿9 小时前
一个简单的Linux 服务器性能优化案例
linux·mysql·nginx
我的K840910 小时前
Flink整合Hudi及使用
linux·服务器·flink
19004311 小时前
linux6:常见命令介绍
linux·运维·服务器