最近琐碎事太多了,都没什么时间来写blog。现在继续写这个android驱动的开发调试
这一章主要是讲如何测试驱动。
1、驱动的简单测试
在上一篇文章中,我们已经把添加驱动模块做完了,并把驱动下载到了板子上。下面将介绍一下如何测试驱动是否正常。
这个ttt驱动,我们实现了一个读、一个写的接口,就此进行简单的测试:
启动板子,进入系统,然后进入命令行(可以用串口,也可以用adb shell)
进入dev目录:
- root@android:/ # cd /dev
- root@android:/dev #
查看 ttt 设备文件是否存在:
- root@android:/dev # ls -l ttt
- crw------- root root 249, 0 2013-04-02 09:58 ttt
进入到/proc目录,验证设备的输入输出:
- root@android:/dev # cd /proc/
- root@android:/proc #
获取 ttt 设备的值:
- root@android:/proc # cat ttt
- 0
- root@android:/proc #
设置 ttt 设备的值:
- root@android:/proc # echo '111' > ttt
- root@android:/proc # cat ttt
- 111
- root@android:/proc #
这里就可以说明: ttt 设备文件已经OK了,输入输出接口都没有问题。
当然,这里还有其他的方式去验证这个输入输出,如:
进入到 /sys/class 的 ttt 设备目录下:
- root@android:/proc # cd /sys/class/ttt/ttt/
- root@android:/sys/class/ttt/ttt # ls
- dev
- power
- subsystem
- uevent
- val
- root@android:/sys/class/ttt/ttt #
通过访问 val 属性文件来读写 ttt 设备的值:
- root@android:/sys/class/ttt/ttt # ls
- dev
- power
- subsystem
- uevent
- val
- root@android:/sys/class/ttt/ttt # cat val
- 111
- root@android:/sys/class/ttt/ttt # echo '123' > val
- root@android:/sys/class/ttt/ttt # cat val
- 123
- root@android:/sys/class/ttt/ttt #
2、编写C程序来测试驱动的读写
切换到root权限,并进入到android源码目录下的 external 目录:
- root@brantyou-ubuntu:~# cd workspace/android-4.0.4_r1.2/external/
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external# ls
- android-mock freetype libpng safe-iop
- antlr fsck_msdos libvpx skia
- apache-harmony genext2fs libxml2 sonivox
- apache-http giflib libxslt speex
- apache-xml google-diff-match-patch libyuv sqlite
- astl grub llvm srec
- bison gtest lohit-fonts srtp
- blktrace guava markdown stlport
- bluetooth harfbuzz mesa3d strace
- bouncycastle hyphenation mksh svox
- bsdiff icu4c mockwebserver tagsoup
- bzip2 iproute2 mtpd tcpdump
- chromium ipsec-tools netcat tinyalsa
- clang iptables netperf tinyxml
- collada javasqlite neven tremolo
- dbus javassist nist-sip v8
- dhcpcd jdiff oauth valgrind
- dnsmasq jhead opencv webkit
- doclava jpeg openssl webp
- dropbear jsilver oprofile webrtc
- e2fsprogs jsr305 pcre wpa_supplicant
- easymock junit ping wpa_supplicant_6
- elfutils kernel-headers ping6 wpa_supplicant_8
- embunit libffi ppp xmlwriter
- emma libgsm proguard yaffs2
- esd liblzf protobuf yapp
- expat libnfc-nxp qemu zlib
- eyes-free libnl-headers qemu-pc-bios
- fdlibm libpcap quake
- flac libphonenumber replicaisland
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external#
由于这个目录下的文件比较多,为了不混淆,我另外新建了一个yapp目录,用于存放相关的程序。
进入到yapp目录,并创建 tttapp 目录:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external/yapp# mkdir tttapp
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external/yapp# ls
- helloapp helloworld tttapp
进入到 tttapp 目录,并创建 tttapp.c 文件:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external/yapp# cd tttapp/
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external/yapp/tttapp# gedit tttapp.c
tttapp.c 文件内容如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define DEVICE_NAME "/dev/ttt"
- int main(int argc, char** argv)
- {
- int fd = -1;
- int val = 0;
- fd = open(DEVICE_NAME, O_RDWR);
- if(fd == -1){
- printf("Failed to open device %s.\n", DEVICE_NAME);
- return -1;
- }
- printf("Read original value:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- val = 100;
- printf("Write value %d to %s.\n\n", val, DEVICE_NAME);
- write(fd, &val, sizeof(val));
- printf("Read the value again:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- close(fd);
- return 0;
- }
创建对应的Android.mk配置文件:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/external/yapp/tttapp# gedit Android.mk
Android.mk 文件内容如下:
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE := tttapp
- LOCAL_SRC_FILES := tttapp.c
- include $(BUILD_EXECUTABLE)
C程序编写完了,下面进行编译:
回到android的源码目录,并执行envsetup.sh:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2# ls
- abi build device hardware out system
- bionic cts docs libcore packages v8.log
- bootable dalvik external Makefile prebuilt vendor
- boot.img development frameworks ndk sdk
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2# . build/envsetup.sh
- including device/moto/stingray/vendorsetup.sh
- including device/moto/wingray/vendorsetup.sh
- including device/samsung/crespo4g/vendorsetup.sh
- including device/samsung/crespo/vendorsetup.sh
- including device/samsung/maguro/vendorsetup.sh
- including device/samsung/smdkc110/vendorsetup.sh
- including device/samsung/smdkv210/vendorsetup.sh
- including device/samsung/torospr/vendorsetup.sh
- including device/samsung/toro/vendorsetup.sh
- including device/samsung/tuna/vendorsetup.sh
- including device/ti/panda/vendorsetup.sh
- including sdk/bash_completion/adb.bash
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2#
执行 make tttapp 编译 tttapp 程序:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2# make tttapp
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.0.4
- TARGET_PRODUCT=full
- TARGET_BUILD_VARIANT=eng
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a
- HOST_ARCH=x86
- HOST_OS=linux
- HOST_BUILD_TYPE=release
- BUILD_ID=IMM76I
- ============================================
- target thumb C: tttapp <= external/yapp/tttapp/tttapp.c
- target Executable: tttapp (out/target/product/generic/obj/EXECUTABLES/tttapp_intermediates/LINKED/tttapp)
- target Symbolic: tttapp (out/target/product/generic/symbols/system/bin/tttapp)
- target Strip: tttapp (out/target/product/generic/obj/EXECUTABLES/tttapp_intermediates/tttapp)
- Install: out/target/product/generic/system/bin/tttapp
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2#
这样就表示编译成功了,我们看一下这个对应的执行程序是否已经生成:
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2# ls -l out/target/product/generic/system/bin/tttapp
- -rwxr-xr-x 1 root root 5524 2013-04-02 11:04 out/target/product/generic/system/bin/tttapp
- root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2#
这样就表示生成了对应的 可执行程序。
好,现在我们把这个生成的 tttapp 程序拷贝到 SD卡上,然后放到板子上去执行。
我这里是放到了U盘上面,从U盘中把tttapp 拷贝到板子上的/data目录中去:
- root@android:/mnt # ls usb
- D3GOther.exe
- EBOOT.bin
- EBOOT.nb0
- IRDA_X.exe
- LOST.DIR
- MapTools.exe
- NK.BIN
- NK_2G.bin
- NK_3G.bin
- NK_3G_y.BIN
- NK_src.bin
- RFIDForProduce.exe
- RFIDTest.exe
- RFID_X.exe
- STEPLDR.bin
- STEPLDR.nb0
- SwitchTools.exe
- WINCE 6软件
- android-samsung-dev_20110830.tar.gz
- barscan.exe
- bbk
- hello
- htcbjwdm.inf
- tttapp
- ubuntu-11.10-desktop-i386.iso
- 凯立德地图
- 机身å·ç (MC60端).exe
- root@android:/mnt #
- root@android:/mnt # ls
- asec
- ext_sd
- obb
- sdcard
- secure
- usb
- root@android:/mnt # cd usb/
- root@android:/mnt/usb # ls
- D3GOther.exe
- EBOOT.bin
- EBOOT.nb0
- IRDA_X.exe
- LOST.DIR
- MapTools.exe
- NK.BIN
- NK_2G.bin
- NK_3G.bin
- NK_3G_y.BIN
- NK_src.bin
- RFIDForProduce.exe
- RFIDTest.exe
- RFID_X.exe
- STEPLDR.bin
- STEPLDR.nb0
- SwitchTools.exe
- WINCE 6软件
- android-samsung-dev_20110830.tar.gz
- barscan.exe
- bbk
- hello
- htcbjwdm.inf
- tttapp
- ubuntu-11.10-desktop-i386.iso
- 凯立德地图
- 机身å·ç (MC60端).exe
- root@android:/mnt/usb # ll tttapp
- ----rwxr-x system media_rw 5524 2013-04-02 11:08 tttapp
- root@android:/mnt/usb # chmod 777 tttapp
- root@android:/mnt/usb # ll tttapp
- ----rwxr-x system media_rw 5524 2013-04-02 11:08 tttapp
- root@android:/mnt/usb # cp tttapp /data
- /system/bin/sh: cp: not found
- 127|root@android:/mnt/usb # mv tttapp /data
- failed on 'tttapp' - Cross-device link
- 255|root@android:/mnt/usb # mv tttapp /data/tttapp
- failed on 'tttapp' - Cross-device link
- 255|root@android:/mnt/usb # cat tttapp > /data/tttapp
- root@android:/mnt/usb # ll /data/tttapp
- -rw-rw-rw- root log 5524 2013-04-02 11:14 tttapp
- root@android:/mnt/usb # cd /data
- root@android:/data # ./tttapp
- /system/bin/sh: ./tttapp: cannot execute - Permission denied
- 126|root@android:/data # chmod 777 tttapp
- root@android:/data # ./tttapp
- [ttt]: ttt_open().
- [ttt]: ttt_read().
- [ttt]: ttt_write().
- [ttt]: ttt_read().
- [ttt]: ttt_release().
- Read original value:
- 123.
- Write value 100 to /dev/ttt.
- Read the value again:
- 100.
- root@android:/data #
拷贝的过程中,出现了一些情况,cp指令不可用,使用cat指令就OK了。由上面最后的几句信息,可以看出这个C程序成功了。测试驱动OK了。
到此,驱动的测试就完了。下一篇将会介绍编写对应的HAL硬件抽象层来访问内核驱动