硬件平台: tiny4412ADK + S700 4GB Flash
Android版本:Android-5.0.2
Linux版本: Linux-3.0.86
Bootloader:Superboot
作者:彭东林
下面的例子是基于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
编译:
[email protected]:~/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
[email protected]:~/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
点击运行,会出错退出。
现在作如下修改:
- frameworks/base/tests/SerialChat/Android.mk
1: #
2: # Copyright (C) 2011 The Android Open Source Project
3: #
4: # Licensed under the Apache License, Version 2.0 (the "License");
5: # you may not use this file except in compliance with the License.
6: # You may obtain a copy of the License at
7: #
8: # http://www.apache.org/licenses/LICENSE-2.0
9: #
10: # Unless required by applicable law or agreed to in writing, software
11: # distributed under the License is distributed on an "AS IS" BASIS,
12: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: # See the License for the specific language governing permissions and
14: # limitations under the License.
15: #
16:
17: LOCAL_PATH:= $(call my-dir)
18: include $(CLEAR_VARS)
19:
20: LOCAL_MODULE_TAGS := tests
21:
22: LOCAL_SRC_FILES := $(call all-subdir-java-files)
23:
24: LOCAL_PACKAGE_NAME := SerialChat
25:
26: LOCAL_CERTIFICATE := platform
27:
28: include $(BUILD_PACKAGE)
- frameworks/base/tests/SerialChat/AndroidManifest.xml
1: <?xml version="1.0" encoding="utf-8"?>
2: <!-- Copyright (C) 2011 The Android Open Source Project
3:
4: Licensed under the Apache License, Version 2.0 (the "License");
5: you may not use this file except in compliance with the License.
6: You may obtain a copy of the License at
7:
8: http://www.apache.org/licenses/LICENSE-2.0
9:
10: Unless required by applicable law or agreed to in writing, software
11: distributed under the License is distributed on an "AS IS" BASIS,
12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: See the License for the specific language governing permissions and
14: limitations under the License.
15: -->
16:
17: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
18: package="com.android.serialchat"
19: android:sharedUserId="android.uid.system">
20:
21: <uses-permission android:name="android.permission.SERIAL_PORT"/>
22:
23: <application android:label="Serial Chat">
24: <activity android:name="SerialChat" android:label="Serial Chat">
25: <intent-filter>
26: <action android:name="android.intent.action.MAIN" />
27: <category android:name="android.intent.category.DEFAULT" />
28: <category android:name="android.intent.category.LAUNCHER" />
29: </intent-filter>
30: </activity>
31: </application>
32: </manifest>
- 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>
在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],所以这里作如下修改:
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
完。