Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226222
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: LINUX

2015-07-04 21:48:29

Nguhyw第一个驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>




static int Nguhyw_open(struct inode *inode, struct file *file)
{
 printk("Nguhyw fister program Nguhyw_open\n");
 return 0;

}



static ssize_t Nguhyw_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{

 printk("Nguhyw fister program Nguhyw_write\n");
 return 0;

}




static struct file_operations Nguhyw_fops = {
 
 .owner = THIS_MODULE,
 .write = Nguhyw_write,
 .open = Nguhyw_open,
};


static int Nguhyw_drv_init(void)
{
 register_chrdev(111 , "Nguhyw", &Nguhyw_fops ); //注册驱动程序
 return 0;
 
}

static void Nguhyw_drv_exit(void)
{
 unregister_chrdev(111 , "Nguhyw"); //卸载驱动
 
 
}

module_init(Nguhyw_drv_init);
module_exit(Nguhyw_drv_exit);

/*******************************************************************************************/
编写好后拿到Linux下编译即可,编译方法复制别人的Makefile文件然后修改其中的 .o
即可,然后拷贝到文件系统目录下,然后重新制作根文件系统,烧写到开发板。
1.  cat /proc/devices  查看目前的设备驱动
2. insmod Nguhyw.ko   安装字符设别驱动
3. 编写测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
 int fd;
 int val = 1;
 fd = open("/dev/Nguhyw", O_RDWR);
 
 if (fd < 0)printf("can't open!\n");

 write(fd, &val, 4);
 return 0;

}
/****************************************************************************************/
4. 编译Nguhyw_first_drv.c 文件  arm-linux-gcc -o Nguhyw_first_drv Nguhyw_first_drv.c 
编译完成后放到文件系统里,烧写到开发板。
5. 执行 ./Nguhyw_first_drv  显示can't open!
6. 创建设备节点 mknod /dev/xxx c 111 0
7. 执行 ./Nguhyw_first_drv  显示
Nguhyw fister program Nguhyw_open
Nguhyw fister program Nguhyw_write


让系统自动分配主设备号:
需修改的代码:
int major;
static int Nguhyw_drv_init(void)
{
 major = register_chrdev(, "Nguhyw", &Nguhyw_fops ); //注册驱动程序
 return 0;
 
}

static void Nguhyw_drv_exit(void)
{
 unregister_chrdev(major , "Nguhyw"); //卸载驱动
 
}


自动创建设备节点:
在Nguhyw。c里 添加代码:
static struct class *Nguhyw_class;
static struct class_device *Nguhyw_class_dev;


设备驱动总结:
1. 编写框架
2. 完善硬件的操作
3. 驱动程序中药使用虚拟地址:













阅读(1368) | 评论(0) | 转发(0) |
0

上一篇:输入子系统概念

下一篇:stm32拼图游戏

给主人留下些什么吧!~~