【ARM】Linux driver porting

Publisher:温柔微笑Latest update time:2022-05-13 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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 [ ].

Write the picture description here

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                                         │ │  

  QQ2440/mini2440 LEDs Driver                                │ │  

  QQ2440/mini2440 PWM beeper Driver                          │ │  

  QQ2440/mini2440 Hello Module sample                        │ │  

  QQ2440 Buttons                         

Write the picture description here

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 , you need to load the driver manually to use it.


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

Write the picture description here

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

Write the picture description here

Step 2: Copy the kernel driver directly

Write the picture description here

Step 3: Compile

Write the picture description here

Step 4: Installation

Write the picture description here

Step 5: Insert the module and observe the phenomenon

Write the picture description here

#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]

Write the picture description here

##2.1 What is registration

Write the picture description here

##2.2What data does register_chrdev submit?

Write the picture description here

###2.2.1 Why is the device number 231?

How to know setting 231

Write the picture description here

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

Write the picture description here

###2.2.3 How to set up the file operation structure


Write the picture description here

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:

Write the picture description here

####Open,close,read这些都好理解


##2.3What does register_chrdev return?

Write the picture description here

###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?

Write the picture description here

###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?

Write the picture description here

###2.3.5 How to assign secondary device numbers?

Write the picture description here

###2.3.6 What is returned if an error occurs?

Write the picture description here

What is ENOMEM?

Write the picture description here

Can you guess now what cdev_add returns?

Write the picture description here

##2.4Where are character devices registered?


##2.5What does devfs_mk_cdev achieve?

Device FileSystem Make CharDevice

Write the picture description here

The main function is to create a device file under the file system. The name of the device file is DEVICE_NAME.

Write the picture description here

###2.5.1 How to use devfs_mk_cdev parameters?

Write the picture description here

###2.5.2 MKDEV Function

Make Device

Write the picture description here

###2.5.3 Access Mode

Chmod ,0777,umask

Write the picture description here

[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.

Write the picture description here

##2.6How to implement the file_operations structure

Write the picture description here

###2.6.1 What functions does qq2440_leds_ioctl implement?

Write the picture description here

###2.6.2 Where is s3c2410_gpio_setpin

Write the picture description here

##2.7 Pin Control

Here, s3c2410_gpio_cfgpin and s3c2410_gpio_setpin are connected to the principles of the previous article "ARM Interface Technology".

Write the picture description here

##2.8 Code call relationship

Write the picture description here

##2.9module_init function

Refer to "linux driver entry function module.docx"

#appendix

Tool download link:

https://github.com/1040003585/Mini2440/tree/master/Tools

Keywords:ARM Reference address:【ARM】Linux driver porting

Previous article:【ARM】ARM interface technology
Next article:【ARM】Making a Linux file system

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号