I'm using a Mega2560 board.

My project needs to store some large constant data. To preserve the first 64kb flash chunk, I write these data on addresses beyond this limit.

But there is either an issue doing that or I'm doing something wrong...

After writing data the first time, I can read all bytes without no problem, but if I rewrite these data with new ones, the new data are not written correctly. It seems that, before reprogramming, the flash over 64kb are not erased.

Is this correct? How can I resolve this issue?

Note that, to access the data over 64kb, I use a far address.

Reading inside the bootloader source (stk500v2) code, I found that it doesn't erase pages whose addresse falls over 64kb to preserve the bootloader area. It seems that the bootloader is placed at the boundary of the first 64kb.

The following is the relevant the portion of the bootloader source:

// erase only main section (bootloader protection)
// APP_END is below 64KB
if (eraseAddress < APP_END )        
    boot_page_erase(eraseAddress);  // Perform page erase

/* Write FLASH */
do {
    lowByte     =   *p++;
    highByte    =   *p++;

    data        =   (highByte << 8) | lowByte;

    address =   address + 2;    // Select next word in memory
    size    -=  2;              // Reduce number of bytes to write by two
} while (size);                 // Loop until all bytes written


migrated from stackoverflow.com Aug 30 '17 at 15:38

This question came from our site for professional and enthusiast programmers.

Your Answer


Browse other questions tagged or ask your own question.