PIC32MZ flash write problem
PIC32MZ2048EFG064, xc32 2.40
I'm not using Harmony and attempting to write my own flash functions based on the flash datasheet.
The flash erase function works. I set a breakpoint on the flash erase function, read flash memory in MPLAB and see that it's random data. I then allow the erase function to execute, re-read the flash memory, and see the entire section of flash is now FFs.
But when the flash write function runs, it writes random data. The good thing is it's writing data, but it's wrong.
unsigned int rowbuff;
NVMADDR = 0x1D008000;
NVMSRCADDR = (unsigned int)((int)rowbuff & 0x1FFFFFFF);
NVMCONbits.NVMOP = 0x3; // NVMOP for Row programming
NVMCONbits.WREN = 1;
NVMInitiateOperation(); // see Example 52-1
NVMCONbits.WREN = 0;
NVMInitiateOperation() is below and taken directly from the datasheet:
int int_status; // storage for current Interrupt Enable state
int dma_susp; // storage for current DMA state
// Disable Interrupts
asm volatile("di %0" : "=r"(int_status));
// Disable DMA
DMACONSET=_DMACON_SUSPEND_MASK; // suspend
while((DMACONbits.DMABUSY)); // wait to be actually suspended
NVMKEY = 0x0;
NVMKEY = 0xAA996655;
NVMKEY = 0x556699AA;
NVMCONSET = 1 << 15; // must be an atomic instruction
// Restore DMA
DMACONCLR=_DMACON_SUSPEND_MASK; // resume DMA activity
// Restore Interrupts
if(int_status & 0x00000001)
NVMCON shows no errors, and I have no idea where the data is coming from that's being written. I do realize rowbuff through rowbuff is indeterminate in this case, but I'm not seeing 0-3 either starting at 0x1D008000.
What am I doing wrong? I thought the example would work no problem, but I guess not.