Why Stk500 and USB?
Until the beginning of this year a simple parallel port programmer was the only good programmer as it could be used for any device. All device dependent information is stored in the programmer software on your computer. The problem is however that the parallel port is slowly disappearing. Apple does not have it at all and the smaller laptops don’t have it either anymore. It’s time to look for alternatives.
Atmel had at last a good idea and published together with the avrstudio version 4.11 a completely redesigned stk500 protocol. The new stk500 protocol is totally incompatible with version 1 of the stk500 protocol but it is the right solution.
The stk500 AVR068 specification needs no longer device dependent data structures in the programmer hardware. All device dependent logic is now in the programmer software running on the PC. As of today avrstudio (for windows) and avrdude (for multiple OSs including Linux) support this version 2 of the stk500 protocol.
The physical hardware interface to the PC is USB. This way we can build a fast and modern programmer which can be used under Linux, BSD, Windows and MacOS X.
The design idea behind this USB AVR programmer
The programmer will be an In System Programmer. That is: you do not have to remove the microcontroller from the circuit. Almost all Atmel microcontrollers have for this purpose an interface called SPI (Serial Peripheral Interface). The microcontrollers can be either spi master or spi slave. A spi slave is the microcontroller which is programmed and the programmer is the master. The master is controlling the clock (pin SCK) on the SPI interface. The purpose of the programmer is therefore to “convert” the stk500 protocol to spi commands. To run spi directly over usb would be too slow for normal programming and development.
The spi protocol is implemented in the microcontroller hardware. We just enable the spi master as described in the datasheet (see references at the end).
The stk500 protocol defines a message format and appropriate commands such as CMD_SET_PARAMETER, CMD_LOAD_ADDRESS, CMD_PROGRAM_FLASH_ISP etc… The protocol sends chunks of data and this is very suitable for USB. The whole protocol is documented in the application note AVR068 (stk500 V2) from Atmel. This stk500 protocol must not be confused with AVR061, which is version 1 of the stk500 protocol and has absolutely nothing to do with version 2. Atmel has done a complete re-design.
Solving the chicken and egg problem
Since this programmer contains already a microcontroller we must find a way to initially program it. For this we need a programmer … the thing that we are about to build…
For the usb interface we use a FT232bm chip. This chip has an interesting mode called “bit-bang” mode. I have written a library called ftdibb (only 2 files .c and .h, available from http://linuxfocus.org/~guido/) which implements this bitbang mode on top of the libUSB library. LibUSB (http://libusb.sourceforge.net) uses the /proc file system to send and receive custom usb messages to any usb device on the usb bus. One problem is that this requires root permissions under Linux, the other problem is that we will send very short messages. Initially (when there is no firmware on loaded in the microcontroller) we have absolutely no storage place and no logic in the programmer. We must send all the commands directly from the PC, bit by bit. USB is fast when you send long chunks of data but it is very slow when you send only 1 bit at a time.
This is however acceptable as it saves us from having to get first a different programmer to load the initial firmware into this programmer. Therefore it is acceptable and you load the firmware only once.
In other words the avrusb500 consists really of two programmers: One internal for the initial loading of the firmware and the actual avrusb500 programmer which is a fast and stk500 V2 based programmer for every day use.
I called the bitbang programmer for the initial loading “bbpg”. I have modified the uisp programmer software for this purpose. You need to download uisp-20050207.tar.gz and then apply the uisp-20050207-usb-bbpg-patch.txt patch (cd uisp-20050207;patch -p1 < uisp-20050207-usb-bbpg-patch.txt ) or you can take the already patched sources (uisp-20050207-usb-bbpg.tar.gz, download at the end). Libusb needs to be installed before you compile the bbpg programmer. The avrusb500-X.Y.tar.gz package contains also a patched and pre-compiled binary. There is no guarantee that a pro-compiled will run on any linux distribution due to the dependencies on the library versions but it will run on many. So if you want to save some compile time then try this one first. You must name this version of uisp “uisp_bbpg”. This is what the makefiles and scripts expect.
The CD which is sold from http://shop.tuxgraphics.org together with the parts for this programmer can also be used for the initial loading of the firmware. It has the additional advantage that you can do this from the CD without root permissions.
The specification from Atmel for the STK500 communication protocol is 37 pages long. It is however not a problem to fit it into an atmega8 microcontroller. It fills about half of the available memory.
Here is the schematic drawing. The left side is the usb to rs232 conversion circuit to provide usb connectivity to the atmega8. The whole circuit is self powered. That is: unlike may other programmers (actually all, except for the simple parallel port programmers) you do not need additional pins to draw the power from the target circuit.
We can therefore continue to use the small little connector introduced with the (Programming the AVR microcontroller with GCC, libc 1.0.4). This is in my opinion the right right solution as you don’t waste space on the PCB for a bulky connector which is used only once.
For more detail: AvrUsb500 — an open source Atmel AVR Programmer using ATmega8