1. 程式人生 > >Android驅動開發---Linux Kernel/HAL Layer/Jni Layer例項全集

Android驅動開發---Linux Kernel/HAL Layer/Jni Layer例項全集

本人在開發Android Nfc POS之初,探索除錯了一番驅動,目前在Nexus 5X 7.1.1上已經調成,之前的步驟可以參考我發的文章,Android驅動開發經驗分享如下。

1. Linux 核心驅動例項

以下均在Android Linux核心目錄下操作, 在drivers目錄下建立驅動目錄hello
這個目下要建立3個檔案,hello.c, Makefile and Kconfig
1.1 hello.c
cd drivers
mkdir hello

vim hello.c
code as follows:

/linux kernel driver: hello.c => /dev/hello

/
please find the code in the below. here skip the code to make the page clean and clear.

1.2 Makefile
Create the Makefile and add:
obj-y += hello.o
1.3 Kconfig
Create Kconfig and add:

config HELLO 
    tristate "Eric: First Android Driver"
default n
help
  This is the first Android driver.

this file is used when we make menuconfig.
1.4 Modify drivers/Makefile


Add following in the end
obj-y += hello.o
1.5 Add the driver into system configuration
Before we build the kernal, we need to config the system.
1.5.1 Modify arch/arm64/Kconfig
Add following in the end
source "drivers/hello/Kconfig"
It seems that this config not work, may be skipped.
1.5.2 Modify drivers/Kconfig

Following the menu:
menu "Device Drivers"
Please add
source "drivers/hello/Kconfig"
1.5.3 Modify drivers/Kconfig

make menuconfig
To enble the menu 'Eric: First Android Driver' in the "Device Drivers" item.
And save , then to build the linux kernel code.

2. 測試驅動

以下是需要操作的目錄在AOSP目錄下

2.1 create the application on externel
在external下建立hello目錄
目錄下將有兩個檔案:hello.c and Android.mk
cd external
mkdir hello
vim hello.c
/ AOSP app : ./external/hello.c =>/system/bin/hello/
Android.mk as follows:

 LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := hello
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)

2.2 build the application
To build the hello application
mmm external/hello/
Add it into the system.img
make snod
2.3 Test/Debug the driver hello

Before flash, we can build the AOSP again, of cource, don't forget replace the linux kernel file 'Image.gz-dtb'.
After flash into the phone, reboot the phone, and then do follows:

C:\Users\ylgi>adb devices
List of devices attached
01059f9781509a67        device

C:\Users\ylgi>adb -s 01059f9781509a67 shell
bullhead:/ $ ls

Android驅動開發---Linux Kernel/HAL Layer/Jni Layer例項全集

cd /system/bin
./hello

Android驅動開發---Linux Kernel/HAL Layer/Jni Layer例項全集

執行效果如上圖,表示Linux驅動已經成功載入。