#1 Driver module transplantation process
##1.1 The first module compilation method - change kconfig
Step 1: Change kconfig
The file is in: Drivers/char/kconfig;
The tristate representation can be compiled in module bool means that it can only be selected to be edited [*] or not edited [ ]. Step 2: Configure the kernel After make menuconfig, a .config file for compilation will be automatically generated. wuchengbing@ubuntu:~/linux/kernel-2.6.13$ make menuconfig Device Drivers ---> Character devices ---> [*] S3C2410 RTC Driver │ │ If make menuconfig does not display this option, you can first cp config_n35 .config->make-> make menuconfig, and then generate the following .config file. wuchengbing@ubuntu:~/linux/kernel-2.6.13$ gedit .config # CONFIG_WATCHDOG is not set # CONFIG_NVRAM is not set # CONFIG_RTC is not set CONFIG_S3C2410_RTC=y CONFIG_QQ2440_LEDS=m CONFIG_QQ2440_PWM_BEEPER=y CONFIG_QQ2440_HELLO_MODULE=m … … If you select [*], that is, yes, the driver will be automatically loaded when the system is running; if you select Step 3: Compile the kernel to get the driver module make, and then generate the corresponding driver module .ko file. wuchengbing@ubuntu:~/linux/kernel-2.6.13$make … … LD vmlinux SYSMAP System.map SYSMAP .tmp_System.map OBJCOPY arch/arm/boot/Image Kernel: arch/arm/boot/Image is ready AS arch/arm/boot/compressed/head.o GZIP arch/arm/boot/compressed/piggy.gz AS arch/arm/boot/compressed/piggy.o CC arch/arm/boot/compressed/misc.o LD arch/arm/boot/compressed/vmlinux OBJCOPY arch/arm/boot/zImage Kernel: arch/arm/boot/zImage is ready Building modules, stage 2. MODPOST CC drivers/char/mini2440_backlight.mod.o LD [M] drivers/char/mini2440_backlight.ko CC drivers/char/qq2440_buttons.mod.o LD [M] drivers/char/qq2440_buttons.ko CC drivers/char/qq2440_hello_module.mod.o LD [M] drivers/char/qq2440_hello_module.ko CC drivers/char/qq2440_leds.mod.o LD [M] drivers/char/qq2440_leds.ko CC drivers/char/qq2440_pwm.mod.o LD [M] drivers/char/qq2440_pwm.ko wuchengbing@ubuntu:~/linux/kernel-2.6.13$ qq2440_leds.ko wuchengbing@ubuntu:~/linux/kernel-2.6.13$ ls driver/char/ … -rw-rw-r-- 1 wuchengbing wuchengbing 1368 Apr 17 20:38 qq2440_hello_module.o -rw-rw-r-- 1 wuchengbing wuchengbing 3108 Apr 17 20:38 qq2440_pwm.o -rw-rw-r-- 1 wuchengbing wuchengbing 1412 Apr 17 20:39 qq2440_hello_module.mod.o -rw-rw-r-- 1 wuchengbing wuchengbing 2247 Apr 17 20:39 qq2440_hello_module.ko -rw-rw-r-- 1 wuchengbing wuchengbing 1404 Apr 17 20:39 qq2440_leds.mod.o -rw-rw-r-- 1 wuchengbing wuchengbing 3133 Apr 17 20:39 qq2440_leds.ko -rw-rw-r-- 1 wuchengbing wuchengbing 1404 Apr 17 20:39 qq2440_pwm.mod.o -rw-rw-r-- 1 wuchengbing wuchengbing 3957 Apr 17 20:39 qq2440_pwm.ko ./mkimage.sh wuchengbing@ubuntu:~/linux/kernel-2.6.13$ cd arch/arm/boot/ wuchengbing@ubuntu:~/linux/kernel-2.6.13/arch/arm/boot$ ./mkimage.sh Image Name: linux-2.6.13 Created: Mon Apr 17 20:33:08 2017 Image Type: ARM Linux Kernel Image (uncompressed) Data Size: 1537008 Bytes = 1500.98 kB = 1.47 MB Load Address: 0x30008000 Entry Point: 0x30008040 wuchengbing@ubuntu:~/linux/kernel-2.6.13/arch/arm/boot$ Step 4: Copy to the file system, mount the board, and insert the module Run the compiled ./a.out of the example program, and the LED on the board will light up. License issues require adding MODU_LICENSE("GPL") to the driver code; ... ... module_init(qq2440_leds_init); module_exit(qq2440_leds_exit); MODULE_LICENSE("GPL"); Sample Program Application, calling the board LED through the driver. #include #include #include #include int main(void) { int fd; int i = 0; fd = open("/dev/leds",O_RDONLY); if(fd==-1) { perror("open failed"); exit(1); } while(1) { ioctl(fd,0,0); ioctl(fd,0,1); ioctl(fd,0,2); ioctl(fd,0,3); sleep(1); ioctl(fd,1,0); ioctl(fd,1,1); ioctl(fd,1,2); ioctl(fd,1,3); sleep(1); } return 0; } ##1.2 The second module compilation method - Makefile Step 1: Write Makefile Step 2: Copy the kernel driver directly Step 3: Compile Step 4: Installation Step 5: Insert the module and observe the phenomenon #2Linux driver principle ##2.0 Where to start The LED program that comes with the development board (qq2440_leds_init) [called by module_init----just know this] ##2.1 What is registration ##2.2What data does register_chrdev submit? ###2.2.1 Why is the device number 231? How to know setting 231 http://blog.csdn.net/zjjyliuweijie/article/details/7001383 It is set to 231 because no one uses 231. ###2.2.2 Give any device name ###2.2.3 How to set up the file operation structure Analysis of each item in file_operations http://blog.csdn.net/sunsea1026/article/details/6586143 ####What is THIS_MODULE? THIS_MODULE is copied to owner in the code. As the name suggests, owner means the owner. When THIS_MODULE is copied to owner, it means that the structure belongs to the current module. Then who is the current module? The current module is: ####Open,close,read这些都好理解 ##2.3What does register_chrdev return? ###2.3.1 Simple and efficient goto Whether to use goto has always been a famous controversial topic. The use of goto in the Linux kernel source code is very extensive, but it is generally limited to error handling! This use of goto for error handling is really simple and efficient. Just make sure to remember to log out and release resources when handling errors! (The opposite order of normal registration and resource application) ###2.3.2 Remember what the ternary operator is? ###2.3.3 What does a return of 0 mean? What does it mean when major returns 0? When Major is true, it can only be > 0, because the type of major is unsigned. Therefore, when major is greater than 0, the function returns 0, indicating that the function is executed successfully! ###2.3.4 Under what circumstances will cd->major be returned? There is only one case where Major is false, which is equal to 0. So what does major=0 mean? When calling the register_chrdev function, If major=0 is passed in, it means that you do not define the device number and it will be automatically assigned by the system! If the major passed in is > 0, it means passing in a self-defined device number without the system automatically assigning it! How do we know? ###2.3.5 How to assign secondary device numbers? ###2.3.6 What is returned if an error occurs? What is ENOMEM? Can you guess now what cdev_add returns? ##2.4Where are character devices registered? ##2.5What does devfs_mk_cdev achieve? Device FileSystem Make CharDevice The main function is to create a device file under the file system. The name of the device file is DEVICE_NAME. ###2.5.1 How to use devfs_mk_cdev parameters? ###2.5.2 MKDEV Function Make Device ###2.5.3 Access Mode Chmod ,0777,umask [Reprint] stat function and structure http://blog.sina.com.cn/s/blog_6dd1df4e0100o50q.html ###2.5.4 Phenomenon The following is the device file generated in the file system after the driver is successfully registered. ##2.6How to implement the file_operations structure ###2.6.1 What functions does qq2440_leds_ioctl implement? ###2.6.2 Where is s3c2410_gpio_setpin ##2.7 Pin Control Here, s3c2410_gpio_cfgpin and s3c2410_gpio_setpin are connected to the principles of the previous article "ARM Interface Technology". ##2.8 Code call relationship ##2.9module_init function Refer to "linux driver entry function module.docx" #appendix Tool download link: https://github.com/1040003585/Mini2440/tree/master/Tools
Previous article:【ARM】ARM interface technology
Next article:【ARM】Making a Linux file system
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- A brief history of channel coding
- Connected cars, where is the future?
- What PCB design practice materials are there?
- EEWORLD University ---- Live playback: The most important component of the analog world - Signal chain and power supply: USB Type-C? PD special session
- Problems connecting Bluetooth module with CC2541
- microchip zero-drift operational amplifier
- Matter Development Guide (VI): Network Configuration and Lighting-App Examples
- GigaDevice GD32E231 DIY Competition Award Ceremony
- 1024 Programmers' Day, are you celebrating it?
- Are there any friends who use Hetai HT66FXX series microcontrollers?