嵌入式设备驱动程序设计

设计一个程序,在用户空间的用户应用程序中产生20个随机数,通过内核空间的设备驱动程序按五行四列的排列输出,并显示能被5整除的数。

分析:要实现这个功能需要做以下工作:


1.编写嵌入式设备驱动程序:实现按五行四列的排列输出,并显示能被5整除的数。

1设主设备号为100,即#define data_MAJOR 100

2在函数data_read()中将用户空间通过参数buf传递来的数据按5行4列的排列输出。在使用printk函数时,输出的格式用“%d”。

3在函数data_write()中找出用户空间通过参数buf传递来的能被5整除的数据输出到屏幕上。在使用printk函数时,输出的格式用“%d”。

4使用Vi编辑器编辑一个设备驱动程序data_drv.c。

/*************************************

*设备驱动程序data_drv.c

*由用户应用程序传递20个数值,在本设备驱动程序中进行排列输出,

*并打印出能被5整除的数

*************************************/

#include<linux/config.h>

#include<linux/kernel.h>

#include<linux/init.h>

#include<linux/devfs_fs_kernel.h>

#include<linux/module.h>

#define data_MAJOR 100

ssize_t data_read(struct file * file,char *buf,size_tcount, loff_t* f_ops)

{//将用户空间通过参数buf传递来的数据按5行4列的排列输出

      printk("data_read[--kernel--]\n");

      for(count=0;count<20;count++){

          printk("%d ",buf[count]);

          if((count+1)%4==0)

             printk("\n");

   }

      returncount;

}

ssize_t data_write(struct file * file, const char*buf, size_t count, loff_t * f_ops)

{//找出用户空间通过参数buf传递来的能被5整除的数据

      printk("data_write[--kernel--]\n");

      for(count=0;count<20;count++){

         if(buf[count]%5==0)

            printk("%d ",buf[count]);

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值