Ubuntu on the Beaglebone: Enabling Analog In, PWM, I2C and SPI

When writing about the Beaglebone in the past, I mentioned that you needed to be running Angstrom in order to have access to certain devices, since they weren’t yet supported by the kernel in Ubuntu.

Well, I’m rather late in giving Ubuntu its props, but that’s no longer true. Ubuntu has caught up to Angstrom in protocol support: analog input, PWM, SPI and I2C are all working.

In order to get these features, you need to have a recent kernel.  I’m not sure exactly which kernel version they were added in — 3.2.twentyish — but current kernel version for Ubuntu  is 3.2.28.  If you’re running an earlier kernel, I’d recommend that you upgrade.

To determine your current kernel version, run the following command

ubuntu@omap:~ uname -r
 3.2.21-psp16

You have a couple of options for getting the latest and greatest Ubuntu kernel.

Creating a new Ubuntu Image

If you aren’t already running Ubuntu on your Beaglebone, you can download a new image.

You’ll need to use Robert C Nelson’s customized Ubuntu build.  The instructions for creating the image are on the eLinux Beagleboard page, under “Demo Image”.   (Don’t be put off by the word “demo” — it’s a fully functioning Ubuntu distribution, with a vast array of available packages).

The procedure is quite reliable and mercifully free of obscure Linux commands, but you will need to be running Linux to write the image to the microSD card.

Before starting, you’ll need to decide which Ubuntu version to run on your Beaglebone.  The latest released version is usually a good bet, currently Precise 12.04.  That’s the one I’m using.

However, if you plan to use a GUI desktop, then you may want to drop back to the previous version, Oneiric 11.10.  That’s because the 12.04 release is compiled with hardware floating point support (aka “armhf”), and while that makes some things faster (the apt-get package manager is noticeably zippier), it reportedly makes some GUI packages too slow to use.  The kernel version in Oneiric is the same as in Precise, so as far as I know you can still get analog in, PWM, I2C and SPI with that release.

You should refer to the eLinux page for the latest instructions, but here are the steps you would currently following to create a microSD card with Precise 12.04 on a Linux desktop:

1. Begin by downloading the image file.  It’s not huge, a little over a hundred megs:

wget http://rcn-ee.net/deb/rootfs/precise/ubuntu-12.04-r5-minimal-armhf-2012-07-29.tar.xz

2.  While you’re waiting for it to download,  insert your microSD card. It needs to be 4G or higher.  Personally, I’m sticking with 4 or 8G cards, since some people have reported problems with higher capacities: it depends on the manufacturer.  Also, I’d suggest you use a USB card reader that has a microSD card slot.  I’ve personally had problems with those SD card-sized adapters that allow you to fit a microSD card into an full SD card slot.

3. Determine what your microSD card device ID is, and make sure it wasn’t automatically mounted by Linux.  The easiest way to determine the device ID is to run the dmesg command after inserting the card: the messages at the end of the output will include the device ID.

The format will be /dev/sdX, where X varies.  For example, on my PC it’s /dev/sde.  Note that you want the device ID, not the partition ID: that is, /dev/sde, not /dev/sde1.

To check if the card was mounted, run the “mount” command. Look at the lines at the bottom of the output to see if they contain your card’s device ID, such as:

/dev/sde1 on /media/sde1 type vfat (rw)

If so, you’ll need to unmount them before writing the image to the card.  Personally, I’ve had problems when unmounting from Ubuntu’s file manager (the device ID is no longer recognized by the OS after doing so), so I’d suggest you do it from the command line:

sudo umount /dev/sde1

4. Once the download from step 1 has completed, you’re ready to run the commands to write the image to the microSD card.

tar xJf ubuntu-12.04-r5-minimal-armhf-2012-07-29.tar.xz
cd ubuntu-12.04-r5-minimal-armhf-2012-07-29

Note: In the following command, replace “/dev/sdX” with the actual device ID from step 2 (e.g. /dev/sde).  Unlike the previous commands, this one needs to be run as root:

sudo ./setup_sdcard.sh --mmc /dev/sdX --uboot bone

This script handles all the nitty gritty work for you: it repartitions the card, builds the boot and root partitions and downloads the latest kernel for you.   The whole thing should be done in 20-30 minutes. If you find it still grinding away after an hour, there’s likely a problem with the card or the way its connected to your PC:  if you ignored my advice from step 2, you might want to reconsider it now.

5. When the script completes, you’re all done.  Unmount the card from your Linux PC, if necessary, then pop it into the Beaglebone.  The initial bootup will be slower than subsequent boots, generally a few minutes.  You’ll need to login as user-ID ubuntu, password temppwd.  The ethernet port is configured for DHCP (using /etc/network/interfaces, not connman, yay!), and SSH is enabled by default.

Upgrading the Ubuntu Kernel

Unlike Angstrom, Ubuntu doesn’t automatically deliver new kernel releases via its package manager, apt-get.  So, if you installed Ubuntu on your Beaglebone awhile ago, you’ll find that kernel version has become quite outdated.  Fortunately, Robert C Nelson has made the upgrade process quite easy to do manually.

The procedure depends on which Ubuntu release you’re running on your Beaglebone.  In my case, running the 12.04 armhf image, instructions are at http://rcn-ee.net/deb/precise-armhf/LATEST-omap-psp:

ABI:1 EXPERIMENTAL http://rcn-ee.net/deb/precise-armhf/v3.2.0-rc2-d0/install-me.sh
ABI:1 TESTING http://rcn-ee.net/deb/precise-armhf/v3.2.0-psp0/install-me.sh
ABI:1 STABLE http://rcn-ee.net/deb/precise-armhf/v3.2.28-psp21/install-me.sh

The “stable” version is, of course, the best choice.   So, to upgrade hte kernel to 3.2.28, I’d run the following commands on my Beaglebone:

wget http://rcn-ee.net/deb/precise-armhf/v3.2.28-psp21/install-me.sh
sudo bash install-me.sh

The script will download and install the kernel and related files (kernel modules, etc.).  The process should take about 5 minutes, depending on the speed of your microSD card.  If all goes well, the script should end with a reboot message:

. . .
`/boot/vmlinuz-3.2.28-psp21' -> `/tmp/boot/zImage'
 `/boot/initrd.img-3.2.28-psp21' -> `/tmp/boot/initrd.img'
 Please Reboot

Instructions for upgrading the kernel in other Ubuntu versions, and for other hardware architectures, can be found in other subdirectories of  http://rcn-ee.net/deb.  For example, if you’re running 11.10 Oneiric on your Beaglebone, upgrade instructions are in http://rcn-ee.net/deb/oneiric-armel/LATEST-omap-psp.  (In Ubunuese, “omap-psp” means Beaglebone, apparently.)

Enabling Analog Input

After booting your new image, you should find PWM, SPI and I2C in their expected file system locations:

ubuntu@omap:~$  ls /dev/i2c*
 /dev/i2c-1 /dev/i2c-3
ubuntu@omap:~$  ls /dev/spi*
 /dev/spidev2.0
ubuntu@omap:~$ ls -Fd /sys/devices/platform/omap/e*
 /sys/devices/platform/omap/ecap.0/ /sys/devices/platform/omap/ehrpwm.0/
 /sys/devices/platform/omap/ecap.1/ /sys/devices/platform/omap/ehrpwm.1/
 /sys/devices/platform/omap/ecap.2/ /sys/devices/platform/omap/ehrpwm.2/
 /sys/devices/platform/omap/edma.0/

Analog support has a twist: the file system interface is implemented in a kernel module, which isn’t loaded by default. To load it, enter the following command:

 sudo modprobe ti_tscadc

The file system interface should then be visible:

ubuntu@omap:~$ ls /sys/devices/platform/omap/tsc/a*
 /sys/devices/platform/omap/tsc/ain1 /sys/devices/platform/omap/tsc/ain5
 /sys/devices/platform/omap/tsc/ain2 /sys/devices/platform/omap/tsc/ain6
 /sys/devices/platform/omap/tsc/ain3 /sys/devices/platform/omap/tsc/ain7
 /sys/devices/platform/omap/tsc/ain4 /sys/devices/platform/omap/tsc/ain8

To have the kernel module loaded automatically when the Beaglebone is rebooted, enter the following commands:

ubuntu@omap:~ sudo -i
root@omap:~# echo ti_tscadc >> /etc/modules
root@omap:~# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

ti_tscadc
root@omap:~# sudo depmod -a

Wrapping Up

You should find that analog input, PWM, I2C and SPI all work the same way in Ubuntu as in Angstrom.  Code that you find for using these features on Angstrom will work on Ubuntu without any changes.  Here are a couple of articles I wrote on using analog input and PWM:

I’ve recently been using both SPI and I2C on the Beaglebone to write to LCDs from Python, and I plan to write articles on these in the future.

This entry was posted in Electronics and tagged , . Bookmark the permalink.

8 Responses to Ubuntu on the Beaglebone: Enabling Analog In, PWM, I2C and SPI

  1. Pingback: Ubuntu on the Beaglebone: Enabling Analog In, PWM, I2C and SPI « adafruit industries blog

  2. jhowardp says:

    A little confused (new to Beaglebone and not a Linux master yet). Do I need both Nelson’s build and the Ubuntu 12.04 build? BTW, thanks for these Beaglebone articles you’ve done.

    • dwatts says:

      Hi,

      You need just the one build: Nelson’s. The commands that I show in the “Creating a New Ubuntu Image” section will download his customized Ubuntu 12.04 image and transfer it to a microSD card. If you’re trying Ubuntu on the Beaglebone for the first time, I’d recommend you use that image.

      You can skip the “Upgrading the Ubuntu Kernel” section of my article for now. You can follow that process in the future if you need to upgrade the kernel to get a new feature or bug fix. But, to get Analog In, PWM, etc. you just need to do the part in the “Creating a New Ubuntu Image” section.

      Dan.

  3. Sang says:

    Hi dwatts,
    This is a precious information. Thank you for all of your beaglebone posts.
    I have a question. The reason that I’ve stick to Angstrom is because of its support for most components in AM334x. Is this true for the current Ubuntu version? More specifically do you know if Ubuntu distribution support openGL ES 2.0 using the sgx ?
    Thanks again.
    S

    • dwatts says:

      Hi,

      Can you give an example of a AM334x feature you need to use on Ubuntu?

      If it’s a feature that’s supported by the kernel, then the answer is probably yes, Ubuntu supports it too. If I understand this post on the Beagleboard Google Group correctly, the kernel is pretty much identical for Angstrom and Ubuntu: https://groups.google.com/forum/?fromgroups=#!topic/beagleboard/JOPE3idmSlk

      SGX support is one of things that the 12.04 armhf build doesn’t have, but it is in Ubuntu 11.10 (though I haven’t ever used it). I believe that SGX support in the armhf build won’t show up until the 3.4 kernel, and maybe not in the initial versions of that kernel. This is based on this section of the eLinux Beagleboard page: http://elinux.org/BeagleBoardUbuntu#SGX_armel.2Farmhf_v3.4.x.2B. Further down on that page, it says that the Beaglebone support is still in development.

      Dan.

  4. Nustian says:

    Hi , i followed your method and got the beaglebone adc up and running. i have 3.2.29-psp22 . I want to use its timers for my application. Unfortunately i dnt have any option of using timers. Is there any command like modprobe ti_tscadc(for adc) for enabling timers?

    • dwatts says:

      Hi,

      Sorry, I don’t know anything about accessing timers through a kernel module — it’s not something I tried to do.

      As you probably already know, you can see what kernel modules are installed using the “modprobe -l” command. If there is a kernel module for timers in Angstrom, you can grep the output of modprobe -l to see if it is included with Ubuntu’s kernel modules. If it’s not there then it’s probably not available for Ubuntu yet, since there don’t seem to be many add-on packages for kernel modules in Ubuntu.

      Dan.

  5. mickeyf says:

    The problem I’ve had with full size card adapters is that they force the SD micro to Read-Only, which of course means you can’t write an image to it. You may (or may not) be able to set this on the adapter itself.

    “psp” is “Platform Support Package” in TI-ese. TI, who makes the AMR335x, supplies some of the lowest level code that the Beagle community uses.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>