diff --git a/README.md b/README.md index 3dcdb37..e66d5c4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/meta-chip-example-java/README.md b/meta-chip-example-java/README.md index ebf74ab..3b5dc0f 100644 --- a/meta-chip-example-java/README.md +++ b/meta-chip-example-java/README.md @@ -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. @@ -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 -- diff --git a/meta-chip-example-java/recipes-skeleton/gpio/files/gpio.java b/meta-chip-example-java/recipes-skeleton/gpio/files/gpio.java new file mode 100644 index 0000000..2444517 --- /dev/null +++ b/meta-chip-example-java/recipes-skeleton/gpio/files/gpio.java @@ -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"); + } + } + } +} diff --git a/meta-chip-example-java/recipes-skeleton/gpio/files/manifest b/meta-chip-example-java/recipes-skeleton/gpio/files/manifest new file mode 100644 index 0000000..143664b --- /dev/null +++ b/meta-chip-example-java/recipes-skeleton/gpio/files/manifest @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Main-Class: dio.gpio.gpio +Implementation-Version: 0.1 +Class-Path: /usr/share/java/dio.jar diff --git a/meta-chip-example-java/recipes-skeleton/gpio/gpio_0.1.bb b/meta-chip-example-java/recipes-skeleton/gpio/gpio_0.1.bb index 3e11dd1..93d1603 100644 --- a/meta-chip-example-java/recipes-skeleton/gpio/gpio_0.1.bb +++ b/meta-chip-example-java/recipes-skeleton/gpio/gpio_0.1.bb @@ -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" @@ -11,7 +11,7 @@ RDEPENDS_${PN} = "openjdk-7-jre openjdk-dio" PR = "r0" SRC_URI = " \ - file://GPIOLEDSample.java \ + file://gpio.java \ file://manifest \ "