Random numbers and Math on AVR32

Probably not a very useful post for veterans but might be useful for those like me who are new to AVR32. In this post, I show how to use the USART example application supplied with AVR32 Studio 2.6, to calculate and print a natural log of random numbers to the serial port.

The Project

We’ll start by creating a new AVR Example Project. Since I have an EVK1001 board I chose the EVK1101 – DRIVERS – USART example for AT32UC3B0256.

The reason for choosing this particular sample app is because I can then nicely show something over the serial port. The default baud rate of the serial port is 57600. You can use one of several  USB to serial adapters if your PC does not have a serial port (very common these days). To establish a terminal with the serial port you can use the excellent TeraTerm (ttsh2) utility.


I added the code that prints the log values after the following line in usart_example.c

  usart_write_line(EXAMPLE_USART, "Hello, this is AT32UC3 saying hello! (press enter)\n");

Here’s how the code looks

  char str[100];
  double r;
  r = (double)rand() / RAND_MAX;
  sprintf(str, "log(%lf) = %lf\n", r, log(r));
  usart_write_line(EXAMPLE_USART, str);
  r = (double)rand() / RAND_MAX;
  sprintf(str, "log(%lf) = %lf\n", r, log(r));
  usart_write_line(EXAMPLE_USART, str);
  r = (double)rand() / RAND_MAX;
  sprintf(str, "log(%lf) = %lf\n", r, log(r));
  usart_write_line(EXAMPLE_USART, str);

You’ll also need to add the following headers to the source file

#include "stdio.h"
#include "limits.h"
#include "math.h"


If you perform a build at this point you’ll probably get an error from the linker (ld) such as

src\usart_example.o: In function `main':
C:\as4e-ide\workspace\usart\Debug/..\src/usart_example.c:299: undefined reference to `log'
collect2: ld returned 1 exit status

The reason being that the linker has no way of knowing that you’ll be using the math library unless you specify so. To do that now, head over to the 32-bit AVR/GNU C Linker settings in the project properties. Add a new library to Libraries (-l) called m.

Now, you should be able to build successfully.

Deploy and test

You can now deploy the elf file to your development board. I used a JTAGICE mkII target from within AVR32 Studio. Here’s how the output should look if all goes well

Hello, this is AT32UC3 saying hello! (press enter)
log(0.970751) = -0.029686
log(0.261253) = -1.342268
log(0.857064) = -0.154242


Edit binary files in Linux

In this post I’ll mention how to edit binary files using vi and the utility xxd that is a part of vi.

vi in hex mode

Use the xxd command by typing :%!xxd. Edit hex data. Quit hex mode with :%!xxd -r.

Using xxd command

You can use the xxd command outside vi. If you have an existing binary file, you can convert it to hex

xxd -g 1 file.bin > file.hex

You can then edit the hex and convert it back to binary

xxd -r file.hex > file.bin

One nice little thing that xxd does is to produce a C static array definition, very convenient for embedding resource files

xxd -i file.bin


netcat is a wonderful utility for those who regularly do network programming. I find myself using netcat to avoid writing code to do simple network IO. Here I document some of the common uses I put it to. A similar replacement utility called ncat is available with nmap, it also works on Windows. The version of netcat used in the examples below is the OpenBSD version distributed by Ubuntu.

Send or Receive data using UDP

netcat -u 8001

Any data you type into stdin is sent to port 8001 of the localhost. You can start another instance of netcat with the command line shown below to receive the data.

netcat -l -u 8001

This can be used to establish a simple UDP based chat mechanism.

Send or Receive data using TCP

The UDP chat example in the previous section can be implemented in TCP by dropping the -u argument from the arguments. TCP is the default mode. This being TCP, the receiver end has to be started first (the server) and then the sender (the client).

Capture audio with GStreamer on PandaBoard

In this post I list some of the audio devices from where sound can be captured on the PandaBoard with Ubuntu 11.04.

Capture from a USB WebCam

To see the audio devices run the following command, or see cards listed by pactl list

cat /proc/asound/cards

Then execute a pipeline such as

gst-launch -v ! alsasrc device=hw2:0 ! audioconvert ! vorbisenc ! webmmux ! filesink location=audio.mkv

To capture using the pulsesrc element, switch to the appropriate audio input device in Sound Preferences then execute a pipeline such as

gst-launch -v ! pulsesrc ! audioconvert ! vorbisenc ! webmmux ! filesink location=audio.mkv

Capture from desktop

Capture any sound currently being played. See sources listed by pactl list for device id.

To capture audio output source on the board, you’ll need to switch to SDP4430 Analog Stereo sound output device in Sound Preferences, and use the device specified below

gst-launch -v ! pulsesrc device=alsa_output.platform-soc-audio.0.analog-stereo.monitor ! audioconvert ! vorbisenc ! webmmux ! filesink location=audio.mkv

To capture HDMI audio output source, you’ll need to switch to PandaHDMI Analog Stereo sound output device in Sound Preferences, and use the device specified below

gst-launch -v ! pulsesrc device=alsa_output.platform-soc-audio.1.analog-stereo.monitor ! audioconvert ! vorbisenc ! webmmux ! filesink location=audio.mkv

Capture from Line In

You’ll need to switch to SDP4430 Analog Stereo sound input device in Sound Preferences. To capture sound from line in (upper 3.5 mm jack of the audio connector) execute

gst-launch -v ! pulsesrc ! audioconvert ! vorbisenc ! webmmux ! filesink location=audio.mkv

WebKit/Gtk+ with WebGL on PandaBoard

Posting this to share the build and test procedure with Ubuntu 11.04 on the PandaBoard (OMAP4). If you have never built Webkit/Gtk+ before take a look at this post first.

Download source for development version 1.5.2 from webkitgtk.org.

Additional Ubuntu Packages

You’ll need to sudo apt-get install the following packages. If you have installed ubuntu-omap4-extras-graphics and ubuntu-omap4-extras-graphics-dev packages from the TI OMAP4 extras respository then you don’t need to install libgles1-mesa-dev and libgles2-mesa-dev below. You already have the hardware accelerated versions.

  • libgail-3-dev
  • mesa-common-dev
  • libgl1-mesa-dev
  • libglu1-mesa-dev
  • libgles1-mesa-dev
  • libgles2-mesa-dev
  • libgstreamer0.10-dev
  • libgstreamer-plugins-base0.10-dev
  • libgtkglext1-dev

Obtain Missing Headers

Assuming you are in the root folder called webkit-1.5.2, here’re the steps to get the missing headers.

cd ..
svn checkout http://svn.webkit.org/repository/webkit/trunk/Source/ThirdParty/ANGLE/ angleproject
svn checkout http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/platform/graphics WebKit
cd webkit-1.5.2
cp ../angleproject/src/compiler/ExtensionBehavior.h Source/ThirdParty/ANGLE/src/compiler/
cp ../angleproject/src/compiler/glslang.h Source/ThirdParty/ANGLE/src/compiler/
cp ../WebKit/Extensions3D.h Source/WebCore/platform/graphics/</pre>


Configure the build

./configure --prefix=/usr --enable-webgl --enable-3d-rendering

Then execute



Execute Webkit/Gtk+ using GtkLauncher with the --enable-webgl flag, and access any of the three.js Context 3D demos. I get a crash with all Context 3D demos, Context 2D demos work.

./Programs/GtkLauncher --enable-webgl

Netflix launches in Brazil

Today Netflix launched in Brazil, an interesting milestone for internet streaming here.

Here’s some info based on first hand usage:

– They stream to the browser. For the technically inclined, they stream using IIS Smooth Streaming to the browser. Their player is built with Silverlight. It works on Google Chrome (the browser I mostly use). They also stream to the Nintendo Wii, Sony Playstation, and other Netflix enabled devices (DVD and Bluray players I would think).

– They don’t stream to the iOS (iPod, iPhone, iPad…) app yet. Android app will probably also not work. If you travel to the US on vacation or business, this should work. I am also able to stream using a VPN connection to a server based in the US. The movie selection is also vastly better there.

– Very few movies and TV shows so far, mostly several years old. Are they afraid to kill DVD sales of new movies and shows? They don’t have subtitles. It is customary in Brazil to watch movies with audio in English and subtitles in Portuguese. That will very much define their success here. Also, no Brazilian movies so far.

Embedded Linux system for PandaBoard with Buildroot

My objective here is to build a custom embedded Linux system for PandaBoard, that runs off the SD card. Buildroot version used is 2011.08. We’ll use the first stage bootloader called X-loader with the board name omap4430panda. The second stage bootloader used is U-Boot, which is built using the omap4_panda board configuration. The Linux kernel uImage is based on kernel source version, it is built using the defconfig omap2plus. The system is configured to use the udev device manager, and to produce the login prompt on the ttyO2 (with an O not zero) serial port. The default busybox package is used for commonly used command line utilities.

PandaBoard boot sequence

The boot sequence is explained in some detail at omappedia.org. We’ll keep that in mind while working with buildroot.

SD card format

The SD card format is explained in great detail here. For the sake of simplicity I used an SD card formatted correctly by writing an Ubuntu 11.04 preinstalled image for OMAP4. You can also obtain the X-loader MLO, U-Boot, and kernel uImage files from that image. Especially, if you want to replace your own binaries with something that works, for debugging.

Obtain and configure buildroot

Here’re the different options that need to be configured. To get the configuration menu execute make menuconfig from the buildroot source folder

  • Target Architecture – choose arm.
  • Target Architecture Variant – choose cortex-A9.
  • Target ABI – choose EABI.
  • Toolchain – change GCC compiler version to 4.6.x, this is required for compiling for cortex-A9. Choose the option Enable large file (files > 2 GB) support.
  • System Configuration – change /dev management to Dynamic using udev. Change Port to run a getty (login prompt) on, to ttyO2 (with an O not zero).
  • Bootloaders – choose U-Boot. In the U-Boot sub menu set U-Boot board name to omap4_panda. Choose X-Loader. In the X-Loader sub menu set x-loader board name to omap4430panda.
  • Kernel – choose Kernel version as Same as toolchain kernel headers (happens to be 3.0.4 when this post was written). Set Defconfig name to omap2plus.

Configure the kernel

To get the configuration menu execute make linux-menuconfig from the buildroot source folder. You’ll have to build at least once so that buildroot downloads the kernel source. You can select kernel modules at any time and rebuild. For instance, enabling the following modules will add support for USB

  • Under Device Drivers, Network device support, USB Network Adapters, check SMSC LAN95XX based USB 2.0 10/100 ethernet devices
  • Under Device Drivers, USB support, check EHCI HCD (USB 2.0) support and TWL6030 USB Transceiver Driver

Configure the filesystem

Edit /output/target/etc/fstab and change the root filesystem type to ext3

# <file system> <mount pt>     <type>   <options>         <dump> <pass>
/dev/root       /              ext3     rw,noauto         0      1


Execute make in the buildroot source folder to build the system.

U-Boot boot script

The kernel boot command line [kernel.org, lkn] is configured to print messages to the serial port ttyO2. Create a text file called boot.script with the following content

fatload mmc 0:1 0x80000000 uImage
setenv bootargs ro elevator=noop vram=32M root=/dev/mmcblk0p2 rootdelay=2 rootfstype=ext3 fixrtc console=ttyO2,115200n8 mem=456M@0x80000000 mem=512M@0xA0000000
bootm 0x80000000

Without rootdelay in the boot script you might get the following error

VFS: Cannot open root device "mmcblk0p2" or unknown-block(2,0)

Now, convert boot.script to the format required by u-boot

mkimage -A arm -T script -C none -n "Pandaboard boot script" -d boot.script boot.scr

Prepare SD card

Copy the following files to the the first partition (FAT32). All files other than boot.scr are to be found at output/images under the buildroot source folder.

  • MLO
  • u-boot.bin
  • boot.scr
  • uImage

Prepare the second ext3 partition (after rm -rf * anything already there) by extracting the contents of output/images/rootfs.tar

sudo tar xvf output/images/rootfs.tar

Boot the system

Now, you’re ready to plug the SD card into the PandaBoard and test the newly minted kernel. Remember to connect your PC to the serial port of the PandaBoard, the startup messages appear there, and you can login to the new system to do whatever you have built it for. If your PC does not have a serial port, you can buy any of several cheap USB to serial adapters. PuTTY is an excellent terminal for serial ports if you are using Windows. You can also use it on Linux, or use stty.

That’s all for now. Further work needs to be done to enable HDMI display, audio, WLAN and other hardware available on the PandaBoard.