2025-04-29 3:03 AM
Hello All,
i want to implement bootloader in stm32g431rbt6 microcontroller using uart, I am looking for guidance and I would appreciate it if anyone could share useful resources, example projects, application notes, or tutorials specific to this MCU or closely related STM32G4 series chips.
Thank you in advance!
Best regards,
2025-04-29 6:24 AM
How to use the ST Open Bootloader for STM32 Microc... - STMicroelectronics Community
2025-04-29 6:24 AM
2025-04-29 6:41 AM
Also consider using the built-in bootloader which is already supported by STM32CubeProgrammer.
2025-04-29 12:39 PM
The standard bootloader works well and you can also jump to it from your application, so you won't need to have control of the STM32's Reset & BOOT pins.
While in principle one can directly jump to the bootloader from anywhere, once the hardware is initialized and peripherals are configured things are not as clear cut, so what I'm doing is set a flag in RAM on a place that's not erased with a system reset (system stack) and check that flag first thing after boot up in main().
So, in my main(), first thing I call is handleBootLoader();
In the situation I use these, there's multiple devices that are daisy chained, so the serial line goes from one device to the next. In order to program one of the devices, the other ones need to go into daisy chain relay mode and just pass through whatever data they receive.
Works well in on an stm32f4:
#include "stm32f4xx_hal.h"
#include "stdbool.h"
#include "protocol_processor.h"
#define BOOTLOADER_ADDRESS 0x1FFF0000 // STM32F40xxx. See https://bit.ly/AN2606
#define BOOTLOADER_FLAG_OFFSET 100 // 4 * 100 = 400 bytes below top of stack
#define BOOTLOADER_FLAG_MAGIC 0xFEEFFEEF
#define RELAYMODE_FLAG_MAGIC 0xEFFEEFFE
typedef void (*pFunction)(void);
static pFunction JumpToBootLoader;
static uint32_t JumpAddress;
extern int _estack;
static uint32_t* bootloader_flag = (uint32_t*) (&_estack - BOOTLOADER_FLAG_OFFSET);
void resetToBootLoader()
{
*bootloader_flag = BOOTLOADER_FLAG_MAGIC;
HAL_NVIC_SystemReset();
}
void resetToRelayMode()
{
*bootloader_flag = RELAYMODE_FLAG_MAGIC;
HAL_NVIC_SystemReset();
}
void handleBootLoader()
{
if( *bootloader_flag == BOOTLOADER_FLAG_MAGIC )
{
*bootloader_flag = 0; // so next boot won't be affected
/* Jump to system memory bootloader */
JumpAddress = *(__IO uint32_t*) (BOOTLOADER_ADDRESS + 4);
JumpToBootLoader = (pFunction) JumpAddress;
JumpToBootLoader();
}
else if( *bootloader_flag == RELAYMODE_FLAG_MAGIC )
{
*bootloader_flag = 0; // so next boot won't be affected
daisyChainRelayMode = true;
}
//*bootloader_flag = 0; -> don't mess if flag not set
}