Raspberry Pi Advanced Course 5: Driven cognitive framework, drive code writing, compilation and testing, drive summary

Driven cognitive framework, code writing, compilation and testing, drive summary

1. Driven cognitive framework

Insert picture description here
2. Write driver code based on the framework

io port driver code: pin4Mode:

#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";   //模块名

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

//led_write函数
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,
};

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");
    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");

3. Compilation of driver code:

1. Take the driver code to the character device driver file in the source tree directory:
Insert picture description here

2. Modify the Makefile:
Insert picture description here

Insert picture description here

3. Go back to the source tree directory and compile the module:

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

A .ko file will be generated:
Insert picture description here
4. Take the generated .ko file to the Raspberry Pi:

scp   pin4test.ko  pi@192.168.1.100:/home/pi
指令   文件名      树莓派账号  IP     文件存放的路径

note: Lets you enter the password of the Raspberry Pi instead of the Ubuntu password
Insert picture description here

5. Compile the test code and get the Raspberry Pi

Test code:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
    
    
	int fd;
	fd=open("/dev/pin4",O_RDWR);
	write(fd,"1",1);
	return 0;
}

Compile the test code:

arm-linux-gnueabihf-gcc pin4test.c -o pin4test

Insert picture description here

Take the generated executable program to the Raspberry Pi:

scp pin4test pi@192.168.1.100:/home/pi

Display under Raspberry Pi;
Insert picture description here

6. Load the driver we just compiled:

sudo insmod pin4Mode.ko

Generate major and minor device numbers under /dev:
Insert picture description here
drive file generation.

7. Run our test code
note: To run with root privileges

sudo   ./pin4test  

At this time, we can't see any information at the upper level, the information is printed in the kernel mode
View print information in kernel mode

dmesg  

Insert picture description here

So far, the driver code compilation test is successful.

8. Drive stage summary:

Kernel driver basic framework

Drive code writing:

Refer to pin4test.c

Kernel driver compilation:

Copy the driver code to driver/char and
modify the Makefile to tell the compiler to compile the driver file
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make modules

Drive test steps

Kernel driver loading:
sodu insmod xxx.ko
Kernel driver unloading:
sodu rmmod xxx No need to write ko to
view the kernel module:
lsmod

Verification steps

Load the driver After the
driver is loaded, the device will be generated, such as: /dev/pin4,
add access rights through sudo chmod 666 /dev/
pin4 Run the test program pin4test to call the driver
The printk of the kernel is the printf of the kernel layer, and the print information can be viewed through dmesg.

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108774834