ATMega2560 - USBasp - program not starting

Hello Forum,
I have some hassle with the flashing of an ATmega2560 over ISP. I think it has something to do with the platform.io params for uploading: I Use just the blink example extended with some serial output on Serial3 USART of the mega. I can compile and upload the program to an Arduino Mega2560 and to an custom board with this processor on it, but the program won’t start. It does not matter if i burn the bootloader on the mega or not (but i am searching for a solution without the bootloader). One strange side effect is that I get an error of avrdude from time to time, but when i erase the chip manually, I can flash it again:
avrdude: verifying …
avrdude: verification error, first mismatch at byte 0x010e
0x50 != 0x71
avrdude: verification error; content mismatch

platform.ini:

[env:megaatmega2560]
    platform = atmelavr
    framework = arduino
    board = megaatmega2560
    mcu = atmega2560
    upload_protocol = usbasp
    upload_flags =
        -c usbasp
        -p atmega2560
        -v
        -i 10
        -Ulfuse:w:0x42:m
        -Uhfuse:w:0xD9:m
        -Uefuse:w:0xFD:m
        -Ulock:w:0x3F:m

Blink.cpp:

#include <Arduino.h>

void setup()
{
  Serial3.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  Serial3.println("1");
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  Serial3.println("2");
  delay(1000);
}

→ avrdude done. Thank you.

Thank you for your time!
Dirk

Maybe verification errors are a side-effect of setting the ‘ISP Clock Delay’ -i switch differently / too low?

Can you explain why chose these exact fuse bits?

I have to admit, that I tested a lot of parameters I found in several forum boards:
I think I can delete the clock delay param. This one I added because of the avrdude mismatch error. But it did not help.

The Fuses are a combination of the “default” values of thisfuse calculator:
http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega2560

The value for the HFuse is from this board. (I can’t find the topic at the moment, but it mantioned, that D9 is for starting the programm without the bootloader)

EFuse enables the BrownOut Detection

Lock results from the following avrdude message:

avrdude: WARNING: invalid value for unused bits in fuse “lock”, should be set to 1 according to datasheet
This behaviour is deprecated and will result in an error in future version
You probably want to use 0x3f instead of 0xff (double check with your datasheet first).

Can you verify that if you use the default fuses and the efuse and hfuse value for the Mega2560 preset on the Arduino ATMega2560 board, the LED works blinking?

lfuse = 0x42 and lock = 0xff as the default values for the ATMega2560.

I tried this config:
[env:megaatmega2560]
platform = atmelavr
framework = arduino
board = megaatmega2560
mcu = atmega2560
upload_protocol = usbasp
upload_flags =
-c usbasp
-p atmega2560
-v
-Uhfuse:w:0xD8:m
-Uefuse:w:0xFD:m

Upload worked fine but the Ardu is not blinking.

Update:
When I now set the HFuse to D9, the Arduino starts blinking, but 16 times slower than it should.
Code : delay(1000);
Reality 16 sec. on - 16 sec off
with some fuse I pull the break of the Mega.

Update:
When I set the LFuse to C2 (disable the CKDIV8) it blinks 8 times faster.
2 sec. on - 2 sec. off

Will cause to enable the BOOTRST vector, so the Arduino framework might need this or it crashes after doing a reset after upload.

Probably crystal oscilator settings related in the lowfuse. Try burning back a LFUSE setting which corresponds to the ATMega board hardware, i.e. 16MHz external crystal. Maybe 0xFF? ("8.00 - ? MHz Oscillator, highest startup time and delay, no divide by 8)

Ok, this looks better now. The LED is blinking and the serial port is sending data, so the baud calculations should work now.
I still run into the avrdude mismatch error from time to time, but as long as I can fix this with the avrdude erase, I can cope with that.

Thank you for your help, @maxgerhardt!

1 Like

So do both boards work now or does the custom board (with a different crystal?) need more work?

I think I have to change the fuse back to the internal crystal for the custom board. Since it has no LED on, I first try my original Code with the Arduino and get back to the custom board then. Hopefully I will handle this the next hour.

1 Like

Finally:
Got the custom board running. here is the platform.ini with which it is working:

[env:megaatmega2560]
platform = atmelavr
framework = arduino
board = megaatmega2560
mcu = atmega2560
upload_protocol = usbasp
board_build.f_cpu = 8000000L
upload_flags =
    -c usbasp
    -p atmega2560
    -v
    -e
    -Ulfuse:w:0xC2:m
    -Uhfuse:w:0xD9:m
    -Uefuse:w:0xFD:m

added the -e to erase the chip before flashing. This stops the avrdude mismatch error.

3 Likes