When I first started programming micro-processors in C, I felt like I had stepped into a time machine that took me back to my early teenage years where I spent long nights learning how to write simple programs on the C64. But alas, after a few nostalgic programming sessions I found myself falling out of love with of procedures, modules and global variables all over again and started looking for ways to apply some of the things I have learned about software development over the last 30 years or so.
Figuring out how to use basic C++ for embedded development helped me leap about ten years ahead in time to my early twenties, where I started learning about object oriented programming in Java. But I still found micro-controller development to be slow, messy and error prone and I missed the rigour and predictability I have achieved in my programming in recent years by applying TDD.
So I started looking for information on how to write unit tests for micro-controller code and quickly came across James W. Greenings book “Test-Driven Development for Embedded C”, which convinced me I was on to something.
Although the book is choke full of good information, it is pretty sketchy on how to set up a development environment for embedded TDD, as is the documentation for CppUTest — the authors C/C++ unit testing framework of choice. And since I have never done much C/C++ development and have no prior experience with GCC, it took me a while to figure out how to set up a decent development environment on my Windows 7 PC.
I set up the environment for the Arduino because I am working on an project for this board at the moment. However, the unit testing tool chain is completely independent from the Arduino tool chain, so it should be a simple matter to adapt this setup to other boards and micro-controllers.
Step 1: Install Sublime Text with Stino
If you already have a good text editor for Arduino development, you can skip this step. However, if you are still using the standard Arduino editor, I strongly recommend that you give Sublime Text a try.
- Download Sublime Text 2 and install
- If you have not already done so, grab the Arudino software and install that as well
- Open Sublime Text
- Click the
Preferences > Browse Packages…
menu to open an explorer window - Browse up one level and then open the
Installed Packages
folder - Download Package Control.sublime-package and copy it into the
Installed Packages
folder - Restart Sublime Text
- Click the menu
Preferences->Package Control
- Type
package control install
and selectPackage Control: Install Package
- Type
arduino
and selectArduino-like IDE
You should now see an Arduino
menu in Sublime Text.
Step 2: Set up the Example Project
Time to get hold of the arduino-tdd-example project and take your new development environment for a spin:
- Open the arduino-tdd-example repository on GitHub and download the zip file
- Unzip the project under your Cygwin home folder (mine is
c:\cygwin\home\Rasmus
in the windows file system) - Open the
helloword.ino
file in Sublime Text - Click the menu
Arduino > Preferences > Select Arduino Application Folder
and select theArduino
folder on your system (e.g.c:\program files\arduino
) - Click
Arduino > Arduino AVR Boards
and select the board you are using - Press
Ctrl+Alt+V
to compile the sketch
Next, try to upload the sketch to your Arduino:
- Connect your Arduino board to your PC
- Click the menu
Arduino > Serial Port
and select the Arduino COM port - Click the menu
Arduino > Serial Monitor > Baudrate
and select9600
(this is what the example project uses) - Press
Ctrl+Alt+M
to start the serial monitor - Press
Ctrl+Alt+U
to upload the sketch to you Arduino
When the sketch has finished uploading, you should see a Hello world!
message in the serial monitor.
Step 3: Install Cygwin and CppUTest
It is possible to use CppUTest with a number of tool chains but the simplest solution is probably to use Cygwin.
- Start the latest installer from the Cygwin website
- On the packages screen, choose to install all
devel
packages (they are around 500 MB, so the installation might take a while… some mirrors are faster that others though!) - Once the installation is complete, locate and open your Cygwin home directory in Windows Explorer (e.g
c:\cygwin\home\Rasmus
) - Download the latest release of CppUTest from the GitHub repository (I used version 3.6)
- Unpack the CppUTest folder
cpputest-x.y
to your Cygwin home directory (thex.y
suffix is the version number you are using) - Open the Cygwin terminal
- Type
cd cpputest-x.y
to open the CppUtest folder - Type
mkdir build_dir; cd build_dir
- Type
cmake ..
- Type
make
- Finally, type
make check
to run the CppUTest tests
All tests should pass.
Step 4: Run the Example Unit Tests
- In the cygwin prompt, navigate top the arduino-tdd-example project folder (use
/cygdrive/c
to access the c: drive in the windows file system) - Type
make
to run build and run CppUTest
And that all there is to it. Now go write some tests for your Arduino projects!