Use Java to control GPIO on Android

From BananaPro/Pi
Jump to: navigation, search

Other languages:
English • ‎中文(简体)‎

GPIO

This tutoral says how to use Java to control GPIO on Android.
Here is the 40Pin GPIO sequence number:

public int Guitar_port[] =          //BOARD MODE                
{
        -1,           // 0
        -1,     -1,   //1,            2
        131,    -1,   //3(SDA2),      4
        130,    -1,   //5(SCK2),      6
        50,     91,   //7(B18),       8(UART0_TX)
        -1,     90,   //9,           10(UART0_RX)
        64,     40,   //11(C0),      12(B8-PWM)
        65,    -1,    //13(C1),      14
        68,     25,   //15(C4),      16(A25)
        -1,     70,   //17,          18(B9)
        89,     -1,   //19(MISO),    20
        88,     69,   //21(MOSI),    22(C5)
        86,     87,   //23(SCLK),    24(B19)
        -1,     51,   //25,          26(CE0)
        48,     46,   //27(B16),     28(B14)
        47,     -1,   //29(B15),     30
        42,     45,   //31(B10),     32(B13)      
        32,     -1,   //33(B0),      34
        33,     28,   //35(B1),      36(A28)
        34,     31,   //37(B2),      38(A31)
        -1,     27,   //39,          40(A27)
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //41-> 55
        -1, -1, -1, -1, -1, -1, -1, -1 // 56-> 63
};

NOTE: -1 represents GPIO Port, there are 7 ordinaty IO port can be allocated on LeMaker Guitar, they are 7,11,13,15,16,18,22 in Guitar_port[].


General IO Port Application

You can calling the export interface to apply for the IO prot. The interface calling path:

/sys/class/gpio/export

The calling method:

writeSysfs("/sys/class/gpio/export",Integer.toString(Guitar_port[port[i]]));

Example:

for (int i = 0;i < 7;i++){
    writeSysfs("/sys/class/gpio/export",Integer.toString(Guitar_port[port[i]]));
    ShellUtils.execCommand("chmod 0777"+" /sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/direction", true);
    writeSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/direction","out");
    ShellUtils.execCommand("chmod 0777"+" /sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/value", true);
}


The Example Analysis:
General IO port application in a array:

int port[] = {7,11,13,15,16,18,22};

Use the export interface to apply IO port and create gpioX node at /sys/class/gpio.

writeSysfs("/sys/class/gpio/export",Integer.toString(Guitar_port[port[i]]));

Use super user permissions to set the 'direction' attribute under gpioX node to be readable, writable and executable.

ShellUtils.execCommand("chmod 0777"+" /sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/direction", true);

NOTE: When write true in the function ShellUtils.execCommand, it will use root to run a linux command.

Set the new gpioX node's direction as out:

writeSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/direction","out");

NOTE: When the state of direction is out/in, the gpioX node used as output/input function.

Use super user permissions to set the 'direction' attribute under gpioX node to be readable, writable and executable.

ShellUtils.execCommand("chmod 0777"+" /sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/value", true);

NOTE: When the gpioX node is used as output, we can change the logic level of GPIO port state by write 1/0 to vale.

Remove the General IO Port

You can calling the unexport interface to apply for the IO prot. The interface calling path:

/sys/class/gpio/unexport

Calling method:

writeSysfs("/sys/class/gpio/unexport",Integer.toString(Guitar_port[port[i]]));

Example:

public void unExport_port(){
    int port[] = {7,11,13,15,16,18,22};
    for (int i = 0;i < 7;i++){
        writeSysfs("/sys/class/gpio/unexport",Integer.toString(Guitar_port[port[i]]));
    }
}

Example Analysis:
Use the interface unexport to remove GPIO port, and remove the gpioX node at this path: /sys/class/gpio.

writeSysfs("/sys/class/gpio/unexport",Integer.toString(Guitar_port[port]));

General IO port to be output

Set the level state of GPIO.
The path:

/sys/class/gpio/gpioX/value

Example:

public void LedOpen(){
    int port[] = {7,11,13,15,16,18,22};
    for(int i = 0;i < 7;i++){
        writeSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/value","1");
    }
}

Example Analysis:
Set the value of gpioX node to be 1/0, the level state of the gpio pin would be high/low.

writeSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port[i]])+"/value","1");

General IO port to be input

The path:

/sys/class/gpio/gpioX/value

Example:

readSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port])+"/value")

Example Analysis:
Read the value of gpioX node to get the leval state of the GPIO pin.

public int getInputVal(int port){
    return Integer.parseInt(readSysfs("/sys/class/gpio/gpio"+Integer.toString(Guitar_port[port])+"/value"));
}

Code for reference:
SimpleGPIO

SPI

40 Pin SPI sequence number:

        89,     -1,   //19(MISO),    20
        88,     69,   //21(MOSI),    22(C5)
        86,     87,   //23(SCLK),    24(B19)
        -1,     51,   //25,          26(CE0)

Load Static Library of SPI

Statement and Load static Library in Class com.lmk.hardware

static {
        try {
                System.loadLibrary("lmkhardwarejni");
        } catch (UnsatisfiedLinkError e) {
                Log.d(TAG, "lmkhardware library not found!");
        }
    }

Statement the Interface in Class com.lmk.hardware

Set bits of SPI:

setSPIBits(int fd, int Bits)

Get bits of SPI:

getSPIBits(int fd)

Set read/write speed of SPI:

setSPIMaxSpeed(int fd, int speed)

Set read/write mode of SPI:

setSPIMode(int fd, int mode)

Send SPI messages:

transferArray(int fd, int[] writebuf,int delay, int speed, int bits)

Use the Interface of SPI

Satement the Interface Function of SPI in Activity

import com.lmk.hardware;

NOTE: You have to use root to open file sidev*.* .

Get the File Descriptor of spidev

fd0 = hardware.open("/dev/spidev0.0",hardware.O_RDWR);

Send Messages to spidev Devices

public int[] at45BufReadI(){
        int rBuf[] = {0xd4, 0xff, 0x00, 0x02, 0xff, 0xff}; // read data from buffer
        return hardware.transferArray(fd0,rBuf,0,5000000,8);
}

Code for reference:
SimpleSPI

I2C

40 Pin I2C sequence number:

        131,     -1,   //3(SDA2),    4
        130,     -1,   //5(SCK2),    6

Load Static Library of I2C

Statement and Load static Library in Class com.lmk.hardware

static {
        try {
                System.loadLibrary("lmkhardwarejni");
        } catch (UnsatisfiedLinkError e) {
                Log.d(TAG, "lmkhardware library not found!");
        }
    }

Statement the Interface in Class com.lmk.hardware

Set I2C slaver address:

setI2CSlaveAddr(int fd, int addr, int force)

Set I2C slaver timeout:

setI2CTimeout(int fd, int timeout)

Set I2C slaver retries times:

setI2CRetries(int fd, int retries)

Set I2C slaver read/write check:

I2CCheck(int fd, int size)

Read a byte from I2C:

I2CReadByte(int fd, int reg)

Write a byte from I2C:

I2CWriteByte(int fd, int reg, int data)

Use the Interface of I2C

Satement the Interface Function of I2C in Activity

import com.lmk.hardware;

NOTE: You have to use root to open i2c-dev.

Get the File Descriptor of i2c-dev

fd0 = hardware.open("/dev/i2c-dev",hardware.O_RDWR);

Send a Byte to a I2C Device

hardware.I2CWriteByte(fd,0x200,0x01)//Write data 0x01 to register 0x200 of I2C.

Read a Byte from a I2C Device

regvalue = hardware.I2CReadByte (fd,0x200)//Read a  data from register 0x200 of I2C.

Code for reference:
SimpleI2C

UART

Load Static Library of UART

/*Baudrate Speedː115200,9600,4800,2400
Data bitsː8、7
Checkbitː'N'、'E'、'O'----->Note,Even,Odɖ
Stop bitː1,2*/

Set UART mode

static public native  FileDescriptor setUartMode(int fd,  int nSpeed, int nBits, int nEvent, int nStop);.

Send string

public static void UartSendSring(FileDescriptor mFd,String sendSring) {

Receive Char

public static char UartReceiveChar() {

Use the Interface of UART

    fd0 = hardware.open("/dev/ttyS0",hardware.O_RDWR);//open ttyS0
    mFd = hardware.setUartMode(fd0,115200,8,'N',1); //Set the relevant parameters, the default can be
    hardware.UartSendSring(mFd,"123654");//Send string"123654"
    char value = hardware.UartReceiveChar();//Character, it is best to read through the thread

Code for reference:
SimpleUART