cancel
Showing results for 
Search instead for 
Did you mean: 

How to Implement Boot Loader in STM32G431RBT6 MICROCONTROLLER

krushna_moukthik
Associate II

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,

4 REPLIES 4
TDK
Super User
Andrew Neil
Super User

https://community.st.com/t5/stm32-mcus-products/how-can-i-use-an-esp32-to-update-the-firmware-of-an-stm32-from/m-p/696326/highlight/true#M254867

https://community.st.com/t5/stm32-mcus-products/how-can-i-use-an-esp32-to-update-the-firmware-of-an-stm32-from/m-p/696331/highlight/true#M254868

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Super User

Also consider using the built-in bootloader which is already supported by STM32CubeProgrammer.

If you feel a post has answered your question, please click "Accept as Solution".
lamare
Associate II

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
}