Android应用层操作GPIO的方法:
http://my.oschina.net/u/1176566/blog/177554
在使用某个GPIO之前首先的保证此GPIO没有正在被使用,否则是无法操作的;
如NAND模块的NCE3/PC18,此io口正被使用,在导入echo 74 > /sys/class/gpio/export时没问题,但是设置方向是无权操作,如图:
下面是我写的DEMO,单独操作GPIO和控制LED操作的代码。之前想用C部分来操作,然后通过JNI把方法暴露给上层,但是貌似C本地代码没有权限,但是在JAVA部分却可以操作。
LED电路部分控制IO为NCE5/SPI2_CLK/PC20;
GPIO.java
============================GPIO.java=================================
package com.test.gpio;
import java.io.DataOutputStream;
import java.io.IOException;
public class GPIO {
/**
* @param args
*/
private static int num, gpio_number;
private static String gpio_num = null;
private static String exportPath;
private static String directionPath;
private static String valuePath;
static Process process = null;
static DataOutputStream dos = null;
public static int gpio_crtl(String gpio, String direction, int level){
if(gpio.length() != 4){
System.out.println("input gpio error!");
return 0;
};
gpio_num = gpio.substring(2, 4); //从String gpio(如PC20)中提取出String 20;
num = Integer.parseInt(gpio_num);//将String 20转化为int 20;
System.out.println("num:"+num+"\n");
if((gpio.indexOf("pa") >= 0) || (gpio.indexOf("PA") >= 0)){
gpio_number = num;
}else if((gpio.indexOf("pb") >= 0) || (gpio.indexOf("PB") >= 0)){
gpio_number = num + 24;
}else if((gpio.indexOf("pc") >= 0) || (gpio.indexOf("PC") >= 0)){
gpio_number = num + 54;
}else if((gpio.indexOf("pd") >= 0) || (gpio.indexOf("PD") >= 0)){
gpio_number = num + 85;
}else if((gpio.indexOf("pe") >= 0) || (gpio.indexOf("PE") >= 0)){
gpio_number = num + 119;
}else if((gpio.indexOf("pf") >= 0) || (gpio.indexOf("PF") >= 0)){
gpio_number = num + 137;
}else if((gpio.indexOf("pg") >= 0) || (gpio.indexOf("PG") >= 0)){
gpio_number = num + 149;
}else if((gpio.indexOf("ph") >= 0) || (gpio.indexOf("PH") >= 0)){
gpio_number = num + 167;
}else if((gpio.indexOf("pi") >= 0) || (gpio.indexOf("PI") >= 0)){
gpio_number = num + 201;
}
exportPath = "echo " + gpio_number + " > /sys/class/gpio/export";
directionPath = "echo out > " + " /sys/class/gpio/gpio" + gpio_number + "/direction";
valuePath ="echo " + level + " > /sys/class/gpio/gpio" + gpio_number + "/value";
System.out.printf(exportPath + "\n" + directionPath + "\n" + valuePath + "\n");
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(exportPath+"\n");
dos.flush();
dos.writeBytes(directionPath+"\n");
dos.flush();
dos.writeBytes(valuePath +"\n");
dos.flush();
dos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return 0;
}
}
==========================GPIO.java end===============================
LED.java
============================LED.java=================================
package com.test.gpio;
import java.io.DataOutputStream;
import java.io.IOException;
public class LED {
static Process process = null;
public static void setLed(){
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes("echo 74 > /sys/class/gpio/export"+"\n");
dos.flush();
//设置引脚功能为输出
dos.writeBytes("echo out > /sys/class/gpio/gpio74/direction"+"\n");
dos.flush();
dos.close();
System.out.println("echo 74 > /sys/class/gpio/export"+"echo out > /sys/class/gpio/gpio74/direction");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void cmdLedOff() {
// TODO Auto-generated method stub
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes("echo 0 > /sys/class/gpio/gpio74/value"+"\n");
dos.flush();
dos.close();
System.out.println("echo 0 > /sys/class/gpio/gpio74/value");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void cmdLedOn() {
// TODO Auto-generated method stub
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes("echo 1 > /sys/class/gpio/gpio74/value"+"\n");
dos.flush();
dos.close();
System.out.println("echo 1 > /sys/class/gpio/gpio74/value");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void ledFlash() {
// TODO Auto-generated method stub
while (true) {
cmdLedOn();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cmdLedOff();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
==============================LED.java end=============================
MainActivity.java
==============================MainActivity.java===========================
package com.test.gpio;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
TextView gpio_name;
TextView direction;
TextView value;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpio_name = (TextView) findViewById(R.id.editText1);
direction = (TextView) findViewById(R.id.editText2);
value = (TextView) findViewById(R.id.editText3);
Button set = (Button)findViewById(R.id.button1);
Button led_on = (Button)findViewById(R.id.button2);
Button led_off = (Button)findViewById(R.id.button3);
Button led_flash = (Button)findViewById(R.id.button4);
LED.setLed();
set.setOnClickListener(this);
led_on.setOnClickListener(this);
led_off.setOnClickListener(this);
led_flash.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
// TODO Auto-generated method stub
System.out.println("gpio crtl");
String gp = gpio_name.getText().toString();
String dir = direction.getText().toString();
int val = Integer.valueOf(value.getText().toString());
GPIO.gpio_crtl(gp, dir, val);
break;
case R.id.button2:
System.out.println("led on");
LED.cmdLedOn();
break;
case R.id.button3:
System.out.println("led off");
LED.cmdLedOff();
break;
case R.id.button4:
System.out.println("led flash");
LED.ledFlash();
break;
default:
System.out.println("no opreate!");
break;
}
}
}
=============================MainActivity.java end==========================
附C实现的操作gpio_crtl,但无法实现。请大神指点,为什么没权限。即使已经remount的系统仍无法操作:
===================================gpio_crtl============================
/*******gpio******
* <for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done>
* Allwinner A20 PAD
* Vstarcam wangjian 2014.07.10
* GPA: 0
* GPE: 119
* GPF: 137
* GPG: 149
* GPH: 167
* GPI: 201
* axp_pin: 229
* GPB: 24
* GPC: 54
* GPD: 85
*******************/
#include<stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
int gpio_crtl(char *gpio, char *direction, int level)
{
int num, gpio_number, status;
char *gp, *name = NULL;
char *exportPath = NULL;
char *directionPath = NULL;
char *valuePath = NULL;
char *gpio_str = NULL;
if(gpio == NULL || direction == NULL)
return -1;
if(strncpy(gp, gpio, 2) == NULL)//从输入字符串GPIO(如PC20)中提取出字符串“PC”
return -1;
if(strncpy(name, const gpio[2], 2) == NULL)//从输入字符串GPIO(如PC20)中提取出字符串“20”
return -1;
gpio_number = atoi(name);//将字符串“20”转化为整数型
switch(gp){
case ("PA" || "pa"):
num = gpio_number;
break;
case ("PB" || "pb"):
num = gpio_number + 24;
break;
case ("PC" || "pc"):
num = gpio_number + 54;
break;
case ("PD" || "pd"):
num = gpio_number + 85;
break;
case ("PE" || "pe"):
num = gpio_number + 119;
break;
case ("PF" || "pf"):
num = gpio_number + 137;
break;
case ("PG" || "pg"):
num = gpio_number + 149;
break;
case ("PH" || "ph"):
num = gpio_number + 167;
break;
case ("PI" || "pi"):
num = gpio_number + 201;
break;
default:
return -1;
}
itoa(num, gpio_str, 10);
sprintf(exportPath, "echo %d > /sys/class/gpio/export ", num);
status = system(exportPath);
if(status == -1)
return -1;
if (!WIFEXITED(status)){//子进程的状态检测
printf("exportPath exit illegal![%d]/n", errno) ;
return -1;
}
sprintf(directionPath, "echo %s > /sys/class/gpio/%s/direction",direction ,strcat("gpio",gpio_str));
status = system(directionPath);
if(status == -1)
return -1;
if (!WIFEXITED(status)){
printf("directionPath exit illegal![%d]/n", errno) ;
return -1;
}
sprintf(valuePath, "echo %d > /sys/class/gpio/%s/value",level ,strcat("gpio",gpio_str));
status = system(valuePath);
if(status == -1)
return -1;
if (!WIFEXITED(status)){
printf("valuePath exit illegal![%d]/n", errno) ;
return -1;
}
}
=======================================================
Android应用层操作GPIO,布布扣,bubuko.com