嵌入式实验六

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验六嵌入式设备驱动程序设计

班级:计算机14-1 学号:140344123139 姓名:23223 成绩:

一、实验目的

1. 掌握设备驱动程序的基本框架结构;

2. 掌握设备进入点的创建、查看、删除方法;

3. 掌握设备驱动程序模块的加载、查看、卸载方法;

4. 编写简单字符设备驱动程序,并测试运行。

二、实验内容

1.编写一个字符设备驱动程序,可实现字符串的读、写操作,再编写一个调用该设备

驱动程序功能接口(打开、关闭、读、写)的用户程序,测试该驱动程序是否能正常运行。

2.对上述驱动程序进行修改,实现设备的互斥访问(设备上锁)。

(1)编译

(2)装载模块

(3)创建设备进入点

(4)测试驱动程序

(5) 程序代码如下

devDrv.c

#include "linux/kernel.h"

#include "linux/module.h"

#include "linux/fs.h"

#include "linux/init.h"

#include "linux/types.h"

#include "linux/errno.h"

#include "linux/uaccess.h"

#include "linux/kdev_t.h"

#define MAX_SIZE 1024

static int my_open(struct inode *inode, struct file *file);

static int my_release(struct inode *inode, struct file *file);

static ssize_t my_read(struct file *file, char __user *user, size_t t, loff_t *f); static ssize_t my_write(struct file *file, const char __user *user, size_t t, loff_t *f);

static char message[MAX_SIZE] = "-------congratulations--------!";

static int device_num = 0;//设备号

static int counter = 0;//计数用

static int mutex = 0;//互斥用

static char* devName = "myDevice";//设备名

struct file_operations pStruct =

{ open:my_open, release:my_release, read:my_read, write:my_write, };

/* 注册模块*/

int init_module()

{

int ret;

/* 函数中第一个参数是告诉系统,新注册的设备的主设备号由系统分配,* 第二个参数是新设备注册时的设备名字,

* 第三个参数是指向file_operations的指针,

* 当用设备号为0创建时,系统一个可以用的设备号创建模块*/

ret = register_chrdev(0, devName, &pStruct);

if (ret < 0)

{

printk("regist failure!\n");

return -1;

}

else

{

printk("the device has been registered!\n");

device_num = ret;

printk("<1>the virtual device's major number %d.\n", device_num);

printk("<1>Or you can see it by using\n");

printk("<1>------more /proc/devices-------\n");

printk("<1>To talk to the driver,create a dev file with\n");

printk("<1>------'mknod /dev/myDevice c %d 0'-------\n", device_num);

printk("<1>Use \"rmmode\" to remove the module\n");

return 0;

}

}

/* 注销模块,函数名很特殊*/

void cleanup_module()

{

unregister_chrdev(device_num, devName);

printk("unregister it success!\n");

}

static int my_open(struct inode *inode, struct file *file)

{

if(mutex)

return -EBUSY;

mutex = 1;//上锁

printk("<1>main device : %d\n", MAJOR(inode->i_rdev));

printk("<1>slave device : %d\n", MINOR(inode->i_rdev));

printk("<1>%d times to call the device\n", ++counter);

try_module_get(THIS_MODULE);

相关文档
最新文档