• AVR Freaks

AnsweredHot!Can PIC32MX share NVM addresses between a bootloader and main program?

Author
Tank2005
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/09 01:45:37
  • Location: 0
  • Status: offline
2019/04/19 06:09:07 (permalink)
0

Can PIC32MX share NVM addresses between a bootloader and main program?

My environment is MPLAB X IDE 5.10/XC32 5.5 for PIC32MX230F064B.
 
I am investigating the firmware update program by HID USB Bootloader which is prepared in MPLAB Harmony v2.06.
 
On the Harmony's sample program(microchip\harmony\v2_06\apps\bootloader), a bootloader runs when a specific switch is pushed.
int APP_Bootloader_ForceEvent(void)
{
/* Check the switch press to trigger bootloader */
if (BSP_SWITCH_STATE_PRESSED == BSP_SwitchStateGet(BTL_TRIGGER_SWITCH))
{
return (1);
}
/* Check the trigger memory location and return true/false. */
if (*(uint32_t *)APP_RESET_ADDRESS == 0xFFFFFFFF)
return (1);

return (0);
}

I thought that if a flag wrote to the NVM while a main program was running, it enters a bootloader program without a pyhsical switch by reading that flag.

// main program.
// Write a boot flag when it requires update.
#define BOOT_FLAG_ADDRESS (0x9D000000 + 0x7400 + 512)
int WriteBootloaderFlag()
{
NVMADDR = KVA_TO_PA(BOOT_FLAG_ADDRESS);
NVMDATA = 0x12345678;
NVMKEY = 0xAA996655;
...and more...
}


// bootloader program
int APP_Bootloader_ForceEvent(void)
{
// Check a boot flag from a main program
if (*(uint32_t*)BOOT_FLAG_ADDRESS == 0x12345678) return 1;
/* Check the trigger memory location and return true/false. */
if (*(uint32_t *)APP_RESET_ADDRESS == 0xFFFFFFFF) return (1);

return (0);
}

However, my code does not work as expected.
 
Is this resolved by fixed the NVM address? Or PIC32 can't share those addresses?
#1
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Can PIC32MX share NVM addresses between a bootloader and main program? 2019/04/19 22:53:43 (permalink)
0
Tank2005
...
I thought that if a flag wrote to the NVM while a main program was running, it enters a bootloader program without a pyhsical switch by reading that flag.

The bootloader reads thta flag while booting up, so you would have to force a reset after setting the flag, it doesn't happen instantly just by writing the flag.
 

However, my code does not work as expected.

What EXACTLY did you do to test it?
As mentioned, just writing to the flag won't do anything instantly.

Nearly there...
#2
Tank2005
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/09 01:45:37
  • Location: 0
  • Status: offline
Re: Can PIC32MX share NVM addresses between a bootloader and main program? 2019/04/21 20:14:47 (permalink)
0
My sample firmware code is this. When I pressed a button connected to RB4, it executes a software reset after successful NVM update.
 
However, it didn't move to the boot loader mode. And I shutdown and powered it by unplugged the power connect, the result is the same.
 
case APP_STATE_SERVICE_TASKS:
{
    int i;
    
    if (PORTBbits.RB4) {
            volatile int res = WriteBootloaderFlag();

            volatile uint32_t v = *(uint32_t*)BOOT_FLAG_ADDRESS;
        if(v == 0xF0F0FFFF){
            RSWRST = 1;
            while(1) ;
        }
    }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    // Executes a main program
    LED1On();
    for (i = 0; i < 10000000; i++);
    LED1Off();
    for (i = 0; i < 10000000; i++);
 
 
 
 
 
 
 

    break;
}

post edited by Tank2005 - 2019/04/24 00:29:14
#3
cvm
Super Member
  • Total Posts : 278
  • Reward points : 0
  • Joined: 2011/09/16 05:16:15
  • Location: 0
  • Status: offline
Re: Can PIC32MX share NVM addresses between a bootloader and main program? 2019/04/22 23:42:04 (permalink) ☼ Best Answerby Tank2005 2019/04/23 20:52:33
0
Problem #1- you most likely cannot write 0xF0F0FFFF to the address you want without first erasing the page (unless you know it will be 0xF?F?FFFF). Since you do not erase the page first, I would assume the value you end up with is 0x?0?0???? (where ? is an unchanged nibble). Remember writing to flash is a 'clear bit' operation only, you cannot set any previously cleared bits. To set any cleared bits requires an erase operation, which I don't see in your code (your bootloader also tries to 'erase' this word with a write).
 
Since you most likely don't want to erase a page address in a running program (without some amount of thought), you could write to some word address a 0x00000000, which would not require a page erase since all you are doing is clearing all bits. The bootloader just has to look for a 0x00000000 at that address. Maybe your chosen address would be safe to erase (while running) and will not ever normally be 0x00000000, or maybe you can just choose an address like the last word available in flash which normally would be in an erased state and writing it to 0 would be harmless.
 
I'm guessing a write of 0 on an MX (without erase) would work but I don't know. On something like a pic32mm which I am more familiar with you cannot do that as it has error correcting flash. I don't think the mx has any such thing so assume it would act like any other 'normal' flash.
 
Problem #2 - you cannot just write a 1 to RSWRST for a software reset, you need to do a system unlock sequence first.
#4
Tank2005
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/09 01:45:37
  • Location: 0
  • Status: offline
Re: Can PIC32MX share NVM addresses between a bootloader and main program? 2019/04/23 20:52:26 (permalink)
0
・ Software reset according to the official document (60001118H)
・ Clear the page by "NVMCONbits.NVMOP = 0b0100;" before writing a flag.
・ Change the destination address immediately before APP_FLASH_BASE_ADDRESS (0x9D000000 + 0x7400 - 512)
 
It works with these fixes. Now, it has been resolved about NVM problems. I'll create a new thread if troubles occured related not to this. Very Thanks.
#5
friesen
Super Member
  • Total Posts : 2058
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: Can PIC32MX share NVM addresses between a bootloader and main program? 2019/04/26 08:22:24 (permalink)
0
Use RCON and test the software reset bit.  Then you can set some common data regions between the two and you can pass parameters between them if you want.  I typically have a common NVM area so that I can read bootloader version, serial numbers, id's, etc from the main application.  These get programmed with the bootloader.

Erik Friesen
#6
Jump to:
© 2019 APG vNext Commercial Version 4.5