Friday, May 18, 2012

Bootloaders: How they Work.

Micro controller units are devices which contain a processing unit, programming and data memory and I/O. Early MCU's could not modify its program memory only the data memory. The program memory of present day MCU's can be modified with an in circuit programmer - ISP or ICSP - thru designated pins on the device or by the processor using special instructions.

A bootloader  is firmware, usually written into upper memory, which is executed when the MCU is reset. Typically, on reset, the bootloader waits for data to be input on the serial port and if this does not happen in a few seconds the bootloader jumps to the previously loaded code. If the data does start flowing, it is checked for correct protocol and loaded into program memory. The Atmel ATmega and the Microchip 18F MCU's handle bootloaders in a similar way.

A bootloader PC application interfaces with the MCU using the serial port. Before using the pc app to download the hex code, the reset switch at the MCU must be pressed. This sequence can be simplified by the PC application first triggering the serial port's DTR signal which at the MCU is connected to the reset pin. On the Arduino this is called auto reset.


Sunday, May 13, 2012

Programming the Optiboot into the ATmega328

I have been using the PicKit2 and a ZIF socket to program the 18F series of MCU. This setup is inexpensive and handy. I would like to have a similar setup for use with the Atmel mcu's.

USBasp: The least expensive ATmega in circuit programmer, ISP,  I have come across is the USBasp which is sold on many sites and all seem to be by different unknown manufacturers. The USBasp sold by HobbyKing for $4.50 had good reviews and a cable with both 10 pin and 6 pin connectors. The Arduino boards, as well as my designs, use a 6 pin ISP connector. I also located 28 pin, 0.3" ZIF sockets made by Textool,  # 228-3341, which I cannot find in the Textool datasheets since it has been discontinued.  I ordered and have received 2 sockets and they indeed have that number. Here are some sources for the USBasp:
  1. HobbyKing of HK: USBasp programming device for Atmel processors
  2. XHeli: KK control board USBasp
  3. Fun4diy: USBasp kit
  4. ProtoStack: USBasp AVR Programmer
Except for the HobbyKing unit all the USBasp above have 10 pin cables so that a 10 pin to 6 pin adapter will be needed. If you are good at making IDC cables you can make a cable similar to HobbyKing's.

EMS ISP Shield:  Evil Magic Science makes a Arduino shield kit that contains a ZIF socket and can be used with the Arduino ISP sketch to program bootloaders into ATmega MCU's. Adafruit sells a similar kit. The bare pcb from the EMS kit, which is sold separately, could be used as a standalone ZIF board with the USBasp to do the same thing.

OptiLoader: This sketch, which can be found here, turns a Arduino board into a stand alone optiboot programmer. The sketch contains images of OptiBoot hex files for several ATmega devices. When the reset switch is pressed, OptiLoader looks at the attached ATmega device to determine what it is and then programs it with the correct OptiBoot. While doing this, sequence data is sent out the serial port which can be monitored with the Arduino IDE serial communication tool.

For a setup using solderless breadboards look here. My setup will use a modified Evil Mad Science ISP shield, purchased as a bare pcb, and an Arduino Duemilanove

AdaLoader: This sketch, which can be found here, is based on the OptiLoader, but uses LED's and a beeper as progress feed back. The AdaLoader sketch has only one bootloader image and the LadyAda site describes how to change the image. The AdaLoader uses LED's for PROG and ERROR connected to pins A0 and D8, respectively, and a beeper to indicate program completion at pin A3. To program the bootloader press reset after loading the ATmega into the ZIF socket assuming your using the ISP shield.

ArduinoISP: This sketch which is part of the Arduino IDE download and can be found in the Examples folder turns an Arduino board into and AVRISP programmer. The ArduinoISP uses LED's for PROG, ERROR and HEARTBEAT connected to pins D7, D8 and D9, respectively.

ArduinoISP2: This sketch which is available at LaydAda is based on the ArduinoISP sketch, but has been modified to work with Arduino IDE 1.0. The ArduinoISP2 uses LED's for PROG and ERROR using A0 and D8. A beeper connected to A3 signals program complete. To program the ATmega328P device, first, dowmload the ArduinoISP2 sketch to the Arduino, then using the IDE do Tools > Burn Bootloader > w/Arduino as ISP. The green LED will light indicating the bootloader is being loaded. When loading is complete the green light will go out and the IDE will show "Done burning bootloader".

EMS ISP Shield Modifications:
Changes
  1. Cut land from reset switch and connect switch to Arduino Reset pin.
  2. Cut land from anode of Hello LED and connect to A0 pin.
  3. Add beeper and connect to pin A3.


New ISP Loader Shield
Project Objective: To design a shield which can be used with all of the above sketches.


Specification List:
  1. 28 pin ZIF socket
  2. Reset switch paralleling Arduino board reset switch
  3. Power switch for socket
  4. Both 6 and 10 pin ISP connectors with target power jumper
  5. LED's for PROG, ERROR and HEARTBEAT
  6. Beeper
  7. Serial LCD connector
The PCB for the new ISP shield has been received and assembled. The shield was plugged into a Duemilanove, the Arduino IDE 1.0 loaded, the Arduino ISP compiled and downloaded. Using the Tool menu, Burn Bootloader was selected and got a AVRdude sync error. I shifted to IDE 0023 with same results. From reading the web, this is a Auto reset problem. At this point I substituted my home grown Arduino clone, GMduino, described elsewhere in this blog. The only real difference between my clone and the commercial boards is the use of a .1uf capacitor for the auto reset circuit and a switch to disable auto reset.. The commercial boards use a resistor. Using the IDE 0023 I programmed ATmega328P device right off. Not only that, disabling auto reset was not necessary. Is it the capacitor? More testing.


ISPL Shield Rev A
Schmatic:

ISP Loader Shield Rev B

Problems with AutoReset: Normally, before a sketch is downloaded to the Arduino the serial port DTR signal  triggers which cause a reset and the bootloader to execute. AVRdude when it connects to the Arduino board also triggers DTR on the serial port causing an Auto Reset.


Still working on this. 6/6/12



Sunday, May 6, 2012

USB FTDI Breakout Problems

Since many of my projects use a USB FTDI breakout with a 6 pin output connector, I have been examining the available breakout boards.  At the same time I have been examining the Optiboot loader for the Arduino which has added additional questions about these breakouts. To date, the only breakout I have purchased is the Basic USB FTDI breakout DEV 09716 from Sparkfun.

First, a study of the FTDI chip shows a pin, VCCIO, which control the interface signal levels for the chip. This pin should be tied to 5V, if driving a 5V devices such as the PIC 18F2520 and the ATmega328, or to 3.3V for 3.3Vdevices. If left unconnected, it defaults to 3.3V. On the Sparkfun it is left floating and a work-around is not easy; therefore, the DEV09716 produces 3.3V signals when driving a 5V ATmega or PIC device. Modern Device has found that with the Optiboot this can lead to download problems.

Though I do not intend to power my development boards from the USB, for those who do, you should noted that the USB can source a maximum of 500ma at 5V, and a maximum of 50ma at 3.3V from the internal LDO regulator on the FTDI chip. Considering the way I will use these breakouts, it would be more useful to tie the power pin of the breakout to VCCIO. Doing this would make sure the interface voltage of the FTDI chip matches the MCU being used; however, in this configuration the breakout could not power the PCB. From the schematic, the Amicus processor card uses this method, that is VCCIO of the FTDI chip tied to the VCC of the 18F25K20. Since the FTDI chip is on the Amicus PCB, the USB can power the board and, since they have an onboard  3.3V regulator, they do not have the current limit problems.

A final requirement for the USB breakout for use with my boards is that the output connector be mounted on the bottom of the breakout as with the Sparkfun DEV 09716.

Here are my recommendations for USB breakouts:
  1. Modern Device, BUB 1 @ $14.. This unit makes it possible to connect up just about any way you want. Because of this it is bigger than the competition. This unit has a poly fuse and a ferite filter.
  2. Modern Device, BUB 2 @ $14. Similar to Friend with addition of an on board 3.3V optional regulator. The output connector is right angle and is shipped unsoldered. Fused.
  3. Adafruit, USB Friend @ $14.75. For my uses this has all the jumpers, as solder bridges, that I need. No fuses or filters.
  4. DFRobot, DFR0065 @ $15.95 from Jameco. This seems to be alright but I have asked some question of DFRobot so we will see what their answers are. Poly fuse.
Whatever unit I purchase it will be tested in several projects and will be reported here.