[Linux] Driver-1-Environment Preparation

table of Contents
  • Preface
  • 1. Development environment setup
    • 1.1 Environmental preparation
      • 1.1.1 Installation tools
      • 1.1.2 Compile the kernel
        • 1.1.2.1 Get the kernel source code
        • 1.1.2.2 Compile the kernel
    • 1.2 Compile and load the kernel driver module
      • 1.2.1 hello routine analysis
      • 1.2.2 Compile with the kernel source code
      • 1.2.3 Load the kernel driver module
    • 1.3 Compile and load device tree
      • 1.3.1 Device tree compilation
        • 1.3.1.1 Use dtc in the kernel to compile according to
        • 1.3.1.2 Compile in the kernel source code (recommended)
        • 1.3.1.3 Load device tree
    • 1.4 Compile and load device tree plug-in
      • 1.4.1 Compile with dtc tool alone
      • 1.4.2 The kernel dtc tool compiles the device tree plug-in
      • 1.4.3 Load device tree plug-in
        • 1.4.3.1 Load with echo command
        • 1.4.3.2 uboot loading
  • reference:


Preface

Coupon https://www.cps3.cn/
  • Take the wildfire iM 6U as an example

1. Development environment setup

Drive operating conditions :

  • A device driver is a program with independent functions. It can be compiled separately but cannot run independently. It is linked to the kernel and runs in the kernel space as a part of the kernel during runtime.
  • Therefore, if we want the kernel module written by us to run on a certain version of the kernel, then we must compile it on that kernel version. If the kernel we compiled and the kernel we are running have different characteristics, the device driver may not be able to run .

The brief compiling steps of the driver :

  • First of all, we need to know the kernel version, and prepare the kernel source code of this version, and use the cross-compilation tool to compile the kernel source code.
  • Secondly, rely on the compiled kernel source code to compile our driver modules and device tree files.
  • Finally, copy the drive module and device tree to the development board to run.

1.1 Environmental preparation

1.1.1 Installation tools

Before compiling the source code, you need to prepare the cross-compilation environment and install the necessary dependencies and tools, such as:

  • gcc-arm-linux-gnueabihf cross compiler
  • bison parser
  • flex lexical analyzer
  • libssl-dev OpenSSL general library
  • Compression software for lzop LZO compression library

Reference command:

  • sudo apt install make gcc-arm-linux-gnueabihf gcc bison flex libssl-dev dpkg-dev lzop

1.1.2 Compile the kernel

Compiling the kernel has the following two purposes:

  • Make a system image and burn it to the development board
  • Keep a copy in the system of the development environment to assist in driver development
1.1.2.1 Get the kernel source code

If the kernel has been programmed to development board, you can command uname -ato view the kernel version.
*After
knowing the kernel version, you can download the kernel source code on its official website. For example, the version is Linux npi 4.1.9.71-imx-r1 in the chapter and can be downloaded on gitee or github ( officially provided by Wildfire )

  • command:git clone https://gitee.com/Embedfire/ebf-buster-linux.git
1.1.2.2 Compile the kernel

The process of compiling the kernel varies according to the steps and Makefiles provided by different officials. The following is an example of wildfire's iM 6U compiled linux kernel.
Create a new working directory separately, put its kernel source code in this directory, switch to the kernel source code directory, find the make_deb.sh script, and modify the configuration parameters inside, such as the kernel compilation location, etc. After modifying the configuration parameters, you only need to execute the script to compile the kernel. (For other kernels, you can refer to this script, or you can write a compilation script by yourself )

deb_distro=bionic
DISTRO=stable
build_opts="-j 6"
build_opts="${build_opts} O=build_image/build"
build_opts="${build_opts} ARCH=arm"
build_opts="${build_opts} KBUILD_DEBARCH=${DEBARCH}"
build_opts="${build_opts} LOCALVERSION=-imx-r1"

build_opts="${build_opts} KDEB_CHANGELOG_DIST=${deb_distro}"
build_opts="${build_opts} KDEB_PKGVERSION=1${DISTRO}"
build_opts="${build_opts} CROSS_COMPILE=arm-linux-gnueabihf-"
build_opts="${build_opts} KDEB_SOURCENAME=linux-upstream"

make ${build_opts}  npi_v7_defconfig
make ${build_opts}
make ${build_opts}  bindeb-pkg
  • O=build_image/build: Specify the location of the compiled kernel.
  • ARCH=arm: The target is the ARM architecture core.
  • KBUILD_DEBARCH=${DEBARCH}: For deb-pkg targets, it is allowed to override the conventional heuristics for deb-pkg deployment.
  • LOCALVERSION=-imx-r1: Use the kernel configuration option "LOCALVERSION" to append a unique suffix to the regular kernel version.
  • KDEB_CHANGELOG_DIST=${deb_distro}
  • KDEB_PKGVERSION=1${DISTRO}:Version Information.
  • CROSS_COMPILE=arm-linux-gnueabihf-: Specify the cross compiler.
  • KDEB_SOURCENAME=linux-upstream: The KDEB_SOURCENAME make variable only controls the name of the packaged source tarball, and does not affect the .deb package name output by bind -pkg and deb-pkg.
  • make ${build_opts} npi_v7_defconfig: Generate configuration files.
  • make ${build_opts} bindeb-pkg: Compile files for packaging.

1.2 Compile and load the kernel driver module

The hello routine can go to Li Zhuming's gitee clone: ​​demo_code_for_mystudy/linux/driverTest/helloModule

1.2.1 hello routine analysis

This is just a module routine, without the driver part. The
necessary content can be divided into the following points:

  • Entry function
  • Export function
  • protocol

hello_module.c

/** @file hello_module.c
* @brief 简要说明
* @details 详细说明
* @author lzm
* @date 2021-02-21 18:08:07
* @version v1.0
* @copyright Copyright By lizhuming, All Rights Reserved
*
**********************************************************
* @LOG 修改日志:
**********************************************************
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
// 入口函数:安装驱动时调用的函数
static int __init hello_init(void)
{
printk(KERN_EMERG "[ KERN_EMERG ] Hello Module Init
");
printk( "[ default ] Hello Module Init
");
return 0;
}
// 出口函数:卸载驱动时调用的函数
static void __exit hello_exit(void)
{
printk("[ default ] Hello Module Exit
");
}
module_init(hello_init);
module_exit(hello_exit);
//MODULE_LICENSE("GPL2");
MODULE_AUTHOR("embedfire ");
MODULE_DESCRIPTION("hello world module");
MODULE_ALIAS("test_module");

1.2.2 Compile with the kernel source code

1.2.3 Load the kernel driver module

The compiled kernel driver module xx.ko can be copied to the ARM board in a variety of ways, such as NFS network file system and SCP commands.
For the mounting method, please refer to the NFS chapter of Li Zhuming's Blog Park.

1.3 Compile and load device tree

The device tree was introduced in Linux 3.x to describe the board-level details of a hardware platform.
The driver routines in this series of notes are dependent on the device tree unless otherwise specified .
The following briefly demonstrates the compilation and loading of the device tree, and the specific principles are explained in the specific chapters.

1.3.1 Device tree compilation

1.3.1.1 Use dtc in the kernel to compile according to

The compiled kernel will automatically generate the dtc tool. The path is: kernel/scripts/dtc/dtc .
Compile command:内核构建目录/scripts/dtc/dtc -I dts -O dtb -o xxx.dtbo xxx.dts

  • Means to compile dts to dtb
1.3.1.2 Compile in the kernel source code (recommended)

When compiling the kernel will automatically compile the device tree, this time, just put the device tree into a predetermined position of source file to the device tree source file, device file tree compiled and we used the device tree files are stored in the kernel source /arch/arm/boot/dts . However, it takes a long time to compile the kernel, so it is recommended to compile only the device tree. The method is as follows:

  • Both commands are executed under the top-level path of the kernel source code ( actually, using the top-level Makefile ):
    • If you execute make distclean in the kernel source code , you must execute the first command to generate the default configuration file.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- npi_v7_defconfig
make ARCH=arm -j4 CROSS_COMPILE=arm-linux-gnueabihf- dtbs
1.3.1.3 Load device tree

Method of replacing the device tree:

  • The first type: The device tree is compiled into the kernel, so recompile the kernel and recreate the mirror image. (Troublesome, not recommended)
  • The second type: replace the compiled device tree or device tree plug-in with the one on the development board. (recommend)
  • The third type: Put the compiled device tree on the development board, **/boot/dtbs/xxx/, modify the boot startup parameters. (recommend)

Check whether the loading is successful:

  • Go to the /proc/device-tree directory to view the loaded device nodes to see if there are any changes.

1.4 Compile and load device tree plug-in

After Linux4.4, the dynamic device tree was introduced, and the device tree plug-in was dynamically loaded into the system for the kernel to recognize.
The device tree plug-in is generally used to modify and add part of the hardware information. If you only add the hardware information of the RGB lamp, you only need to compile the .dts file of the RGB lamp as .dtbo .
When compiling the device tree plug-in, there is no need to recompile the entire device tree plug-in, only the modified part.

1.4.1 Compile with dtc tool alone

Both the device tree and the device tree plug-in are compiled using the DTC compilation tool.
After the device tree is compiled, the .dtb file is obtained ;
and the device tree plug-in is compiled to obtain the .dtbo file.
Use the one-click compilation tool provided by Wildfire:

  • Address: git clone https://gitee.com/Embedfire/ebf-linux-dtoverlays.git
  • The device tree plug-in source file to be compiled is placed in the ebf-linux-dtoverlays/overlays/ebf directory, and then back to the root directory of the compilation tool ebf-linux-dtoverlays/ execute "make".
  • The generated .dtbo is located in the ~/ebf-linux-dtoverlays/output directory.
  • It should be noted that if you get an error after executing "make", you can try to uninstall device-tree-compiler first (the uninstall command is: "sudo apt-get autoremove device-tree-compiler"), reinstall, and then click ebf-linux-dtoverlays/basic/fixdep file permissions, modify the permissions command: "chmod 777 scripts/basic/fixdep".

1.4.2 The kernel dtc tool compiles the device tree plug-in

Compiling the device tree plug-in is similar to compiling the device tree. Here, the dtc tool in the kernel is used to compile and compile the device tree plug-in.
Compile command:内核构建目录/scripts/dtc/dtc -I dts -O dtb -o xxx.dtbo xxx.dts

  • Means to compile dts to dtbo

1.4.3 Load device tree plug-in

First copy the device tree plug-in file to the development board.

1.4.3.1 Load with echo command

First create a new directory under /sys/kernel/config/device-tree/overlays/ with a custom name.
Then dtbo firmware echo into the path properties file or dtbo content cat to dtbo properties file.

echo xxx.dtbo >/sys/kernel/config/device-tree/overlays/xxx/path
# 或
cat xxx.dtbo >/sys/kernel/config/device-tree/overlays/xxx/dtbo

Delete device rmdir /sys/kernel/config/device-tree/overlays/xxxplug-ins: .

1.4.3.2 uboot loading

Different boards may not support it. Just
modify the environment variable file, enter the /boot directory and modify vim uEnv.txt

reference:

  • Li Zhuming Blog Park
  • Wildfire

Guess you like

Origin blog.csdn.net/weixin_48967543/article/details/114928988