6月25日作业

 驱动代码


#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/wait.h>
 
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
 
struct cdev *cdev;
unsigned int minor = 0;
dev_t devno;
unsigned int major = 500;
struct class *cls;
struct device *dev;
 
char kbuf[128];
// 定义等待队列头
wait_queue_head_t wq_head;
 
int mycdev_open(struct inode *inode, struct file *file)
{
    return 0;
}
 
ssize_t mycdev_read(struct file *file, char __user *user, size_t size, loff_t *lof)
{
    return 0;
}
 
ssize_t mycdev_write(struct file *file, const char __user *user, size_t size, loff_t *lof)
{
    unsigned long ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_from_user(kbuf, user, size);
    if (ret)
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    printk("%c", kbuf[0]);
    switch (kbuf[0])
    {
    case '1':
        gpiod_set_value(gpiono1, 0);
        break;
    case '2':
        gpiod_set_value(gpiono1, 1);
        break;
    case '3':
        gpiod_set_value(gpiono2, 0);
        break;
    case '4':
        gpiod_set_value(gpiono2, 1);
        break;
    case '5':
        gpiod_set_value(gpiono3, 0);
        break;
    case '6':
        gpiod_set_value(gpiono3, 1);
        break;
    }
    return 0;
}
 
int mycdev_close(struct inode *inode, struct file *file)
{
    return 0;
}
 
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};
 
// 定义相关成员函数
// probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev) // 当和设备匹配成功之后执行probe
{
 
    int ret, i;
    // 初始化等待队列头
    init_waitqueue_head(&wq_head);
    // 1.实例化字符设备驱动对象
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        ret = -ENOMEM;
        goto OUT1;
    }
    printk("字符设备对象实例化成功\n");
    // 2.部分初始化字符设备驱动对象
    cdev_init(cdev, &fops);
    // 3.申请设备号
    if (major == 0) // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto OUT2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    else // 静态指定设备号
    {
        devno = MKDEV(major, 0);
        ret = register_chrdev_region(devno, 3, "mycdev");
        if (ret)
        {
            printk("静态设备号失败\n");
            goto OUT2;
        }
    }
    printk("设备号申请成功\n");
    // 4.将字符设备驱动对象注册进内核
    ret = cdev_add(cdev, devno, 3);
    if (ret)
    {
        printk("注册字符设备驱动失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动成功\n");
    // 5.自动创建设备节点
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备信息失败\n");
            ret = PTR_ERR(dev);
            goto OUT5;
        }
    }
    printk("自动创建设备节点成功\n");
 
    // 解析MEM类型的资源
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res == NULL)
    {
        printk("解析资源失败%d\n", __LINE__);
        return -ENOMEM;
    }
 
    // 解析中断类型的资源
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("解析资源失败%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("mem:%x  irq:%d\n", res->start, irqno);
 
    // 解析出GPIO编号并申请
    gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono1))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono1);
    }
 
    gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono2);
    }
 
    gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono3);
    }
    printk("GPIO信息解析成功\n");
    return 0;
 
OUT5:
    //释放提交成功的设备节点信息
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //销毁目录
    class_destroy(cls);
OUT4:
    cdev_del(cdev);
OUT3:
    unregister_chrdev_region(devno,3);
OUT2:
    kfree(cdev);
OUT1:
    return ret;
}
 
// remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行remove
{
    // 灭灯
    gpiod_set_value(gpiono1, 0);
    gpiod_set_value(gpiono2, 0);
    gpiod_set_value(gpiono3, 0);
    // 释放GPIO编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
 
    unregister_chrdev(major,"mycdev");
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
 
// 在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {
        .compatible = "hqyj,myplatform1",
    },
    {
        .compatible = "hqyj,myplatform2",
    },
    {},
};
 
// 分配对象并初始化
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "aaaaa",
        .of_match_table = oftable, // 指定设备树匹配表的首地址
    },
 
};
 
// 一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

应用层代码:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
int main(int argc, const char *argv[])
{
    char buf[64];
    int fd = open("/dev/mycdev0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
    }
    while (1)
    {
        printf("请输入灯的编号\n");
        printf("1(LED1灭) 2(LED1亮) 3(LED2灭) 4(LED2亮) 5(LED3灭) 6(LED3亮)\n");
        printf("请输入>>");
        scanf("%s",buf);
        write(fd, buf, sizeof(buf));
    }
    close(fd);
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python和GPIO库来实现数码管显示当前系统期,首先你需要安装`RPi.GPIO`库(如果你还没有安装)以及可能用于控制数码管的库,如`rpigpio`或`RPi.GPIO`结合`shiftregister`。以下是一个简单的示例,它使用了`RPi.GPIO`库和一个7段数码管模拟器(这里假设你有一个8位的共阴极数码管): ```python import RPi.GPIO as GPIO from time import sleep # 数码管引脚定义(根据你的硬件配置) D0 = 17 D1 = 18 D2 = 22 D3 = 23 D4 = 24 D5 = 25 D6 = 27 D7 = 22 COM = 7 # 初始化GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(D0, GPIO.OUT) GPIO.setup(D1, GPIO.OUT) GPIO.setup(D2, GPIO.OUT) GPIO.setup(D3, GPIO.OUT) GPIO.setup(D4, GPIO.OUT) GPIO.setup(D5, GPIO.OUT) GPIO.setup(D6, GPIO.OUT) GPIO.setup(D7, GPIO.OUT) GPIO.setup(COM, GPIO.OUT) # 定义数码管字符映射 segmap = { '0': '0000', '1': '0001', '2': '0011', '3': '0010', '4': '0110', '5': '0111', '6': '0101', '7': '0100', '8': '1111', '9': '1110', 'A': '1101', 'B': '1100', 'C': '1000', 'D': '1001', 'E': '1011', 'F': '1010', } def display_date(): current_date = datetime.datetime.now().strftime('%Y%m%d') segments = list(segmap[char] for char in current_date) # 反转COM端口来控制数码管 GPIO.output(COM, GPIO.HIGH) for i in range(8): GPIO.output(i, segmap[segments[i]]) sleep(0.01) # 控制显示速度 GPIO.output(COM, GPIO.LOW) # 显示完毕后清零 try: while True: display_date() except KeyboardInterrupt: GPIO.cleanup() # 退出时清理GPIO ``` 注意:这只是一个基本的模拟,实际硬件数码管可能会有不同的驱动方式,请根据你的硬件调整。此外,这个例子假定你的数码管是共阴极的,如果是共阳极的,数码管字符映射会有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值