通过软件编程PIC32内部闪存

时间:2014-02-20 20:53:52

标签: flash pic pic32

我正在尝试编程并验证PIC32MX360F512L上的内部闪存块。我有一个功能,一次擦除,编程和验证4096字节块。当我运行它时,函数挂起试图验证第一个字节。

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

该函数挂起试图验证该行闪存的第一个WORD:

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

擦除和数据写入似乎有效。是什么原因造成的?

此代码适用于其他应用程序。

1 个答案:

答案 0 :(得分:0)

你覆盖哪个内存块?那里有什么数据?您是否覆盖了您在编写时可能产生的加载器或某些中断处理程序所使用的某些功能?