The seventh chapter of Android Deep Exploration

Source: Internet
Author: User

After reading the seventh chapter of this book, I Learned:

1. Linux Driver Implementation principle:

The Linux driver writes data to the hardware through I/O memory, which is a mapping of the hardware in host memory that is connected to the host through various interfaces. The Linux kernel provides several functions that interact with I/O Memory: Ioread16, IOREAD32, Iowrite16, Iowrite32, and so on. Each hardware connected to Linux has a map header address in I/O memory, which you need to specify when reading and writing functions.

2. Writing LED drivers:

① creating LED-driven device files:

1> initialize Cdev:cdev_init ()

2> Specify the device number:

Law one: alloc_chrdev_region () automatically assigns an unused master device number, and the secondary device number defaults to 0. The parameter dev represents the device number pointer, the Baseminor parameter assigns the secondary device number, and count indicates the assigned secondary device number range, and name indicates the device file name.

Law II: register_chrdv_region () Specify the device number directly. The from parameter is the device number, and count indicates the assigned secondary device number range, and name indicates the device file name.

3> Adds a character device to the character device array in the kernel: Cdev_add (). The function called _map () is responsible for adding information about the device files to the probes array that holds the established device files.

4> Use the Class_create macro to create a struct class that contains some file-related variables and some callback function pointer variables.

5> Create a Device file: Leds_device_create (), Led_init () is the LED-driven initialization function in which the direct call to Leds_device_create ()

Note: The LED secondary device number is always 10.

② uninstalling LED-driven device files: Call Device_destroy (), Class_destroy (), unregister_chrdev_region () function, Leds_destroy_device () Used to uninstall led device files in which the Leds_exit () is called to complete the removal of LED driver files.

③ set the register and initialize the LED driver:

1>. LED has two pins: GPB0, GPB1. One GPI0 port connected to the ARM processor and the other connected to the power VCC3. GPI0 low-power LED light, high-power off at ordinary times. The control LEDs need to be set up with 3 registers: Gpmcon (port configuration Register), Gpmdat (port data Register), GPMPUD (port pull-up register). The virtual addresses of the 3 registers are: 0xf04500820 0xf04500824 0xf04500828

2>. Initialize Register: the LEDS_INIT_GPM () function writes the address of 3 registers, calling Ioread32 (), iowrite32 () to read and write data in the virtual address. Call LEDS_INIT_GPM () in Leds_init () to complete the initialization of the Register.

④ Control LEDs:

Law one: Control LEDs via strings: Using File_operations.write ()

Method Two: Control LEDs via I/o command: Using File_operations.ioctl ()

Parameters of the ⑤led Drive module:

Use the Module_param_array (NAME,TYPE,NUMP,PERM) macro to specify the module parameters in the array form for the Linux driver, where nump represents a pointer to a variable that stores the length of the array, and perm represents the access rights for the parameter file.

3. Test LED Driver:

① a generic program for writing test I/O control commands:

#~/drivers/ioctl/build.sh

#编译ioctl

② using the NDK to test led drivers:

③ using Java test LED driver

4. LED-driven porting:

The simplest porting of Linux drivers is to compile them under different Linux kernel versions, modifying the Linux driver source to try not to modify the Linux driver interface.

The seventh chapter of Android Deep Exploration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.