Monday, January 11, 2016

Breadboarding Embedded Code, The Easy Way...


Those of us who work in the Analog World are intimately familiar with the concept of breadboarding little bits of circuits so that we can better understand their limitations before we commit them to a PCB layout. We just get our trusty Soldering Iron, some parts and build away.

In the small embedded world we have much the same wants, many times we don't have access to a full emulator for the processor that we are using and debugging is painful using writes to a serial debugging port, etc. Not at all like when we write code on a PC using something like Visual Studio where the debugging is so easy and quick.

This came up recently (again) when I was developing a new PID control algorithm for a controller project. I wanted to Breadboard the C code on my PC so that I could quickly play some What-If's with the code and input parameters before committing the code to the actual board for integration, where the debugging is at least 10 times slower.

Using Visual Studio C# is out of the question for developing, and testing simple integer C code because all the integer math in C# is limited to Signed 32 Bit math. If you use C# like this you then have casts all over the place to keep your desired variables in the right format, like uint8's! That's ugly and not useful at all.

Visual C++ is an option, but it isn't quick nor disk efficient what with it's multi-Gigabyte disk footprint. Too much overhead and it is just overkill.

In the past I used to use a 1990 DOS version of Microsoft Quick C, unfortunately it doesn't play very well with 64 bit Windows anymore. So I looked around for another option. Some folks have ported DOS Turbo C to run on Windows in a DOS Box, but that looked too clunky. After all, who wants to give up a nice simple Windows GUI?

I have also tried to use Microchips MPLAB simulator, but it is very slow to simulate native PIC machine code, even on a multi GHz PC. No… What is needed is not a target simulator but a simple basic, but complete “Standard C” compiler to test straight C code on a PC.

After a few minutes of searching I found Pelles C [1] a wonderfully simple, but complete freeware C and fully functional IDE written by Swedish developer Pelle Orinius. This is a native implementation of C on Windows with a very easy to use native debugger and you can easily spit stuff out to a console window using printf(). Just like with Quick C, but modernized.

The footprint is very light at 18 MB for the basic compiler and 51MB if you want to include the windows extensions to write x64 windows apps with it.

There are some tutorials on YouTube, most notably the one on how to get debugging going and in 20 minutes I was writing, compiling and debugging projects on my own, and that perfectly fit my needs.

This is truly a very easy to use C compiler and fully functional IDE (with code complete) requiring no funny headers or setups to run on Windows. The only headache I had was with the usual Windows User Account Control (UAC), where on my first attempt to compile, I was warned that the files could not be written to disk. A quick setting of the directory permissions to me for full read/write access solved the problem.

The simplest program you can write in C is literally,

int main(void)
{
int var;
var = 10;
}

No #includes needed and you can single step through this program to see the execution as simple as it is.

To get way more complex and printf() something to the output window is no harder than this,

#include <stdio.h>
int main(void)
{
int var = 0xab;
printf(“Var = %0x”, var);
}

I always write my code with standard C typedef's for my variables just so it is clear what type I mean (and will actually get). In my code, variables are always declared like this,

uint8_t var;
int16_t var2;
uint32_t var3;

I thought that I would have to write a small <stdint.h> include file for these definitions, but no, there it was, in the Pelles C include directory. Now that's nice, I didn't even have to write <stdint.h>, that's a first!

As I said, no funny windows setups required, just plain and simple “C”. With all the features of a modern Windows IDE (read: Code Completion) and as a plus the IDE even uses Visual Studio like Icons and shortcut keys, like F9 to toggle a breakpoint.

Using Pelles C we have a simple, modern way to Breadboard our embedded C code in a time effective manor using all the resources of our PC's and not depending on actual hardware and it's constrained debugging.

This is really good and useful software...


References:

[1] Pelles C homepage: www.pellesc.de


By: Steve Hageman / www.AnalogHome.com
We design custom electronic Analog, RF and Embedded system for all sorts of industrial products and clients. We would love to hear from you if we can assist you on your next project.


No comments:

Post a Comment