• 字符设备驱动(二)按键点灯



    title: 字符设备驱动(二)按键点灯
    tags: linux
    date: 2018-11-21 18:06:37
    toc: true

    字符设备驱动(二)按键点灯

    数据交互

    用户内存和内核内存是独立的,在各自的地址空间实现。内核与用户函数交互需要使用copy_from_usercopy_to_user.为什么?点这个

    1. 直接使用那个地址很不安全,如果应用程序传过来一个非法地址,就有可能panic系统。或者应用程序可以给出一个指针指向kernel地址(>PAGE_OFFSET),那样就可以随意访问kernel page数据。
    2. 对于大多数有MMU的平台,情况就有了一些变化:用户空间传过来的指针是在虚拟地址空间上的,它指向的虚拟地址空间很可能还没有真正映射到实际的物理页面上。

    物理地址访问

    Linux 的驱动程序直接操作不了物理地址,需要使用虚拟地址去映射,使用函数ioremap,释放使用iounmap

    gpio_va = ioremap(0x56000000, 0x100000);
    iounmap(gpio_va);
    

    查询方式按键

    目的: 通过查询方式查到按键按下,打印按键值,如果按键被按下的话,点亮led.

    驱动程序设计

    #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>
    
    
    
    volatile unsigned long *gpfcon;
    volatile unsigned long *gpfdat;
    volatile unsigned long *gpgcon;
    volatile unsigned long *gpgdat;
    
    
    static struct class *drv_class;
    static struct class_device	*drv_class_dev;
    
    static int drv_open(struct inode *inode, struct file *file)
    {
    	int minor = MINOR(inode->i_rdev);
    	printk("drv_open=%d
    ",minor);
    
    	/* 配置GPF0,2为输入引脚 */
    	*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
    	/* 配置GPG3,11为输入引脚 */
    	*gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2)));
    
    	/* 配置GPF4,5,6为输出 */
    	*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
    	*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
    
    	return 0;
    }
    
    static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
    {
    	//int minor =  MINOR(file->f_dentry->d_inode->i_rdev);
    	//printk("drv_write=%d
    ",minor);
    
    	int val;
    	copy_from_user(&val, buf, count); //	copy_to_user();
    
    	if (val == 1)
    	{
    		// 点灯
    		*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    	}
    	else
    	{
    		// 灭灯
    		*gpfdat |= (1<<4) | (1<<5) | (1<<6);
    	}
    	return 0;
    }
    
    static ssize_t drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
    {
    	/* 返回4个引脚的电平 */
    	unsigned char key_vals[4];
    	int regval;
    
    	if (size != sizeof(key_vals))
    		return -EINVAL;
    
    	/* 读GPF0,2 */
    	regval = *gpfdat;
    	key_vals[0] = (regval & (1<<0)) ? 1 : 0;
    	key_vals[1] = (regval & (1<<2)) ? 1 : 0;
    	/* 读GPG3,11 */
    	regval = *gpgdat;
    	key_vals[2] = (regval & (1<<3)) ? 1 : 0;
    	key_vals[3] = (regval & (1<<11)) ? 1 : 0;
    
    	copy_to_user(buf, key_vals, sizeof(key_vals));
    	
    	return sizeof(key_vals);
    }
    
    
    static struct file_operations drv_fops = {
    	.owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
        .open   =   drv_open,     
    	.write	=	drv_write,
    	.read	=	drv_read,	   
    };
    
    static int major;
    static int drv_init(void)
    {
    	int minor=0;
    	major=register_chrdev(0, "drv", &drv_fops); // 注册, 告诉内核
    	drv_class = class_create(THIS_MODULE, "drv");
    	drv_class_dev = class_device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xyz%d", minor);
    
    	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    	gpfdat = gpfcon + 1;
    	gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
    	gpgdat = gpgcon + 1;
    	return 0;
    }
    
    static void drv_exit(void)
    {
    	unregister_chrdev(major, "drv"); // 卸载
    	class_device_unregister(drv_class_dev);
    	class_destroy(drv_class);
    	iounmap(gpfcon);
    	iounmap(gpgcon);
    }
    
    module_init(drv_init);
    module_exit(drv_exit);
    MODULE_LICENSE("GPL");
    
    
    

    测试程序设计

    
    #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;
    	unsigned char key_vals[4];
    	int cnt = 0;
    
    	if (argc <= 1)  {printf("nofile select !
    "); return ;}
    	fd = open(argv[1], O_RDWR);
    	if (fd < 0)
    	{
    		printf("can't open!
    ");
    	}
    	
    	while (1)
    	{
    		read(fd, key_vals, sizeof(key_vals));
    		if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
    		{
    			val=1;
    			printf("%04d key pressed: %d %d %d %d
    ", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
    			write(fd, &val, 4);
    		}
    		else
    		{
    			val=0;
    			write(fd, &val, 4);
    		}
    	}
    	return 0;
    }
    

    测试

    1. 加载驱动insmod dri.ko

    2. 测试运行./test /dev/xyz0

    3. 按键按下,打印按键的同时点灯

    4. 运行后使用ctrl+c终止程序,使用ctrl+z程序休眠,继续输入q退出,使用ps命令还能看到,但是使用top命令看到使用cpu为0,使用kill -9 pid关闭进程

      CTRL+Z停止进程并放入后台
      jobs 显示当前暂停的进程
      fg %N 使第N个任务在前台运行
      bg %N 使第N个任务在后台运行(%前有空格)
      
    5. 启用后台运行./test /dev/xyz0 &

    6. 查看下占用率top,占用99%

    7. 关闭程序才能卸载模块

      ps 查看任务pid
      kill -9 pid #关闭进程
      rmmod dri.ko 卸载程序
      
  • 相关阅读:
    华为2019软件题
    图像的存储格式转化(python实现)
    windows+两个ubuntu系统的引导启动问题
    《视觉SLAM十四讲》课后习题—ch6
    视觉SLAM十四讲课后习题—ch8
    LINQ根据时间排序问题(OrderBy、OrderByDescending)
    Element的扩展
    CSharp
    jQuery函数使用记录
    日记越累
  • 原文地址:https://www.cnblogs.com/zongzi10010/p/9999212.html
Copyright © 2020-2023  润新知