Monday, November 08, 2010

Arroyo Goes South for the Winter

Ever since moving to the ARM11-based TI OMAP3530 processor on the BeagleBoard, I would have sworn I would never have a reason to revisit the ARM9-based Atmel AT91 processor on the AT91RM9200-EK evaluation kit. But recent events have proven me wrong. What where those events?

I screwed up.

I'm passing this along as a cautionary tale of working with tool chains in the embedded realm. For those of you who write a lot of C and C++ code for the Linux platform and compile on the same architecture on which your code will run, for most of you that would be the Intel Pentium architecture, you may not know how good you've got it. When doing embedded development, it's quite common -- in fact typical -- to compile on one processor but run the resulting binaries on a completely different processor.

In my case, I build on a Linux-based Intel Pentium server, but run the resulting binaries (including a Linux kernel) on an ARM9, ARM11, or PowerPC processor, depending on the project. So my GNU C compiler has been compiled and configured to execute on an Intel Pentium processor, which is my host or build architecture, but it generates instructions for, say, an ARM9 processor, which is my target architecture. This is called cross compilation.

Not only does my gcc/g++ compiler generate completely different machine instructions in its .o and a.out files, but uses a completely different set of GNU header files than it would if I were compiling for my Pentium server, header files that are specific to my target architecture. When I'm compiling very low level code, the header files may be specific not only to the target architecture, but to the Linux kernel version running on the target.

This is where I tripped up. Back when I originally worked on Diminuto and Arroyo on the AT91RM9200-EK board, I just happened to be using the same Linux kernel version on my build server as I was on my ARM9 target processor. Everything worked just fine, but it worked coincidentally. I recently upgraded my build server to a later version of Linux, and also, in an unrelated effort, upgraded my compiler suite to the latest version of the excellent GNU-based CodeSourcery ARM tool chain that supports the ARM11 instruction set. Just as a test I tried building Diminuto and Arroyo, and they wouldn't build. The first thing I suspected was the new tool chain. So I fell back to the old tool chain I had used to originally build these old projects. And they still wouldn't build.

It didn't take that much sleuthing to realize that the problem was in the kernel header files. The wrong kernel header files. I needed to use the kernel header files for the kernel I had built myself, not the ones that came with the tool chain, nor the ones on my build server.

(The fact that the build worked at all is still kind of remarkable considering even using the same kernel, my build server had the header files for the Pentium x86 architecture while Arroyo needed the header files for the arm architecture. If you peruse the source distribution for the Linux kernel you will see that it can be built for many different architectures with many different architecture-dependent header files. Apparently for the header files that Arroyo needed they weren't that different.)

Fixing this was a bit of an effort. I had to churn the Diminuto and Arroyo make-based build systems, but also, as a precaution, the Desperado build system just to make sure I hadn't messed up my C++ build process.

And of course, that wasn't enough: I had to do a soup-to-nuts Arroyo kernel and root file system build using the revised build process and new CodeSourcery tool chain, then go through a complete installation on to the AT91RM9200-EK board, and go through the update process just to make sure I hadn't screwed something else up. This all occurred at the same time I happened to be replacing my old IBM ThinkPad T30 running Windows XP with a Apple Mac Mini with 27" Cinema Display as my desktop development environment. When something failed, I had lots of stuff to look at.

As you may guess, I eventually got it all to work. But it wasn't easy. It brings to mind how unexpectedly difficult (not to mention time consuming) embedded development can be, even (or, frankly, especially) when using open source tools and source code.

Below is a picture of the whole mess. The huge Cinema display dominates the picture. The compact Mac Mini sits on a shelf above it. You can see the green AT91RM9200-EK board to the lower right of the photograph. The temporarily abandoned tiny red BeagleBoard is looking a little lonely just below the center of the photograph.



Arroyo Goes South for the Winter

But wait, what's this: a Windows 7 notebook in the bottom center of the photograph? That's right. When you do embedded development, you often live and die by the serial console terminal. And despite having tried several USB-to-serial converters and two different serial console software tools, I have discovered that there is nothing so far in the Mac environment equivalent to my beloved Putty utility. I'd be glad to be proven wrong.

Update (2011-01-03)

As I mentioned in the comments, I'm pretty happy with MacWise, a commercial terminal emulator for the Mac. I'm using it with a TrendNet USB-to-Serial adaptor/cable. To get MacWise to work reliably with the TrendNet device I had to download a new driver for the USB-to-Serial chipset from the Apple support web site. But it's been smooth sailing since then.

Tuesday, October 19, 2010

Android and Contraption

Back in July I wrote briefly about Cascada, my little project to move my software developed as part of my Diminuto project from the AT91RM9200-EK board (which uses the Atmel AT91 ARM9 processor) to the smaller, much cheaper, and more powerful BeagleBoard (which uses the TI OMAP 3530 ARM11 processor) with the TinCanTools Zippy2 expansion board. Cascada turned out to be so straightforward, I was a little disappointed. I didn't really feel like I'd gotten the learning experience I'd expected.

So I evolved the project into Contraption, where I run the "Rowboat" port of Android, the Google mobile device environment, to the BeagleBoard. This would have been a no-brainer too had I not insisted on getting the Zippy2 Ethernet port working. That required I reverse engineer the configurations of the Angstrom U-Boot bootloader and Linux kernel for the BeagleBoard that support the Zippy2, use what I learned to build the Linux that come with Android (which is slightly different), substitute in the U-Boot that initializes the Zippy2, and get the whole mess running on my hardware.

A few evenings of head scratching, and the next thing you know I'm displaying my blog on the Android browser.

Contraption

Scattered around my tiny lab bench is (roughly left to right) a WiFi to Ethernet bridge (which will play a role in a later article), the tiny BeagleBoard, an Ethernet switch, a BDI3000 JTAG debugger (mostly unnecessary), a fan (also mostly unnecessary except to keep me cool), and a powered USB hub. The keyboard and mouse on the left are connected to an ancient ThinkPad I use as a window and TFTP server, and the smaller keyboard and mouse on the right are connected to the BeagleBoard. Below is the Android home screen running on the BeagleBoard.

Google Android ("Rowboat") on BeagleBoard

Why Android? Lots of reasons, but the most compelling was that I had come across a technical report written by some students at the University of Applied Sciences Northwestern Switzerland about using Android in industrial automation. I'm all about frameworks for real-time software design, and this was right up my alley. It will also be an excuse to brush up my Java skills and learn how to develop in the Android environment.

Somewhat ironically, since I've spent the past nearly fifteen years working for one Bell Labs spin-off or another, my interest in Android is not really related to mobile phones. I have a whole other project in mind. But that will have to wait for a future article.

Wednesday, October 06, 2010

Design Patterns and the Hylaean Theoric World

Neal Stephenson's novel Anathem is a 980 page discourse on philosophy, mathematics, and science disguised as a damn good science fiction novel. The philosophers, mathematicians, and scientists in his novel have the concept of the Hylean Theoric World (HTW), a realm in which the immutable truths of their universe and, as it turns out, ours, can be found. This is their version of our Platonic Idealism: the fact that the number three is prime is true no matter where you live, in what era you live in , and what your laws of nature may be. The concept exists independent of everything else, as if it inhabited its own reality.

One of the more controversial ideas of HTW (and of Platonic Idealism) is that that independent reality is a real place. But while it's hard to argue with the fact that three is prime, it's a little tougher to convince people that there is an alternate universe where the fact that three is prime originates. I can appreciate, however, that point of view.

That's why for decades, scientists and science fiction writers pondering how to communicate with intellects on another planet have assumed that the starting point for any such dialogue would be mathematics. You would establish a symbolism and nomenclature to represent stuff that you knew your opposite number on the other planet would understand, regardless of the fact that they might have tentacles, breath methane, and use smell as their principle sense organ.

Mathematics would be the root of a common inter-species language, but perhaps not science, at least not all science. There is some evidence, still quite controversial, that the laws of nature might not be exactly the same throughout the universe. So you run the risk of the the tentacled folk pondering your latest missive and saying "These crazy aliens, just when we thought we understood them, they go on about this Fine Structure Constant as if it were constant! We have clearly misunderstood something." The good news, though, is that anyone with whom we are likely to have a dialogue approaching anything like real-time would have to be close enough to us that this isn't going to be an issue.

It's easy to see how mathematics would live firmly in the Hylaen Theoric World, even if all of chemistry and physics might not. What else?

How about design patterns? One of the struggles authors of design pattern books have is how to represent design patterns. You have to appreciate the problem here, because while design patterns are not tied to a specific language, they are often much more easily implemented in one language than another. (I probably did something like dependency injection even back when I used FORTRAN IV, but I have no idea how I might have done it. I remember frequently resorting to writing subroutines for FORTRAN in assembler back in those days, because assembler had pointers.)

The classic Gang of Four design pattern book uses a mixture of text, diagrams, and source code in Smalltalk and C++. Some design patterns books have been published in separate C++ and Java editions. Donald Knuth invented his own assembler-like language just to tackle the same issue in his classic The Art of Computer Programming series of books on algorithms.

But the design patterns themselves really are language-agnostic. So I claim they are examples of Platonic Ideals, and live in the HTW. If we could get past the representation issue, design patterns would be another way to communicate with our tentacled colleagues.

Many design patterns are not obvious, and understanding them and successfully applying them carries enormous potential consequences. Sure, the tentacled ones would nod each of their heads and appreciate our explanation of their well known adapter, facade, and proxy patterns. But when we beam more complicated ideas like map-reduce at them, their tentacles might start shaking as they understood the implications of a powerful distributed computing architecture. We could, perhaps inadvertently, change their world. Or vice versa: they could change ours by transmitting a meme so powerful that it captured our intellects and imaginations.

The other day I listened to some of my colleagues try to define what they meant by "elegant code". Here is how I have defined it in the past: elegant code is that which reveals a higher truth. But it wasn't until I was reading Anathem that I realized that what I was really saying was elegant code is that which reveals a Platonic Ideal pattern that was previously unknown to me, one that offers me a glimpse into the reality that is the Hylaen Theoric World. One that I am totally going to appropriate and use myself.

Because when you are an engineer, Rule One is: steal all the good ideas.