Arduino bootloader on a mega128

Hello, I know this is a little off the topic of the Arduino, but Wiring doesn't seem to provide any information about the bootloader. I just got a nice ET-AVR Stamp (m128) module from futurlec.com and would like to use the same bootloader on it. From what I can see the bootloader already compiles properly for the mega128 (the only problem so far is EEPE isn't defined). Now I'd like to upload the bootloader, but avrdude refuses to upload it, spitting out a "initialization failed, rc=-1" error. My guess is that fuse bits need to be changed and so does the "--section-start=.text". Do you guys have any idea about this?

Thanks in advance :)

You may need to change the fuses and section-start settings, but the error you're getting makes it sound as though something more fundamental is going wrong during the upload. Are you using a hardware programmer to upload the bootloader? Which one? What command line are you using? Is the board powered? Can you upload other code to it (besides the bootloader)?

Apparently I had the ISP connector backward (even though the red line and the 1 matched and powered seemed to be applied properly). I changed the fuses to efuse0xFD high:0x99 low:0x7f (used the nice fuse calculator) but haven't changed the text start as I had no idea what to put. It uploaded fine after I reversed the connector :P What should I do about "while(bit_is_set(EECR,EEPE));"? I just have it commented out at the moment. Anyhow, I can't seem to upload a sketch. I used the Arduino's FTDI for USB>TTL. AFAIK the mega128 uses PE0 and PE1 for the serial. I also noticed that the wiring.c file included in the Arduino distribution doesn't compile for the mega128.

But hey! It's a good start :)

Ok I changed the fused to efuse:0xFE hfuse:0x19 lfuse:0x3f

16K CK + 64ms BoD enabled at 4v Boot reset vector enabled Boot Flash at 4k SPI/JTAG/On-Chip Debug enabled Watchdog Enabled

Now the let on PB7 flashes rapidly. Resetting the board shows a ~half second delay before the led starts flashing. But I still can't upload on either PE0/1 nor PD2/3.

As far as I can tell, the monitor function should be enabled as the MCU type is m128. But nothing shows up on the serial port.

EDIT: I changed the fuse again to E:0xFF H:0x98 L:0xBF this time the led flashes three times and goes off. But still no go on the monitor function or uploading.

EDIT 2: I noticed the Wiring software had the upload rate set to 115200 I changed it back to 19200. But again, it's a no go.

Ok so I added some echo'ing after the led flashing. I can confirm that serial on PE0/1 works properly. But no upload :/

EDIT: I had failed to notice the !!! required to enter the monitor. Yeap the monitor works too!

What happens when you try to upload? What if you try uploading from the command line with avrdude and the verbose flag set (-v -v -v -v)?

Here's the output from avrdude

$ avrdude -c stk500 -P /dev/tty.usbserial-A4001cTz -b 19200 -p m128 -U flash:w:led_blinks.hex -v -v -v

avrdude: Version 5.3.1, compiled on Jun 10 2007 at 13:39:01
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         System wide configuration file is "/usr/local/etc/avrdude.conf"
         User configuration file is "/Users/mlalonde/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port            : /dev/tty.usbserial-A4001cTz
         Using Programmer      : stk500
         Overriding Baud Rate  : 19200
avrdude: successfully opened stk500v1 device -- please use -c stk500v1
         AVR Part              : ATMEGA128
         Chip Erase delay      : 9000 us
         PAGEL                 : PD7
         BS2                   : PA0
         RESET disposition     : dedicated
         RETRY pulse           : SCK
         serial program mode   : yes
         parallel program mode : yes
         Timeout               : 200
         StabDelay             : 100
         CmdexeDelay           : 25
         SyncLoops             : 32
         ByteDelay             : 0
         PollIndex             : 3
         PollValue             : 0x53
         Memory Detail         :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    12    64    0 no       4096    8      0  9000  9000 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         33     6   128    0 yes    131072  256    512  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500
avrdude: ser_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding

Here's how I've setup the fuses:

Try adding one more -v (four total) so we can see any bytes that are going back and forth.

Here is it

$ avrdude -C ~/Applications/Development/Arduino/arduino-0009/tools/avr/etc/avrdude.conf -c stk500 -P /dev/tty.usbserial-A4001cTz -b 19200 -p m128 -U flash:w:led_blinks.hex -v -v -v -v

avrdude: Version 5.3.1, compiled on Jun 10 2007 at 13:39:01
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         System wide configuration file is "/Users/mlalonde/Applications/Development/Arduino/arduino-0009/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/mlalonde/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port            : /dev/tty.usbserial-A4001cTz
         Using Programmer      : stk500
         Overriding Baud Rate  : 19200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: successfully opened stk500v1 device -- please use -c stk500v1
         AVR Part              : ATMEGA128
         Chip Erase delay      : 9000 us
         PAGEL                 : PD7
         BS2                   : PA0
         RESET disposition     : dedicated
         RETRY pulse           : SCK
         serial program mode   : yes
         parallel program mode : yes
         Timeout               : 200
         StabDelay             : 100
         CmdexeDelay           : 25
         SyncLoops             : 32
         ByteDelay             : 0
         PollIndex             : 3
         PollValue             : 0x53
         Memory Detail         :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    12    64    0 no       4096    8      0  9000  9000 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         33     6   128    0 yes    131072  256    512  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500
avrdude: Send: A [41] . [80]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [02] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [81]   [20] 
avrdude: ser_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding

Weird, it's just asking for the firmware version number, which is just a constant in the bootloader source code. And it can get the first byte, which makes it seem like the bootloader is running properly. Do you always get the same output?

Does the monitor mode work reliably?

Maybe the serial / USB communication is unreliable? A loose wire possibly?

Ok so I rigged up a max232 and used another usb>serial adaptor. That seemed to have helped. Also I changed to bootloader to for it to user UART2, which doesn’t have pins on the 10 pin ICSP. That seemed to have helped too (and anyway it’s more practical cause I can keep the USBTiny plugged in at the same time and debugging with screen). I also tied RTS to reset with a resistor to allow soft-reset. Here’s the output at this point:

$ avrdude -C ~/Applications/Development/Arduino/arduino-0009/tools/avr/etc/avrdude.conf -c stk500v1 -P /dev/tty.usbmodem0020121D1 -b 19200 -p m128 -U flash:w:led_blinks.hex -v -v -v -v

avrdude: Version 5.3.1, compiled on Jun 10 2007 at 13:39:01
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         System wide configuration file is "/Users/mlalonde/Applications/Development/Arduino/arduino-0009/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/mlalonde/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port            : /dev/tty.usbmodem0020121D1
         Using Programmer      : stk500v1
         Overriding Baud Rate  : 19200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
         AVR Part              : ATMEGA128
         Chip Erase delay      : 9000 us
         PAGEL                 : PD7
         BS2                   : PA0
         RESET disposition     : dedicated
         RETRY pulse           : SCK
         serial program mode   : yes
         parallel program mode : yes
         Timeout               : 200
         StabDelay             : 100
         CmdexeDelay           : 25
         SyncLoops             : 32
         ByteDelay             : 0
         PollIndex             : 3
         PollValue             : 0x53
         Memory Detail         :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    12    64    0 no       4096    8      0  9000  9000 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         33     6   128    0 yes    131072  256    512  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
avrdude: Send: A [41] . [80]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [02] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [81]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [01] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [82]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [98]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [03] 
avrdude: Recv: . [10] 
         Hardware Version: 2
         Firmware Version: 1.16

avrdude: Send: A [41] . [84]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [85]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [86]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [87]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [89]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: Send: A [41] . [81]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [01] 
avrdude: Recv: . [10] 
avrdude: Send: A [41] . [82]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: Recv: . [10] 
avrdude: Send: B [42] . [b2] . [00] . [00] . [01] . [01] . [01] . [01] . [03] . [ff] . [ff] . [ff] . [ff] . [01] . [00] . [10] . [00] . [00] . [02] . [00] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: Send: E [45] . [05] . [08] . [d7] . [a0] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: Send: P [50]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: AVR device initialized and ready to accept instructions

Reading |                                                    | 0% 0.00savrdude: Send: V [56] 0 [30] . [00] . [00] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
avrdude: Send: V [56] 0 [30] . [00] . [01] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
Reading | #################                                  | 33% 0.02savrdude: Send: V [56] 0 [30] . [00] . [02] . [00]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [00] 
avrdude: Recv: . [10] 
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.

avrdude: Send: Q [51]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 

avrdude done.  Thank you.

My Arduino does the same "avrdude: Device signature = 0x000000". It's a chip I burnt myself with the usbtiny. There's no side effect and I know from the usbtiny forum that others have the same "issue" and all report it's not really an issue. But here if I use -F, the upload also fails.

Which avrdude are you using? The one that comes with Arduino has been patched in a way that should fix the 0x000000 device signature problem. In any case, as you say, it shouldn't be a problem (there are two ways to read the device signature, and avrdude happens to use one while the bootloader only implements the other). What error do you get if you use the -F flag or the avrdude from Arduino?

I tried both OSX AVR pkg's avrdude and the Arduino's. Same result, it fails with avrdude: stk500_recv(): programmer is not responding either while writing or writing. After that the bootloader seems to reset (the leds flashes). And that's with or without the soft reset. (Actually it seems to fail more often than anything while writing at 28%)

Do you know if you're trying to write to the EEPROM? If so, the lines you commented out could be a problem.

Otherwise, I'm not quite sure what to suggest. It sounds like flaky communication. You could try recompiling the bootloader to run at a slower baud rate and adjust your command line accordingly. Or just burn code to the board with whatever programmer you used to get the bootloader on there.

No I'm not trying to do anything with the EEPROM (unless it does something by default). I tried reducing the speed to 9600 but the exact same thing happens. The upload fails in the middle (5% this time) and the bootloader resets (led flashes). As for the flaky connection, I just tried going back to using the Arduino's FTDI... and same thing! Except now it fails at 28% 0.o Is it only me that has these weird problems all the time? :p

EDIT: I also changed the text section to --section-start=.text=0x1E000 as per the original ATMegaBOOT Makefile. Same thing. I'm starting to blame noise on the board. There are a few other ICs (all I2C) on there, but these were working fine, and there are also plenty of 0.1µF caps all around the board. Maybe I should have the CKOPT fuse high?

This morning I was able to “upload” something. I didn’t change anything, I just hit enter on the same command as before when I woke up. But the program didn’t work. But I just noticed there actually an error when verifying the bootloader burn (the rest of the burn seems to work):

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x1e000
         0x0c != 0xff
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

make: *** [isp] Error 1

Also, when I use uisp to read back the fuses (couldn’t get that to work from avrdude) here is what I get

$ uisp -dprog=stk500 -dserial=/dev/tty.usbmodem0020121D1 -dspeed=9600 -dpart=atmega128 --rd_fusesFirmware Version: 1.16
Atmel AVR ATmega128 is found.
Firmware Version: 1.16

Fuse Low Byte      = 0x00
Fuse High Byte     = 0x00
Fuse Extended Byte = 0x00
Calibration Byte   = 0x00  --  Read Only
Lock Bits          = 0x00
    BLB12 -> 0
    BLB11 -> 0
    BLB02 -> 0
    BLB01 -> 0
      LB2 -> 0
      LB1 -> 0

I’m all out of ideas now :S

Hmm, you don't want any verification errors when you burn the bootloader. Especially not in the first byte. :)

Were you trying to read the fuses back through the bootloader? That probably won't work. You need to do it with your hardware programmer.

Can you get the bootloader to burn with no verification errors?

Ok everything seems to work. Uploading too. But not the program. I had it compiled with wiring (only way to get it to produce a .hex is to fail an upload, go figure). Couldn't get the build to work from a Makefile, I'll work on that and get back :)

It appears that when I uploaded it removed the bootloader 0.o The program would load right away and I couldn't enter the bootloader anymore. Now when I try to burn the bootloader again "avrdude: initialization failed, rc=-1" :S