Skip to content

Commit

Permalink
java gpio: commit, tested and running
Browse files Browse the repository at this point in the history
  • Loading branch information
myfreescalewebpage committed Sep 17, 2016
1 parent 1523775 commit 9fda2bf
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Layers
--

The following layers are available:
* meta-chip-example-java: JAVA OpenJDK with Hello World application.
* meta-chip-example-java: JAVA OpenJDK with Hello World and DIO applications to test the installation and show how to drive the hardware.
* meta-chip-example-1wire: usage of 1-Wire devices. Demonstrate how to easily patch the kernel with fragments and the device tree with patch files.
* meta-chip-example-spidev: usage of SPI interface. Second example to demonstrate how to patch the kernel with fragments and the device tree with patch files.

Expand Down
17 changes: 15 additions & 2 deletions meta-chip-example-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Images

The following images are available:
* chip-image-example-java-helloworld: image with JAVA and Hello World application to test the installation.
* chip-image-example-java-dio: image with JAVA and GPIO application to show how to drive the hardware from JAVA application.

The wanted image is chosen during the build with bitbake command.

Expand All @@ -36,12 +37,24 @@ Add "meta-chip-examples/meta-chip-example-java" and "meta-java" to the bitbake l

See the README file of the meta-chip layer (from my GitHub https://github.com/myfreescalewebpage/meta-chip) to check details about building and flashing images on the C.H.I.P. board.

After flashing the image on the C.H.I.P. board, the file "helloworld.jar" will be in "/home/root". Execute the following command:
### chip-image-example-java-helloworld

java -classpath /home/root -jar /home/root/helloworld.jar
After flashing the image on the C.H.I.P. board, the file "helloworld.jar" will be in "/usr/local/java". Execute the following command:

java -jar /usr/local/java/helloworld.jar

The text "Hello World!" in the console indicates that the application is correctly executed.

### chip-image-example-java-dio

After flashing the image on the C.H.I.P. board, the file "gpio.jar" will be in "/usr/local/java". Connect a LED throw a resitor on the pin PE5 (CSI-D1 on C.H.I.P.). Execute the following command:

java -Djava.security.policy=/home/root/java.policy -jar /usr/local/java/gpio.jar PE5

The LED is blinking several times and the application terminates.

Important note: the policy file is set to permit access to the hardware.


Contributing
--
Expand Down
66 changes: 66 additions & 0 deletions meta-chip-example-java/recipes-skeleton/gpio/files/gpio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dio.gpio;

import jdk.dio.*;
import jdk.dio.gpio.*;
import java.io.IOException;

public class gpio {

public static void main(String[] args) {

/* Check arguments */
if ((args.length != 1) || (args[0].length() < 3)) {
System.out.println("Expecting pin as arguement");
System.exit(0);
}

/* Convert to port and pin */
char port = args[0].toUpperCase().charAt(1);
int number = Integer.valueOf(args[0].substring(2), 10).intValue();
if ((port < 'A') || (port > 'Z') || (number < 0) || (number > 32)) {
System.out.println("Impossible to decode pin port and number");
System.exit(0);
}

/* Compute pin number: 32 * (port - 'A') + pin */
int pinNumber = 32 * (port - 'A') + number;

/* Start blinking */
System.out.println("Blinking LED on GPIO pin number " + pinNumber + " (" + args[0] + ")");

GPIOPin pin = null;

try {

GPIOPinConfig pinConfig = new GPIOPinConfig(DeviceConfig.DEFAULT, pinNumber, GPIOPinConfig.DIR_OUTPUT_ONLY, GPIOPinConfig.MODE_OUTPUT_PUSH_PULL, GPIOPinConfig.TRIGGER_NONE, false);
pin = (GPIOPin)DeviceManager.open(GPIOPin.class, pinConfig);

boolean pinOn = false;
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
pinOn = !pinOn;
pin.setValue(pinOn);
}

} catch (IOException ioe) {

System.out.println("IOException while opening device. Make sure you have the appropriate operating system permission to access GPIO devices");

} catch(InterruptedException ie) {

System.out.println("InterruptedException while blinking the LED");

} finally {

try {

System.out.println("Closing GPIO pin number " + pinNumber + " (" + args[0] + ")");
if (pin != null) pin.close();

} catch (Exception e) {

System.out.println("Received exception while trying to close device");
}
}
}
}
4 changes: 4 additions & 0 deletions meta-chip-example-java/recipes-skeleton/gpio/files/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Manifest-Version: 1.0
Main-Class: dio.gpio.gpio
Implementation-Version: 0.1
Class-Path: /usr/share/java/dio.jar
4 changes: 2 additions & 2 deletions meta-chip-example-java/recipes-skeleton/gpio/gpio_0.1.bb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUMMARY = "JAVA GPIOLEDSample OpenJdk DIO example"
SUMMARY = "JAVA gpio OpenJdk DIO example"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
Expand All @@ -11,7 +11,7 @@ RDEPENDS_${PN} = "openjdk-7-jre openjdk-dio"
PR = "r0"

SRC_URI = " \
file://GPIOLEDSample.java \
file://gpio.java \
file://manifest \
"

Expand Down

0 comments on commit 9fda2bf

Please sign in to comment.