<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi Everyone<br>
    </p>
    <p><br>
    </p>
    <p>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. <br>
    </p>
    <p>The subject is a leading question. I know the cause and the
      solution :) <br>
    </p>
    <p>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)
      <br>
    </p>
    <p>So what is going on with this error?</p>
    <p>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) <br>
    </p>
    <p>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) <br>
    </p>
    <p>If you should create a program that uses >  245760 bytes but
      less than the max 253952, You can get this error. <br>
    </p>
    <p>On a fresh atmega2560, if you upload a program that meets the
      above size requirements, it will upload without issues.</p>
    <p>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.) <br>
    </p>
    <p>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)</p>
    <p>What is going on?</p>
    <p>Turns out there is a bug in the arduino boot loader, its been a
      very very long time.</p>
    <p>In the boot loader is this snippet of code <br>
    </p>
    <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 11.9px; margin-top: 0px; margin-bottom: 16px; overflow-wrap: normal; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgb(246, 248, 250); border-radius: 3px; color: rgb(36, 41, 46); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; font-size: 11.9px; padding: 0px; margin: 0px; background: initial; border-radius: 3px; word-break: normal; white-space: pre; border: 0px; display: inline; overflow: visible; line-height: inherit; overflow-wrap: normal;">#if FLASHEND > 0x0F000
        #define BOOTSIZE 8192
#else
        #define BOOTSIZE 2048
#endif

#define APP_END  (FLASHEND -(2*BOOTSIZE) + 1)</code></pre>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">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</p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">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.  <br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">So 0x3C000--0x3E000 is
      not erased and can't be reused till a full erase is done! <br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">(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)</p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;"><br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">I've updated the code
      and submitted a patch for it <a
        href="https://github.com/msproul/Arduino-stk500v2-bootloader/issues/7">https://github.com/msproul/Arduino-stk500v2-bootloader/issues/7</a></p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">The details of the patch
      can be found here. <a
href="https://github.com/arduino/Arduino-stk500v2-bootloader/pull/1/files">https://github.com/arduino/Arduino-stk500v2-bootloader/pull/1/files</a></p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;"><br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">So if you come across
      this error, update your boot loader with the patched version. <br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;"><br>
    </p>
    <p style="box-sizing: border-box; margin-top: 0px; margin-bottom:
      16px; color: rgb(36, 41, 46); font-size: 14px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: 400; letter-spacing: normal; text-align: start;
      text-indent: 0px; text-transform: none; white-space: normal;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255); text-decoration-style:
      initial; text-decoration-color: initial;">Hope this was
      interesting to someone, it was an interesting puzzle to solve for
      me. <br>
    </p>
  </body>
</html>