led驱动程序和test程序分析

来源:互联网 发布:北京市大数据公司 编辑:程序博客网 时间:2024/05/19 15:18

led驱动程序:

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <mach/gpio-samsung.h>
#include<linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>

static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
volatile unsigned long *gpfcon =NULL;
volatile unsigned long *gpfdat =NULL;
    
static int  first_drv_open(struct inode *inode, struct file *file){
    *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 first_drv_write(struct file *file, const char __user *buf,
             size_t count, loff_t *ppos)
{
    printk("first_drv_write\n");
    int val;
    copy_from_user(&val,buf,count);
    if(val==1)
        {*gpfdat &=~((1<<4)|(1<<5)|(1<<6));}
    else
        {*gpfdat |=((1<<4)|(1<<5)|(1<<6));}
    return 0;
        

}

static struct file_operations first_drv_fops={
    .owner = THIS_MODULE,
    .open = first_drv_open,
    .write = first_drv_write,

};

int major;
int first_drv_init(void)
{
    major = register_chrdev(0, "first_drv", &first_drv_fops);
    
    firstdrv_class=class_create(THIS_MODULE, "first_drv");
    
    firstdrv_class_dev=device_create(firstdrv_class , NULL, MKDEV(major, 0), NULL,"xyz");
    printk("first_drv\n");
    
    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    
    gpfdat = gpfcon + 1;
    
    return 0;

}


int first_drv_exit(void)
{
    unregister_chrdev(major, "first_drv");
    device_unregister(firstdrv_class_dev);
    class_destroy(firstdrv_class);
    printk("first_drv_exit\n");
    iounmap(gpfcon);
    return 0;
}

module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");


分析:

定义
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
volatile unsigned long *gpfcon =NULL;
volatile unsigned long *gpfdat =NULL;

static int  first_drv_open(struct inode *inode, struct file *file)配置引脚


static ssize_t first_drv_write(struct file *file, const char __user *in,
             size_t size, loff_t *off)点灯和关灯


copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.



static struct file_operation first_drv_fops调用open函数




int first_drv_init(void)用来加载驱动
  /* 创建firstdrv类 */  
    firstdrv_class = class_create(THIS_MODULE, "firstdrv");
 /* 在firstdrv类下创建xxx设备,供应用程序打开设备*/  
    firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");




gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//gpfcon识别虚拟地址,ioremap函数作用将物理地址映射到虚拟地址给gpfcon识别
gpfdat = gpfcon + 1;//因为在之前定义过gpfcon和gpfdat为长整型为4个字节,加1就是加4位
ps:
void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
入口: phys_addr:要映射的起始的IO地址;
size:要映射的空间的大小;
flags:要映射的IO空间的和权限有关的标志;
功能: 将一个IO地址空间映射到内核的虚拟地址空间上去,便于访问;



int first_drv_exit(void)用来卸载驱动
在驱动模块卸载函数__exit中的cdev_del(struct cdev *)函数调用后即注销字符设备后调用 unregister_chrdev_region(dev_t devno,  unsigned
count)函数释放在驱动加载函数__init中通过register_chrdev_region、alloc_chrdev_region,注册的主设备号。(cdev_del函数、unregister_chrdev_region函数缺一不可);



module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");



copy_to_user和copy_from_use区别
copy_to_user和copy_from_user就是在进行驱动相关程序设计的时候,要经常遇到的两个函数。
由于内核空间与用户空间的内存不能直接互访,因此借助函数copy_to_user()完成用户空间到内核空间的复制,函数copy_from_user()完成内核空间到用户空间的复制。
原因:read和write 的buff 参数是在用户空间


test应用程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
    int fd;
    int val = 1;
    fd = open("/dev/xyz", O_RDWR);
    if (fd < 0)
    {
        printf("can't open!\n");
    }
    if (argc != 2)
    {
        printf("Usage :\n");
        printf("%s <on|off>\n", argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "on") == 0)
    {
        val  = 1;
    }
    else
    {
        val = 0;
    }
    
    write(fd, &val, 4);
    return 0;
}

argc参数个数

argv元素格式


fd =open("/dev/xyz",O_RDWR)打开设备节点,读写打开

ps:
O_RDONLY 只读打开。
O_WRONLY 只写打开。
O_RDWR 读、写打开。
O_APPEND 每次写时都加到文件的尾端。
O_CREAT 若此文件不存在则创建它。使用此选择项时,需同时说明第三个参数mode,用其说明该新文件的存取许可权位。
O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则出错。这可测试一个文件是否存在,如果不存在则创建此文件成为一个原子操作。
O_TRUNC 如果此文件存在,而且为只读或只写成功打开,则将其长度截短为0。
O_NOCTTY 如果p a t h n a m e指的是终端设备,则不将此设备分配作为此进程的控制终端。
O_NONBLOCK 如果p a t h n a m e指的是一个F I F O、一个块特殊文件或一个字符特殊文件,则此选择项为此文件的本次打开操作和后续的I / O操作设置非阻塞方式。
O_SYNC 使每次w r i t e都等到物理I / O操作完成。



if(fd<0)
{printf("can't open!\n");}fd为小于0则没有打开设备节点



if(argc !=2){printf("Usage :\n");printf("%s<on|off>\n", argv[0]);return 0;}在串口终端如果输入不是两个参数会提示你输出printf后的内容




if(strcmp(argv[1],"on")==0)   //比较第2个参数不是on则val为1,联系其write函数中则关灯,
        {
            val =1;
        }
    else
        {
            val=0;
        }
    write(fd, &val, 4);

0 0