我的第一个嵌入式linux驱动(基于韦东山led驱动)

来源:互联网 发布:网络摄像头哪个牌子好 编辑:程序博客网 时间:2024/05/16 01:35
/*目的:通过应用函数来实现在底层打印相关内容
*2016年4月29日22:08:36
*/
#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>


/*
first_drv_open函数的形参其实就是first_drv_open结构体中的open函数的参数,因为调用我们自己写的first_drv_open
函数时,会通过主设备号找到first_drv_open结构体,然后执行first_drv_open结构体中的open函数,主严是为了参数的传递


*/
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("first_drv_open\n");
return 0;
}


static int first_drv_write(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{

printk("first_drv_write\n");
return 0;
}


/*
file_operations是内核中的结构体,里面定义了一些函数,


*/


static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,    //表示将first_drv_open函数的地址赋给file_operations结构体中open函数,
.write = first_drv_write,//以后执行 first_drv_open函数时会自动调用file_operations结构体中的open函数
};




//入口函数
int first_drv_init(void)
{
register_chrdev(111,"first_dev",&first_drv_fops);//注册函数,告诉内核
return 0;


}




//出口函数
void first_drv_exit(void)
{
unregister_chrdev(111,"first_dev");//卸载驱动


}


/*修饰
module_init是定义一个结构体,结构体里面有一个函数指针,指向first_drv_init这个入口函数?
当我们用insmod加载一个驱动时,内核会自动找到这个结构体,调用里面的函数指针,指向入口函数
*/


module_init(first_drv_init);
module_exit(first_drv_exit);


/*
实现过程:当使用insmod加载驱动时,首先通过module_init(first_drv_init);找到入口函数first_drv_init,
该函数告诉内核主设备号111和first_drv_fops结构体一起向内核注册,这样以后在对主设备号111操作时,
就会调用first_drv_fops结构体中相同的函数来处理。

*/




//测试函数




#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/xxx",O_RDWR);//xxx表示名字不重要
if(fd < 0)
printf("can't open!"\n);
write(fd,&val,4);


return 0;


}
/*
一般函数是可以带参数的,main函数也不例外,格式通常采用这种固定形式。
由于main
不被其他函数调用,所以不能从程序中获取参数。实际上参数是执行时从操作系统上获取?
模琣rgc是参数个数,argv是参数列表。
当我们要运行一个可执行文件时,在DOS
提示符下键入文件名,再输入实际参数即可把这些实参传送到main的形参中去。


argc是你主程序参数的个数。


argv[0]是你编译出来执行时候程序的名字。
argv[1].....是你主程序需要的参数。
举例说明:如下程序 aa.c
#include<stdio.h>
#include<stdlib.h>
#include<stdring.h>
int main(int argc, char *argv[])
{
        printf("%d\n",argc);
        printf("%s\n",argv[0]);
        printf("%s\n",argv[1]);
        printf("%s\n",argv[2]);
        return 0;
}
编译: gcc -o hello aa.c (也就是编译出来的执行文件叫hello,这是linux
上的编译方式)
执行: hello aa  bb
结果:
2
hello
aa
bb


*/






/*
#include<fcntl.h>
intopen(constchar*pathname,intflags);
intopen(constchar*pathname,intflags,mode_tmode);
返回值:成功则返回文件描述符,否则返回-1


对于open函数来说,第三个参数仅当创建新文件时(即 使用了O_CREAT 
时)才使用,用于指定文件的访问权限位(access permission bits)。pathname 
是待打开/创建文件的POSIX路径名(如/home/user/a.cpp);flags 
用于指定文件的打开/创建模式,这个参数可由以下常量(定义于fcntl.h
)通过逻辑位或逻辑构成。


1
2
3


O_RDONLY只读模式
O_WRONLY只写模式
O_RDWR读写模式




write: int write(int fd, char * buf, unsigned count);


*/


/*测试结果是:can't open!
因为在板子上不存在/dev/xxx,如果想要在板子上测试成功的话
需要使用mknod /dev/xxx c 111 0
c 代表字符   111 代表主设备号 0 代表次设备号
mknod /dev/xxx c 111 0 这行命令代表创建设备节点,
把/dev/xxx设备设为字符设备,主设备号为111,次设备号为0;




*/

0 0
原创粉丝点击