当前位置: 首页 > article >正文

底层驱动day8作业

代码:

//驱动程序
#include<linux/init.h>
#include<linux/module.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include<linux/timer.h>

struct device_node *dnode;
//unsigned int gpiono;
char kbuf[128] = {0};
//定义定时器对象
struct timer_list mytimer;
struct gpio_desc *gpiono;


int major;
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret=copy_from_user(kbuf,ubuf,size);

    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    printk("%d\n",kbuf[0] - 48);
    gpiod_set_value(gpiono,kbuf[0] - 48);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .write = mycdev_write,
    .release = mycdev_close,
};

void handler(struct timer_list *timer)
{
    printk("hello world\n");
    mod_timer(timer,jiffies + 5 * HZ);
}


static int __init mycdev_init(void)
{
    dnode = of_find_node_by_path("/myled");

    if(NULL == dnode)
    {
        printk("解析设备节点信息失败\n");
        return  -ENXIO;
    }
    //获取LED1 GPIO编号
    gpiono = gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono);
    }
    //初始化定时器对象
    timer_setup(&mytimer,handler,0);
    mytimer.expires = jiffies + HZ;
    //注册定时器
    add_timer(&mytimer);
    
     major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i; // 向上提交三次设备节点信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");



    return 0;
}

static void __exit mycdev_exit(void)
{
    del_timer(&mytimer);
    
    //gpio_free(gpiono);
    
    gpiod_put(gpiono);

    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}


module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
//应用程序
#include<stdio.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/myled0",O_RDWR);
    while(1)
    {
        printf("请输入>>>");
        scanf("%s",buf);
        write(fd,buf,1);
    }
    
    return 0;
}

 实验现象:

 


http://www.kler.cn/news/108080.html

相关文章:

  • 【C++】智能指针:auto_ptr、unique_ptr、share_ptr、weak_ptr(技术介绍 + 代码实现)(待更新)
  • Megatron-LM GPT 源码分析(三) Pipeline Parallel分析
  • AWS SAP-C02教程11-解决方案
  • C#,数值计算——分类与推理,基座向量机的 Svmgenkernel的计算方法与源程序
  • 中微爱芯74逻辑兼容替代TI/ON/NXP工规品质型号全
  • 【杂记】Ubuntu20.04装系统,安装CUDA等
  • python爬虫之feapder.AirSpider轻量爬虫案例:豆瓣
  • PHP简单实现预定义钩子和自定义钩子
  • Linux国产系统无法连接身份证读卡器USB权限解决办法
  • nrf52832 开发板入手笔记:J-Flash 蓝牙协议栈烧写
  • Nginx 的配置文件(负载均衡,反向代理)
  • Spring Security: 整体架构
  • uniapp-图片压缩(适配H5,APP)
  • 10月Java行情 回暖?
  • 【机器学习可解释性】4.SHAP 值
  • 第10期 | GPTSecurity周报
  • scratch接钻石 2023年9月中国电子学会图形化编程 少儿编程 scratch编程等级考试三级真题和答案解析
  • 力扣第763题 划分字母区间 c++ 哈希 + 双指针 + 小小贪心
  • 制作自己的前端组件库并上传到npm上
  • MySQL实战2
  • 华为c语言编程规范
  • 【Unity】RenderFeature应用(简单场景扫描效果)
  • Linux学习第26天:异步通知驱动开发: 主动
  • 基于Headless构建高可用spark+pyspark集群
  • React中useEffect Hook使用纠错
  • 大彩串口屏读写文件问题
  • Proteus仿真--从左往右流水灯仿真(仿真文件+程序)
  • React之如何捕获错误
  • PlantSimulation访问本地Excel文件的方法
  • 10分钟了解JWT令牌 (JSON Web)