10.20-驱动-作业

a.应用程序通过阻塞的io模型来读取number变量的值

b.number是内核驱动中的一个变量

c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0

d.在按下按键的时候需要同时将led1的状态取反

e.驱动中需要编写字符设备驱动 f.驱动中需要自动创建设备节点

g.这个驱动需要的所有设备信息放在设备树的同一个节点中

驱动代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>

int major, minor = 0;
struct cdev *cdev;
struct class *cls;
struct device *dev;
//定义一个指向设备节点的指针
struct device_node *node;
struct device_node *node_led;
struct device_node *node_irq;
struct property *pr; //属性结构体指针
int gpiono1;
struct gpio_desc *desc_led1;
int irqno;
int number = 0;
wait_queue_head_t wq_head; //定义等待对头
int condition = 0;
int chdev_open(struct inode *inode, struct file *file)
{
    return 0;
}
int chdev_close(struct inode *inode, struct file *file)
{
    return 0;
}
ssize_t chdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    //阻塞
    ret = wait_event_interruptible(wq_head, condition);
    if (ret)
    {
        printk("接收阻塞休眠\n");
        return ret;
    }
    //把父进程拷贝到内核的数据再拷贝给子进程
    if (size > sizeof(number))
        size = sizeof(number);
    ret = copy_to_user(ubuf, &number, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    condition = 0;

    return size;
}
ssize_t chdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    return 0;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    return 0;
}
//中断处理函数
irqreturn_t irq_handler(int irq, void *dev)
{
    gpiod_set_value(desc_led1, !gpiod_get_value(desc_led1));
    number ^= 1;
    //唤醒
    condition = 1;
    wake_up_interruptible(&wq_head);
    return IRQ_HANDLED;
}
struct file_operations fops = {
    .open = chdev_open,
    .read = chdev_read,
    .write = chdev_write,
    .release = chdev_close,
    .unlocked_ioctl = ioctl,
};
int pdrv_probe(struct platform_device *pdr)
{
    //获取设备信息
    printk("%s:%d\n", __FILE__, __LINE__);
    irqno = platform_get_irq(pdr, 0);
    if (irqno < 0)
    {
        printk("获取资源失败\n");
        return irqno;
    }
    if (request_irq(irqno, irq_handler, IRQF_TRIGGER_FALLING, NULL, NULL))
    {
        printk("注册中断失败\n");
        return -1;
    }
    //获取GPIO编号
    desc_led1 = gpiod_get_from_of_node(pdr->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(desc_led1))
    {
        printk("获取GPIO编号失败\n");
        return PTR_ERR(desc_led1);
    }
    return 0;
}
int pdrv_remove(struct platform_device *pdr)
{
    printk("%s:%d\n", __FILE__, __LINE__);
    return 0;
}
//定义compatible表
struct of_device_id oftable[] = {
    {.name = "myplatform"},
    {},
};
//定义并初始化对象
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "test",
        .of_match_table = oftable, //设备树匹配
    },
};
static int __init mycdev_init(void)
{
    int ret;
    dev_t devno;
    cdev = cdev_alloc();
    if (NULL == cdev)
    {
        printk("初始化驱动设备对象失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    cdev_init(cdev, &fops);
    ret = alloc_chrdev_region(&devno, minor, 1, "my_led");
    if (ret)
    {
        printk("申请设备号失败\n");
        goto ERR2;
    }
    major = MAJOR(devno);
    minor = MINOR(devno);
    ret = cdev_add(cdev, MKDEV(major, minor), 1);
    if (ret)
    {
        printk("字符设备驱动注册失败\n");
        goto ERR3;
    }
    cls = class_create(THIS_MODULE, "chdev_led");
    if (IS_ERR(cls))
    {
        printk("向上提交节点失败\n");
        goto ERR4;
    }
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");
    if (IS_ERR(dev))
    {
        printk("创建逻辑节点失败\n");
        ret = PTR_ERR(dev);
        goto ERR5;
    }
    //注册对象
    platform_driver_register(&pdrv);
    //初始化队列头
    init_waitqueue_head(&wq_head);
    return 0;
ERR5:
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), 1);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //销毁设备节点
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    // 注销字符设备驱动
    cdev_del(cdev);
    // 释放设备号
    unregister_chrdev_region(MKDEV(major, minor), 1);
    // 释放动态申请的空间
    kfree(cdev);

    gpiod_set_value(desc_led1, 0);
    gpiod_put(desc_led1);

    //注销中断
    free_irq(irqno, NULL);
    //注销对象
    platform_driver_unregister(&pdrv);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用层代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>

int main(int argc, char const *argv[])
{
    int fd;
    int number;
    char buf[128] = {};
    int which;
    fd = open("/dev/led", O_RDWR);
    if (fd < 0)
    {
        printf("打开文件失败\n");
        exit(-1);
    }
    while (1)
    {
        read(fd, &number, sizeof(number));
        printf("number = %d\n", number);
    }

    close(fd);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: IAR Embedded Workbench IDE - 8051 10.20.1是一款8051微控制器开发的集成开发环境(IDE)。它提供了一个直观的用户界面,支持多种开发工具和调试器,并提供了丰富的产品示例和模板来快速开发嵌入式系统。此版本还带来了许多改进和功能增强。 该IDE提供了一个完整的开发生态系统,从编写代码、编译代码、调试代码到部署代码。它支持多种程序语言,包括C、C ++和汇编语言,并且可以与许多不同的设备和板级系统集成,以满足各种不同的嵌入式系统开发需要。 此版本还带来了许多增强功能,包括一个新的快速构建选项,使构建和编译更快,并提供了更好的用户体验。它还提供了内置的版本控制和代码分析工具,用于更好的管理代码,并帮助开发人员找到和修复潜在的问题。 总的来说,IAR Embedded Workbench IDE - 8051 10.20.1是一款功能强大且易于使用的工具,适合8051微控制器的嵌入式系统开发。它提供了丰富的产品示例和模板,帮助开发人员快速构建应用程序,并提供了一组丰富的功能和增强功能,以帮助开发人员更快地开发和调试嵌入式系统。 ### 回答2: IAR Embedded Workbench IDE是一款面向8051微控制器的集成开发环境(IDE),版本号为10.20.1。它是由瑞典IAR Systems公司开发的一个功能强大的软件,专门用于开发嵌入式系统。 该软件提供了完整的应用程序开发流程,包括从代码编辑、编译到仿真和调试等一系列功能,使开发者能够更加高效地编写和调试嵌入式应用程序。它支持多种编程语言,包括C语言和汇编语言,还提供了丰富的代码库,以加速开发过程。 IAR Embedded Workbench IDE的主要特点包括: 1. 支持单步调试和断点调试,准确定位程序中的错误。 2. 具有优秀的代码生成和优化能力,可以提高程序的运行速度和效率。 3. 提供全面的CPU和外设仿真支持,可以快速验证程序的正确性。 4. 支持多种调试接口和连接标准,兼容多种单片机和调试工具。 总之,IAR Embedded Workbench IDE作为一款专业的嵌入式开发工具,具有稳定性高、功能丰富、效率高等优点,可以极大地简化嵌入式应用程序的开发过程。 ### 回答3: IAR Embedded Workbench IDE - 8051 10.20.1是一种用于嵌入式系统开发的综合开发环境(IDE)。它是专为8051微控制器而设计的,可用于编写和调试8051微控制器应用程序。该IDE提供了高度集成的开发环境,可帮助开发人员更快地构建和调试代码。 该IDE具有一系列工具,包括C编译器、汇编器、链接器、调试器和仿真器。它还提供了丰富的库,包括标准C库、数学库、字符串库、输入/输出库和设备驱动库等。这些库可以帮助开发人员更快地编写应用程序,实现更高的效率和更少的错误。 同时,该IDE还提供了基于Windows操作系统的GUI,使用户可以在视觉上对应用程序进行分析和调试。它还支持多种编程语言,包括C、C++和汇编语言。例如,开发人员可以使用C语言来编写应用程序,并在汇编语言级别进行优化和调试。 总的来说,IAR Embedded Workbench IDE - 8051 10.20.1是一款功能全面、易于使用的嵌入式开发环境,适合开发各种应用程序。它可以帮助开发人员在更短的时间内构建更好的应用程序,并提供更高的效率和更少的错误。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值