RPi with an Oscilloscope and Java Embedded: Ready for Benchmarking (Part 5)

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.

/*
 * Example Code
 */

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

//...

  RandomAccessFile fileHandle = new 
    RandomAccessFile("/dev/mem", "rw");

  FileChannel fileChannel = fileHandle.getChannel();

  MappedByteBuffer gpioMem = fileChannel.map(
    FileChannel.MapMode.READ_WRITE, BCM2708_PERI_BASE + 0x200000, 
    PAGE_SIZE);

So, we have our Java code. Let's take a look if this works on the Raspberry Pi with our Java Embedded stack...

Comments:

I can't wait any longer - stop teasing us!!!

I want to see how to control I/O from Java!

Posted by guest on March 24, 2013 at 06:28 PM PDT #

Oooh. I can't wait for this on. I'm writing the GPIOFilePin for the Armature project right now and using standard JDK IO. Getting sample code for using NIO would be fantastic.

Posted by Ranx on March 27, 2013 at 02:42 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today