一、平台介紹
硬件平台: tiny4412ADK + S700 4GB Flash
Android版本:Android-5.0.2
Linux版本: Linux-3.0.86
Bootloader:Superboot
作者:彭東林
郵箱:pengdonglin137@163.com
二、代碼目錄
下面的例子是基於Andrioid源碼中自帶的一個串口應用SerialChat,Android已經把與之相關的Freamwork、JNI代碼實現了,其中直接在JNI中調用了Linux的系統調用,沒有遵循Android的HAL架構,根本就沒有提供HAL。
下面是相關的代碼路徑:
APP:
frameworks/base/tests/SerialChat/
SerialManager:
frameworks/base/core/java/android/hardware/SerialManager.java
SerialService:
frameworks/base/services/core/java/com/android/server/SerialService.java
SerialPort:
frameworks/base/core/java/android/hardware/SerialPort.java
JNI:
frameworks/base/services/core/jni/com_android_server_SerialService.cpp
frameworks/base/core/jni/android_hardware_SerialPort.cpp
AIDL:
frameworks/base/core/java/android/hardware/ISerialManager.aidl
三、編譯運行
-
編譯
root@ubuntu:~/tiny4412_android5/android-5.0.2# . setenv including device/lge/mako/vendorsetup.sh including device/lge/hammerhead/vendorsetup.sh including device/generic/mini-emulator-x86_64/vendorsetup.sh including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh including device/generic/mini-emulator-mips/vendorsetup.sh including device/generic/mini-emulator-x86/vendorsetup.sh including device/generic/mini-emulator-arm64/vendorsetup.sh including device/friendly-arm/tiny4412/vendorsetup.sh including device/moto/shamu/vendorsetup.sh including device/samsung/manta/vendorsetup.sh including device/asus/deb/vendorsetup.sh including device/asus/fugu/vendorsetup.sh including device/asus/tilapia/vendorsetup.sh including device/asus/grouper/vendorsetup.sh including device/asus/flo/vendorsetup.sh including sdk/bash_completion/adb.bash root@ubuntu:~/tiny4412_android5/android-5.0.2# mmm frameworks/base/tests/SerialChat/ ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=5.0.2 TARGET_PRODUCT=full_tiny4412 TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a-neon TARGET_CPU_VARIANT=cortex-a9 TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_OS=linux HOST_OS_EXTRA=Linux-3.13.0-32-generic-x86_64-with-Ubuntu-14.04-trusty HOST_BUILD_TYPE=release BUILD_ID=LRX22G OUT_DIR=out ============================================ make: Entering directory `/root/tiny4412_android5/android-5.0.2' target Java: SerialChat (out/target/common/obj/APPS/SerialChat_intermediates/classes) Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes-jarjar.jar Copying: out/target/common/obj/APPS/SerialChat_intermediates/emma_out/lib/classes-jarjar.jar Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.jar Proguard: out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar ProGuard, version 4.10 Reading program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar] Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-junit_intermediates/classes.jar] Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar] Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar] Reading library jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar] Preparing output jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/proguard.classes.jar] Copying resources from program jar [/root/tiny4412_android5/android-5.0.2/out/target/common/obj/APPS/SerialChat_intermediates/classes.jar] target Dex: SerialChat Copying: out/target/common/obj/APPS/SerialChat_intermediates/classes.dex target Package: SerialChat (out/target/product/tiny4412/obj/APPS/SerialChat_intermediates/package.apk) Install: out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk make: Leaving directory `/root/tiny4412_android5/android-5.0.2'
#### make completed successfully (13 seconds) ####
-
安裝和修改
adb install –r out/target/product/tiny4412/data/app/SerialChat/SerialChat.apk
點擊運行,會出錯退出。
現在作如下修改:
1、frameworks/base/tests/SerialChat/Android.mk
# # Copyright (C) 2011 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := tests LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := SerialChat LOCAL_CERTIFICATE := platform include $(BUILD_PACKAGE)
2、frameworks/base/tests/SerialChat/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2011 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.serialchat" android:sharedUserId="android.uid.system"> <uses-permission android:name="android.permission.SERIAL_PORT"/> <application android:label="Serial Chat"> <activity android:name="SerialChat" android:label="Serial Chat"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
3、frameworks/base/core/res/res/values/config.xml
為什么要修改這個文件呢?在這個文件中配置那些設備節點作為串口設備,在tiny4412平台一共有四個串口,COM0和COM3在底板上用DB9引了出來,對應的設備節點是/dev/ttySAC0、/dev/ttySAC1、/dev/ttySAC2、/dev/ttySAC3,所以修改如下:
<!-- List of paths to serial ports that are available to the serial manager. for example, /dev/ttyUSB0 --> <string-array translatable="false" name="config_serialPorts"> <item>"/dev/ttySAC0"</item> <item>"/dev/ttySAC1"</item> <item>"/dev/ttySAC2"</item> <item>"/dev/ttySAC3"</item> </string-array>
4、frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java
在frameworks/base/tests/SerialChat/src/com/android/serialchat/SerialChat.java中:
@Override public void onResume() { super.onResume(); String[] ports = mSerialManager.getSerialPorts(); if (ports != null && ports.length > 0) { try { mSerialPort = mSerialManager.openSerialPort(ports[0], 115200); if (mSerialPort != null) { new Thread(this).start(); } } catch (IOException e) { } } }
從上面的代碼可以發現,調用getSerialPorts返回的就是在config.xml中配置的串口設備,這里ports[0]就是”/dev/ttySAC0”,在這里我們使用COM3,對應的是ports[3],也就是/dev/ttySAC3,所以這里作如下修改:
mSerialPort = mSerialManager.openSerialPort(ports[3], 115200);
重新編譯:
mmm frameworks/base/core/res/ # 編譯出來的是framework-res.apk,替換/system/framework下的framework-res.apk
mmm frameworks/base/tests/SerialChat/ # 先卸載,在安裝 (卸載 adb uninstall com.android.serialchat)
四、測試
下面是測試結果:
- 這個是串口調試助手,運行在PC機上。
下載地址: http://pan.baidu.com/s/1nWHP0
完。