[chbot] Anyone seen this error ? avrdude: verification error, first mismatch at byte 0x3c000 on atmega2560

Peter Ellens ellensp at gmail.com
Mon Apr 20 19:12:50 BST 2020

Hi Everyone

While we are all locked away I have taken up some firmware debugging and 
found something surprising and thought you might want to hear about it.

The subject is a leading question. I know the cause and the solution :)

If you search the internet there are lots of people reporting this 
issue, sometimes at 0x3c001, and lot of people saying your atmega2560 is 
faulty. or claim your sketch is to large, some say use a different boot 
loader, the latter does work...  (first clue)

So what is going on with this error?

If you have an atmega2560 board (and most of us have used these at one 
time or another) you will probably have uploaded new firmware into the 
flash via the boot loader.  (even if it was only the blink example)

The standard arduino atmega2560 boot loader is at 0x3E000 and is 8k in 
size. The atmega2560   has 256K of flash memory. This means you can (in 
theory) upload programs that are up to 256K-8K large (253952 bytes)

If you should create a program that uses >  245760 bytes but less than 
the max 253952, You can get this error.

On a fresh atmega2560, if you upload a program that meets the above size 
requirements, it will upload without issues.

But. if you then change your code enough to change the bytes in the >  
245760 bytes range and upload you get the error: avrdude: verification 
error, first mismatch at byte 0x3c000 (or what ever byte past 0x3c000 
has changed.)

Now uploading a new boot loader, or even the same boot loader will reset 
back to fresh and you can upload one more time. (as this erases the 
entire flash)

What is going on?

Turns out there is a bug in the arduino boot loader, its been a very 
very long time.

In the boot loader is this snippet of code

|#if FLASHEND > 0x0F000 #define BOOTSIZE 8192 #else #define BOOTSIZE 
2048 #endif #define APP_END (FLASHEND -(2*BOOTSIZE) + 1)|

FLASHEND is 0x3FFFF on atmega2560's so the the BOOTSIZE is set to 8192, 
but the APP_END is set to (0x3FFFF - (2*0x2000) + 1) = 0x3BFFE

Later the code uses the line "if (eraseAddress < APP_END )" So uploading 
over via the boot loader will only erase bytes from 0x00000 to 0x3BFFE.

So 0x3C000--0x3E000 is not erased and can't be reused till a full erase 
is done!

(For those not familiar with this.  When you write to the flash you only 
set the 0 bits, the erase sets all bits to 1. Without the erase you 
can't change any 0's to 1's)

I've updated the code and submitted a patch for it 

The details of the patch can be found here. 

So if you come across this error, update your boot loader with the 
patched version.

Hope this was interesting to someone, it was an interesting puzzle to 
solve for me.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20200421/f75ad012/attachment.html>

More information about the Chchrobotics mailing list