Android Open Accessory bottom up with Nexus 4

OHCI devised from NXP USB hostlite.

Introduction

I am very interested in the idea of Android Open Accessory. But I don't like the online compiler and all the embedded libraries. Maybe I would like to use the libraries later but not now. Now I just want to program from the bottom up to realize the AOA function. I almost finished it, and will gradually record every steps here.

Setup

  • Compiler
  • Editor
  • Flash Download
  • Board
  • CMSIS
  • NXP USB hostlite for USB driver example
  • Android side APK

Compiler I'm using is YAGARTO v2.23.1. It supports both Win 7 and Mac OS. I didn't use the newlib to shrink code size. YAGARTO is actually sealed GNU ARM. It works well! And above all it's free. ARM Cortex-M3 supported of course.

EditorEmacs + Etags to read code, browsing, and edit everything. So you don't need SourceInsight or Eclipse any more. It support Win 7 and Mac OS also.

Flash Download Flashmagic, Win7 and Mac OS supported. It's free. But the MAC version won't work on my Mac Air, so my solution is a Win XP virtual machine.

Board I have bought an mBed board, but because I need a USB Host controller in this project so I bought another LPC1768 board with USB host socket. It just saved me a lot of time in debugging hardware wiring. The name of the board is "Open1768", made by "waveshare" , a company from China. Cost is 188 RMB, which is about 30 US $. Cheaper, huh?

CMSIS Yeah, you must use this lib for Cortex-M3. Very convenient for I/O or register reference. Version 2.01. This version is not the latest.

USB host stack example I use NXP hostlite for example. For a newbie like me, the program is short and readable. You can advise it to your need easily.

Android side APK Yeah, you do need an APK on the android phone. So NXP 0.98 USB library includes a full APK source code, I read it and devise it. What I need is some button on both sides. That's enough.

About Android Accessory Mode

VID & PID Initially it's 18D1(VID) and 4EE2(PID) on my Nexus 4. According to AOA spec v1.0, you need to 1st read AOA protocol, which return "0x2000" in my case; 2nd send 6 "identifying strings"; 3rd set Accessory mode and reenumerate. Because I don't know I need to do software USB - reset before send any USB command, I stuck in this step for several days.After resetting, you read device-descriptor agian, now it's correct 18D1(VID) and 2D00(PID), just like what the specs says. Of course I didn't open USB debugging, some notes on stackoverflow says they need to open USB debugging to enter Accessory Mode, but not in my case.

Program

OHCI is a protocol to hide lower layer SIE details. You realize USB packet setup and in/out by manipulating with certain registers. I do spend a lot of time in reading USB protocol to understand every bit in the packet setting . "USB made simple" is a great tutorial to start and be familiar. Salute!

Bulk transfer is the USB transfer type used by AOA. The theoretical maximum transfer speed is to be 64KB/s, i.e. 512kbps. Enough , huh? 1ms per frame, 64Bytes per frame. It's used for bi-direction data transfer, and music transfer in AOA v2.0. Isochronous transfer is not used yet.

Poll Message First time I saw "poll message" every 100 ms, I'm very strange. Later I found, a bulk-read from host accessory will stuck until it read something. That's the reason why the APK has to feed the accessory host. Another solution would be the accessory to query handset for any update message. In the later case, host accessory send a command to handset and get the corresponding answer.

UI interface

/media/uploads/spike/aoa1.png

Summary

I think I'm almost done with this small project. I press the button on the phone, 2 LEDs on the board will be turned on. I press the key on the board , the color of a text on the phone will change. Now I have to solve the problem of key button bouncing. The phone get several commands when I press once.

I also tried with galaxy S3, it works. But the endpoint number is definitely different from Nexus 4.

Future Work

Optimize the code organization.

Think about a light-weight OS, then maybe a ready OHCI stack for the whole work.

I will move back to mbed board, and try to learn it's versatile libraries.

What can be made out of Android Accessory?

  • a weight meter interface, you can read your weight and store it in the cloud using your own android phone. It's my first thought.
  • a health care appliance, thermometer or something else, you can store your body data with your android phone.


1 comment on Android Open Accessory bottom up with Nexus 4:

20 Mar 2014

Hello! I have a idea to control LEDs, motor via Internet with an application on Android. I also want to receive some values from my mbed (temperature, humidity,...) Can you help me? What I have to code on my mbed, android mobile phone?

Please log in to post comments.