September 2006

Monthly Archive

Unit Testing of Embedded Code

Posted by benoit on 18 Sep 2006 | Tagged as: embedded, ramblings

I am challenged (on so many levels! :-)). I write code in C# and C++ on PCs running Windows. This makes it easy to practice TDD and write unit tests for my new code.
The large legacy code-base is something else entirely, but we’re making (slow) progress. Life is good…on windows.

I also happen to write code for embedded 8-bit micro-controllers, hence my challenge. How does one write unit tests for the embedded code?

Some of the code is all in assembly, so clearly it does not lend itself to unit tests. Some of the programs are really small and do a simple job, but others are significant in size (over 5,000 LOC). The only way I have come up to perform unit testing is to create a windows unit testing application and suck in the embedded code. This works reasonably well, but there are a few stumbling blocks:

Hardware Specific Libraries

Micro-controllers are somewat different from your regular run of the mill Pentium. In addition to the processor core, they have a number of “peripherals”: integrated serial interface, Analog-to-Digital converters, Input-Output pins, etc… Compiler vendors often provide libraries which allow you to interface with the peripherals without having to resort to using assembly code. For example, input(PIN_A2) would return the logic state (1 or 0) of pin A2. If we simply attempt to compile such code on windows, the compiler will complain loudly. At a minimum, you need to provide stubs; ideally you would want to emulate some of the hardware (e.g. serial port)

Assembly & non-standard compilers

Sometimes, you simply need to set a specific register to control something. The easiest way to do this is a single line of assembly code: IOE spo, 0x2E. All compilers targeting embedded systems provide a way to generate inline assembly (yes, even GCC). The problem is, Visual C++ does not like that! Unfortunately, in those cases, you are usually relegated to using conditional compiles. Sometimes life is just like that.

A related problem is the compiler vendors who have a “mostly” C compiler, but make it replete with non-standard extensions. The best (worst?) example of this is Z-World’s Dynamic C. It does not have #include statements, they are #use. Functions are declared with a special type of comment. The C compiler has been exended to use “costate” (a simple form of multi-tasking). This can make it just about impossible to include the normal code in a windows application. If you are serious about testing your code, you might just have to resort to cut & paste. Not something pleasant!

I’d like your opinion on this. Is there a better way to unit test embedded code? Am I going off the deep end wanting to unit test embedded code?

P.S.

If you are an embedded engineer, and have no idea what I’m bleating about, visit the following:

Test Driven Development

  • James Newkirks’ blog (Creator of NUnit)
  • Steve Eichert’s blog
  • Roy Osherove’s blog(I’m not stalking you Roy! I won’t write about you in my next post, I promise!)

Actually, go see Darrell Norton’s blog entry on TDD resource. It’s all there!

One thing you must realize is that a lot of the TDD & Unit Testing material on the web is rooted in a Java heritage, and as such assumes the language provides certain facilities (reflexion, object orientation, etc…). This does not hold true (very often) in an embedded environment. Such is the plight of the embedded engineer…we truly are the Sons of Martha.

A Celebrity shook my hand…I’m never going to wash it!

Posted by benoit on 17 Sep 2006 | Tagged as: ramblings

While I was at the beach, cogitating about my next blog entry to the sound of the Atlantic Ocean, something incredible happened!

Roy Osherove read my entry about the derth of embedded blogs, and found it interesting enough to write about (at 1 AM!).

This is like having Bono overhear one of you pithy line, stop, shake your hand and say “Good Point!”. Next thing you know, you tell your friends “He shook my hand! I’m never going to wash it!”. Clearly people will think you are strange.

The danger with being touched by a celebrity is that I won’t be able to get through the door of the office tomorrow (due to an inflated head)! As gratifying as this is, I must stay grounded and remember that I’m just a little guy.

Simply because I have 3 readers, clock in at a Technorati rank of 1,642,105 and don’t even show up on google, doesn’t mean I am “all that and a bag of chips”!
The question on my mind is this: How did you guys find me? I can’t even find myself!

Am I alone?

Posted by benoit on 17 Sep 2006 | Tagged as: embedded, ramblings

One of the reason I decided to start this blog was due to the paucity of blogs related to the field of embedded development. Embedded.com has excellent content, and the columnists are a joy to read, but nothing in the blogosphere!

.NET has news sites like Larkware, it has blogosphere celebrities like Chris Sells , Scott Hanselman, and Larry O’Brien. Roy Osherove is big in both .NET and the Agile community. Talking about Agile, they have their own stars: Ron Jeffries, Robert Martin and the whole bunch at ObjectMentor.

But what about the embedded world? Where are the high profile bloggers? WindRiver, GreenHills, FreeScale, AMCC where are you?

Monad should rock your world!

Posted by benoit on 16 Sep 2006 | Tagged as: .NET

Catching up on Hanselminutes, I was listening to podcast #10 about WSH aka Monad. Half-way through the show, I had an epiphany.

Monad allows you to create automated Acceptance Tests (and Functional Testing) without the need for much infrastructure.

Since Monad understands .NET objects, you simply have to create cmd-lets in your application. These can be used to control the application from the Monad command line, or via a script.

These same cmd-lets can be used by the GUI to drive the underlying application (you did separate the application logic from the GUI right?). You now simply have to test the GUI layer, which is simpler than testing the whole application via the graphical interface.

Definitely check out Scott Hanselman’s blog entry on Monad for more details.

This is heady stuff!

Who says blogging is simple?!

Posted by benoit on 15 Sep 2006 | Tagged as: ramblings

I’m simply trying to get this off the ground, and it seems there is a ton of things to do, most of which I have not clue about… I work with technology, and I’m even somewhat proficient at it, but I believe that I’m a luddite at heart.

I was on the trailing edge when it came to CDs… I Didn’t get my first CD player until 1994. We didn’t get a DVD player until 2000 (only because it was free!)

This theme is carying over into the blogosphere… del.icio.us, technocrati, feedburner

Give me an ISR to debug any day!

« Previous PageNext Page »