Android USB Host + USB VCP Driver [Part 3]

The Abstraction Layer

Once the prototype works and could establish a great communication between the Xoom and the FT232 IC, I decided to separate the project in order to let anyone creates a VCP Driver adding just a few methods.
The Abstraction Layer is present in the USB VCP Library for Android. It consists of 5 classes as you can see in the following picture:

VCPDriver

It is the main class. It represents the driver which lets the user connect and disconnect from the device. It also handles the low level connection and disconnection from the USB Device, and provides 5 abstract methods.

The user should call “connect” with a valid RS232 Configuration and a (may be null) object that implements the Async. Receveiver Interface. The “disconnect” method should be used in order to de-initialize variables but must call VCPDriver.closeDevice() in order to release all the USB stack.

 
“transfer” is a generic method that lets the user do synchronised transactions. The user can call this method in order to send and/or receive a package from the device.
“initialize” should be called from “connect” and will perform several controlTransfer commands in order to setup the device (including the RS232 configuration).

Finally, the AsyncReceiver is an interface with a very simple method. If your class implements this interface, you will receive async information from the driver.
As the VCPDriver has some abstract methods, all the class is abstract. So for every IC manufacturer, you should create a specialised Driver class, as I have created the FT232Driver class.
 
 

Android USB Host + USB VCP Driver [Part 2]

(Un)fortunately I have just signed a NDA (Non-Disclosure Agreement) with FTDI Chip in order to develop the Android version of their FT(2)232 chip.

This agreement was approved by FTDI so I cannot publish either the documentation I have just received, or the source code I develop based on that documentation.
I will go on writing about my experience developing the USB VCP driver for Android entirely on Java, and  may be as a result of the project an obfuscate binary will be published in order to communicate with the FT(2)232 family.
One more thing to mention: FTDI writes very clear and nice documentation.

Android USB Host + USB VCP Driver [Part 1]

I have connected a USB device that creates a USB VCP profile to my Motorola Xoom in order to enable my Xoom apps to interface with RS232 devices.
My small test program shows the following details for the USB device:

Model: /dev/bus/usb/002/002
Id: 2002
Class: 0
SubClass: 0
Protocol: 0
VendorId: 1659
ProductId: 8963
Number of Interfaces: 1
>> Interface 0 -> 3 Endpoints

With all the information we can tell the device to open a custom application when the USB device is attached. To do so, the following information should be added to the ‘activity’ tag in the Manifest.

<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/usb_device_filter" />

The XML file is a resource file that contains information about the USB Device, that should be placed in /res/xml

<resources>
<usb-device vendor-id="1659" product-id="8963" class="0" subclass="0"
 protocol="0"/>
</resources>

When the activity starts, it can retrieve the USB Device information by calling:

device = (UsbDevice)getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);

If the device is not null, then you can open an UsbInterface and get the UsbEndpoint in order to read and write information.

How Android sees an attached USB Device

In the long way that my project must walk, I decided to start testing some small things in order to understand how the Android USB Host API and Open Accessory API (two different things) work.
Basically the USB Host API lets your Android device acts as a computer. With this API you can connect from a Bluetooth USB adapter to any standard USB device that you can find in a store. The Open Accessory API works the other way, your Android acts as a device, so you have to connect a USB Host controller in the other side. I don’t find this second approach very useful. First because not all the microcontrollers in the world support USB Host (Remember, your Android is a Device for the Open Accessory API) Second because it does not allow seemless development of a product using a computer and a natural port to Android using the USB Host API. I still cannot find three nice examples of when I will prefer to use my Tablet as a USB Device, but I can find millions of application for my Tablet (and Phone) to be a USB Host.
So in this short post, I will show you how it a USB Device attached to a  Honeycomb-enabled device looks like. Take special attention to the fact that even though Android 2.x has an option USB toolkit, and the classes have the same name, they belong to a different package (so the imports are not the same).
The Motorola Xoom with Android 3.2 is USB Host capable, and even thought I connected a USB keyboard and it didn’t work in the OS, the following short example demonstrates that the Motorola Xoom detects a USB Bluetooth adapter.

When attaching the USB Bluetooth adapter the following message can be seen using “logcat” (connected via TCP as the USB port is busy with the adapter)



 

Debugging Android wirelessly

Recently I flashed my Motorola Xoom with Android 3.2 in order to start playing with USB Host API and Open Accessory API, but both functionalities are implemented on the unique USB port that the Xoom has. This is not a problem when you own a Dock, or you own a Acer A500 which has one USB host and one USB device. Debugging an Android application using abd can be done wirelessly over WiFi using the followings instructions.

When debugging applications that use USB accessory or host features, you most likely will have USB hardware connected to your Android-powered device. This will prevent you from having an adb connection to the Android-powered device via USB. You can still access adb over a network connection. To enable adb over a network connection:

  1. Connect the Android-powered device via USB to your computer.
  2. From your SDK platform-tools/ directory, enter adb tcpip 5555 at the command prompt.
  3. Enter adb connect <device-ip-address>:5555 You should now be connected to the Android-powered device and can issue the usual adb commands like adb logcat.
  4. To set your device to listen on USB, enter adb usb.

 

ARM7 Programming under Mac OS X: Part 1 – ISP

Programming on a Mac is quite similar as you do on a Linux PC, and developing for ARM7 is not the exception. In this first post I’ll show you how to setup the In System Programming feature of the LPC2138/01 in your Mac, and check that the MCU is communicating successfully with your Mac.
The first thing you have to download  is lpc21isp, this minimalistic piece of software is a programming tool that communicates with the ARM7 bootloader. You can find it at Yahoo Groups (http://tech.groups.yahoo.com/group/lpc21isp/). The lastest version (lpc21isp_180) is OS-X compatible.
Once you have it in your disk, unpack (yes, this is done automatically once downloaded) and open a Terminal. Locate the unpacked folder and type ‘make -f Makefile clean all’. This  should produce an output as you see in this screenshot

The result of the build process produces a file called “lpc21isp” which is executable. You have to invoce this file specifying the operations to perform, the serial port, the speed of the serial port, and the xtal frecuency in KHz.

For locating the serial port on a Mac, just look into /dev. In my enviroment I was using a custom USB-RS232 I have created a long time ago, which OS X identifies as /dev/tty.usbserial-A800cwuH

The following image shows how to invoke a detection and its result.

While lpc21isp says “Synchronizing” you should enter bootloader mode by holding P0.14 in low and performing a reset.

Last, but not least, here there are some pictures of the LPC2138/01 board and the Mac. The LPC2138/01 is mounted on an adapter I made, and near that adapter you can see the RS232 to USB adapter. Everything is powered with a 3.3V power supply.