Introduction
The Celtic Engineer is a weekly newsletter produced by Celtic Engineering Solutions. We hope you enjoy it. If you have any suggestions for topics, would like to give feedback or want your email added to the distribution list please send an email to [email protected]
As if we needed another protocol
These days it seems like everyone has their own way of doing things. When it comes to programming a microcontroller, that statement still holds true. If you are like me, then when you find something that works you keep using it until you can’t because that makes your life easier. I have gotten used to using a JTAG connector and while I am not an expert, it feels comfortable and I like that feeling.
In the past few months, I was working with the engineers at COG9 (follow the link to get to their website) in SLC and they were programming an MSP430 for a project we were working on using Spi-Bi-Wire. When hardware and firmware folks work together, it is very useful if both parties have hardware and both can independently program the Flash. While this was not a very difficult task, it was challenging and I thought it might be useful to go over how I did it for the first time so it will be easier if you decide to jump in as well.
What is Spy-Bi-Wire?
As the name implies it is a two-wire interface (if you don’t count power and ground, which you will need whether you count them or not). It is a serialized JTAG protocol developed by Texas Instruments for their family of MSP430 microcontrollers. You will see two SBW pins on these microcontrollers, SBWTCK (Spi-Bi-Wire Test Clock) and SBWTDIO (Spi-Bi-Wire Test Data Input Output).
The protocol uses time-division multiplexing (right?!) and allocates three timeslots, TMS_SLOT, TDI_SLOT and TDO_SLOT. That is probably more than we need to know unless you plan to make your own programmer. I used the MSP-FETFlash Emulator Tool (the black box). It has a 14-pin output connector. You are, of course, only going to use 4 of those.
Let’s get dirty
The first pin is the easiest, ground. That is pin 9 on the ribbon connector. Next you need to connect power. Here you have a choice. If your target board has its own power you will connect it to pin 4 of the ribbon connector (VCC_TARGET). If on the other hand you want to have the programmer to provide power you will connect the VCC of your target to pin 2 of the ribbon connector.
Next comes the fun stuff. Connect SBWTDIO to the TDO pin, pin 1 on the ribbon connector. And finally, connect SBWTCK to the TCK line, pin 7 of the ribbon connector. As you can see from the picture in figure 1, I hacked one of the adapter boards I talked about last week, but you could easily do this with a perf board of some kind.

Figure 1 SWD programming setup

I am sure there are many ways of programming the MCU, but I was provided with a program.hex file. I downloaded the MSP430Flasher, which is an executable so be prepared to run it out of a command window. After opening a command window, and pointing it at the MSPFlasher directory where I had also placed my copy of program.hex, I issued the following command: msp430flasher.exe -w program.hex. I got an error. It did not work!
Mayday-Mayday
I realized that when I use the regular JTAG programmer with an IDE, I always start by erasing the chip (because if you don’t it usually does not work). So, I did that and got the same error. As I poured over datasheets and web pages I found a note that said to make sure that the distance from the target to the programmer did not exceed 20 cm. If you take a look at figure 1 it will be obvious I had made a boo-boo. The ribbon cable alone is that long. As I looked at the available instructions in the command window I spotted my salvation, the ability to control the transfer rate (default is medium speed). I issued the following commands:
     Msp430flasher.exe -j slow -e erase_all
     Msp430flasher.exe -j slow -w program.hex
I had to cycle power (as indicted in the datasheet) but that was it. I had just used a 4-pin connector to program an MCU. Pretty neat. And I didn’t have to go and beg the firmware guy to program my hardware (they hate that you know).
Now what?
The next step is, of course, to make a better interface for the next time. I will make a small board that has a 14-Pin keyed connector on one side and a 4-pin connector on the other. I will make a shorter 14-pin ribbon connector and I will make sure that the 4-pin cable or connector, as well as the 4-pin connector on the PCB, are clearly marked so I know the orientation to plug them in.
I hope you found this useful. If you give it a try, please let me know about your experience.
Final thoughts
This newsletter is sponsored by Celtic Engineering Solutions LLC, a design engineering firm based out of West Jordan, Utah, which can be found on the web at: www.celticengineeringsolutions.com. You can find the newsletter on the company blog, LinkedIn or in your inbox by subscribing. Send your emails to The Celtic Engineer at: [email protected], with the subject line SUBSCRIBE.