1

I am attempting to communicate with a Pi header using Android Things Developer Preview 5. Below is the class I have created to communicate with the header as per the official Android Things documentation:

public class UartComm {
private static final String UART_DEVICE_NAME = "UART1";
private UartDevice mDevice;

private void configureUartFrame(UartDevice uart) throws IOException {
    // Configure the UART port
    uart.setBaudrate(115200);
}

public void onCreate() {
    try {
        PeripheralManagerService manager = new PeripheralManagerService();
        List<String> deviceList = manager.getUartDeviceList();
        if (deviceList.isEmpty()) {
            Log.i(TAG, "No UART port available on this device.");
        } else {
            Log.i(TAG, "List of available devices: " + deviceList);
        }
        mDevice = manager.openUartDevice(UART_DEVICE_NAME);
        configureUartFrame(mDevice);
        mDevice.registerUartDeviceCallback(mUartCallback);
    } catch (Exception e) {
        Log.w(TAG, "Unable to access UART device", e);
    }
}

public void readUartBuffer(UartDevice uart) throws IOException {
    // Maximum amount of data to read at one time
    final int maxCount = 40;
    byte[] buffer = new byte[maxCount];

    uart.read(buffer, maxCount);
    String data = new String(buffer, "UTF-8");

    Log.d(TAG, data);
}

private UartDeviceCallback mUartCallback = new UartDeviceCallback() {
    @Override
    public boolean onUartDeviceDataAvailable(UartDevice uart) {
        // Read available data from the UART device
        try {
            readUartBuffer(uart);
        } catch (IOException e) {
            Log.w(TAG, "Unable to access UART device", e);
        }

        // Continue listening for more interrupts
        return true;
    }

    @Override
    public void onUartDeviceError(UartDevice uart, int error) {
        Log.w(TAG, uart + ": Error event " + error);
    }
};
}

In my MainActivity I create an instance of UartComm by doing UartComm device = new UartComm() and the proceed to call device.onCreate()

I have also modified /boot/cmdline.txt and removed the console=serial0,115200 and replaced it with console=tty0, I have also tried just removing the console line without adding console=tty0. In /boot/config.txt I have also removed enable_uart=1 and core-freq=400 and also added dtoverlay=pi3-miniuart-bt I have also tried to remove Bluetooth support altogether by doing dtoverlay=pi3-disable-bt to no avail.

I have tested that the header works and is configured correctly in Rapsbian, where I swapped /dev/ttyAMA0 and /dev/ttyS0 and it worked correctly. I was able to run the screen command on Raspbian with a default baud rate of 115200 and was able to get the desired information.

I would like to do the same in Android Things Developer Preview 5 and have the Bluetooth run over the mini-uart ttyS0 and the header run over ttyAMA0. My desired result is for the header to be accessible over UART0.

An older USB serial device that has the same functionality works, but I would prefer the UART device be physically on top of the Pi, so that is not an option.

1 Answers1

1

Might be wrong but shouldn't:

private static final String UART_DEVICE_NAME = "UART1";

be UART0 i.e.

private static final String UART_DEVICE_NAME = "UART0";

I did a UART example here https://github.com/blundell/androidthings-uart/blob/master/app/src/main/java/com/blundell/tut/MainActivity.java (obviously different hardware) But it's connected to raspberry pi pins in the same way like so:

enter image description here

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks for the reply after changing the variable to UART0 I get this error: Unable to access UART device com.google.android.things.pio.PioException: android.os.ServiceSpecificException: BCM14 failed to apply the required pin mux: Invalid argument (code 22) – Nisarga Patel Aug 16 '17 at 19:43
  • Also I see you are using UART0, did you have to modify /boot/config.txt or /boot/cmdline.txt in any way to get that to work? – Nisarga Patel Aug 16 '17 at 20:05
  • yes I did, edited it as explained in the official docs. But now if you use the new AndroidThings `0.5.0` you don't have to with Pin Multiplexing : https://developer.android.com/things/hardware/raspberrypi-mode-matrix.html – Blundell Aug 16 '17 at 20:15
  • Thank you very much! I flashed a new image of Android 0.5.0 and loaded everything up as normal and it worked out of the box. I'm also trying to run a Bluetooth Gatt server do you know if that would interfere with UART0 would I have to disable bluetooth or is there a way I can run it in Miniuart mode? – Nisarga Patel Aug 16 '17 at 20:36
  • You need to open MINIUART instead of UART0 if you need to use Bluetooth at the same time. There are limitations to using this port that may affect your application. They're described in more detail in the RPi documentation: https://www.raspberrypi.org/documentation/configuration/uart.md – devunwired Aug 16 '17 at 21:49