How do I rewrite a PIC32 boot flash via code? - c

I have an application that needs to rewrite my bootloader on a PIC32 device. After I disable interrupts, I try to erase 0x1fc00000 with NVMErasePage, but it does not erase, and NVMErasePage return a good status. How can I erase the boot flash from code?

Not an expert on these things but, there is a register with a boot flash write protect bit, search the PIC32Mxxx/2xx data sheet for this.
DEVCFG0: DEVICE CONFIGURATION WORD 0
Check that out.

Related

How Does STM32 Factory Bootloader Allow for Reflashing at any Time?

I'm aware that on some STM32 boards, they come with a built in bootloader in their ROM. Quoting from STM32 Application Note
The bootloader is stored in the internal boot ROM (system memory) of STM32 devices, and is
programmed by ST during production. Its main task is to download the application program to the
internal Flash memory through one of the available serial peripherals (such as USART, CAN,
USB, I2C, SPI).
However the ability to reflash at any time, i.e. trigger a reflash when your board is a running a program confuses me. My understanding is that a bootloader is the first piece of code to execute and then launches your user program. However, to facilitate this flashing at any time, won't the bootloader have to constantly poll for a request to reflash?
The bootloader only polls for an update on start-up/reset. So not "anytime" in the sense of "spontaneously" - you do have to trigger it. If the programmer has access to the reset pin or control of power, it can of course invoke a restart.
Moreover the ROM bootloader only runs at all if the BOOT pins are set appropriatlely. The boot modes are Flash, SRAM or ROM.
Your understanding is right. Almost.
In case of STM32, you need to set up the chip to run the bootloader, typically using BOOT pins. When the bootloader runs, it checks if the programming is necessary by monitoring the specific peripherals. If the programming is not triggered, it jumps to the user program.
Therefore, you cannot reprogram at any time, only at boot time.
Here is an example bootloader flowchart for programming over UART or I2C.
See this application note on STM32 bootloader for more details.

STM32 F407VG Boot into different (main-)applications

For a project I just started working on, I need to write a firmware in C that lets me boot into two different firmware versions. The task is to be able to update a device which includes an STM in the field using the RS485 Port with an Intel .hex file. My idea was to place the two firmware versions at designated starting addresses in the flash, including some kind of checksum for data integrity. According to the flash module organization found in the reference manual, my first thought was to place one version into sector 10 (starting at 0x080C0000) and the other version into sector 11 (starting at 0x080E0000). After every reset, the STM32 should boot into a "bootmanager" which is just minimum code that decides, whether the firmware in sector 10 or sector 11 is the newer version. I want to clarify my idea in the following graphic:
[Rough process][1]
[1]: https://i.stack.imgur.com/xLowh.png
The 128kBytes of every sector are sufficient. So far, I was able to write Single Bytes into the Flash and read them afterwards. Also, I have already set up a working UART communication using the RS485.
My questions
Can I just write the .hex file into the Flash as it is without modification like
:020000040800F2
:1000000002200B
etc?
As I am unexperienced with with jumps: How should I perform the jump from the "bootmanager" into sector 5 firmware? Are the adresses automatically relative to the entry point in sector 5?
Can you give me keywords or tell me, what challenges I will encounter?
*EDIT: I'm aware that the STM itself contains a bootloader. Unluckily, the RS485 device is hardwired to the GPIO pins used by USART2. According to the reference manual, the internal bootloader can only be used by USART1 and USART3, CAN2 and USB OTG FS
Can I just write the .hex file into the Flash as it is without
modification like
no you cant. You need to modify the linker script to archive it
You need to have the whole both applications in the FLASH so divide it 50/50%.
I usually add some serial FLASH to have a copy of the firmware if both of the images are damaged.
Yuo need to write the custom bootloader.
Most of the STM32 microcontrollers have support for dual memory bank and on the fly update (cf AN4767 - On-the-fly firmware update for dual bank STM32 microcontrollers)
This will allow to perform exactly what you require.
Usually the microcontroller will need to have a bootloader and 2 banks for the image.
When booting the bootloader will start and check which version it would have to boot and set the start address accordinngly.
This application note is for the F7 series but you can check it to see how it could work for your specific microcontroller (cf AN4826 - STM32F7 Series Flash memory dual bank mode).
Regarding your questions:
The code would be written as usual but twice (or 2 different firmwares)
Look into the application notes referenced and keywords such as: dual bank, on-the-fly update, DFU, etc

Can I initialise and use USB-CDC purely in RAM?

The chip is an Energy Micro EFM32380f1024 ARM microcontroller and I am using IAR ARM Embedded Workbench. I am aware of the __ramfunc directive however accomplishing initialising and accessing USB completely in RAM (as the flash is going to be completely erased) requires all USB libraries that will be used to be placed in RAM?
This will be used to upgrade the firmware on the microcontroller hence the flash erase. The USB is initialised and used (for normal use by the firmware) for serial communications. I do not wish to use the bootloader for firmware upgrades.
as the flash is going to be completely erased
Not a good idea. In case the update process fails to write the new program completely and the power is lost, your device will be bricked.
Using a bootloader is strongly recommended when you want the flash to be updatable by a user.

Issue with boot loader

I am using OMAP3515 processor (Generic CortexA8 Device) for my project and want to boot the system form UART3.
The boot loader code which I am using is working fine when using emulator i.e it is able to write the image into FLASH.
The same boot loader code when I am sending through UART it is executing in RAM but unable to write the image into FLASH.
You may need to unlock the flash. Some systems provide a option to lock and unlock flash.

Is bootloader required to run a firmware?

I am currently working on firmware for a Stellaris ARM microcontroller board and I am running the SYS/BIOS RTOS.
I was wondering if the bootloader is required on the board when I upload my firmware onto it. Can I overwrite the bootloader on the flash with my .bin file, or am I required to offset my start address to preserve the bootloader.
In the general case (i.e. not specific to Stellaris), software is software, the bootloader is software, your application is software, the processor cannot tell the difference so quite evidently a bootloader is not required, the software that runs at reset could as easily be your software.
However the obvious benefit of a bootloader is the ability to apply in-field updates without connecting special equipment; you might regret loosing that capability.
Some chips (again generally, check your data sheet for Stelaris specifics) have a bootloader in mask ROM rather than Flash and you cannot delete or overwrite that, but usually configuration pins can be set to select the boot behaviour in order to by-pass the bootloader for example.
No you can use jtag and dont need running software in order to stop and re-flash the firmware.

Resources