• AVR Freaks

Hot!PIC32MZ flash write problem

Author
jrmymllr
Super Member
  • Total Posts : 182
  • Reward points : 0
  • Joined: 2011/06/11 06:05:25
  • Location: MPLS
  • Status: offline
2020/03/28 05:07:01 (permalink)
0

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[512]; 
rowbuff[0]=0x1111;
rowbuff[1]=0x2222;
rowbuff[2]=0x3333;
rowbuff[3]=0x4444;
NVMADDR = 0x1D008000;
NVMSRCADDR = (unsigned int)((int)rowbuff & 0x1FFFFFFF);
NVMCONbits.NVMOP = 0x3; // NVMOP for Row programming
NVMCONbits.WREN = 1;
NVMInitiateOperation(); // see Example 52-1
while(NVMCONbits.WR);
NVMCONbits.WREN = 0;

 
NVMInitiateOperation() is below and taken directly from the datasheet:
 
void NVMInitiateOperation(void)
{
 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
 if(!(dma_susp=DMACONbits.SUSPEND))
 {
 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
 if(!dma_susp)
 {
 DMACONCLR=_DMACON_SUSPEND_MASK; // resume DMA activity
}
 // Restore Interrupts
if(int_status & 0x00000001)
{
asm volatile("ei");
}
}

 
NVMCON shows no errors, and I have no idea where the data is coming from that's being written. I do realize rowbuff[4] through rowbuff[511] 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.
#1

1 Reply Related Threads

    andersm
    Super Member
    • Total Posts : 2796
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: PIC32MZ flash write problem 2020/03/28 06:05:47 (permalink)
    0
    Have you read application note AN1600?
    #2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5