• AVR Freaks

Hot!UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH

Page: 12 > Showing page 1 of 2
Author
SincPelp
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2020/03/17 08:55:32
  • Location: 0
  • Status: offline
2021/01/15 02:57:36 (permalink)
0

UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH

Hi,
I have designed a custom PCB with PIC32MZ and an external flash memory. I want to update the firmware of the PIC32 without the need for an external programmer or debugger.
To do this, I have proceeded as follows:
- With a custom software I save the .hex file in the external flash memory.
- Before I do a checksum check of each line to avoid errors.
- I have read document AN1388 but it does not clarify how to proceed in my case.

I have several questions, the first is: Should I use the Harmony bootloader ?. In this case:

- What type of bootloader: USART, I2C, SDCARD, ...?
- Build an Application Linker Script? or Build a Bootloader?
- Program External SPI FLASH?
- Trigger Type -> MEMORY (my custom PCB has no buttons)

Is this the correct setting?

If the Harmony bootloader is not necessary, can someone tell me the steps to follow?

I am quite lost in this matter...
thanks.
#1

34 Replies Related Threads

    Noggin
    Junior Member
    • Total Posts : 112
    • Reward points : 0
    • Joined: 2008/07/07 14:49:34
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/15 14:29:31 (permalink)
    0
    Is there an actual requirement to use the external memory? You wrote:
    I want to update the firmware of the PIC32 without the need for an external programmer or debugger.
    If that is your only real requirement, then you don't need the external memory. A bootloader, even a simple one, may be sufficient for what you want to do. Even a small bootloader can easily support using a UART to receive data, erase the application, receive a .hex file, validate the line, and write it to flash.
    Based on the little information you've provided, it doesn't sound like you have any specific plan in place and that you're flexible on what you do. What is it about your case that doesn't work with AN1388?
    #2
    RISC
    Super Member
    • Total Posts : 5960
    • Reward points : 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/15 16:50:28 (permalink)
    3 (1)
    Hi,
    Most usual bootloader is UART.
    So you can for example connect a PC to your board top update it using a USB <==> UART cable
    You can also use USB HOST MSD bootloader to your system can update itself from a USB thumb drive.
    Harmony v3 provides most of these bootloaders 
    Regards
     

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #3
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/16 12:07:06 (permalink)
    0
    Thanks for your answers.
     
    The .hex file with the firmware is on an external SPI flash memory. How do I pass the firmware to the PIC32 flash memory?
     
    The existing bootloader types in Harmony 2 are USART, I2D, USB and SD CARD, but there is no SPI type. Do I have to change the code and adapt it to SPI?
     
    In the Harmony configuration, Do I have to check the option "Build an Application Linker Script?" or "Build a Bootloader?".
     
    Thanks in advance.

     
    #4
    Noggin
    Junior Member
    • Total Posts : 112
    • Reward points : 0
    • Joined: 2008/07/07 14:49:34
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/16 14:01:04 (permalink)
    0
    If you examine the USART bootloader, you should be able to figure out where the data comes in (from the USART) and how it passes that data to the bootloader. At least, that is how I would expect it to work, I haven't looked at it myself. What you'll want to do is replace the code that accepts data over the USART with some that reads from the SPI flash.
     
    I don't know the answer about using Harmony to build an application linker script and/or bootloader. I tend to take the default linker script and modify it as I need it. I feel that modifying linker scripts is not an easy or straight forward task, so I'd suggest using any examples you can from Harmony.
    #5
    cirilo.b
    Junior Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2020/09/08 18:40:42
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/16 14:08:34 (permalink)
    0
    You can look at an example bootloader that I wrote:
    https://gitlab.com/cbernardo/olimex_pic32hmz144_boot
     
    There is a custom linker script for the bootloader and main app.  The bootloader works with the tool provided with AN1388.  Since the bootloader targets a specific hardware board you may need to make modifications to use it.  In the linker scripts you might want to modify the alignment of EBASE depending on how many interrupts you use.
    #6
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/17 05:28:40 (permalink)
    0
    Thanks for the answers.
     
    I found a post that someone had configured the Harmony 2 to read the existing .hex file on an external SPI flash drive but did not give details of how they did it.
     
    I know that I have to start from a USART type bootloader and modify the reception of data so that it is taken from the external SPI flash memory but I am concerned about some function that I see in Harmony 2.
     
    When you enable the option "Build a Bootloader" the following function appears that you can enable: "Program External SPI FLASH?". What is this function used for? I cannot find information about it in the documentation.
     
    Thanks in advance.
    #7
    cirilo.b
    Junior Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2020/09/08 18:40:42
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 00:22:13 (permalink)
    0
    I doubt you need to program any external SPI flash at all. The dev kits typically have some form of external NAND flash to play with; this is useful for storing data such as graphics if you don't want to use an SD card for that purpose.  If you design your own board it is possible to use the external NAND as extended Flash memory to store executable code and data.  I doubt those topics cannot be explained in any significant detail in these forums. I don't know what Harmony's capabilities are since I avoid ever using it. Since the dev kits are not designed to take advantage of the NAND Flash Execute In Place, I suspect that the "Program External Flash" utility is to program additional data such as graphics into the external flash. I wouldn't be surprised if those features are inadequately documented; high-level ecosystems like Harmony typically take the "follow this recipe, don't think and you'll be alright" approach.
    #8
    Luca Pascarella
    Junior Member
    • Total Posts : 112
    • Reward points : 0
    • Joined: 2007/05/28 00:53:17
    • Location: The Netherlands
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 01:01:17 (permalink)
    0
    I believe it is worth clarifying that you need to install in advance a bootloader on your device to update the main firmware. Sayed that. You need to develop two firmware(s), one is the main application and the second is the bootloader that, usually for safety and simplicity, will never be updated. For easy, MPLABX offers a way to link projects together. This allows you to select as a loadable subproject of the bootloader the main application. Then MPLABX handles the two projects together; you can flash both firmware in one shot or debug them together as a single application.
     
    Here the complex part. To allow both the main application and the bootloader to coexist in the same memory flash, you need to deal with linker scripts of both applications. In other words, you need to specify which regions or addresses of the memory must be used. For example, PIC32MZ has a small double-panel flash to host a bootloader and big main memory to host the main firmware. If I remember correctly, checking the bootloader option in Harmony, it generates a linker script that uses the boot flash space for hosting the bootloader while not-checking the flag it generates a traditional linker script for the main application. These two linker scripts are tailored for the example applications mentioned by you as the UART, USB, etc. Most probably, you need to start from these examples understand them in deep and adapt the code to your needs.  
     
    A few notes. The term boot flash does not imply it is designed for hosting a bootloader. For instance, in PIC32MX this memory is too small for containing an entire bootloader, instead, it is more for the booting phase while in PIC32MZ the double panel boot flash allows you to have more flexibility and put an entire, even small, bootloader. In both scenarios, you need to develop two projects with two different linker scripts only in this way you may delegate to a running firmware (for a convention this application is called bootloader) to read/receive bytes from SPI, UART, UDP, and flash it in the physical main memory.
     
    Luca
    #9
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 03:56:48 (permalink)
    0
    Thanks for the answers.
     
    Ok, I understand that I must have two projects in MPLAB, one is the application and the other is the bootloader.
     
    I have opened the demos proposed by the AN1388 document but they have a strong dependence on the Development Board and Development Kits (Explorer 16, Starter Kits), included in Hardwareprofile and I am using a custom board, without LEDs or switches that allow to enter the firmware upgrade mode. Also, they are programmed for PIC32MX which in general has less memory than the PIC32MZ2048 that I am using.
     
    Another problem is that none of these examples allow firmware data from an external SPI flash memory. My board uses this memory to store data since my device is a dataloger. Part of this is reserved for storing the firmware binary.
     
    The idea is this:
    - I save the bootloader in the memory space of the PIC32 reserved for this purpose.
    - If the PIC32 goes into reset mode (I disconnect the power from my board), load the bootloader and check if reset vector address is erased.
    - If reset vector address is erased, the bootloader starts reading the binary data from the external SPI flash memory and copies it to the space reserved for this purpose in the PIC32. When done, set reset vector address to zero.
     
    I have a question: the first time I save the bootloader on the PIC32, the external SPI flash memory is empty, I need to run my application, to save the firmware binary to the external SPI flash memory. To solve it I have to include in the Loadables folder the project of the main application. When I compile the project, the .hex file includes the bootloader and the main application. Is this true? When the PIC32 boots, it checks the reset vector address and loads the main application. So from here, I can use my application to pass the binary to the external SPI flash memory.
     
    If I start from the examples described in AN1388 I have to make many changes and adaptations that now I don't know how to do them. And I do not have time!!.
     
    Luca, could you pass me the bootloader you made for the PIC32MZ2048? I think it is very similar to what I need. I am very grateful for your help.
     
    Thanks in advance.
    #10
    friesen
    Super Member
    • Total Posts : 2169
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 06:31:56 (permalink)
    5 (1)
    It seems like maybe you are in over your head?
     
    Is this a work project?
     
    You really need to define your needs first.  How do you want to update this, web ui, thumb drive, sd card?  Each has its own tricks.  Also, does this need encryption, etc.  an1388 really is only a reference guide at best.
     
    I personally don't like the idea of giving up 50% space with dual flash panel idea.  Also, I think you'll have to be more careful this way to prevent bricking.
     
    With external memory, there are some good ways to make this work.
    post edited by friesen - 2021/01/18 06:40:56

    Erik Friesen
    #11
    Luca Pascarella
    Junior Member
    • Total Posts : 112
    • Reward points : 0
    • Joined: 2007/05/28 00:53:17
    • Location: The Netherlands
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 06:58:04 (permalink)
    3 (1)
    Unfortunately, I believe it is not so simple, and you may need a bit of study and experiments to fully understand the logic behind it. In any case, you have to smash with linker scripts to adapt it to your device or rely on what Harmony generates for you as is. In the latter case, the bootloader generated by Harmony is no more than a simple application with a special linker script. Most probably, you need to adapt the UART transfer with the SPI read you need. This should kind of doable with a little knowledge.
     
    I would suggest studying the H3 UART bootloader too that is really straightforward to your need https://github.com/Microc...y/bootloader_apps_uart
    In my opinion, it is a good overview of how the bootloader and app work together and how to trigger the bootloader at reset time from different sources.
     
    As a side note, you may be confusing with terminology. The reset vector is a crucial part of both bootloader and app, something closed to the hardware; if you improperly erase/update this sector, you cannot boot the application anymore, hopefully generating a runtime exception.
     
    Finally, about my bootloader(s), you can dig with a public light variant https://github.com/lucapa.../BroadcasterBootloader instead, the one you refer to is way too complicated to start dealing with other than not publically available. It uses the PIC32MZ double panel boot flash to update the bootloader itself rather than the application target. It does various things that may slightly overkill the normal usage and actually does not use standard .hex files but a proprietary shrunk/signed binary to save space and discourage unwanted copies.
    #12
    cirilo.b
    Junior Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2020/09/08 18:40:42
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 13:07:31 (permalink)
    0
    Why do you want to store your firmware in the external flash when you already have a UART bootloader? That just doesn't make sense. + simply checking for 0xffffffff at the reset address is not a reliable way to operate - what happens if someone cuts the power while you're programming the application?  Then you have data at the reset address, but your application will be corrupted.
     
    Maybe it would be better if you explained what you need to do in some detail; we can't help if it not clear to us.
    #13
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/18 23:08:16 (permalink)
    0
     
    Thanks for the answers.
     
    Sorry, maybe I didn't explain well.
     
    My project consists of a datalogger that collects data in short intervals of time. This datalogger must be connected to the ETHERNET network and has a screen that shows the data taken and other information.
     
    To do this project, I designed a custom board in which I put a PIC32MZ2048 and added a flash memory to store the data taken by the sensors. This flash memory communicates with the PIC32 through SPI.
     
    I programmed a firmware for the PIC32 to collects the sensor data, it does certain operations with them and save it in the SPI flash memory. Also to show data on the display. I use Harmony 2 on this firmware.
     
    Also, I programmed a PC application (C #) to communicate with the datalogger and configure the options.
     
    My customer wants to update the firmware from the PC application, without using the Microchip update application. To do this, I thought to send the .hex file (from my PC application) to the PIC32, this checks the checksum of each row and stores it in binary format in a reserved space of the SPI flash memory. When the file has been completely transferred I generate a reset, the bootloader reads the SPI flash memory and does the update.
     
    Thanks in advance.
    #14
    friesen
    Super Member
    • Total Posts : 2169
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 05:59:51 (permalink)
    0
    Do you have usb or uart, or user accessible way to enter bootload mode, like a button?

    Erik Friesen
    #15
    friesen
    Super Member
    • Total Posts : 2169
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 06:25:09 (permalink)
    5 (2)
    I have done ethernet style bootloaders in two ways, both using external memory.
     
    #1. Pure ethernet bootloader with user button to enter bootloader mode if necessary.  Application can save image in reserved area of external memory and soft reset the processor.  The bootloader then uses RCON or equivalent to detect if it should check external flash for image.  The bootloader also has a basic page where you can post an image if there is no valid image, and none in flash.
     
    #2. Mixed usb bootloader.  The bootloader checks for drive and if found looks for image.  Similar to above, if RCON checks as software reboot, looks in external memory.  This way, application can use ethernet to save image to external flash.
     
    Both options above should allow for end user recovery in all cases.
     
    There are some basic checks and balances in a bootloader that are missing in AN1388.  Some can be found here https://www.microchip.com/forums/m382076.aspx But basically, the image should be validated after install somehow.

    Erik Friesen
    #16
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 09:17:15 (permalink)
    0
    Thanks for the answers.
     
    The board does not have a push button or usb.
     
    I think I need the bootloader #1. Your PC application does the same as mine.
     
    My PCB does not have a push button, can I use a specific memory location to enter bootloader mode?
     
    The steps are:
    - The PC application saves the image in the reserved space of the flash memory and soft reset the processor.
    - The bootloader checks the memory position 0xXXXXXXXX.
    - If the value is 0xFF then access sector XX of the flash memory where the binary of the .hex file is located and carry out the update process.
     
    I could do it like this?

     
    Thanks in advance.
    #17
    friesen
    Super Member
    • Total Posts : 2169
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 10:12:09 (permalink)
    0
    You can, but what happens if you send out a malformed hex file that doesn't behave?  You essentially have it bricked at that point.

    Erik Friesen
    #18
    SincPelp
    New Member
    • Total Posts : 30
    • Reward points : 0
    • Joined: 2020/03/17 08:55:32
    • Location: 0
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 10:46:08 (permalink)
    4 (1)
    My PC application sends multi-line packets to the PIC. It analyzes each line and checks the checksum. If the checksum is correct, save the line in flash memory (in binary). In this way I reduce possible failures.
    Thanks in advance.
    #19
    friesen
    Super Member
    • Total Posts : 2169
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: offline
    Re: UPDATE FIRMWARE ON PIC32MZ CUSTOM PCB WITH EXTERNAL FLASH 2021/01/19 11:19:02 (permalink)
    0
    Right.  But what if your application quits behaving?  Its a secondary problem to the bootloader not doing right.

    Erik Friesen
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2021 APG vNext Commercial Version 4.5