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

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

package com.android.gpio;  import com.android.gpio.R;  import android.app.Activity;  import android.os.ServiceManager;  import android.os.Bundle;  import android.os.IGpioService;  import android.os.RemoteException;  import android.util.Log;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  public class Gpio extends Activity implements OnClickListener {  private final static String LOG_TAG = "com.android.Gpio";  private IGpioService gpioService = null;  private Button setButton = null;  private Button clearButton = null;    /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  gpioService = IGpioService.Stub.asInterface(  ServiceManager.getService("gpio"));  setButton = (Button)findViewById(R.id.button_set);  clearButton = (Button)findViewById(R.id.button_clear);  setButton.setOnClickListener(this);  clearButton.setOnClickListener(this);  }    @Override  public void onClick(View v) {  if(v.equals(setButton)) {   try{int val='1';gpioService.setVal(val);} catch (RemoteException e){Log.e(LOG_TAG,"setting value");}} else if(v.equals(clearButton)) {try{int val='0';gpioService.setVal(val);   } catch (RemoteException e){Log.e(LOG_TAG,"setting value");   }   }  }  }  
布局res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <Button       android:id="@+id/button_clear"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="@string/clear"       android:textStyle="bold"       android:layout_marginTop="140dp"/>   <Button       android:id="@+id/button_set"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="@string/set"       android:textStyle="bold"        android:layout_marginTop="0dp"/></LinearLayout>  
字符串res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>  <resources>     <string name="app_name">GPIO</string>     <string name="set">Set</string>     <string name="clear">Clear</string>  </resources>  
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.gpio"    android:versionCode="1"    android:versionName="1.0">    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">      <activity android:name=".Gpio"                android:label="@string/app_name">          <intent-filter>              <action android:name="android.intent.action.MAIN" />              <category android:name="android.intent.category.LAUNCHER" />          </intent-filter>      </activity>    </application>  </manifest>   
编辑工程目录下Android.mk:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := GpioLOCAL_CERTIFICATE := platforminclude $(BUILD_PACKAGE)include $(call all-makefiles-under,$(LOCAL_PATH))
编辑build/target/product/generic_no_telephony.mk:

在PRODUCT_PACKAGES := \ 后加入Gpio \

编译工程会生成Gpio.apk

make TARGET_PRODUCT=am335xevm_sk -j8 OMAPES=4.x 
到这一步一切准备工作都已就绪,开始更新目标板am335evm的文件系统:(左为host,右为evm)

out/target/product/am335xevm_sk/system/app/Gpio.apk                 ==>   rootfs/out/target/product/am335xevm_sk/system/framework/services.jar       ==>   rootfs/system/framework/out/target/product/am335xevm_sk/system/framework/framework.jar      ==>   rootfs/system/framework/out/target/product/am335xevm_sk/obj/lib/libandroid_servers.so       ==>   rootfs/system/lib/out/target/product/am335xevm_sk/obj/lib/gpio.default.so             ==>   rootfs/system/lib/hw/

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

/dev/AdrIO                0666   root       root
启动,安装Gpio.apk,打开GPIO:

现象:

点击"Set"按钮:     D1灯亮    点击"Clear"按钮:   D1灯灭

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

更多相关文章

  1. Android之菜单总结
  2. Android使用Retrofit进行网络请求
  3. Android官方入门文档[1]创建一个Android项目
  4. android 创建桌面快捷方式 、插件
  5. AIR Native Extension的使用(Android)一 : 打包ane
  6. Android架构分析之使用自定义硬件抽象层(HAL)模块
  7. 创建android逐帧动画的两种方式
  8. SlidingMenu和ActionBarSherlock结合做出出色的App布局,Facebook
  9. 使用sencha cmd创建android应用

随机推荐

  1. npm install 出现npm ERR! cb() never ca
  2. Oracle OCP 071【中文题库】考试题-第19
  3. 盘点2021年最为流行的七个数据建模工具
  4. Python中的3D绘图命令
  5. 【JS基础入门】JavaScript中创建对象的7
  6. LeCun力荐,PyTorch官方权威教程书来了,意外
  7. 技术·原创 | 四步部署零信任SDP产品助力
  8. 单芯片每秒1000万亿次运算:谷歌TPU原班团
  9. 数栈数据安全案例:混合云环境数据库备份容
  10. RabbitMQ消息中间件技术精讲