SAM4N problems with memcpy from flash

1 post / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

I have created a custom bootloader that uses an SPI flash to store images. Everything is working well, except that when the firmware first boots it needs to backup the current firmware to the flash. To do this, I have a loop that does a memcpy from a pointer to the SAM flash memory into a buffer, and then writes the buffer to the SPI flash. What I have discovered is that after memcpy the contents of the buffer are not always the same as what is actually stored in the SAM flash memory. Here is an excerpt from the spi flash write function to make this clearer:


flash_write(spi_flash_ptr addr, uint32_t size, uint8_t *buf) {
    uint8_t cmd[4 + PAGE_SIZE];
    int8_t retval = ERR_IO_ERROR;

    if (size > PAGE_SIZE) {
        retval = ERR_INVALID_ARG;
        goto end;

    cmd[0] = PageProgram;
    cmd[1] = (uint8_t)((addr >> 16) & 0xff);
    cmd[2] = (uint8_t)((addr >> 8) & 0xff);
    cmd[3] = (uint8_t)(addr & 0xff);

    /* We need to write enable before we can write */
    if (write_enable() != STATUS_OK) goto end;

    memcpy(cmd + 4, buf, size);

This failure is repeatable with certain flash memory addresses: when this function is called with buf = 0x404300, and size = 256, the bytes stored at 0x404354 and 0x0404355 are copied incorrectly. If I stop execution just after the memcpy (last line), cmd has incorrect content, but only for those two bytes, the rest is perfect.


I can't really understand how this can happen. I have played with the EFC settings. By default it is set to 128 bit access mode with 6 wait states. These don't seem to make any difference however. 


Can anyone provide any help? Thanks.

Last Edited: Fri. Nov 10, 2017 - 03:28 PM