侧边栏壁纸
    • 累计撰写 302 篇文章
    • 累计收到 527 条评论
    嵌入式第二次测试解答
    我的学记|刘航宇的博客

    嵌入式第二次测试解答

    刘航宇
    2021-05-12 / 3 评论 / 284 阅读 / 正在检测是否收录...

    具体操作流程参考上篇文章:
    嵌入式实战—应用open调用内核open,应用close调用内核close通信程序
    hello.c需要修改为:

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    #include <linux/cdev.h>
    #include <linux/kdev_t.h>
    #include <linux/fs.h>   //file_operations 
    MODULE_LICENSE ("GPL");
    int i = 10;
    struct file_operations ops;
    struct cdev mycdev;
    int major = 254;
    int minor = 5;
    dev_t dev;
    int j= 0;
    int k= 0; 
    int open(struct inode *pinode,struct file *pfile)
    {
        j++;
        printk(KERN_INFO "liuhangyu myopen run j= %d\n",j);
        return 0;
    } 
    int close(struct inode *pinode,struct file *pfile)
    {
        k++;
        printk(KERN_INFO "liuhangyu myclose run k= %d\n",k);
        return 0;
    }
    void myuart_init(void)
    {
        int ret;
        mycdev.owner=THIS_MODULE;
        dev = MKDEV(major,minor);
        ret = register_chrdev_region(dev,5,"myuart");
        if(ret==-1)
        {
            printk(KERN_INFO "dev cant't use\n");
            ret = alloc_chrdev_region(&dev,5,1,"myuart");   //automatic allocation dev
            if(ret==0)
            {
                printk(KERN_INFO "alloc : major= %d\n",MAJOR(dev));
            }
            else
            {
                printk(KERN_INFO "alloc error\n");
                return ; 
            }
        }
        else
            {
            printk(KERN_INFO "dev can use\n");
            }
        ops.owner=THIS_MODULE;
        ops.open=open;
        ops.release=close;
        cdev_init(&mycdev,&ops);
        cdev_add(&mycdev,dev,5);  //mycdev send dev,equipment 1
        return ;
     }  
     static int  __init hello_init (void)
    {
        i ++;
        myuart_init();    //init
        printk (KERN_INFO "driver init,i = %d\n",i);
        return 0;
    }
    
    static void  __exit hello_exit (void)
    {
        i ++; 
        printk (KERN_INFO "driver exit,i = %d\n",i);
        unregister_chrdev_region(dev,5);
        cdev_del(&mycdev); 
    }
    
    module_init (hello_init);
    module_exit (hello_exit);

    app.c需要修改为

    #include "stdio.h"
    #include "fcntl.h"
    #include "unistd.h"
    #include "stdlib.h"
    int main()
    {
        int fd;
        int q = 0;
        while(1)
        {
            fd = open("./myuart",O_RDWR,0777) ;
            if(fd==-1)
            {
                printf("open error\n");
                return -1;
             }
             q ++;
             printf("liuhangyu open  run q = %d\n",q);
             sleep (3);
             close(fd);
             q ++;
             printf("liuhangyu close  run q = %d\n",q);
             sleep (3);
        }
        return 0;
    }

    直接在此输入下面这个命令,才能./app
    mknod ./myuart c 254 5

    5
    边界链码、归一化链码与一阶差分链码
    « 上一篇 2021-05-22
    matlab实现灰度图像边缘提取
    下一篇 » 2021-05-11

    评论 (3)

    取消
    1. 头像
      二今
      Windows 10 · Google Chrome

      请问一下为啥到./app.c的时候会open error呀?步骤和程序都一样的,只是把名字改了呀表情

      回复
      1. 头像
        刘航宇 作者
        Android · Google Chrome
        @ 二今

        没有mknod或者不对应mknod的

        回复
      2. 头像
        二今
        Windows 10 · Google Chrome
        @ 二今

        我dmesg后看到出现page flip error 和assuming drive cache :write through表情

        回复