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
    boot_spm_busy_wait();       // Wait until the memory is erased.
    eraseAddress += SPM_PAGESIZE;   // point to next page to be 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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.