Arduino
Flashing an Arduino bootloader
- ATMega32 is not immediately supported, but is about equal to an ATMega8.
- There is a sketch that turns an arduino into an ISP (in system programmer), you can use that to load an arduino bootloader to an extra chip.
First I downloaded the latest version of optiboot, as it supports ATMega32. I put the bootloaders/optiboot directory in /usr/share/arduino/hardware/arduino/bootloaders/ after backing up the original version.
Then came the crucial question: what pin do I put the LED on?? PB5 (pin 13) seems standard after some quick googling.
running omake gave errors, so I ran make myself:
make atmega32 AVR_FREQ=8000000L LED=B5
this gave some very weird characters in the compile command. They seemed to originate from AVR_FREQ. So I edited the makefile, and changed the line setting the frequency to:
atmega32: AVR_FREQ ?= 8000000L
There, now it compiles.
So now we have a custom bootloader for the ATMega32. The next step is uploading.
For that, I loaded the ArduinoISP sketch, and made a connector according to the comments in that file. I didn't bother with pin 7.
Pins on the ATMega32 1. MOSI PB5 2. MISO PB6 3. SCK PB7 4. Reset
Using avrdude for programming:
avrdude -c arduino -p atmega32 -P /dev/ttyACM0 -b 19200 -e -U lfuse:w:0x64:m -U hfuse:w:0xfd:m -U flash:w:optiboot_atmega32.hex -F
But this gives errors.
avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14 avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x01 avrdude: stk500_initialize(): (a) protocol error, expect=0x14, resp=0x10 avrdude: initialization failed, rc=-1 Double check connections and try again, or use -F to override this check. avrdude: stk500_disable(): unknown response=0x12 avrdude done. Thank you.
It turns out this is because of the auto-reset feature on Arduino. It can be disabled by adding a 10uF capacitor between reset and ground. After this is done, I still get an error.
$avrdude -c arduino -p atmega32 -P /dev/ttyACM0 -b 19200 -e
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s avrdude: Device signature = 0x000000 avrdude: Yikes! Invalid device signature. Double check connections and try again, or use -F to override this check.
Well, let's override it then:
$avrdude -c arduino -p atmega32 -P /dev/ttyACM0 -b 19200 -e -F avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.02s avrdude: Device signature = 0xffffff avrdude: Yikes! Invalid device signature. avrdude: Expected signature for ATMEGA32 is 1E 95 02 avrdude: erasing chip avrdude: safemode: lfuse changed! Was ff, and is now 0 Would you like this fuse to be changed back? [y/n] n avrdude: safemode: hfuse changed! Was ff, and is now 0 Would you like this fuse to be changed back? [y/n] n avrdude: safemode: Fuses OK avrdude done. Thank you.
I decided to connect the 16MHz crystal with 2 18pf caps. Then it doesn't complain about the device signature any more.
$avrdude -c arduino -p m32 -P /dev/ttyACM0 -b 19200 -B 10 -U lfuse:r:-:h -F avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.02s avrdude: Device signature = 0x1e9502 avrdude: reading lfuse memory: Reading | ################################################## | 100% 0.02s avrdude: writing output file "<stdout>" 0x64 avrdude: safemode: Fuses OK avrdude done. Thank you.
I chose hfuse=0xdc, lfuse=0xff. Then I could write the bootloader file.
avrdude -c arduino -p atmega32 -P /dev/ttyACM0 -b 19200 -U flash:w:optiboot_atmega32.hex
I now have an ATMega32 with an Arduino bootloader. Time to unsolder the ISP pins and connect an ftdi board. This requires 4 connections: the RX/TX lines, the DTR line (used for reset), and GND. Additionally, I'll be using 5V from the USB port.
Then, I mistakenly swapped the voltage wires when adding the FTDI board. ATMega32 is now dead. I soldered in another one, this drew 100ma. I then soldered an ATMega32, but he's not working either (not sure what the problem is, possibly the clock).
After all those issues I decided that maybe it's not worth the trouble unsoldering these processors from boards that rework couldn't fix. I went to a shop and bought an ATMega168 and an ATMega328. They both cost the same, 3,95€! I followed the arduino guide, and I managed to get the bootloader installed from the Arduino IDE: Select "Arduino as ISP", connect everything to the new mcu, and upload the bootloader. The only "weird" thing is afterwards if you want the auto-reset feature: you need a couple capacitor between DTR and the reset pin... This should not only create a reset pulse, but also a double voltage when DTR goes from 0 to 1.
Mistakes
- I thought I could let it run on the internal oscillator, but I could not load the fuse bits. The chip had been salvaged, so it was programmed to run on the xtal oscillator. Maybe that's the reason? The future will tell...
- I soldered the crystal in unused pins of the breakout board. I didn't pay attention to the fact that it has an overhang, now pin 10 of the atmel is obstructed.
- When connecting the microcontroller to the usb port, I reversed polarity. this caused an overcurrent condition and the usb port shut down, but not before frying the mega32. I tried cutting off the controller in an effort to replace it, but the solder pads came loose (I had some pads come loose before when unsoldering wires, it's not a high quality pcb). I think this ends the experiment for now.
- Before unsoldering a PCB with a gas burner, I should have manually unsoldered the SMT caps close to the xtal, they are hard to locate afterwards....