Setting a Pin MUX from Java

Hello everyone,

I am working on building some java objects to help simplify my communication with hardware attached my BeagleBone. One such class is meant to help setup the pin mux for the project. Focusing currently on opening a GPIO line on P8/Pin 3. In my proof of concept code I have tried two ways todo this:

  1. All java. FileOutputStream and File objects.

`
byte muxValue = 0;

// direction = input (bit 32)
// mode = 7 (bit 1,2,4)
// pull up/down = down (bit 16)
// pull active = true (bit 8)
// binary result would be = 010111 or 00010111 (with extra leading zeros that aren’t used for anything)
muxValue = 0x17;

// todo: figure out why I can’t write to the file from java
File gpioMuxFile = new File("/sys/kernel/debug/omap_mux/gpmc_ad6");
FileOutputStream muxOut = new FileOutputStream(gpioMuxFile);

muxOut.write(muxValue);
muxOut.close();

`

Which results in a stack trace on the line

muxOut.write(muxValue);

Exception in thread "main" java.io.IOException: Invalid argument at java.io.FileOutputStream.write(Native Method) at com.greggharrington.flyingtoaster.poc.GPIOPOC.main(GPIOPOC.java:27)

Very confusing exception. I did some searching, but nothing came up of use.

The second way I tried to handle this was by use the command line method which I have tested by hand and confirmed works.

`
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(“echo 0x17 > /sys/kernel/debug/omap_mux/gpmc_ad6”);

// wait for the command to return, don’t care about the result for now
int result = pr.waitFor();

System.out.println("setting the mux result - " + result);
`

Which results in the omap_mux settings not changing.

If I cat the gpmc_ad6 file after either of these methods I see the following:

root@beaglebone:/sys/kernel/debug/omap_mux# cat /sys/kernel/debug/omap_mux/gpmc_ad6 name: gpmc_ad6.gpmc_ad6 (0x44e10818/0x818 = 0x0000), b NA, t NA mode: OMAP_PIN_OUTPUT | OMAP_MUX_MODE0 signals: gpmc_ad6 | mmc1_dat6 | NA | NA | NA | NA | NA | gpio1_6

However if I run the command used above

echo 0x17 > /sys/kernel/debug/omap_mux/gpmc_ad6

It works and another cat will show that it has changed the settings.

root@beaglebone:/sys/kernel/debug/omap_mux# echo 0x17 > /sys/kernel/debug/omap_mux/gpmc_ad6 root@beaglebone:/sys/kernel/debug/omap_mux# cat /sys/kernel/debug/omap_mux/gpmc_ad6 name: gpmc_ad6.gpio1_6 (0x44e10818/0x818 = 0x0017), b NA, t NA mode: OMAP_PIN_OUTPUT | OMAP_MUX_MODE7 signals: gpmc_ad6 | mmc1_dat6 | NA | NA | NA | NA | NA | gpio1_6

Final notes:

  • I am running all commands as root and the JVM as root.
  • I can connect to and adjust other files correctly when it comes the GPIO process. For instance, no problems writing to /sys/class/gpio/export to open or /sys/class/gpio/unexport to close the GPIO lines.
  • I have confirmed my hardware is working by running commands on the command line and testing the output of the lines. Everything worked perfectly that way.
  • I have confirmed java can write to various files to control the GPIO lines once the mux is setup correctly on the command line.
  • I tried using a muxOut.write(byte[]) method and the muxOut.write(int) versions too.
    Thanks for help everyone. I will post final working code once I have it working.

Cheers,

Gregg

Try to change

byte muxValue=0x17;

to
String muxValue=“0x17”;

You need to write to the mux file a string with hexadecimal representation of the mux value, not a single byte with the value.

j.

``

``

Genius!!! Worked great! Thanks J!

Ok, full code coming soon, in the mean time this worked great:

`
File gpioMuxFile = new File("/sys/kernel/debug/omap_mux/gpmc_ad6");
FileOutputStream muxOut = new FileOutputStream(gpioMuxFile);

muxOut.write(“0x17”.getBytes());
muxOut.close();
`

Cheers,

Gregg