// Copyright 2015 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland // www.source-code.biz, www.inventec.ch/chdh // // This module is multi-licensed and may be used under the terms of any of the following licenses: // // LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html // EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal // // Please contact the author if you need another license. // This module is provided "as is", without warranties of any kind. // // Home page: http://www.source-code.biz/snippets/java/SerialPortChannel package biz.source_code.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * A serial port channel channel for BeagleBone. */ public class SerialPortChannelBeagleBone extends SerialPortChannel { private static final String portNamePrefix = "/dev/ttyO"; public static boolean isPortAccessible (String portNo) { File file = new File(portNamePrefix + portNo); return file.exists() && file.canRead() && file.canWrite(); } /** * Creates a serial port channel. * * @param portNo * The serial port number, e.g. "2" (for "/dev/ttyO2"). * @param baudRate * The baud rate for the serial port, e.g. 9600. */ public SerialPortChannelBeagleBone (String portNo, int baudRate) throws IOException { super(portNamePrefix + portNo, baudRate); try { setBeagleBoneRegisters(portNo); } catch (Exception e) { System.err.println("Warning: Unable to set BeagleBone registers. " + e); }} // Sets registers of the AM335X processor to configure the serial port i/o pins. // This is only necessary for Android distributions where the serial ports are not already pre-configured. // It may also be necessary to change the file system permissions, e.g. in init.rc: // chmod 666 /dev/ttyO1 // chmod 666 /dev/ttyO2 // chmod 666 /dev/ttyO4 // chmod 666 /dev/ttyO5 // chmod 222 /sys/kernel/debug/omap_mux/uart1_rxd // chmod 222 /sys/kernel/debug/omap_mux/uart1_txd // chmod 222 /sys/kernel/debug/omap_mux/spi0_sclk // chmod 222 /sys/kernel/debug/omap_mux/spi0_d0 // chmod 222 /sys/kernel/debug/omap_mux/gpmc_wait0 // chmod 222 /sys/kernel/debug/omap_mux/gpmc_wpn // chmod 222 /sys/kernel/debug/omap_mux/lcd_data9 // chmod 222 /sys/kernel/debug/omap_mux/lcd_data8 private static void setBeagleBoneRegisters (String channel) throws IOException { if (channel.equals("1")) { writeReg("uart1_rxd", 20); writeReg("uart1_txd", 0); } else if (channel.equals("2")) { writeReg("spi0_sclk", 21); writeReg("spi0_d0", 1); } else if (channel.equals("4")) { writeReg("gpmc_wait0", 26); writeReg("gpmc_wpn", 6); } else if (channel.equals("5")) { writeReg("lcd_data9", 24); writeReg("lcd_data8", 4); } else { throw new IOException("Unsupported BeagleBone serial port channel " + channel + "."); }} private static void writeReg (String regName, int regValue) throws IOException { FileOutputStream f = null; try { f = new FileOutputStream("/sys/kernel/debug/omap_mux/" + regName); f.write(Integer.toString(regValue).getBytes()); f.flush(); } finally { if (f != null) { f.close(); }}} }