0
votes

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

  1. Can I just write the .hex file into the Flash as it is without modification like
:020000040800F2 
:1000000002200B

etc?

  1. 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?

  2. 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

2
The best solution would be to have PIC code to run it directly in the flashed location, but no compiler will ever guarantee fully PIC code. Another solution could be to have 2 images linked for the 2 bank address downloading the one appropriate for the flash position (same image build, different linker descriptor). Eventually the bootloader can move vector table in RAM and fix some address. Best choice move to a dual banked MCU...Frankie_C
@Frankie_C do you know how to place the program with int vectors in another place in the FLASH. How do you want to create the PIC code - please elaborate.0___________
@Frankie_C Moving to another MCU is not an option since the hardware will not be altered.user44791
So, now my idea is to define a function in a separate c-file which is called "preMain". I will include it into the startup code just before calling main. I will try to place this function at a dedicated address in the flash so it is not included into the actual firmware.user44791
It will. Your bootloader will be doing it(receiving data and writing the flash)0___________

2 Answers

1
votes

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.

0
votes

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