STM32 gotchas
170.Programming 'G0/'C0 from built in bootloader has to be well-planned, as default bootloader entry depends on empty FLASH

Some users wish to program STM32 only through its built-in bootloader, avoiding the SWD connections - possibly to spare pins and/or PCB area in designs, where some of the interfaces used in bootloader are brought out anyway.

In the 'G0 (and related 'C0) families, the bootloader entry method (described as Pattern 11 in AN2606) in a factory-new chip uses the fact that empty FLASH results in boot from system emory(where the built-in bootloader resides). This allows to program a factory-new chip using the built-in bootloader.

Similar bootloader entry mechanism exists in other STM32 families - most notably, Pattern 6, used in several models of 'F0, 'L0, 'L4 families. However, there is a notable difference between Pattern 6 and Pattern 11: in the factory-new default setting of option bits in Pattern 6 enables using the BOOT0 pin (which is often a dedicated pin in related models) to enter the bootloader - option bit nBoot0_SW, which if cleared, would replace BOOT0 pin by nBoot0 bit, is by default set.

Contrary to that, in Pattern 11, the option bit nBOOT0_SEL steering between BOOT0 pin (which in these models is often shared with a GPIO) and nBoot0 option bit, is by default set which means that nBoot0 option bit is selected - and that in turn is by default set, which means no bootloader entry. In other words, factory-new default setting of option bits involves no mechanism how to enter the bootloader by outside manipulation of pins. That means, that after the initial programming of FLASH thus FLASH is not empty anymore1, bootloader entry is not available.

So, if users wish to have continuing access to bootloader in some form, either the firmware (possibly, a custom bootloader) should be written so that it deliberately enters the bootloader code upon some stimulus; or, should the BOOT0 pin be used, programming the nBOOT0_SEL option bit should be the first step when a factory-new chip is programmed.

1. The FLASH empty check mechanism (setting FLASH_ACR.EMPTY bit) is performed only upon power-on reset or explicit reload of option bytes using FLASH_CR.OBL_LAUNCH. So, after initial programming of FLASH, upon "pushbutton reset" without power cycle, the bootloader is entered again. This may result in even larger surprise to the unaware user, that after a subsequent powerdown-powerup cycle there's no bootloader entry anymore.