分享如何在Android系统控制CB2的GPIO
参考了各位大神分享了在linux上控制GPIOde1经验,和 limex GPIO文章,才完成一下代码。希望对各位坛友有所帮助。镜像版本:cb_a20_android42_v1.04_8188eu-en_US.img
开发环境: IntelliJ IDEA
工具 : Dragonface,直接修改sysconfig1.lhs文件、boot logo等功能。package com.xxx.xxx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class GPIOAPI {
private Process shell;
private OutputStream os;
private InputStream is;
public String GPIO_Init() {
try
{
//root user
shell = Runtime.getRuntime().exec("su", null, new File("/system/bin/"));
os = shell.getOutputStream();
is=shell.getInputStream();
//shell.waitFor();
return null;
}
catch (IOException g){
return g.getMessage();
}
}
public int GPIO_Output(int portnum,int status) {
String ONOFF=Integer.toString(status);
int rtn=0;
try{
switch(portnum)
{
case 1:
os.write(("echo "+ONOFF+" > /sys/class/gpio_sw/PD0/data \n").getBytes("ASCII"));
break;
case 2:
os.write(("echo "+ONOFF+" > /sys/class/gpio_sw/PD1/data \n").getBytes("ASCII"));
break;
default:
rtn=-1;
break;
}
os.flush();
}
catch (IOException g){
rtn=g.hashCode();
}
return rtn;
}
public int GPIO_Input(int portnum) {
int rtn=0;
try{
switch(portnum)
{
case 1:
is.read(("cat /sys/class/gpio_sw/PD24/data \n").getBytes("ASCII"));
break;
case 2:
is.read(("cat /sys/class/gpio_sw/PD25/data \n").getBytes("ASCII"));
break;
default:
rtn=-1;
break;
}
}
catch (IOException g){
rtn=g.hashCode();
}
return rtn;
}
public int GPIO_Close()
{
int rtn=0;
try
{
os.close();
is.close();
shell.waitFor();
shell.destroy();
}
catch (IOException g){
rtn=g.hashCode();
}
catch (InterruptedException e) {
rtn=e.hashCode();
}
return rtn;
}
} 这个太复杂了。。。
直接在init.sun7i.rc修改权限,然后通过读写文件来写字符串就行了。。 //写GPIO文件
public int GPIO_Output(int portnum,int status)
//读GPIO文件
public int GPIO_Input(int portnum)我才开始学android和linux,如果你有更好的方法,请分享代码,谢谢! 对分享者致以敬意!
在CB3上也可以这样吗? 本帖最后由 wkloh 于 2014-1-6 22:48 编辑
zaneabc 发表于 2014-1-6 20:08 static/image/common/back.gif
对分享者致以敬意!
在CB3上也可以这样吗?
我没有cb3,理论上是可以的。
不过请通过 Dragonface重新配置cb3 android 镜像文件sysconfig1.lhs内的 GPIO。
举例:
gpio_used = 1
gpio_num = 4
gpio_pin_1 = port:PD00<1><default><1><default>
gpio_pin_2 = port:PD01<1><default><1><default>
gpio_pin_3 = port:PD23<1><default><default><default>
gpio_pin_4 = port:PD24<0><default><default><default>
请参考 http://linux-sunxi.org/A20/PIO。 本帖最后由 xiaolonghun1 于 2015-8-12 14:58 编辑
测试了一下,报错了。
xiaolonghun1 发表于 2015-8-12 14:54 static/image/common/back.gif
测试了一下,报错了。
GPIO_Init() 好像没调用吧?
gqjjqg 发表于 2015-9-15 11:08 static/image/common/back.gif
GPIO_Init() 好像没调用吧?
好像是没有调用,后来 用了别的方式进行解决了
xiaolonghun1 发表于 2015-9-22 10:13 static/image/common/back.gif
好像是没有调用,后来 用了别的方式进行解决了
楼主请问怎么解决的呀?
yxqlf 发表于 2015-12-17 14:09 static/image/common/back.gif
楼主请问怎么解决的呀?
没有用这个配置的方式。直接写成一个驱动模块的形式,让它在/dev 下面以一个字符设备驱动的形式进行存在。然后
通过ioctl函数对其进行相应的调用。这一他的io口寄存器有一个专门的宏定义头文件,你找找看,读写寄存器赋值需要用到它
页:
[1]
2