Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

Telit Cinterion IoT Developer Community

ConceptBoard: Java GPIO lib

Tutorial, April 23, 2014 - 8:17pm, 11778 views

Using external GPIOs with JavaME - A library for Gemalto ConceptBoard and Gemalto Luna2 Terminal

Introduction

The external GPIOs on the Gemalto Concept Board and the Gemalto Luna2 Terminal operate at a different logical level (5 volts) than the Cinterion Module’s GPIO pins (1.8 volts). The level conversion is performed by directional level shifters. To switch the direction, the module sends I2C commands to a co-processor, controlling the level shifter directions.

In order to transparently use the external GPIOs in JavaME code from within the Module, you can use our GPIO library. It takes care of properly setting the level shifter directions.

Prerequisites

  • Gemalto EHSx Software Development Kit
  • Concept Board or Luna2 Terminal
  • GPIOlib

GPIOlib files

  • Gpio5V_Factory.java: Hardware dependent factory to create instances of the In- and OutPort5V
  • InPort5V.java: Encapsulation of InPort class that automatically adjusts the level shifter directions
  • OutPort5V.java: Encapsulation of OutPort class that automatically adjusts the level shifter directions

Create demo application

First create a new JavaME Project and import the GPIOlib-files ( Gpio5V_Factory.java, InPort5V.java, OutPort5V.java)

We then create our main MIDlet (e.g.  “Test_Routine.java”).

 import gpio.GPIO5V_Factory;
import gpio.InPort5V;
import gpio.OutPort5V;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.cinterion.io.I2cBusConnection;

public class Test_routine extends MIDlet {

  public Test_routine() {
    // TODO Auto-generated constructor stub
  }

  protected void destroyApp(boolean unconditional)
      throws MIDletStateChangeException {
    notifyDestroyed();
  }

  protected void pauseApp() {
    // TODO Auto-generated method stub
  }

  protected void startApp() throws MIDletStateChangeException {
      
  }
  }
} 

The next step is to create a simple test routine.

  private void test() {
    InPort5V inPort = null;
    OutPort5V outPort = null;
    GPIO5V_Factory factory = new GPIO5V_Factory(GPIO5V_Factory.CONCEPT); 

We create a GPIO5V_Factory for either the Luna2 Terminal or the ConceptBoard. In the next step, this factory is used to instantiate InPort5V and OutPort5V objects.

 try {
      int counter = 0;
      Vector outPins = new Vector(1);
      Vector values = new Vector(1);
      outPins.addElement("GPIO8");
      values.addElement(Integer.valueOf("0"));
      outPort = factory.getOutPort5V(outPins, values);
     
      
      
      try {
		Thread.sleep(1000);
	} catch (InterruptedException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}
      
      
      
      Vector inPins = new Vector(1);
      inPins.addElement("GPIO7");
      inPort = factory.getInPort5V(inPins);
    
      while (counter < 4) {
        System.out.print(counter + "; OutPort: " + counter % 2);
        try {
          outPort.setValue(counter);
        } catch (IOException e1) {
          e1.printStackTrace();
        }
        try {
          System.out.println("; InPort: " + inPort.getValue());
        } catch (IOException e) {
          e.printStackTrace();
        }
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        counter = 1 + counter;
      }
      outPort.release();
      inPort.release();
    } catch (IOException e) {
      e.printStackTrace();
    } 

First, two vectors are defined and filled with one element each. These are the pin id and the initial value (0=low, high=1) for the OutPort to be created by the factory. In order to ensure that the I2C commands to adjust the level shifters were executed by the co-processor, we sleep for 1 second before setting up the input ports.

For the input port we only define the pin id and no initial state.

The while loop repeats four ***** following steps:

  1.  Print out the current cycle + the value the pin will be set to (counter %2)
  2. Set the output pin to the value of the counters LSB
  3. Print out the current input value of the defined input pin.
  4. Sleep
  5. Increment counter

Finally the previously used ports are released.

The last Step to launch our test routine from within the startApp() method.

 protected void startApp() throws MIDletStateChangeException {
      test();
    destroyApp(true);
  } 

Run demo application

Now you just have to load the compiled program to your board and start the application. You will see GPIO6 being toggled and GPIO7 being read out every two seconds.

Further information

The library and the test routine are attached to this post. For further information please refer to our Java Documentation.

Please feel free to share your experience with the GPIOlib in the developer community.

I just tried the library and works like a charm.

But I still have a question. What if we want to set a GPIO to 1 until we disable it?

Is there any AT command or piece of code to do so?

I've implemented a solution using ATCommand:

 public static void on(final int num){
		ATCommand atc = null;
		try {
			atc = new ATCommand(false);
			atc.send("at^scpin=0,"+(num-1)+",0,0 \r");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				atc.release();
				System.out.println("GPIO"+num+" on");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}	 
	}   

Hi,

You could re-write your on() method as below:

Note: I have released the GPIO resource for cleanliness in on(). You must at least release it by the time you get to destroyApp() else it will be blocked until the next time the module restarts.

Hi,

We have released an updated version of the library with comments and documentation references.

We also implemented the on() example above using the library.

I do hope this helps :)

Hi,

we are using GPIO pins 7 and 8 .

Its working on EHSx module with our board

and working with concept board B80 and BGS5

but not working with our board and BGS5

its resetting module.

can u plz suggest on this,

Does the code works if I'm using the EHS6 with the Starter KIT B80?

Worked with the Concept Board (EHS6). It's suposed to work also with the Starter Kit

Hi,

With the B80 starter kit there is no need for this code.

It is only needed on (i) the EHSx Concept board or (ii) the EHSxT terminals where we have I2C controlled level convertors to protect the module's GPIOs from external devices/equipment.

Best regards

Simon

If you would like examples of "bare" GPIO usage then have a look at either the "Windmill" demo or "X-Nucleo" (with SensorLogic) demo and their (input) ButtonHandler.java and (output) GPIOHandler.java classes.

Best regards

Simon

Now updated to allow EHSx Terminals to output digital voice over the PCM/DAI interface.

Author

Paul Mock's picture
Paul Mock

Contributors