0


9.20 作业

myplatform.c

#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

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

设备树

结果

标签: linux c

本文转载自: https://blog.csdn.net/Remi____/article/details/133094259
版权归原作者 Remi____ 所有, 如有侵权,请联系我们删除。

“9.20 作业”的评论:

还没有评论