Thursday Mar 21, 2013

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...

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
« March 2013 »
SunMonTueWedThuFriSat
     
1
2
3
5
6
9
10
11
13
15
16
17
19
20
22
23
24
25
26
27
29
30
31
      
Today