2025-07-07 11:43 AM - edited 2025-07-07 11:44 AM
I'm developing a program and we want to be able to flash it via bootloader in production with a UART
To do that, we are using a NUCLEO-F302R8 board to flash a custom board with a STM32G0B1 following this post: https://community.st.com/t5/stm32-mcus/how-to-program-an-stm32-with-another-stm32-using-an-embedded/ta-p/612362
It worked, but only when flash was erased. In order to work in case of accidental disconection of UART wires, we need to be able to flash it in any case by pulling BOOT pin high
The option bytes were:
nBOOT1=1, nBOOT0=1, nBOOT_SEL=1. That means I was in the fourth line scenario, it attempted to boot from Main Flash and only booted from bootloader when it was empty regardless of BOOT0 pin state
To fulfill the requirements of the project, I needed to set the first and second lines scenario, so it would boot from Main Flash when BOOT0 pin was low and boorloader when high. To do that, I changed the option bit nBOOT_SEL from 1 to 0
Now, with nBOOT1=1, nBOOT0=1, nBOOT_SEL=0, I was able to flash from bootloader every time that BOOT was high, which is what I wanted. The issue was that when I tried to connect from SWD to develop the rest of the project, I found that I was unable to connect from SWD. I tried everything: connect from under reset, power down, core, software and hardware reset and it was impossible.
I could still flash the memory through bootloader, so I tried to erase it completely and succeded but still could not connect through SWD, so finally I decided to change back the option bits to nBOOT1=1, nBOOT0=1, nBOOT_SEL=1. From then on, I have been unable to connect through both bootloader and SWD. I understand that nBOOT_SEL=1 caused to enter Main Flash regardless of BOOT pin level, but that does not explain why SWD became unresponsive
Is there a way to restore the chip from this state? And what could have caused it?
This is the function I used to change option bytes (because I could no longer do it from STM32CubeProgrammer without SWD)
void ConfigureBootSTM32G0B1(uint8_t nBOOT_SEL, uint8_t nBOOT0, uint8_t nBOOT1)
{
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
FLASH_OBProgramInitTypeDef OBInit = {0};
HAL_FLASHEx_OBGetConfig(&OBInit);
uint32_t newUserConfig = OBInit.USERConfig;
if (nBOOT_SEL)
newUserConfig &= ~OB_USER_nBOOT_SEL;
else
newUserConfig |= OB_USER_nBOOT_SEL;
if (nBOOT0)
newUserConfig &= ~OB_USER_nBOOT0;
else
newUserConfig |= OB_USER_nBOOT0;
if (nBOOT1)
newUserConfig &= ~OB_USER_nBOOT1;
else
newUserConfig |= OB_USER_nBOOT1;
OBInit.OptionType = OPTIONBYTE_USER;
OBInit.USERType = OB_USER_nBOOT_SEL | OB_USER_nBOOT0 | OB_USER_nBOOT1;
OBInit.USERConfig = newUserConfig;
if (HAL_FLASHEx_OBProgram(&OBInit) != HAL_OK) {
Error_Handler();
}
if (HAL_FLASH_OB_Launch() != HAL_OK) {
Error_Handler();
}
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}
2025-07-07 6:50 PM
Why can't you connect with SWD? What happens when you try? Unless you are erasing/programming flash, reassigning SWD pins, or otherwise, the connection will work. If the chip is in the bootloader, you can connect with SWD. No way to disable SWD apart from setting RDP level 2.