Search code examples
picmplabdspic

EZBL_EraseAll or EZBL_NVMKEY not working in mplab x IDE version 5.4


I have a bootloader working for mplab x IDE v4.2 for years. It is compilated with XC16 1.35.

We are updating it to be used on MPalb x IDE v5.40. I have been able to make this by creating an example of ezbl and replacing the files with my code from version 4.2.

I had to make small changes to certain files (just few undefined ports not defined in newest version).

My bootloader searchs for a firmware update in a SD memory and burns it to the flash. As I said this code is working perfectly in 4.2

The thing here is that EZBL_EraseAll is not deleting the flash memory in version 5.4. If i set it to verbose mode I get this:

 Write: 002198  9059FE DD19C8 710083 90610E
         0021A0  514F8A 360003 400408 488489 37FFE9 BE050C 97B60F 97B69F
         0021B0  EF6870 20A9C2 97B03F 97B0CF 07337D E00400 3A0003 EFA850
  Read:  002198  320009 B2C206 3A000A 200045
         0021A0  FB8039 32010A 2006C6 E10406 3A0004 200025 B00065 FB8039
         0021B0  320103 B2C250 320024 B2C460 3200E3 781F88 E9040F B001CF
  Write: 0021C0  EF2852 37009B 97B03F 97B0CF 884280 884291 560661 5E86E0
         0021D0  97B0DF 660201 97B16F 668002 EB0080 DD01C0 200002 B82061
         0021E0  710100 718181 90602E 504FE3
                                               MISMATCH (program and erase restricted; bootloader mismatch)
  Write: 0021C0  EF2852 37009B 97B03F 97B0CF 884280 884291 560661 5E86E0
         0021D0  97B0DF 660201 97B16F 668002 EB0080 DD01C0 200002 B82061
         0021E0  710100 718181 90602E 504FE3
  Read:  0021C0  B2C3B0 320073 B2C200 32006F B2C0D0 32002E B2C110 320019
         0021D0  B2C0D0 320017 B2C190 320069 B2C1F0 320067 B101CF 78044F
         0021E0  B2C1C0 3200AC B2C1D0 3200D3
  Write: 0021E8  3A001A 20A9C0 97E9EF 797003
         0021F0  97B80F 2FF004 97B2FF 620205 EB0280 DD2B48 DE2248 730204
         002200  DE2AC8 20A9D1 78F104 E80081 797080 20F001 600001 B80261
                                                                           MISMATCH (program and erase restricted; bootloader mismatch)
  Write: 0021E8  3A001A 20A9C0 97E9EF 797003
         0021F0  97B80F 2FF004 97B2FF 620205 EB0280 DD2B48 DE2248 730204
         002200  DE2AC8 20A9D1 78F104 E80081 797080 20F001 600001 B80261
  Read:  0021E8  B2C080 3200D8 B2C030 3200D6
         0021F0  B2C200 3200D4 B2C220 3200D2 B2C200 3200D0 3700D4 E80408
         002200  37FF6C A6F01A 37000B 2002D7 786C07 200010 550385 EA8BD7
  Write: 002210  DD2BC8 DE2248 738204 DE2AC8 20A9F0 787104 370069 504FE2
         002220  3A000E 20A9C0 97E8EF 797001 2FF004 97B1FF 620203 EB0280
         002230  DD2B48 DE2248 730204 DE2AC8

Write operating are not working either

Some sectors are FFFF when read is shown , specially last sectors, and write operations dont modify them.

EZBL_NVMKey is set to 0xFC21

Well, I would like to know why the same code is working for version 4.2 and not for version 5.4 even when compiler is in both case XC16.

Some of my log:

NVMKEY = 0xFC21
NVMKEY before erase = 0xFC21
Erasing ... done
Terminating: code = 0xFFE8
EZBL_Install_SPI_FILE2Flash (hard): -24 EZBL_ERROR_HARD_VERIFY_ERROR: Bootloader read-back verification failure.)

Thanks in advance.


Solution

  • Finally I was able to fix it. I will make this answer a small guide with the steps for ezbl projects.

    I comment the answer here because it will be usefull for other peoples.

    When you change version 4.2 to version 5.4 even when compiler is the same version (1.35) and ezbl is the same version too (2.11) is that you need to take specially care about file of "hardware initializers" folder.

    I changed it and i set the values to the same values than previous version, but it didnt work. I exited with code -24 and also with -25.

    Another important thing is follow all the ezbl steps right, i will comment about them later.

    You need to open the right file inside "hardware initializers folder" for your device, in my case pic24j256gb410_explorer_16.c

    First important thing.. FCY speed, here i had the default speed of the samples (16m). If you set it wrong you will see for example, serial ports transmitting to different speed (4x faster than expected), but not in bootloader, in the final app. Bootloader serial speed will work even if you set wrong the FCY speed.

    //#define FCY         16000000ul      // Changing this automatically changes the PLL settings to run at this target frequency
    #define FCY          4000000ul
    

    Another important detail, set serial speed to the right speed.

    const long EZBL_COMBaud = 115200;
    

    Remove from file (comment), all those lines that are not useful to your project.

    Btw, if you use certain microcontrollers, some of the pins used there for default file example doesnt exists. Be careful.

    Finally the most important thing happens here:

    EZBL_SET_CONF(_FSEC, BWRP_OFF & BSS_OFF & BSEN_OFF & GWRP_OFF & GSS_OFF & CWRP_OFF & CSS_DIS & AIVTDIS_DISABLE)
    EZBL_SET_CONF(_FOSCSEL, FNOSC_FRC & PLLMODE_PLL96DIV2 & IESO_OFF)
    EZBL_SET_CONF(_FOSC, POSCMOD_XT & OSCIOFCN_ON & SOSCSEL_ON & PLLSS_PLL_PRI & IOL1WAY_OFF & FCKSM_CSECME)
    EZBL_SET_CONF(_FWDT, WDTPS_PS1024 & FWPSA_PR32 & FWDTEN_SWON & WINDIS_OFF & WDTWIN_PS75_0 & WDTCMX_LPRC & WDTCLK_LPRC)
    EZBL_SET_CONF(_FPOR, BOREN_ON & LPCFG_ON)
    EZBL_SET_CONF(_FICD, ICS_PGx2 & JTAGEN_OFF & BTSWP_ON)
    EZBL_SET_CONF(_FDS, DSWDTPS_DSWDTPS0D & DSWDTOSC_LPRC & DSBOREN_ON & DSWDTEN_ON)
    EZBL_SET_CONF(_FDEVOPT1, ALTCMPI_DISABLE & TMPRPIN_OFF & TMPRWIPE_OFF & ALTVREF_ALTVREFDIS)
    

    You need to set config bits for your project, again inside this file.

    Why do you set them with EZBL_SET_CONF? because those config bytes will be included in the generated .merge.s file, and tell and force linker in the final app to use them.

    ; Bootloader code block intended for program region 'FSEC'
    ; 0x02AF80 to 0x02AF84, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FSEC, address(0x02AF80), code, keep
    .pword      0x00FFFF, 0xFFFFFF                                                              /* 0x02AF80 ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FOSCSEL'
    ; 0x02AF98 to 0x02AF9C, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FOSCSEL, address(0x02AF98), code, keep
    .pword      0x0000F8, 0xFFFFFF                                                              /* 0x02AF98 ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FOSC'
    ; 0x02AF9C to 0x02AFA0, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FOSC, address(0x02AF9C), code, keep
    .pword      0x00001B, 0xFFFFFF                                                              /* 0x02AF9C ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FWDT'
    ; 0x02AFA0 to 0x02AFA4, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FWDT, address(0x02AFA0), code, keep
    .pword      0x004BDF, 0xFFFFFF                                                              /* 0x02AFA0 .K....                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FPOR'
    ; 0x02AFA4 to 0x02AFA8, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FPOR, address(0x02AFA4), code, keep
    .pword      0x000001, 0xFFFFFF                                                              /* 0x02AFA4 ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FICD'
    ; 0x02AFA8 to 0x02AFAC, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FICD, address(0x02AFA8), code, keep
    .pword      0x000081, 0xFFFFFF                                                              /* 0x02AFA8 ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FDS'
    ; 0x02AFAC to 0x02AFB0, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FDS, address(0x02AFAC), code, keep
    .pword      0x0080DF, 0xFFFFFF                                                              /* 0x02AFAC ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FDEVOPT1'
    ; 0x02AFB0 to 0x02AFB4, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FDEVOPT1, address(0x02AFB0), code, keep
    .pword      0x00001E, 0xFFFFFF                                                              /* 0x02AFB0 ......                   */
    .popsection
    
    ; Bootloader code block intended for program region 'FBOOT'
    ; 0x801800 to 0x801804, length 0x000004 (6 bytes; needs 0 pages)
    .pushsection    EZBL_BTLDR_CONFIG_WORD_FBOOT, address(0x801800), code, keep
    .pword      0x000003, 0xFFFFFF                                                              /* 0x801800 ......                   */
    .popsection
    

    If you set it to the bootloader, it will work for bootloader but not for final application, and int will fail in verification process.

    The final and one of the harder problems (and in this case, i think that it is a bug) is the first of those config flags lines.

    In my previous project i used

    EZBL_SET_CONF(_FSEC, BWRP_OFF & BSS_OFF & BSEN_OFF & GWRP_OFF & GSS_OFF & CWRP_OFF & CSS_DIS & AIVTDIS_DISABLE)
    

    but this generated a different value from previouis version for FSEC config register, i dont remember the value, it started with 0x8..., the problem here is that bootloader was looking for a different value, and i got -25 exit code if i'm not wrong.

    I enabled debuggin for bootloader uncommenting

    #define EZBL_DEBUG     // Uncomment to allow debugging messages to be printed to stdout
    #define VERBOSE        // Uncomment to have verbose printing of all flash write commands and data
    

    It is important that ezbl by default tries the bootloading process without modifying the memory (to see if the imported file is right) and then it flashes it. So in debugging mode you will see the file failing in first step for app memory addresses (bootloader addresses must be right or you are using a wrong bootloader file).

    After a lot of tried i change that line to:

    EZBL_SET_CONF(_FSEC, 0xFFFF)
    

    to force the config flags for _FSEC to the right value and the value wanted. I dont know why this version gave me a different value than i got in version 4.2.

    When you are able to compile the project, it will generate a .merge.s file and a merge.gld file. As you must know, you need to use those file in your final project.

    The .merge.s file goes to source files and the .merge.gld goes to linker files.

    VERY IMPORTANT. if you change bootloader file you will need to compile again the final app. In my case it included more steps because the update was done though a SD card, so i had to compile the final app and copy it to the SD and this was one of the main erros that i had, i was offering to the bootloader an incompatible version of the bootloader file (Even if the hash is the same, because the hash is generated with the first lines of ezbl_boot.mk) and it will not change when you make changes to your code, but final bootloader file will be different.

    BOOTID_VENDOR = "xxxxx"
    BOOTID_MODEL  = "xxxx"
    BOOTID_NAME   = "12/11/2020"
    BOOTID_OTHER  = "GA410-GA406"
    

    When bootloader file is different verification process will fail in the bootloading update process.

    All those was the most important problems and things to take care. If i remember more i will edit and add them.

    Note: In my original post i though that it was about nvmkey or eraseall, i was fully wrong...

    As i said first step when flashing is verify if the file is right, so you will get a lot of errors in flashing process when debug is enabled because it is not flashing the addresses and then read will be always 0xFFFF. After verifying finish, the real process will start. Here is where it cant fail, if it fails, then you probably mixed bootloaders versions, or you have a problem with the config bits (final app will probaby works fine).