Ramping Up…

Learning CurveImagine yourself on a curve, a logarithmic graph of sorts, the learning curve between beginner and expert. Where are you now? Where do you want to be? How can you get there in reasonable time? Whatever the subject, you need a plan to ramp up your skills to achieve a goal.
Of course, there is no single learning curve in software development. Wouldn’t it be wonderful to become an expert Web designer, mobile app developer, embedded systems programmer, ebook author, robotics experimenter and (fill in your dream job) all at once? Eh, probably not in one lifetime. Let’s narrow the focus, pick a platform, and build a few simple programs to start. The topic I’ve chosen to explore is embedded software. I have several goals in this blog…to advance my programming abilities, to gain visibility in the contracting world, and to spark an interest in others who might need a hand up. With a background in computer science, hardware interfacing, software packaging and testing, I am somewhere on the curve, just as you are.

Ramp Up!Ramping up means acquiring and installing all the necessary tools and becoming familiar with them enough to be productive. I’ve been ramping up for a while, trying various webby things to enable me to pursue the goals mentioned earlier.

It’s been a long haul in the wee hours with Google and YouTube, searching for a viable toolset. The options are many. For Web presentation I’ve settled on Bluehost, WordPress, and perhaps Artisteer in the future. And for hardware I tried Renesas RX62N, considered Arduino, but eventually chose to develop on a TI Launchpad MSP430.

In case you’re wondering why the MSP430, once again, three reasons. It’s super cheap, ultra-low power, and (drum roll, please……) my current work assignment targets a high end version of the MSP430 chip. Ba-da-bing, ba-da-boom!

But seriously, the chip is super easy to program via a USB wire. Texas Instruments provides free tools and boatloads of example programs to get you going. Ramp Up!

Stop ‘n Go Lights

By now you should have installed Code Composer Studio* and the USB drivers that came with it.  The USB drivers let us download to the flash ROM in the microcontroller and also let us talk to the chip from a terminal window.  More about that later.

Let’s start with the simplest of applications, a blinking lights demo.  That’s embedded programming’s version of “Hello World”. The LaunchPad has two LEDs we can control, conveniently colored red and green.  All we need to figure out is how to write to the output pins connected to the LEDs and some kind of delay mechanism to slow down the blink rate to human speeds.  Simple enough.

Hop over to TI’s Code Example page and grab the zip file for your particular processor. For those of us using the ‘2553 chip that would be at MSP430G2xx3. Unzip it in a convenient location and pull out this file: msp430g2xx3_1.c.  Copy it to your EW project directory.  In EW, right-click on the project name and add the new file to the project.  Double-click it to open it in the source code window.  The code should look like this:

//**************************************************************
//  MSP430G2xx3 Demo - Software Toggle P1.0
//
//  Description; Toggle an LED by xor'ing P1.0 in software.
//  ACLK = n/a, MCLK = SMCLK = default DCO
//
//                MSP430G2xx3
//             -----------------
//         /|\|              XIN|-
//          | |                 |
//          --|RST          XOUT|-
//            |                 |
//            |             P1.0|-->LED
//
//  Originally by D. Dang
//  Texas Instruments, Inc
//  December 2010
//  Built with CCS Version 4.2.0, etc.
//**************************************************************
#include  <msp430g2553.h>

void main(void)
{
    // Stop the watchdog timer.
    WDTCTL = WDTPW + WDTHOLD;

    // Set P1.0 direction to output.
    P1DIR |= 0x01;

    while (1)
    {
        volatile unsigned int i;

        // Toggle P1.0 using exclusive OR.
        P1OUT ^= 0x01;

        // Delay
        for (i = 50000; i != 0; i--)
        { }
    }
}

New LaunchPadLook at the comment block at the top of the file. Every source file should have a comment block like this explaining the purpose of the file and details that help the programmer understand how it all fits together. TI’s examples include a little textual block diagram. This example shows the reset button and LED output. P1.0 is shorthand for Port 1, Pin 0. If you read the silkscreen on the LaunchPad, you will see “P1.0” above the red LED at the bottom of the board, above a black jumper. Leave the jumper installed in order to turn on the LED. Also notice “P1.6” above the green LED on the right. Ignore the XIN and XOUT pins for now. Those pins connect to an optional watch crystal.

#include  <msp430g2553.h>

Now let’s pick the program apart. The first line below the comment block is an “include” compiler directive. Compiler directives don’t generate machine code, but they tell the compiler to take notice and do something. In this case, the compiler will read the contents of file “msp430g2553.h” and insert it here before continuing on. The “.h” suffix on the file name indicates a C header file. Header files typically contain definitions and references to exteral functions, that is, functions defined in a corresponding “.c” file.

void main(void)

Next we come to the main program function, which in most cases is creatively named “main”. The voids indicate that main returns nothing when it finishes (the left “void”) and takes no parameters (“void” parameter list in parentheses).

void main(void)
{
    // Stop the watchdog timer.
    WDTCTL = WDTPW + WDTHOLD;
  ...
}

C is a block structured language. Related sections of code are grouped into blocks surrounded by curly braces. Executable statements that do the work are in the main program block. The very first step in an embedded program is usually to turn off the watchdog timer (WDT) so it won’t interfere with initial configuration of the hardware. WDT serves as a dead-man’s stick, resetting the microcontroller unless the program resets the WDT before it expires. It’s a last resort for the program to repair itself when it goes off into the weeds. Because demo programs are not mission critical, they usually run with WDT turned off.

WatchdogYou might be wondering exactly how that line works. WDCTL is the control register for the watchdog timer. WDTPW is ?. We add WDTHOLD which is the WDT hold bit, turning that bit on in the control register. Look at the register diagram on page in the MSP430G2xx2 User’s Guide. It shows the bit positions for WDTP and WDTHOLD within the WDCTL control register. [TODO: research this.}

*NOTE:  IAR Embedded Workbench is a bit slicker than Code Composer Studio, in my opinion, BUT after a few weeks I hit the 4K code limit.  I copied my source code into a CCS project and haven’t looked back.

[TODO – add procecure for compiling and running the demo. Follow along the steps in CCS to get it right.]

TI MSP430 LaunchPad Development Kit

TI MSP430 LaunchPad photo

Welcome!  Let’s start today with a review of the Texas Instruments MSP430 LaunchPad kit, aka the MSP-EXP430G2.  It’s a great value for the experimenter at only $4.30. The kit includes a ‘G2553, the premier chip in the Value Line of microcontrollers. Almost an oxymoron, isn’t it. Lucky for us, this chip hits the sweet spot of cheap and loaded with features.

TI upgraded the kit in late 2011 to include this more capable microcontroller and a lesser spare chip, the ‘G2452. I’m not sure why the spare is included, maybe for breadboarders or people who can’t keep their bug-zappy fingers off the pins.

How do these devices stack up?  Check out their features in the table below, or on TI’s Product Search page. Future articles will focus on the MSP430G2553, so pay particular attention to the stats on that puppy.

TI MSP430 LaunchPad Comparison

Feature

Original Kit

New Kit

MSP430G2321

MSP430G2452

MSP430G2553

Flash ROM 2K 8K 16K
SRAM 128 bytes 256 bytes 512 bytes
GPIO 10 16 24
16-bit Timers 1 1 2
UART 0 0 1
10-bit ADC Channels 8 8 8
Temperature Sensor Yes Yes Yes
Max Frequency 16 MHz 16 MHz 16 MHz

Have I “sparked” your interest yet? Alrighty, then. You’ll need your own board and a little free software to keep up with the program. Do it now, and you’ll be up and running in a week. I’ll do my best to keep you afloat as we push the LaunchPad to its limits. These links oughta get you moving…

If you’re adventurous, feel free to choose an alternate Integrated Development Environment (IDE) such as IAR Embedded Workbench or Open Source MSPGCC, but you’ll be on your own (or maybe not…).  I use Code Composer Studio simply because I hit the code size limit on IAR EW.  The Kickstart compiler is limited to 4 kilobytes of executable code, which can be a problem if you suck in even a few C libraries.  The code limit in CCS is 16K.  We’re building from the ground up, with no cushy libraries.  Well, maybe some definition files, but NO bloat!  We’ll be good for a while.

You’ll need a decent terminal emulator like HyperTerminal (gone from Windows 7), PuTTY, or Tera Term.  I prefer PuTTY because it’s always worked for me, from way back in the terminal days.

New MSP430 LaunchPad Board Have you ordered that LaunchPad yet?  While you’re waiting on it, see if you can install the IDE and get it working.  You’ll want to set it up for the MSP430G2553 target, and select the simulator option for now, rather than the actual hardware.  Create a project and experiment.

As far as I know, you have to re-do settings every time you make a new project.  This is my only beef with IAR EW and to some extent CCS.  [TODO: verify steps to clone project in CCS]  Other than a few odd crashes…there is no easy way to clone a project!  I like to build on past projects, and keep them intact.  The closest I have come to cloning is to create a new project, change settings, copy files from the old project, ADD files to the new project, and beat on it until it compiles.  Wheeee!

Next up…

Blinkety-blinkety-blink.  We’re not ready for “Hello World” yet.  Welcome to embedded programming!

And eventually…

Bit-banging on the serial port when you can’t find a UART.  And…fixing TI’s mistake.  Any guesses what it could be?