 Hello and welcome to this video about empty check mechanism on STM32 microcontrollers. Empty check mechanism is present on some STM32 microcontrollers and it forces the MCU to boot into the system bootloader if the content of the first address in flash is empty. This simplifies programming of a virgin devices because there is no need to tie the bootpins into certain logical level. But on the downside it can cause problems especially if user is not aware of it. The system bootloader configures multiple GPIOs into the output mode which can be a problem if the device is programmed on an assembled PCB. These GPIOs can be connected to some external logic or in worst case tied directly to ground or VDD which could result in short circuit and possible damage of the microcontroller. A second common issue is that after the programming is finished and the programmer resets the MCU with a reset pin the code inside user flash is still not executed and instead the MCU boots again into the system bootloader even though the flash is no longer empty. There is an empty check bit which must be cleared and we will have a look on multiple ways to do this. But first let's have a look on which STM32 families we can find this empty check mechanism. It is present on all G0s and WBs part numbers and on subset of L0s, L4s and F0s. So let's imagine you have an empty STM32 device on an assembled PCB and you want to flash it without going to the system bootloader. The following procedure can apply both for prototyping or production programming. The MCU must be kept under reset before and after applying the power. At this point all the GPIOs are in analog mode, in other words in high impedance except for the debug pins. The debugger can connect under reset and hold the MCU at the reset vector, in other words it holds the core before it executes any instruction. Now the flash can be programmed and when it's finished the last step is to clear the empty check bit, otherwise the MCU would boot again into the system memory after next reset. So the empty check bit can be cleared by power cycling the board which might not be an option if for example the battery is already assembled on the product. The second option is to perform option byte relaunch which will generate a system reset and resampling of the empty check bit. The last option is to clear the empty bit directly in the flash configuration register but this option is available only on STM32 G0 and WB. Let's now move to the hands on part. I will try flashing an empty STM32 without entering the system bootloader. I will use STM32 G0 nuclear board and I will connect an external yellow LED to one of the system bootloader interfaces which is the UART1 transmit signal on PA9. When the system bootloader is executed this GPIO is configured in an output mode and the yellow LED turns on so we have some visual feedback. For loading the test binary I will use key programmer. Firstly I will connect to the target under reset. Then I will flash the binary which simply blinks the green LED on the nuclear. Lastly I will apply one of the procedures to clear the empty bit. So what I have here is a G0 nuclear board with an empty internal flash. So after power on the microcontroller boots into the system memory and it configures all the communication interfaces including the PA9 and that's why you see the yellow external LED light up. So this is something we want to prevent. So how can we do that? I open the key programmer and you see that in the STLINK mode you see three options normal hotplug and under reset. I select connect under reset and what I can do is to press the reset button physically here on the board. Keep the microcontroller under reset and at the same time connect with the STLINK. If I release the reset button you see the debugger connected shortly after but quick enough that the bootloader is not yet executed. The yellow LED is not light up. We can double check the program counter in the CPU tab you see that the program counter is at this address which is in fact the reset vector of the system bootloader. So now we can program our application I have it prepared here as a hex file so I'll simply drag and drop it into the key programmer and we can double check I will press download the file download is complete and at this point I want the user code to be executed. If I simply reset the microcontroller will still boot into the system memory. So one way to clear the empty check flag is to power cycle but I will try the second option which is to trigger the option byte launch. I do not want to change any option byte but you can trigger the option byte launch even if you don't make any modifications. Here with the key programmer I have to do at least something so let me for example select the write protection option bytes and I will for example modify the start address from 3e to 3d. This does not make actually anything as long as the start address is higher than the end address there is no write protection so this is simply just dummy modification to perform the option byte launch. So I will apply this change option byte successfully programmed I can now disconnect and you see the program starts to execute the green LED blinks and the microcontroller did not enter system bootloader during the process. As a last note I would like to point to you one useful document that's the application node AN2606 where it describes the system memory boot mode on STM32 families. I hope you find this video useful and hope to see you next time.