0

I am trying to program and verify a block of internal Flash on a PIC32MX360F512L. I have a function that erases,programs and verifies a 4096 byte block at a time. When I run it, the function hangs trying to verify the first byte.

BOOL Loader_ProgramFlashBlock(unsigned long int adr, unsigned int *p )
{
unsigned long int CurrentAddress;
unsigned long int PageEndAddress;
unsigned int     *pData;
unsigned int      nvmResult;

// Calculate the beginning and ending addresses of the page.
CurrentAddress = adr;
PageEndAddress = CurrentAddress + FLASH_BLOCK_SIZE;
pData          = (unsigned int *)p;

    // Check to see if the page has been erased
    { 
        // If not, erase the page & log track it
        nvmResult = NVMErasePage((void *)CurrentAddress);
        if (nvmResult != 0)
        {
            // Error erasing Flash page
            return FALSE;
        }
    }

    // Program the block to Flash
    while (CurrentAddress < PageEndAddress)
    {
        if ( NVMWriteWord( (void *)CurrentAddress, *pData ) != FALSE )
        {
            // Error Writing Flash
            return FALSE;
        }
        pData++;
        CurrentAddress += sizeof(unsigned int);
    }

    // Verify that the block was written correctly
    // (This check will identify writes to a Flash block that was not fully erased.)
    CurrentAddress = adr;
    pData          = (unsigned int *)p;
    while (CurrentAddress < PageEndAddress)
    {
        // Compare buffer contents to Flash contents
        if (*((unsigned int *)PA_TO_KVA1(CurrentAddress)) != *pData)
        {
            // Flash and buffer did not match.
            return FALSE;
        }
        pData++;
        CurrentAddress += sizeof(unsigned int);
    }


    return TRUE;

} // Loader_ProgramFlashBlock

The function hangs trying to verify the first WORD of the the flash at the line:

if (*((unsigned int *)PA_TO_KVA1(CurrentAddress)) != *pData)

The erase and the data write appear to have worked. Any suggestions what is causing this?

This code works in another application.

Dennis Kerrisk
  • 2,715
  • 2
  • 17
  • 22

1 Answers1

0

Which memory block do you override? What data is situated there? Don't you override some functions used by your loader, or some interrup handlers, that may accure while you are writing?

kirill
  • 366
  • 1
  • 8