• AVR Freaks

AnsweredHot!write operation fails when writing program flash

Author
Paba
Junior Member
  • Total Posts : 106
  • Reward points : 0
  • Joined: 2014/03/25 08:03:07
  • Location: 0
  • Status: offline
2020/08/07 02:07:26 (permalink)
0

write operation fails when writing program flash

Hi
On a PIC32MX230F128L I'm trying to write into the program memory flash for a bootloader application. The implementation of these functions such as "Write Word" or "Erase Flash" are done according to the Microchip Family Reference Sect. 5 "Flash Programming". For writing the following two functions are used:
uint8_t NVMemWriteWord(void* address, uint32_t data)
{
    unsigned int res;
    // Load data into NVMDATA register
    NVMDATA = data;
     // Load address to program into NVMADDR register
    NVMADDR = (unsigned int) address;
    //NVMADDR = KVA_TO_PA((unsigned int)address);
    // Unlock and Write Word
    //res = NVMemOperation(0x4001);
    res = NVMUnlock (0x4001);
    // Return Result
    return res;
}

 
unsigned int NVMUnlock (unsigned int nvmop){
    unsigned int status;
    // Suspend or Disable all Interrupts
    asm volatile ("di %0" : "=r" (status));
    // Enable Flash Write/Erase Operations and Select
    // Flash operation to perform
    NVMCON = nvmop;
    // Write Keys
    NVMKEY = 0xAA996655;
    NVMKEY = 0x556699AA;
    // Start the operation using the Set Register
    NVMCONSET = 0x8000;
    // Wait for operation to complete
    while (NVMCON & 0x8000);
    // Restore Interrupts
    if (status & 0x00000001){
        asm volatile ("ei");
    } else {
        asm volatile ("di");
    }
    // Disable NVM write enable
    NVMCONCLR = 0x0004000;
    // Return WRERR and LVDERR Error Status Bits
    return (NVMCON & 0x3000);
}

Whenever I try to write a word, the NVMCON.WRERR bit is set to one. Means: the write did not work but the error is set. I don't know why this always happens because from my point of view it should work because the target address is within the program memory range of this device. When inspecting the program memory with MPLABX Execution Memory Viewer I also see that the memory holds 0xFFFFFFFF at the desired address. Therefore it is already erased and should not need a preceding erase. For example the following line from the Intel Hex file which starts at address 0x1D00B000:
:10b00000022c400f0000000000601a40c0045a7f6c

It should be written to address 0x1D00B000. The first word to write is 0x0f402c02. But it always fails.
So far I did not find any documentation which would tell what could cause the WRERR bit to set to '1'. Does anyone know or could give me a hint what could cause the flash write operation to fail?
#1
NorthGuy
Super Member
  • Total Posts : 6291
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: write operation fails when writing program flash 2020/08/07 06:54:51 (permalink)
2 (1)
You may need a 250 ns delay after setting nvmop.
#2
Paba
Junior Member
  • Total Posts : 106
  • Reward points : 0
  • Joined: 2014/03/25 08:03:07
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/09 23:58:57 (permalink)
4 (1)
From my point of view a delay should not be necessary since Microchip did not mention anything about a delay in their Application Note. Furthermore I should have seen it working when stepping through the code because there I have even longer delays after setting nvmop.
 
However, I did try it with a delay but as predicted: it doesn't change anything. I do still have the WRERR bit set when writing to flash.
 
Maybe one could tell me what could cause the WRERR bit to set? I can imagine that if I try to write to a protected area that the WRERR bit will be set. But in my case this should not be the case. Or could the processor be in a bad state to write into the flash?
#3
wdy
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2016/10/05 06:32:30
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/10 05:18:17 (permalink)
0
@Paba: One possible problem could be your clock. Check the datasheet carefully, there was some kind of minimum, although not stressed enough. Maybe it was something like 4 MHz, but I don't have my notes with me at the moment.
 
If you're starting with the default fuses, there is FRCDIV=8 (by memory), so at least dial it back to FRCDIV=1 and do a test. Try the same with PBDIV for good measure, too, as I can't remember which one was the source. Better yet, study the datasheet.
#4
wdy
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2016/10/05 06:32:30
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/10 05:26:32 (permalink)
0
NorthGuyYou may need a 250 ns delay after setting nvmop.

Now that you mention it, there were two spots in the programming flow which seemed to require delays according to documents here and there, but flash programming seems to work fine without any delays on the 1xx/2xx (on my desk, room temperature, etc). And I feel tiny bit uneasy about such things.
 
I was left with the impression that this errata was specific to 3xx/4xx, and maybe even only the first gen, where the second digit is even. And then the bad documentation process made the things hard to follow. Anyone in the know of the real situation, or at least collected enough personal evidence?
 
Any employee of Microchip daring to clarify? Or even fix the relevant documents?
Which of the PIC32 families do really need delays in the programming flow (and for how long), and which do not?
post edited by wdy - 2020/08/10 05:28:57
#5
NorthGuy
Super Member
  • Total Posts : 6291
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: write operation fails when writing program flash 2020/08/10 06:52:33 (permalink)
5 (2)
wdy
Any employee of Microchip daring to clarify? Or even fix the relevant documents?
Which of the PIC32 families do really need delays in the programming flow (and for how long), and which do not?



MZ doesn't. Of the rest, some do and some don't. I never did all-inclusive tests for this. I simply use delays for all except MZ.
#6
Jim Nickerson
User 452
  • Total Posts : 6736
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: online
Re: write operation fails when writing program flash 2020/08/10 07:50:44 (permalink)
3 (2)
You might ask support to get a response from a Microchip employee Smile: Smile 
#7
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: write operation fails when writing program flash 2020/08/10 13:46:27 (permalink) ☄ Helpfulby Jim Nickerson 2020/08/11 05:38:30
3.67 (3)
I assume it was the OP who gave a 1 star (i.e. -2) rating to Jim's actually correct advice.
If you want to a definitive response, ASK MICROCHIP. This is a user support forum. We are users, not Microchip employees.
The way to ask Microchip is to open a "Support Ticket", via http://support.microchip.com
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
Paba
Junior Member
  • Total Posts : 106
  • Reward points : 0
  • Joined: 2014/03/25 08:03:07
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/10 23:21:30 (permalink)
0
@wdy: Since the PCBA is working with USB, I am using a 48MHz external Primary Oscillator. PLLIDIV is set to DIV_12 to reach the 4MHz for the PLL. PLL_MULT and PLL_ODIV is set to reach the global target frequency of 40MHz (PLL_MUL = 20, PLL_ODIV = 2). Check my configuration bits:
 
// DEVCFG3
// USERID = No Setting
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_12 // PLL Input Divider (12x Divider)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)

// DEVCFG1
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)

// DEVCFG0
#pragma config DEBUG = OFF
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)

 
I tried to use different global clock speeds and changed the PLL_ODIV to 2, 4 and 8. None of them worked for writing to the program flash. It is always the same behavior. After writing the first time to the program memory at address 0x1D00B000, the WRERR bit is set. If the global clock gets too slow (in my case when PLL_ODIV was set to 8, the USB connection stops to work due to a too slow frequency).
However, it seems that the clock speed does not have any influence on the write to the flash.
#9
wdy
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2016/10/05 06:32:30
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 05:24:46 (permalink)
0
@Paba: try this pseudocode:
 

{
        NVMADDR = ..... ;
        NVMDATA = ..... ;
        NVMCON = 0x4001; // program a word
        while (NVMCON & 0x0800);

        di ();
        NVMKEY = 0xAA996655;
        NVMKEY = 0x556699AA;
        NVMCONSET = 0x8000;
        ei ();
        while (NVMCON & 0x8000);
 
        // delay here?
 
        NVMCONCLR = 0x4000;

        return NVMCON & 0x3000;
}

#10
wdy
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2016/10/05 06:32:30
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 06:06:02 (permalink)
0
NorthGuyYou may need a 250 ns delay after setting nvmop.

How about before clearing the 14th bit of NVMCON, the comment "delay here?" above?
#11
NorthGuy
Super Member
  • Total Posts : 6291
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 06:39:19 (permalink)
0
wdy
NorthGuyYou may need a 250 ns delay after setting nvmop.

How about before clearing the 14th bit of NVMCON, the comment "delay here?" above?



It is all done by that point.
#12
Paba
Junior Member
  • Total Posts : 106
  • Reward points : 0
  • Joined: 2014/03/25 08:03:07
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 06:45:28 (permalink)
0
It didn't work. Seems to be a tricky one!
I tried both, one time a delay after setting the "nvmop" and one time before clearing the write enable in the NVMCONCLR register.
 
The initial state is always as the following:
NVMADDR = 0x9D00B000
NVMCON = 0x00000000
NVMDATA = 0x0F402C02
NVMKEY = 0x00000000
NVMSRCADDR = 0x00000000
 
After writing the state changes to:
NVMADDR = 0x9D00B000
NVMCON = 0x00002001
NVMDATA = 0x0F402C02
NVMKEY = 0x00000000
NVMSRCADDR = 0x00000000
post edited by Paba - 2020/08/11 07:07:41

Attached Image(s)

#13
NorthGuy
Super Member
  • Total Posts : 6291
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 09:49:08 (permalink) ☄ Helpfulby Paba 2020/08/11 22:58:41
4.5 (2)
The address in NVMADDR is supposed to be 0x1d00b000. I don't know if 0x9d00b000 would work.
#14
wdy
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2016/10/05 06:32:30
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 12:35:29 (permalink) ☼ Best Answerby Paba 2020/08/11 22:58:15
0
NorthGuy
wdy
NorthGuyYou may need a 250 ns delay after setting nvmop.

How about before clearing the 14th bit of NVMCON, the comment "delay here?" above?



It is all done by that point.

This is from my notes, so I must have collected it from some murky Microchip document. Don't forget FSM sync/handshake and running on async clocks - this is why these delays are needed, even at this point.
 
NorthGuy
The address in NVMADDR is supposed to be 0x1d00b000. I don't know if 0x9d00b000 would work.

Indeed, I thought this is obvious to the OP - the Flash module works with physical (bus) addresses. @Paba even talked about 0x1D00B000 in the first post.
Related - cannot remember anymore if virtual address (some more high bits on) works or not, so there is a good chance the error is purely from the wrong address.
post edited by wdy - 2020/08/11 12:37:25
#15
Paba
Junior Member
  • Total Posts : 106
  • Reward points : 0
  • Joined: 2014/03/25 08:03:07
  • Location: 0
  • Status: offline
Re: write operation fails when writing program flash 2020/08/11 23:03:59 (permalink)
0
@wdy and @NorthGuy: This was it! Thanks a lot.
I didn't even waste a thought about physical and virtual addresses because I already used the same code in other bootloader applications too and there it worked.
However, it is running now.
#16
Jump to:
© 2020 APG vNext Commercial Version 4.5