The sixth chapter of Android Deep Exploration

Source: Internet
Author: User
Tags dmesg

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

    1. Each driver in the Linux system is mapped to a file (device file/driver file), which is stored in the/dev directory. The function of the rollback function is to enable the Linux driver to respond to the application, allowing the device files and applications to interact with the interface.

    1. Write Linux drivers (General framework):

① Load Drive: module_init () Unload drive: Module_exit ()

② Registration Device File: Misc_register () logoff device file: Misc_deregister ()

③ specifies the driver-related information: Module_author, Module_license (required), Module_alias, Module_description, and other macros.

④ Specifies the return function: Trigger each function by event: Open/read/write/icntl/close, etc.

⑤ Writing Makefile files: modifying targets and paths and cross-compiling tools

⑥ compiling Linux drivers: Make

⑦ Installing Linux drivers: Insmod/modprobe

Uninstall Linux driver: Rmmod

    1. Write a Linux driver that counts the number of words (verbose):

① set up the directory to store the driver and write the driver source file word_count.c, write makefile file:

    • Obj-m: =WORD_COUNT.O

Word_count=y "=PROCESS.O DATA.O

② initialization and Exit drive: Module_init (Word_count_init), Module_exit (Word_count_exit)

③ specifying driver-related information: Viewing the driver's own information #modinfo Word_count

Module_author, Module_license (required), Module_alias, Module_description, and other macro information are placed at the end of the source code.

④ Specify the back function: Word_count_read (), Word_count_write ()

Because kernel space programs cannot directly access user spatial data, the kernel interacts with the user spatial data using Copy_to_user () and Copy_from_user () in two functions.

⑤ compiled driver Source: #make –C (Linux kernel header file directory) m= (the directory where the driver is located)

Build Linux driver module files after compilation Word_count.ko

⑥ Installing Linux drivers: #insmod Word_count.ko

To see if the installation was successful: # Lsmod | grep word_count

Uninstall Linux driver: #rmmod word_count

View log information for Linux driver output: #dmesg | grep Word_count | Tail–n 2 or #cat/var/log/syslog | grep Word_count | Tail–n 2

    1. Test Linux drivers:

① testing in a virtual machine:

Write a test program (TEST.C),

#gcc test.c–o Test

#./test

View test results in terminal

Test in ②android Simulator:

Enter the Android Simulator goldfish to configure the Linux kernel, then #make menuconfig Enter the settings screen to select the second item, then enter the submenu to select the top three items, save exit. Finally recompile the Linux kernel.

Execute the build.sh script to finish compiling, uploading, installing the driver, and then entering the Android emulator terminal using "echo", "DMESG" test drive and view the results.

③ using the Android NDK test:

Start the simulator,

#emulator –AVD Myavd–kernel/root/kernel/goldfish/arch/arm/boot/zimage

④ uses Java code to directly manipulate device file tests.

⑤ using the S3C6410 Board test:

Recompile the test.c file and pass the compiled test source to the Development Board, which is similar to the Android emulator.

recompiling the Android NDK program in eclipse runs on the s2c6410 Development Board.

Use Java code testing in the s3c6410 Development Board.

⑥ to compile the driver into the Linux kernel test

Place the source code (WORD_COUNT.C) in the <linux kernel directory >/drivers/char directory

Modify the Kconfig> file to add before Endmenu:

Config Word_count

BOOL "Word_count Driver"

Help

This is a word count driver. It can get a word count from/dev/wordcount

Modify the Makefile file, insert: Obj-$ (config_word_count) + = WORD_COUNT.O

Set the. config file with the menu item: #make menuconfig Select "Device Drivers"-"Character Devices"-"word_count_driver"

Compile the Linux kernel: Enter/root/kernel/goldfish, execute #make (prior to granting access to the device in advance)

    1. Develop and test Linux drivers using eclipse:

①eclipse Development: Build C Project--build C source File link--set include path--compile Linux driver

② Test Linux driver: Import test.c file--set include path--build Target--build Project--Run test program

The sixth 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.