当前位置: 代码迷 >> 驱动开发 >> Android 驱动开发系列2
  详细解决方案

Android 驱动开发系列2

热度:65   发布时间:2016-04-28 10:28:33.0
Android 驱动开发系列二

最近琐碎事太多了,都没什么时间来写blog。现在继续写这个android驱动的开发调试

这一章主要是讲如何测试驱动。

 

1、驱动的简单测试

在上一篇文章中,我们已经把添加驱动模块做完了,并把驱动下载到了板子上。下面将介绍一下如何测试驱动是否正常。

这个ttt驱动,我们实现了一个读、一个写的接口,就此进行简单的测试:

启动板子,进入系统,然后进入命令行(可以用串口,也可以用adb shell)

进入dev目录:

[email protected]:/ # cd /dev[email protected]:/dev #

 

查看 ttt 设备文件是否存在:

[email protected]:/dev # ls -l tttcrw------- root     root     249,   0 2013-04-02 09:58 ttt


进入到/proc目录,验证设备的输入输出:

[email protected]:/dev # cd /proc/[email protected]:/proc #


获取 ttt 设备的值:

[email protected]:/proc # cat ttt0[email protected]:/proc #


设置 ttt 设备的值:

[email protected]:/proc # echo '111' > ttt[email protected]:/proc # cat ttt111[email protected]:/proc #


这里就可以说明: ttt 设备文件已经OK了,输入输出接口都没有问题。

当然,这里还有其他的方式去验证这个输入输出,如:

进入到 /sys/class 的 ttt 设备目录下:

[email protected]:/proc # cd /sys/class/ttt/ttt/[email protected]:/sys/class/ttt/ttt # lsdevpowersubsystemueventval[email protected]:/sys/class/ttt/ttt #


通过访问 val 属性文件来读写 ttt 设备的值:

[email protected]:/sys/class/ttt/ttt # lsdevpowersubsystemueventval[email protected]:/sys/class/ttt/ttt # cat val111[email protected]:/sys/class/ttt/ttt # echo '123' > val[email protected]:/sys/class/ttt/ttt # cat val123[email protected]:/sys/class/ttt/ttt #


 

2、编写C程序来测试驱动的读写

切换到root权限,并进入到android源码目录下的 external 目录:

[email protected]:~# cd workspace/android-4.0.4_r1.2/external/[email protected]:~/workspace/android-4.0.4_r1.2/external# lsandroid-mock    freetype                 libpng         safe-iopantlr           fsck_msdos               libvpx         skiaapache-harmony  genext2fs                libxml2        sonivoxapache-http     giflib                   libxslt        speexapache-xml      google-diff-match-patch  libyuv         sqliteastl            grub                     llvm           srecbison           gtest                    lohit-fonts    srtpblktrace        guava                    markdown       stlportbluetooth       harfbuzz                 mesa3d         stracebouncycastle    hyphenation              mksh           svoxbsdiff          icu4c                    mockwebserver  tagsoupbzip2           iproute2                 mtpd           tcpdumpchromium        ipsec-tools              netcat         tinyalsaclang           iptables                 netperf        tinyxmlcollada         javasqlite               neven          tremolodbus            javassist                nist-sip       v8dhcpcd          jdiff                    oauth          valgrinddnsmasq         jhead                    opencv         webkitdoclava         jpeg                     openssl        webpdropbear        jsilver                  oprofile       webrtce2fsprogs       jsr305                   pcre           wpa_supplicanteasymock        junit                    ping           wpa_supplicant_6elfutils        kernel-headers           ping6          wpa_supplicant_8embunit         libffi                   ppp            xmlwriteremma            libgsm                   proguard       yaffs2esd             liblzf                   protobuf       yappexpat           libnfc-nxp               qemu           zlibeyes-free       libnl-headers            qemu-pc-biosfdlibm          libpcap                  quakeflac            libphonenumber           replicaisland[email protected]:~/workspace/android-4.0.4_r1.2/external# 

由于这个目录下的文件比较多,为了不混淆,我另外新建了一个yapp目录,用于存放相关的程序。

进入到yapp目录,并创建 tttapp 目录:

[email protected]:~/workspace/android-4.0.4_r1.2/external/yapp# mkdir tttapp[email protected]:~/workspace/android-4.0.4_r1.2/external/yapp# lshelloapp  helloworld  tttapp

 

进入到 tttapp 目录,并创建 tttapp.c 文件:

[email protected]:~/workspace/android-4.0.4_r1.2/external/yapp# cd tttapp/[email protected]:~/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配置文件:

[email protected]:~/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 := optionalLOCAL_MODULE := tttappLOCAL_SRC_FILES := tttapp.cinclude $(BUILD_EXECUTABLE)


C程序编写完了,下面进行编译:

回到android的源码目录,并执行envsetup.sh:

[email protected]:~/workspace/android-4.0.4_r1.2# lsabi       build        device      hardware  out       systembionic    cts          docs        libcore   packages  v8.logbootable  dalvik       external    Makefile  prebuilt  vendorboot.img  development  frameworks  ndk       sdk[email protected]:~/workspace/android-4.0.4_r1.2# . build/envsetup.sh including device/moto/stingray/vendorsetup.shincluding device/moto/wingray/vendorsetup.shincluding device/samsung/crespo4g/vendorsetup.shincluding device/samsung/crespo/vendorsetup.shincluding device/samsung/maguro/vendorsetup.shincluding device/samsung/smdkc110/vendorsetup.shincluding device/samsung/smdkv210/vendorsetup.shincluding device/samsung/torospr/vendorsetup.shincluding device/samsung/toro/vendorsetup.shincluding device/samsung/tuna/vendorsetup.shincluding device/ti/panda/vendorsetup.shincluding sdk/bash_completion/adb.bash[email protected]:~/workspace/android-4.0.4_r1.2# 

 

执行 make tttapp 编译 tttapp 程序:

[email protected]:~/workspace/android-4.0.4_r1.2# make tttapp============================================PLATFORM_VERSION_CODENAME=RELPLATFORM_VERSION=4.0.4TARGET_PRODUCT=fullTARGET_BUILD_VARIANT=engTARGET_BUILD_TYPE=releaseTARGET_BUILD_APPS=TARGET_ARCH=armTARGET_ARCH_VARIANT=armv7-aHOST_ARCH=x86HOST_OS=linuxHOST_BUILD_TYPE=releaseBUILD_ID=IMM76I============================================target thumb C: tttapp <= external/yapp/tttapp/tttapp.ctarget 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[email protected]:~/workspace/android-4.0.4_r1.2# 

这样就表示编译成功了,我们看一下这个对应的执行程序是否已经生成:

[email protected]:~/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[email protected]:~/workspace/android-4.0.4_r1.2# 

这样就表示生成了对应的 可执行程序。

 好,现在我们把这个生成的 tttapp 程序拷贝到 SD卡上,然后放到板子上去执行。

我这里是放到了U盘上面,从U盘中把tttapp 拷贝到板子上的/data目录中去:

[email protected]:/mnt # ls usbD3GOther.exeEBOOT.binEBOOT.nb0IRDA_X.exeLOST.DIRMapTools.exeNK.BINNK_2G.binNK_3G.binNK_3G_y.BINNK_src.binRFIDForProduce.exeRFIDTest.exeRFID_X.exeSTEPLDR.binSTEPLDR.nb0SwitchTools.exeWINCE 6è?????android-samsung-dev_20110830.tar.gzbarscan.exebbkhellohtcbjwdm.inftttappubuntu-11.10-desktop-i386.iso????????·??°??????è???·? (MC60???).exe[email protected]:/mnt #[email protected]:/mnt # lsasecext_sdobbsdcardsecureusb[email protected]:/mnt # cd usb/[email protected]:/mnt/usb # lsD3GOther.exeEBOOT.binEBOOT.nb0IRDA_X.exeLOST.DIRMapTools.exeNK.BINNK_2G.binNK_3G.binNK_3G_y.BINNK_src.binRFIDForProduce.exeRFIDTest.exeRFID_X.exeSTEPLDR.binSTEPLDR.nb0SwitchTools.exeWINCE 6è?????android-samsung-dev_20110830.tar.gzbarscan.exebbkhellohtcbjwdm.inftttappubuntu-11.10-desktop-i386.iso????????·??°??????è???·? (MC60???).exe[email protected]:/mnt/usb # ll tttapp----rwxr-x system   media_rw     5524 2013-04-02 11:08 tttapp[email protected]:/mnt/usb # chmod 777 tttapp[email protected]:/mnt/usb # ll tttapp----rwxr-x system   media_rw     5524 2013-04-02 11:08 tttapp[email protected]:/mnt/usb # cp tttapp /data/system/bin/sh: cp: not found[email protected]:/mnt/usb # mv tttapp /datafailed on 'tttapp' - Cross-device link[email protected]:/mnt/usb # mv tttapp /data/tttappfailed on 'tttapp' - Cross-device link[email protected]:/mnt/usb # cat tttapp > /data/tttapp[email protected]:/mnt/usb # ll /data/tttapp-rw-rw-rw- root     log          5524 2013-04-02 11:14 tttapp[email protected]:/mnt/usb # cd /data[email protected]:/data # ./tttapp/system/bin/sh: ./tttapp: cannot execute - Permission denied[email protected]:/data # chmod 777 tttapp[email protected]:/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.[email protected]:/data #


拷贝的过程中,出现了一些情况,cp指令不可用,使用cat指令就OK了。由上面最后的几句信息,可以看出这个C程序成功了。测试驱动OK了。 

 

 

到此,驱动的测试就完了。下一篇将会介绍编写对应的HAL硬件抽象层来访问内核驱动
  相关解决方案