关闭

androidgpio操作

1214人阅读 评论(0) 收藏 举报
分类:
在嵌入式设备中对GPIO的操作是最基本的操作。一般的做法是写一个单独驱动程序,网上大多数的例子都是这样的。其实linux下面有一个通用的GPIO操作接口,那就是我要介绍的 “/sys/class/gpio” 方式。 
首先,看看系统中有没有“/sys/class/gpio”这个文件夹。如果没有请在编译内核的时候加入   Device Drivers  —>  GPIO Support  —>     /sys/class/gpio/… (sysfs interface)。 
/sys/class/gpio 的使用说明: 
01 gpio_operation 通过/sys/文件接口操作IO端口 GPIO到文件系统的映射 
02 * 控制GPIO的目录位于/sys/class/gpio 
03 * /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号 
04 * /sys/class/gpio/unexport 用于通知系统取消导出 
05 * /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号        base,寄存器名称,引脚总数 导出一个引脚的操作步骤 
06 * 首先计算此引脚编号,引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数 
07 * 向/sys/class/gpio/export写入此编号,比如12号引脚,在shell中可以通过以下命令实现, 
echo 12 > /sys/class/gpio/export 
  命令成功后生成/sys/class/gpio/gpio12目录,如果没有出现相应的目录,说明此引脚不可导出: 
08   
09 * direction文件,定义输入输入方向,可以通过下面命令定义为输出 
10   echo out > /sys/class/gpio/gpio12/direction 
11 * direction接受的参数:in, out, high, low。high/low同时设置方向为输出, 
    并将value设置为相应的1/0。 
12 * value文件是端口的数值,为1或0. 
13   echo 1 >/sys/class/gpio/gpio12/value 
下面在2440下进行一下测试 
1.取得GPIO信息,在终端中敲入以下命令 
1 $ cd /sys/class/gpio 
2 $ for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done 
终端中显示如下 
1 GPIOA: 0 
2 GPIOE: 128 
3 GPIOF: 160 
4 GPIOG: 192 
5 GPIOH: 224 
6 GPIOB: 32 
7 GPIOC: 64 
8 GPIOD: 96 
2.计算GPIO号码 
我们把GPE11用来控制LED。 
GPE0的头是128,GPE11 就是128+11 = 139. 
1 $ echo 139 > /sys/class/gpio/export 
ls 一下看看有没有 gpio139 这个目录 
3.GPIO控制测试。 
控制LED所以是输出。 
所以我们应该执行 
1 $ echo out > /sys/class/gpio/gpio139/direction 
之后就可以进行输出设置了。 

1 $ echo 1 > /sys/class/gpio/gpio139/value



==========================================================

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;

  }

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:152424次
    • 积分:2499
    • 等级:
    • 排名:第15262名
    • 原创:88篇
    • 转载:82篇
    • 译文:2篇
    • 评论:21条
    最新评论