当前位置:网站首页>On nanopi M1 (Quanzhi H3) kernel driver programming HelloWorld (compilation mode I)

On nanopi M1 (Quanzhi H3) kernel driver programming HelloWorld (compilation mode I)

2022-04-23 22:03:00 July meteor

Method 1 :

          development environment :VM+Ubuntu

          Compile environment :linux3.4( Quanzhi official BSP)

          Cross compiler tool :arm-linux-gcc 4.4.3

1. To write helloworld Device source code

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/cdev.h>
#include<linux/miscdevice.h>
#include<linux/init.h>
#include<linux/device.h>
#include<linux/string.h>
#include<linux/types.h>
#include<linux/moduleparam.h>
#include<linux/errno.h>

#define DEVICE_NAME "helloworld"
#define DEVICE_MAJOR 237

int device_open(struct inode *inode,struct file *file)
{
        printk("<0>the helloword device open.. \n");
        return 0;
}
static ssize_t device_read(struct file *file,char __user *buf,size_t count,loff_t *f_pos)
{
        printk("<0>the helloword device reading.. \n");
        return 1;
}

static ssize_t device_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos)
{
        printk("<0>the helloword device writing.. \n");
        return 1;
}
int device_release(struct inode *inode,struct file *file)
{
        printk("<0>the helloword device release \n");
        return 0;
}

struct file_operations helloworld_ops=
{
        .owner = THIS_MODULE,
        .open  = device_open,
        .read  = device_read,
        .write = device_write,
        .release = device_release,
};

//init
static int __init Helloworld_init(void)
{
        int ret;

        printk("<0>module init \n");

        ret = register_chrdev(DEVICE_MAJOR,DEVICE_NAME,&helloworld_ops);
        if(ret<0)
        {
                printk("<0>%s can't initialized! \n",DEVICE_NAME);
                return ret;
        }

        printk("<0>%s initialized! \n",DEVICE_NAME);
        return 0;
}

//exit
static void __exit Helloworld_exit(void)
{
        printk("<0>module exit.. \n");
        unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
        printk("<0>exited \n");
}

module_init(Helloworld_init);
module_exit(Helloworld_exit);


MODULE_AUTHOR("xxx");
MODULE_DESCRIPTION("Nanopi NEO Helloworld Driver");
MODULE_LICENSE("GPL");

take hellowrold.c Copied to the lichee/linux-3.4/drivers/char/ Under the folder , And in Makefile Add... At the end :

obj-m                      += helloworld.o

obj-m Compiled into modules , And the corresponding obj-y Means compiled into the kernel ,$( Variable ) The value of can be y or m.

then , Return to /lichee Catalog , Recompile the kernel

./build.sh -p sun8iw7p1 -b nanopi-h3 -m kernel

stay lichee/linux-3.4/drivers/char/ The directory will generate .ko file

take .ko File copy to Nanopi m1 Any directory

2. stay nanopi m1 Write applications on the client side app.c

Simulate the use of the device :

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
        char buf[2] = {0,1};    //Âú×ã¸ñʽ
        size_t n = 1;
        int fd;
        fd = open("/dev/helloworld",O_RDWR);    //É豸Ãû£¬´ò¿ª·½Ê½£¬È¨ÏÞ
        if(fd < 0)
        {
                printf("Can't open the helloworld device.\n");
                return -1;
        }
        else
                printf("Open the helloworld device successfully.\n");

        write(fd,buf,n);
        read(fd,buf,n);
        close(fd);

        printf("app over\n");
}

3. Using kernel modules :

Mount the kernel module :

insmod helloworld.ko

View the list of kernel modules :

lsmod

Check whether there is a device node :

ls /dev/hello*

Create device nodes :

mknod /dev/helloworld c 237 0

c Represents that the device is a character device ,237 Is the master device number of the device ,0 This is the equipment number . After success ,/dev Generate under directory helloworld Device file .

Compiling the application :

gcc -o app app.c

Execute the application :

./app

The display module is opened successfully

Uninstall the kernel module :

rmmod helloworld

Delete device files :

rm /dev/helloworld

Complete the loading device of kernel module , Build device node , Operating equipment , Unload the device , The function of deleting device files .

版权声明
本文为[July meteor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200609125644.html