新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 嵌入式Linux設備驅動開發之:實驗內容——test驅動

        嵌入式Linux設備驅動開發之:實驗內容——test驅動

        作者: 時間:2013-09-13 來源:網絡 收藏

        本文引用地址:http://www.104case.com/article/257106.htm

        11.7實驗內容——

        1.實驗目的

        該實驗是編寫最簡單的字符驅動程序,這里的設備也就是一段內存,實現簡單的讀寫功能,并列出常用格式的Makefile以及驅動的加載和卸載腳本。讀者可以熟悉字符的整個編寫流程。

        2.實驗內容

        該實驗要求實現對虛擬設備(一段內存)的打開、關閉、讀寫的操作,并要通過編寫測試程序來測試虛擬設備及其驅動運行是否正常。

        3.實驗步驟

        (1)編寫代碼。

        這個簡單的驅動程序的源代碼如下所示:

        /*test_drv.c*/

        #includelinux/module.h>

        #includelinux/init.h>

        #includelinux/fs.h>

        #includelinux/kernel.h>

        #includelinux/slab.h>

        #includelinux/types.h>

        #includelinux/errno.h>

        #includelinux/cdev.h>

        #includeasm/uaccess.h>

        #defineTEST_DEVICE_NAMEtest_dev

        #defineBUFF_SZ1024

        /*全局變量*/

        staticstructcdevtest_dev;

        unsignedintmajor=0;

        staticchar*data=NULL;

        /*讀函數*/

        staticssize_ttest_read(structfile*file,

        char*buf,size_tcount,loff_t*f_pos)

        {

        intlen;

        if(count0)

        {

        return-EINVAL;

        }

        len=strlen(data);

        count=(len>count)?count:len;

        if(copy_to_user(buf,data,count))/*將內核緩沖的數據拷貝到用戶空間*/

        {

        return-EFAULT;

        }

        returncount;

        }

        /*寫函數*/

        staticssize_ttest_write(structfile*file,constchar*buffer,

        size_tcount,loff_t*f_pos)

        {

        if(count0)

        {

        return-EINVAL;

        }

        memset(data,0,BUFF_SZ);

        count=(BUFF_SZ>count)?count:BUFF_SZ;

        if(copy_from_user(data,buffer,count))/*將用戶緩沖的數據復制到內核空間*/

        {

        return-EFAULT;

        }

        returncount;

        }

        /*打開函數*/

        staticinttest_open(structinode*inode,structfile*file)

        {

        printk(Thisisopenoperationn);

        /*分配并初始化緩沖區*/

        data=(char*)kmalloc(sizeof(char)*BUFF_SZ,GFP_KERNEL);

        if(!data)

        {

        return-ENOMEM;

        }

        memset(data,0,BUFF_SZ);

        return0;

        }

        /*關閉函數*/

        staticinttest_release(structinode*inode,structfile*file)

        {

        printk(Thisisreleaseoperationn);

        if(data)

        {

        kfree(data);/*釋放緩沖區*/

        data=NULL;/*防止出現野指針*/

        }

        return0;

        }

        /*創建、初始化字符設備,并且注冊到系統*/

        staticvoidtest_setup_cdev(structcdev*dev,intminor,

        structfile_operations*fops)

        {

        interr,devno=MKDEV(major,minor);

        cdev_init(dev,fops);

        dev->owner=THIS_MODULE;

        dev->ops=fops;

        err=cdev_add(dev,devno,1);

        if(err)

        {

        printk(KERN_NOTICEError%daddingtest%d,err,minor);

        }

        }

        linux操作系統文章專題:linux操作系統詳解(linux不再難懂)

        linux相關文章:linux教程



        上一頁 1 2 3 4 下一頁

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 昌都县| 尼勒克县| 盐源县| 镇巴县| 玛多县| 齐河县| 土默特左旗| 巴楚县| 淮阳县| 南澳县| 深水埗区| 舟山市| 商城县| 台前县| 南召县| 溆浦县| 镇江市| 颍上县| 中牟县| 崇明县| 沧州市| 仪陇县| 彰化市| 纳雍县| 进贤县| 清原| 聊城市| 精河县| 拉孜县| 葫芦岛市| 旅游| 万宁市| 黎城县| 娄底市| 北京市| 垣曲县| 东山县| 龙里县| 扎囊县| 民丰县| 柏乡县|