Making Internet of Things (IoT) Smarter with Java Embedded

Thursday Mar 28, 2013

Well, the best laid plans of Raspberry Pi, Java Embedded and all that. There's some type of device access issue on the Raspberry Pi when trying to NIO memory map /dev/mem. So, I'm going to have take some time to debug that.

Instead let's go back to what we know works. Using pure Java Embedded API's we can use the file handle to get to the GPIO pins for access. So, let's roll back and start there again and incrementally move toward trying to match what we should be able to do with NIO.

Here's the code that we will roll back to. You can try this at home to see what type of speed you can get on your oscilloscope. I'll report what I have also.

Thursday Mar 21, 2013

One way to convert the C language GPIO access of /dev/mem is to use a Java NIO memory mapped buffer to open the Raspberry Pi memory as a device (with a Linux file descriptor), then get the file channel, then map it into the Java NIO accessible memory.

This is essentially the same process that Gordon Drogon is using in his WiringPi.c file. Instead of Java NIO, he is using the C and the Linux mmap() function to a file descriptor pointing to /dev/mem. So, our Java translation of that same functionality would look something like this.

Monday Mar 18, 2013

The next step in making Java Embedded just as fast as C native for Raspberry Pi GPIO pulse-wave modulation (PWM) is to match what C native does in the fastest programming case.

And, this means the place to look is in Gordon Drogon's canonical C native Wiring Pi library for the Arduino-compatible RPi GPIO. It's the best C code implementation currently out there, and it's what Pi4J uses underneath it's Java implementation.

What we see when we carefully read the wiringPi.c file is that Gordon is using C native memory mapping of "/dev/mem" to access the GPIO portion of the RPi memory map for the quickest access possible. That's awesome! That's a very simple way to get at the GPIO pins with high performance for both reading and writing. So, of course we can do the same in Java (without JNI) using an NIO memory mapped file/device for that same high performance (in theory). Cool!

Next, this needs to be translated from C to Java. We do this using NIO (New I/O) in Java SE Embedded. Note that NIO is not available in Java ME Embedded. Java SE Embedded has the full set of Java SE API's (including NIO) which is the crucial part of this exercise in order to get the same performance as C native.

The Call for Proposals is open. Have
something interesting to present to
the world’s largest gathering of
Oracle technologists and business
leaders? Making breakthrough
innovations with Java or MySQL? We
want to hear from you...

Let's see how many talks we can get this year on Java Embedded and the Raspberry Pi. Let me know if there are other Java Embedded areas that you'd like to see covered. You have until April 12, 2013. Don't delay...

Tuesday Mar 12, 2013

The first step in our investigation is to reproduce what Robert Savage saw in his benchmarking. See: Java GPIO Benchmarking

In his study, Robert Savage measured the Pi4J library on Oracle Java JDK 8 Preview for Linux/ARM yielding about 153 kHz on the Raspberry Pi GPIO output. So, following the scientific method, we should be able to reproduce the experiment.

And, with the following Java Embedded app on my RPi running with Oracle JDK 8 Preview for Linux/ARM and using my HP logic analyzer/oscilloscope I'm seeing about the same: 166 kHz. Click on the above photo to zoom in and look at the red circle area.

And, Robert reports he can get around 5 MHz using C native. So, that's our goal. In the next blog posts we'll explore ways to get our 166 kHz score on the benchmark a lot higher, hopefully matching C native with 5 MHz...

Friday Mar 08, 2013

You now have your PicoScope or your extravagant logic analyzer with oscilloscope, and you're ready to hook it up to your Raspberry Pi to measure your Java app. It's pretty straight-forward.

Attach your scope probe (refer to your documentation for your specific oscilloscope), then find an open GPIO pin that you'll use. I'm going to use GPIO24 which is Pin #18 on the header (see diagram below).

The easiest way to do this is to take a male/female jumper wire (See: link) and connect the female end to Pin #18 (GPIO24) and the male end to the probe hook (See: top photo).

Then connect the alligator clip on the probe (which needs to be connected to ground -) to anything metal on the Raspberry Pi that is grounded. One safe place that is grounded is the outer part of the USB port (See: top photo). That's where I clipped my probe alligator clip.

That's it. You're connected now to use your scope. Next we'll take a look at different Java Embedded apps to drive the GPIO24 pin and start to benchmark the different approaches...

Thursday Mar 07, 2013

So, the oscilloscope that Gary C. and Vlad D. found for our Java Embedded on Raspberry Pi analysis (see top photo) is actually a fancy-schmancy HP 16700A logic analyzer with an HP 16534A oscilloscope module -- which in plain English means it cost SavaJe beaucoup bucks to buy (4 G's back in 2001). And, I'm guessing that as a typical RPi hobbyist, you're not going to want to drop $4k on your project just to measure the frequency of a Java Embedded generated PWM wave. Am I right?

Thought so. Well, if you are interested in using an oscilloscope to play around with, there is hope. For example, here's a price-friendly PicoScope 2205A Oscilloscope.

This PicoScope only costs $275 at Allied Electronics, see PicoScope link. It does pretty much the same thing as the HP logic analyzer I have in my office, but at a much lower price... and with a lot less noise!

So, the first step for all you developers playing along at home is to beg, borrow, or buy an oscilloscope, or the PicoScope for a fraction of the price (if you don't have the luxury of using an HP logic analyzer from a previous high tech acquisition that is). Once you have that, come back to my future blog post and see how to hook it up to your Raspberry Pi for some GPIO benchmarking.

Monday Mar 04, 2013

Yes, through hard work and determination, well... actually through the luck of the donation of an old leftover logic analyzer with an oscilloscope module from the former SavaJe acquisition via Sun Microsystems, Inc., which is now here at Oracle, we now have a scope here in the office connected to our Raspberry Pi running Java Embedded technology! Woo-hoo!

I want to thank Gary C. and Vlad D. in their help tracking down this awesome (er, uh... a bit old) piece of electronic equipment. In a back storage closet somewhere in the darkest reaches of our Burlington, MA office, this logic analyzer with oscilloscope module from SavaJe days, sat collecting dust. But, luckily Gary remembered it was sitting there unused, when I asked if he knew if we had a scope in our group for some needed RPi/Java Embedded work. And, then Vlad and Barb helped ship it out here to California and the rest is history.

After dusting it off, plugging it in, and firing it up, it works!

What you see in the top photo is a computer screen grab of the oscilloscope output reading of the GPIO port signal from the Raspberry Pi with a Java Embedded app driving a Pulse Width Modulation (PWM) wave. And look, Ma, no native C programming involved!

We can go over this in detail in my future blog posts so that you can do this at home as well, as we benchmark how fast we can get Java Embedded to drive that GPIO pin signal (I'm hoping for > 5MHz, but what you see in the graph above is a signal driving a blinking LED, so don't get all hypercompetitive... yet). And, might as well compare it to C native, since everyone else does.