Raspberry Pi Driver Framework

1. The underlying driver framework of Raspberry Pi:

#include <linux/fs.h>		 	//file_operations声明
#include <linux/module.h>    	//module_init  module_exit声明
#include <linux/init.h>      	//__init  __exit 宏定义声明
#include <linux/device.h>	 	//class  devise声明
#include <linux/uaccess.h>   	//copy_from_user 的头文件
#include <linux/types.h>     	//设备号  dev_t 类型声明
#include <asm/io.h>          	//ioremap iounmap的头文件

static struct class* pin4_class;
static struct device* pin4_class_dev;

static dev_t devno;                     //设备号
static int major = 231;  		        //主设备号
static int minor = 0;			        //次设备号
static char* module_name = "pin4";   	//模块名


static int pin4_open(struct inode* inode, struct file* file)
{
    
    
    printk("pin4_open\n");  //内核的打印函数,和printf相似
    return 0;
}


static int pin4_read(struct file* file, char __user* buf, size_t count, loff_t* ppos)
{
    
    
    printk("pin4_read\n");
    return 0;
}


static ssize_t pin4_write(struct file* file, const char __user* buf, size_t count, loff_t* ppos)
{
    
    
    printk("pin4_write\n");
    return 0;
}


static struct file_operations pin4_fops = {
    
    
    .owner = THIS_MODULE,
    .open = pin4_open,
    .write = pin4_write,
    .read = pin4_read,
};





int __init pin4_drv_init(void)		//实际驱动入口
{
    
    

    int ret;
    devno = MKDEV(major, minor);													//创建设备号
    ret = register_chrdev(major, module_name, &pin4_fops);  						//注册驱动,告诉内核,把这个驱动加入到内核驱动的链表中

    pin4_class = class_create(THIS_MODULE, "myfirstdemo");          				//让代码在/dev自动生成设备
    pin4_class_dev = device_create(pin4_class, NULL, devno, NULL, module_name);  	//创建设备文件
    return 0;
}

void __exit pin4_drv_exit(void)
{
    
    

    device_destroy(pin4_class, devno);
    class_destroy(pin4_class);
    unregister_chrdev(major, module_name);  //卸载驱动
    //卸载驱动与创建的顺序相反

}

module_init(pin4_drv_init);  //入口,内核加载该驱动的时候,这个宏会被调用
module_exit(pin4_drv_exit);
MODULE_LICENSE("GPL v2");   //声明GPL规范

file name:pin4driver.c

2. Compile

! ! ! pin4driver.cCopy to the kernel source code before compiling linux-rpi-4.14.y/drivers/charand modifyMakefile

(1) Enter the linux-rpi-4.14.y/drivers/charpath in the kernel source code and modify Makefile:

cd /xxx/xxx/linux-rpi-4.14.y/drivers/char
vi Makefile

(2) Add the following handles:

obj-m                           += pin4driver.o

Insert picture description here
(3) Compile:
! ! /linux-rpi-4.14.yCompile under path

ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make modules

linux-rpi-4.14.y/drivers/charThe path generated the .kofile:
Insert picture description here

3. Driver under Raspberry Pi:

(1) PutLow-level driver fileswithCross-compiled upper-level driver fileorC file (compiled on Raspberry Pi) Copy to Raspberry Pi (the steps are not explained here)

(2) Load (pin4) kernel driver:

sudo insmod pin4driver.ko

Expansion: delete (pin4) kernel driver:sudo rmmod pin4driver


You can check whether the pin4 device driver is generated under dev, and check the device number:

ls /dev/pin4 -l

Insert picture description here

Or view the driver:

lsmod

Insert picture description here
(3) Let all users canRead and write/dev/pin4, (Otherwise the upper driver file cannotRead and write/dev/pin4):

sudo chmod 666 /dev/pin4

(4) ImplementationCompile / cross compileOkUpper driveFile
(5) View kernel startup information:

dmesg

Guess you like

Origin blog.csdn.net/lcx1837/article/details/113665615