This chapter will complete a real Linux drive. The Linux driver is used to control 4 LEDs on the Development Board. That is, by sending data to the Linux driver, you can control the switch of the LED light.
First, the realization and principle of LED drive
Although the Linux driver deals directly with hardware, it is not the Linux driver that writes data directly to the memory in the hardware, but interacts with the native I/O memory (I/O memories, which are located in the kernel space). The so-called I/O memory is a mapping of the host memory through a variety of interfaces (PCI, USB, Bluetooth, Ethernet, etc.) that connect to the host (PC, phone) hardware (network card, sound card, camera, etc.).
Second, write led driver
1. Create LED-driven device files
First step: Class with the Cdev_init function.
Step Two: Specify the device number
Step three: Use the Cdev_add function to add a character device to the character device array in the kernel
Fourth step: Create a struct class using the Class_create macro
Fifth step: Create a device file using the Device_create function
2. Uninstalling the LED-driven device files
The functions used in Device_destory, Class_destory, and Unregister_chrdev_region methods
3. Setting the register and initializing the LED driver
4. Control LED
Controlling LEDs with strings
Controlling LEDs with I/O commands
Module parameters for 5.LED drives
Test LED Driver This chapter describes some of the more complex test methods that can not only send strings to led device files, but also send I/O control commands. These test methods include executable program testing, NDK testing, and Java testing.
1. General procedures for writing test I/O control commands
2. You can use the NDK to test the LED driver
3. Test LED driver using Java
Four, LED-driven transplant
Linux drivers must be compiled with the Liunx kernel currently in use to install on the current Linux or Android system. Therefore, the simplest porting of LED drivers is to compile them under different Linux kernel versions.
The LED driver is the first Linux driver to actually deal with hardware in this book. Although the LED driver is not complicated, it only controls 4 LEDs, but the LED driver already includes all the necessary parts of the Linux drive. A complete Linux drive consists mainly of internal processing and a reduced component with hardware interaction. The internal processing mainly refers to the Linux driver loading, unloading, and device file-related action processing (read and write device files, send I/O commands to the device file), as well as business logic. Interaction with hardware mainly refers to the data interaction with the registers in the hardware through the functions of iowrite32,. IOREAD32, etc. The porting of Linux drivers on different platforms is also handled in these two parts.
The LED will blink for me: control Light Emitting diodes