嵌入式内核及驱动开发之学习笔记(三) 点灯实验
完成应用程序通过驱动控制硬件的实现。实验建立在之前的框架上,我们先实现用户层与内核层之间的数据交互,驱动程序拿到用户传来的指令后,就可以执行点灯的动作了。
应用程序与驱动数据交互
对于驱动程序而言,使用copy_to_user 和 copy_from_user函数与应用程序进行数据交互。当应用程序read时,对应驱动程序copy_to_user;当应用程序write时,对应驱动程序copy_from_user。
copy_to_user是将数据从内核空间拷贝到用户空间
int copy_to_user(void __user * to, const void * from, unsigned long n)
 copy_from_user将数据从用户空间拷贝到内核空间
int copy_from_user(void * to, const void __user * from, unsigned long n)
 
驱动与硬件的连接
地址的映射
控制外设,其实就是控制寄存器地址。由于MMU(内存管理单元),内核层的驱动不能直接操作硬件上的地址,通过虚拟地址来实现。
驱动程序 --> 虚拟地址 --> MMU --> 物理地址(寄存器) --> 硬件
地址映射
void *ioremap(cookie, size)
 cookie: 物理地址 size: 长度(字节位单位) 返回值是虚拟地址
去映射
void iounmap(void __iomem *addr)
 addr 映射之后到虚拟地址
实现过程
驱动程序的实现
在之前的框架的基础上,一方面对下实现了硬件的操作,加载模块时将控制LED灯的引脚(GPX2_7)地址的映射,并设置引脚控制寄存器为输出模式;另一方面,实现对应用层接口的封装,当用户程序向驱动写数据时,chr_drv_write函数接收并且判断该数值,如果是正数则点亮LED灯,否者灭灯。
  
   - 
    
     
    
    
     
      //chr_drv.c
     
    
- 
    
     
    
    
     
      #include <linux/init.h>
     
    
- 
    
     
    
    
     
      #include <linux/module.h>
     
    
- 
    
     
    
    
     
      #include <linux/fs.h>
     
    
- 
    
     
    
    
     
      #include <linux/device.h>
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      #include <asm/uaccess.h>
     
    
- 
    
     
    
    
     
      #include <asm/io.h>
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      #define GPX2_CON 0x11000C40 
     
    
- 
    
     
    
    
     
      #define GPX2_SIZE 8
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      volatile unsigned long *gpx2conf;
     
    
- 
    
     
    
    
     
      volatile unsigned long *gpx2dat;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      static unsigned int dev_major = 250;
     
    
- 
    
     
    
    
     
      static struct class *devcls;
     
    
- 
    
     
    
    
     
      static struct device *dev;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      ssize_t chr_drv_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos);
     
    
- 
    
     
    
    
     
      ssize_t chr_drv_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos);
     
    
- 
    
     
    
    
     
      int chr_drv_open(struct inode *inode, struct file *filp);
     
    
- 
    
     
    
    
     
      int chr_drv_close(struct inode *inode, struct file *filp);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      const struct file_operations my_fops = {
     
    
- 
    
     
    
    
     
      	.open = chr_drv_open,
     
    
- 
    
     
    
    
     
      	.read = chr_drv_read,
     
    
- 
    
     
    
    
     
      	.write = chr_drv_write,
     
    
- 
    
     
    
    
     
      	.release = chr_drv_close,
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      };
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      static int __init chr_drv_init(void)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	printk("-------%s-------------\n", __FUNCTION__);
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     	//向系统申请设备号
     
    
- 
    
     
    
    
     	int ret;
     
    
- 
    
     
    
    
     
      	ret = register_chrdev(dev_major, "chr_dev_test", &my_fops);
     
    
- 
    
     
    
    
     	if(ret == 0){
     
    
- 
    
     
    
    
     		printk("register ok\n");
     
    
- 
    
     
    
    
     
      	}else{
     
    
- 
    
     
    
    
     		printk("register failed\n");
     
    
- 
    
     
    
    
     		return -EINVAL;
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//创建设备结点
     
    
- 
    
     
    
    
     
      	devcls = class_create(THIS_MODULE, "chr_cls");
     
    
- 
    
     
    
    
     
      	dev = device_create(devcls, NULL, MKDEV(dev_major, 0), NULL, "chr2");
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//将物理地址映射成为虚拟地址,用指针指向这个地址
     
    
- 
    
     
    
    
     
      	gpx2conf = ioremap(GPX2_CON, GPX2_SIZE);
     
    
- 
    
     
    
    
     
      	gpx2dat = gpx2conf + 1;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//GPX2_7设置成输出模式
     
    
- 
    
     
    
    
     
      	*gpx2conf &= ~(0xf<<28);
     
    
- 
    
     
    
    
     
      	*gpx2conf |= (0x1<<28);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      static void __exit chr_drv_exit(void)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	printk("-------%s-------------\n", __FUNCTION__);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//取消地址映射
     
    
- 
    
     
    
    
     	iounmap(gpx2conf);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//销毁这个设备结点
     
    
- 
    
     
    
    
     	device_destroy(devcls,  MKDEV(dev_major, 0));
     
    
- 
    
     
    
    
     	class_destroy(devcls);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     	//释放这个设备号
     
    
- 
    
     
    
    
     	unregister_chrdev(dev_major, "chr_dev_test");
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      module_init(chr_drv_init);
     
    
- 
    
     
    
    
     
      module_exit(chr_drv_exit);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      MODULE_LICENSE("GPL");
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      static int kernel_val = 555;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      // read(fd, buf, size);
     
    
- 
    
     
    
    
     
      ssize_t chr_drv_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	//printk("-------%s-------\n", __FUNCTION__);
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     	int ret;
     
    
- 
    
     
    
    
     
      	ret = copy_to_user(buf, &kernel_val, count);
     
    
- 
    
     
    
    
     	if(ret > 0)
     
    
- 
    
     
    
    
     
      	{
     
    
- 
    
     
    
    
     		printk("copy_to_user error\n");
     
    
- 
    
     
    
    
     		return -EFAULT;
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      ssize_t chr_drv_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	//printk("-------%s-------\n", __FUNCTION__);
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     	int ret;
     
    
- 
    
     
    
    
     	int value;
     
    
- 
    
     
    
    
     
      	ret = copy_from_user(&value,  buf, count);
     
    
- 
    
     
    
    
     	if(ret > 0)
     
    
- 
    
     
    
    
     
      	{
     
    
- 
    
     
    
    
     		printk("copy_to_user error\n");
     
    
- 
    
     
    
    
     		return -EFAULT;
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	if(value){
     
    
- 
    
     
    
    
     
      		*gpx2dat |= (1<<7);
     
    
- 
    
     
    
    
     
      	}else{
     
    
- 
    
     
    
    
     
      		*gpx2dat &= ~(1<<7);
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int chr_drv_open(struct inode *inode, struct file *filp)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	printk("-------%s-------\n", __FUNCTION__);
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int chr_drv_close(struct inode *inode, struct file *filp)
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	printk("-------%s-------\n", __FUNCTION__);
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
 
应用程序
在应用层的角度,一切皆是文件。程序把驱动当成一个文件(设备结点)来操作,打开文件后,间隔一段时间写入0或1,最终实现led灯的闪烁。
  
   - 
    
     
    
    
     
      //chr_test.c
     
    
- 
    
     
    
    
     
      #include <stdio.h>
     
    
- 
    
     
    
    
     
      #include <string.h>
     
    
- 
    
     
    
    
     
      #include <stdlib.h>
     
    
- 
    
     
    
    
     
      #include <sys/types.h>
     
    
- 
    
     
    
    
     
      #include <sys/stat.h>
     
    
- 
    
     
    
    
     
      #include <fcntl.h>
     
    
- 
    
     
    
    
     
      #include <unistd.h>
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int main(int argc, char *argv[])
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     	int fd;
     
    
- 
    
     
    
    
     	int value = 0;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	//打开设备结点
     
    
- 
    
     
    
    
     
      	fd = open("/dev/chr2", O_RDWR);
     
    
- 
    
     
    
    
     	if(fd < 0)
     
    
- 
    
     
    
    
     
      	{
     
    
- 
    
     
    
    
     		perror("open");
     
    
- 
    
     
    
    
     		exit(1);
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
     	//读操作
     
    
- 
    
     
    
    
     	read(fd, &value, 4);
     
    
- 
    
     
    
    
     	//printf("___USER___: value = %d\n", value);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	while(1)
     
    
- 
    
     
    
    
     
      	{
     
    
- 
    
     
    
    
     		//写操作
     
    
- 
    
     
    
    
     
      		value = 0;
     
    
- 
    
     
    
    
     		write(fd, &value, 4);
     
    
- 
    
     
    
    
     		sleep(1);
     
    
- 
    
     
    
    
     		
     
    
- 
    
     
    
    
     		//写操作
     
    
- 
    
     
    
    
     
      		value = 1;
     
    
- 
    
     
    
    
     		write(fd, &value, 4);
     
    
- 
    
     
    
    
     		sleep(1);
     
    
- 
    
     
    
    
     		
     
    
- 
    
     
    
    
     
      	}
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	close(fd);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     	return 0;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      }
     
    
 
Makefile文件
负责编译管理工程。
  
   - 
    
     
    
    
     
      ROOTFS_DIR = /nfs/rootfs
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      APP_NAME = chr_test
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      CROSS_COMPILE = /home/linux/soft/gcc-4.6.4/bin/arm-none-linux-gnueabi-
     
    
- 
    
     
    
    
     
      CC = $(CROSS_COMPILE)gcc
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      ifeq ($(KERNELRELEASE), )
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      KERNEL_DIR = /mnt/hgfs/sharefolder/kernel/linux-3.14-fs4412
     
    
- 
    
     
    
    
     
      CUR_DIR = $(shell pwd)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      all :
     
    
- 
    
     
    
    
     
      	make -C  $(KERNEL_DIR) M=$(CUR_DIR) modules
     
    
- 
    
     
    
    
     
      	$(CC) $(APP_NAME).c  -o $(APP_NAME)
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     
      clean :
     
    
- 
    
     
    
    
     
      	make -C  $(KERNEL_DIR) M=$(CUR_DIR) clean
     
    
- 
    
     
    
    
     	
     
    
- 
    
     
    
    
     
      install:
     
    
- 
    
     
    
    
     
      	cp -raf *.ko $(APP_NAME)  $(ROOTFS_DIR)/drv_module
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      else
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      obj-m += chr_drv.o
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      endif
     
    
- 
    
     
    
    
      
     
    
 
查看实验结果
 编译并移动文件到nfs根目录
  
   - 
    
     
    
    
     
      root@linux:/mnt/hgfs/sharefolder/kernel/linux-3.14-fs4412/drivers/mydrivers/chr_drv# make
     
    
- 
    
     
    
    
     
      root@linux:/mnt/hgfs/sharefolder/kernel/linux-3.14-fs4412/drivers/mydrivers/chr_drv# make install
     
    
 
  
开发板加载模块,执行应用程序
 [root@farsight drv_module]# ls
 chr_drv.ko  chr_test
 [root@farsight drv_module]#
 [root@farsight drv_module]# insmod chr_drv.ko
 [ 4803.030000] -------chr_drv_init-------------
 [ 4803.035000] register ok
 [root@farsight drv_module]# ./chr_test
 [ 4809.940000] -------chr_drv_open-------
 ___USER___:  value = 555
 ^C[ 4817.835000] -------chr_drv_close-------
[root@farsight drv_module]#
  
观察开发板上led是闪烁状态。
文章来源: blog.csdn.net,作者:hinzer,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/feit2417/article/details/84066291
- 点赞
- 收藏
- 关注作者
 
             
           
评论(0)