Monthly Archive for September, 2008

Identifying ttyUSB ports in python

While preparing a NSLU2 for phase 2 of SMSAGE with Ralph I’ve come across one really annoying issue.. It doesnt seem deterministic which order USB devices are enumerated, so if you have two USB to serial adapters, ttyUSB0 and ttyUSB1, they may switch names after a reboot!

This python script identifies them for you. (angstrom openembedded).

Run it directly:
python identty.py

or:
import identty
ports=identty.identify()

Source:

import os

def identify():
   ports=[]
   o=os.popen(’cat /proc/tty/driver/usbserial’)
   p=o.read()
   q=p.split(’\n’)
   for i in range(len(q)):
      r=q[i].partition(’”‘)[2].partition(’”‘)[0]
      if len(r):
         ports.append(['/dev/ttyUSB'+str(i-1),r])
   return ports

if __name__==”__main__”:
   p=identify()
   for i in p:
      print i[0]+” : “+i[1]

Auto power management for the Angstrom NSLU2

For the SMSAGE project, Ralph and I have been equipping a security camera housing with a NSLU2 ‘netslug’ running from a lead-acid battery and a solar charger.

In order to conserve power, the installation will run on a ‘duty cycle’ of around 20% of the day.



I followed the instructions here and modded the NSLU2 to connect its RTC chip to its power-on circuit, it also necessitated patching the driver for the x-1205 timer chip to allow the rtcalarm utility to set a wake-up alarm.

Here is the source to the adapted RTC driver which works in the latest angstrom openembedded (2007.12, kernel 2.6.21.7)

Shell commands can now be used to set a wake-up time and poweroff the system, for instance:


rtcalarm -s 300
poweroff

Will turn the slug off for 5 minutes.

As recommended here I also blacklisted /dev/sda1 from being automatically mounted, as it also the root filing system. The poweroff command, even when executed from a running python script, seems to do a good job of closing the ’slug down cleanly, however having the root filing system mounted twice seems to be too much and the card is being detected by Ubuntu as being ‘dirty’: which can’t be a good thing in the long run.

Mounting a CF card as root for gumstix openembedded

A small technical note/ mnemonic, CF-root configuration in openembedded for gumstix is a little tricky / underdocumented.

Enable CF-root:

uncomment MACHINE_FEATURES += “cfroot” in
openembedded/com.gumstix.collection/conf/machine/YOURMACHINE.conf

and build the kernel & fs, flash the gumstix as normal.

Prepare your CF card.. create a minimum sized FAT16 partition and format the rest of the card as EXT2



This is how it should look in gparted:

copy openembedded/extras/cf-root/gumstix-factory.script to the FAT16 partition:
also copy your new kernel image as ‘uImage’
i.e. cp openembedded/tmp/deploy/glibc/images/gumstix-custom-connex/uImage-2.6.21-r1-gumstix-custom-connex.bin /media/disk-1/uImage

copy the tar.gz of your new rootfs to the EXT2 partition and extract it ie.

cp openembedded/tmp/deploy/glibc/images/gumstix-custom-connex/Angstrom-gumstix-basic-image-glibc-ipk-2007.9-test-20080917-gumstix-custom-connex.rootfs.tar.gz /media/disk-2
cd /media/disk-2
gunzip Angstrom-gumstix-basic-image-glibc-ipk-2007.9-test-20080917-gumstix-custom-connex.rootfs.tar.gz
tar -xvf Angstrom-gumstix-basic-image-glibc-ipk-2007.9-test-20080917-gumstix-custom-connex.rootfs.tar

And you may be able to boot the card.

I had to do one more step, because I don’t have a wifi module and the wifi option was enabled in gumstix-custom-connex.conf, as detailed here, when the networking startup script tries to bring up the wlan interface it ejects the card… not very helpful.

I just commented out the wlan0 interface in /etc/networking/interfaces, but if you removed the option from gumstix-custom-connex.conf it would probably have the same effect.

Adding an extra USB port to the NSLU2

While putting together the hardware for inquisitive devices, I’ve come across one surprising thing:

There seems to be a big problem with USB 2.0 480MBs hubs (or at least the ones we bought, admittedly they were only $3)

The audio computing nodes we are using comprise a ‘netslug’ device, a USB storage key, a bluetooth dongle and a sound dongle.

The only one of these that will work on a USB 2.0 hub is the storage key.

The other devices can be detected, but:

The sound card gives an error when you try and use it.
The bluetooth chip floods the system with error messages and it grinds to a standstill.

I plugged them into an old USB 1.1 hub and they worked perfectly.

We have another solution: there are 3 extra USB ports inside the netslug you can solder onto, as described here.


Using a spare usb extension cable, solder d- and d+ (pins 2 and 3 of the extension port) to the inside side of resistors 160 and 161.
Power and ground go to the same pins (1 and 4) of the back of the existing USB port.
Using an extension cable means that devices can be plugged in and out of the extra port.
(note: there may not be sufficient power supplied for bus-powered devices on the two connected ports. I used a memory key and a bluetooth dongle on these with no problem)


The best way to install the extra port is by threading the cable through the (unused) hole on the netslugs motherboard. This is just about the only way to get the case to close with the extra cable installed.
As you can see we applied hotglue to protect the soldered connections and secure the cable through the hole.



We also noticed that this batch of ’slugs (from amazon.com) were running slower than the other one. After reading this, we checked the output of /proc/cpuinfo and found the ’slugs were achieving ~133 bogomips- i.e. they had been underclocked to 133mhz at the factory. Removal of resistor 83 with a snips brought them up to speed!

configuring the NSLU2 for a distributed sound installation

Here is a compilation of things I’ve discovered in setting up some NSLU2 storage routers to act as a bluetooth-sensing audio synthesizing network for the inquisitive devices project (a collaboration with Jonah Brucker-Cohen)

Choice of distro.
I am using Angstrom openembedded, a relatively new embedded linux distro, for several reasons
-I am already familiar with openembedded from the gumstix
-It uses glibc, giving better compatibility with desktop software
-It achieves superior floating point emulation performance with the EABI

-Its possible to compile openembedded for BE (big endian) or LE (little endian) operation. I chose bigendian as it should in theory make network operations a little faster.

Software platform:
We are using bluez, python, simpleosc, and pda. This environment will not fit in the NSLU2’s 8MB of internal flash storage, necessitating interfacing a 2GB USB key to the ’slug.

Although its possible to boot linux the normal way, and install software to a mounted drive rather than the default (root) location, support for this is hit and miss. It can work ok for high level software, however packages that contain libraries and kernel modules get pretty confused. For this reason We have reconfigured ‘Apex’, the angstrom bootloader, to use the USB key as the root filing system. The steps involved are:-

Download the compiled openembedded distribution from here
Install the upslug2 tool to reflash the slug. As I’m using ubuntu this involved typing:

sudo apt-get install upslug2


Although I do have the greatest sympathy for windows users who will have to jump through hoops to do this.

Once upslug2 is ready, the netslug can be flashed by turning it on with the reset button depressed for 10 seconds (until the status light turns red), and then (while connected to the same network) issuing:

sudo upslug2 -d eth1 -i Angstrom-base-image-glibc-ipk-2007.11RC3-ixp4xxbe-nslu2.bin

(I used the -d eth1 option as my laptop was using wifi to connect to the router the netslug was plugged into).

-And this just works, instantly, detecting the netslug, reflashing, verifying and rebooting into the new OS. A big improvement on this!

Wait for the netslug to reboot and ssh into it (i.e. ssh root@192.168.1.6 )

Bring ipkg upto date and install apex-env: this allows theparameters of the apex bootloader to be modified from within linux.


ipkg update
ipkg install apex-env

Use apex-env to tell the netslug to boot from the extrnal flash drive:

apex-env setenv cmdline “console=ttyS0,115200n8 root=/dev/sda1 rootfstype=ext2 rw rootdelay=10″

Its then necessary to put the root folder structure onto the USB drive. On Ubuntu I used gunzip to unpack the tar.gz of the rootfs distribution, I copied this onto the newly formatted ext2 drive and then used:

sudo tar -xvf Angstrom-base-image-glibc-ipk-2007.11RC3-ixp4xxbe.rootfs.tar

To replicate the root filing system. I then inserted the USB key in the netslug, rebooted and voila!, df reported 1840328 blocks free.

I then installed sound, bluetooth and python onto the new, large root filing system.

ipkg install task-base-bluetooth
ipkg install kernel-module-snd-usb-audio
ipkg install kernel-module-snd-pcm-oss
ipkg install alsa-lib
ipkg install python-pybluez

depmod - a is necessary to make the bluetooth & sound modules visible.

As before, I added a stratup script to initialise the sound hardware:

#!/bin/sh
echo “Configuring sound…”
modprobe snd-usb-audio
modprobe snd-pcm-oss
echo “Finished configuring sound.”

Save this to /etc/rcS.d/S90sound and make it executable.