Tuesday, August 17, 2010

Enabling the Serial Port on a Nexus One

The Nexus One USB port works as a serial port when there's no power coming into the connector. But the stock kernel sets it up as a console. To use it as a general-purpose serial port, you have to use a kernel with CONFIG_USB_ANDROID_ACM enabled and CONFIG_MSM_SERIAL_DEBUGGER disabled.

If you want to skip installing the NDK, downloading and compiling the kernel, you can try this precompiled zImage.

We did this on a rooted Nexus One with an unlocked bootloader running Cyanogen CM6. It's probably not necessary to be running Cyanogen for this to work.

Download NDK from here:
http://developer.android.com/sdk/ndk/index.html

Install NDK using instructions from the website.

Make sure arm-eabi-gcc is in your path. It's in android-ndk-r4b/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin.
(export PATH=$PATH:/path/to/android-ndk-r4b/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin to add it)


Make sure your machine has git: $ sudo apt-get install git-core
$ git clone git://android.git.kernel.org/kernel/msm.git
$ git branch -a
$ git checkout -b android-msm-2.6.32 origin/android-msm-2.6.32
$ cd msm
$ ARCH=arm CROSS_COMPILE=arm-eabi- make mahimahi_defconfig
$ ARCH=arm CROSS_COMPILE=arm-eabi- make menuconfig
Exit the menu and say "yes" when it asks if you want to save.

$ gedit .config
Turn on CONFIG_USB_ANDROID_ACM (uncomment it and change "is not set" for "=y").
Turn off CONFIG_MSM_SERIAL_DEBUGGER (comment it out)
Turn off CONFIG_MSM_FIQ_SUPPORT
$ ARCH=arm CROSS_COMPILE=arm-eabi- make


The kernel image should compile and is found in arch/arm/boot/zImage

Try kernel without permanently storing it on the phone. Download fastboot from here:
http://developer.htc.com/adp.html

Reboot into fastboot mode. Power up the Nexus One while holding the trackball. Plug in the USB cable and the fastboot screen should say "fastboot usb".
$ sudo ./fastboot boot /path/to/msm/arch/arm/boot/zImage


If you get this message, then you probably forgot to run as root.
sm/arch/arm/boot/zImage
creating boot image...
creating boot image - 2232320 bytes
< waiting for device >


Once the phone boots up, use adb shell to ls /dev/tty* and if you see dev/ttyMSM0, it worked. (We haven't tried actually using the port yet).

When you reboot you'll be back into your old kernel. Haven't flashed this for real yet.

Check out the xda-developers.com thread linked below for more info on actually using the serial port.

The following sites were helpful:
http://androidboss.com/custom-linux-kernel-for-nexus-one/
http://forum.xda-developers.com/archive/index.php/t-625434.html

6 comments:

  1. Hi,

    Great thanks to your tutorial about enabling the serial port on nexus one. I've followed your tutorial and was able to see ttyMSM0 in /dev. However, when I use serialport (http://code.google.com/p/android-serialport-api/downloads/detail?name=SerialPort_0.7.apk), I was told the app cannot open ttyMSM0. I wonder if you have been able to use ttyMSM0 with some applications. Thank you very much!

    ReplyDelete
  2. Glad it was useful to you!

    I found that ttyMSM0 defaults to read-only and it's reset every time you boot up. I have a blog post here on how to make ttyMSM0 available to everything. I made it read-write for all user levels, which included apps, and that fixed the problem for me. I hope it works for you!

    ReplyDelete
  3. Hi Colia,What kind cable do you use to get the serial port? Did you refer to sven's method http://sven.killig.de/android/N1/serial/
    or any other hardware modification?

    Thanks

    ReplyDelete
  4. Hi Liang,

    I wrote this post a while ago, but at the time, I believe we were using a USB to serial cord (USB from the phone to serial on the. . .arduino?).

    ReplyDelete
  5. Hi,
    I am having a Android box having USB host and i wan to connect my box with Sharp TV via RS232 serial port. I want to build one APK which will handles all TV controls including changing the TV channels. Can u please help me on this please. How can i communicate android box and Television.
    Thanks.
    SAM

    ReplyDelete
  6. If you have a USB to serial adapter, then this post can help you get the phone to "speak" serial through the USB port. As for the rest, I bet you could write a script to do that using the sl4a app (though I'm not sure if it's being well maintained). It's probably easier to find/write an app in java and get that installed on the phone. Best of luck with it!

    ReplyDelete