无网不进  
软硬件开发

创建Android Application工程:使用Eclipse的Android插件ADT创建Android工程,工程名称为Gpio,创建完成后将工程目录拷贝到packages/apps/文件夹下,并删除工程目录下的gen文件夹,不删除的话会造成类重复的错误。

src/com/android/gpio/Gpio.java:

 

[javascript] view plain copy
 
  1. package com.android.gpio;    
  2. import com.android.gpio.R;    
  3. import android.app.Activity;    
  4. import android.os.ServiceManager;    
  5. import android.os.Bundle;    
  6. import android.os.IGpioService;    
  7. import android.os.RemoteException;    
  8. import android.util.Log;    
  9. import android.view.View;    
  10. import android.view.View.OnClickListener;    
  11. import android.widget.Button;    
  12.   
  13. public class Gpio extends Activity implements OnClickListener {    
  14. private final static String LOG_TAG = "com.android.Gpio";    
  15. private IGpioService gpioService = null;    
  16.   
  17. private Button setButton = null;    
  18. private Button clearButton = null;    
  19.     
  20. /** Called when the activity is first created. */    
  21. @Override    
  22. public void onCreate(Bundle savedInstanceState) {    
  23.     super.onCreate(savedInstanceState);    
  24.     setContentView(R.layout.main);    
  25.   
  26.     gpioService = IGpioService.Stub.asInterface(    
  27.     ServiceManager.getService("gpio"));    
  28.   
  29.     setButton = (Button)findViewById(R.id.button_set);    
  30.     clearButton = (Button)findViewById(R.id.button_clear);    
  31.   
  32.     setButton.setOnClickListener(this);    
  33.     clearButton.setOnClickListener(this);    
  34. }    
  35.     
  36. @Override    
  37. public void onClick(View v) {    
  38.     if(v.equals(setButton)) {     
  39.     try{  
  40.         int val='1';  
  41.         gpioService.setVal(val);  
  42.     } catch (RemoteException e){  
  43.         Log.e(LOG_TAG,"setting value");   
  44.     }     
  45.     }   
  46.     else if(v.equals(clearButton)) {  
  47.     try{  
  48.         int val='0';  
  49.         gpioService.setVal(val);  
  50.        } catch (RemoteException e){  
  51.         Log.e(LOG_TAG,"setting value");   
  52.        }      
  53.        }    
  54.     }    
  55. }    

布局res/layout/main.xml:

 

 

[javascript] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.    android:orientation="vertical"    
  4.    android:layout_width="fill_parent"    
  5.    android:layout_height="fill_parent">    
  6.   
  7.    <Button  
  8.        android:id="@+id/button_clear"  
  9.        android:layout_width="match_parent"  
  10.        android:layout_height="wrap_content"  
  11.        android:text="@string/clear"  
  12.        android:textStyle="bold"  
  13.        android:layout_marginTop="140dp"/>  
  14.    <Button  
  15.        android:id="@+id/button_set"  
  16.        android:layout_width="match_parent"  
  17.        android:layout_height="wrap_content"  
  18.        android:text="@string/set"  
  19.        android:textStyle="bold"   
  20.        android:layout_marginTop="0dp"/>  
  21. </LinearLayout>    

字符串res/values/strings.xml:

 

 

[javascript] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.    <string name="app_name">GPIO</string>    
  4.    <string name="set">Set</string>    
  5.    <string name="clear">Clear</string>    
  6. </resources>    

AndroidManifest.xml:

 

 

[javascript] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  3.   package="com.android.gpio"    
  4.   android:versionCode="1"    
  5.   android:versionName="1.0">    
  6.   <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">    
  7.     <activity android:name=".Gpio"    
  8.               android:label="@string/app_name">    
  9.         <intent-filter>    
  10.             <action android:name="android.intent.action.MAIN" />    
  11.             <category android:name="android.intent.category.LAUNCHER" />    
  12.         </intent-filter>    
  13.     </activity>    
  14.   </application>    
  15. </manifest>     

编辑工程目录下Android.mk:

 

 

[javascript] view plain copy
 
  1. LOCAL_PATH:= $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_MODULE_TAGS := optional  
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)  
  5. LOCAL_PACKAGE_NAME := Gpio  
  6. LOCAL_CERTIFICATE := platform  
  7. include $(BUILD_PACKAGE)  
  8. include $(call all-makefiles-under,$(LOCAL_PATH))  

编辑build/target/product/generic_no_telephony.mk:

 

在PRODUCT_PACKAGES := \ 后加入Gpio \

编译工程会生成Gpio.apk

 

[javascript] view plain copy
 
  1. make TARGET_PRODUCT=am335xevm_sk -j8 OMAPES=4.x   

到这一步一切准备工作都已就绪,开始更新目标板am335evm的文件系统:(左为host,右为evm)

 

[javascript] view plain copy
 
  1. out/target/product/am335xevm_sk/system/app/Gpio.apk                 ==>   rootfs/  
  2.   
  3. out/target/product/am335xevm_sk/system/framework/services.jar       ==>   rootfs/system/framework/  
  4.   
  5. out/target/product/am335xevm_sk/system/framework/framework.jar      ==>   rootfs/system/framework/  
  6.   
  7. out/target/product/am335xevm_sk/obj/lib/libandroid_servers.so       ==>   rootfs/system/lib/  
  8.   
  9. out/target/product/am335xevm_sk/obj/lib/gpio.default.so             ==>   rootfs/system/lib/hw/  

确保rootfs下 ueventd.rc 已经添加:(Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层访问硬件驱动  提到)

 

[javascript] view plain copy
 
  1. /dev/AdrIO                0666   root       root  

启动,安装Gpio.apk,打开GPIO:

 

现象:

[javascript] view plain copy
 
  1. 点击"Set"按钮:     D1灯亮      
  2.   
  3. 点击"Clear"按钮:   D1灯灭  

可见编写的APP通过调用硬件服务已经成功操作了AM335寄存器,改变AM335X_GPIO_LED4的电平,通过N沟MOS器件控制LED D1的亮灭。

posted on 2018-01-10 13:46  无网不进  阅读(222)  评论(0)    收藏  举报