problem Here, we have checked "Reset after programming", the purpose is to reset the chip after programming, you can run the user code. Then click the "Start" button to start programming. First, there is a check mark inside the chip to mark whether the chip is empty. This flag is used when the BOOT0 pin is defined to boot from Main Flash memory . When this flag is set to "1", the chip is considered empty and the system will boot the bootloader from Systemmemory to allow the user to download the code, even though the BOOT0 pin is now defined to boot from Main Flash memory. This flag is only updated when Option bytes is loaded : when the address 0x0800 0000 reads 0xFFFF FFFF, this flag is set to "1", otherwise it is "0". This means that when the user code needs to be executed after a system reset after programming a blank, it must be re-powered to generate or set OBL_LAUNCH in the FLASH_CR register to start Optionbyteloader reset to clear the check mark. a) The BOOT0 pin is used; Remove the checkmark of "nBoot0_SW_Cfg", click "Apply", change to nBoot0 and nBoot1 in the option byte to control the boot area iii. Open the code file that needs to be programmed, and click the "Program Verify" button in the dialog box. Check "Reset after programming" and click "Start" to complete the programming.  This way you can see that the user code is already running, is it very simple. ST-LINK_CLI -c SWD UR ST-LINK_CLI -ME ST-LINK_CLI -p xxxxxxxx.hex-v "while_programming" ST-LINK_CLI -OBnBOOT0_SW_Cfg=0 ST-LINK_CLI –Rst ST-LINK_CLI.exe is located in the STM32 ST-LINK Utility installation directory. For the command, please refer to ST-LINKUtility UM.pdf. Nantong Boxin Electronic Technology Co., Ltd. , https://www.ntbosen.com
A customer used the STM32F091RCT6 in the design of its products. The customer used ST-Link to program the STM32F091RCT6 and found that after programming the empty slice, the user code must be re-powered to run; however, if it is not a blank, the user code can be run directly after programming. Since the customer's test system directly enters the test mode after the chip is directly programmed, it will bring some trouble if the blank chip needs to be powered off. The customer wants to understand this and hopes to find a way to run the user code directly after the film is programmed.
Research
1. Restore problem
Here, use the NUCLEO-F091RC board with STM32F091RCT6 for problem recovery and connect this Nucleo board to your computer via a USB cable. Open the STM32 ST-LINK Utility, click the “Connect to the target†button to connect the chip. After connecting, open a .hex file code that flashes the prepared LED. Click the “Program verify†button to prepare for programming.
After programming, it can be seen that the LED light flashes, but it does not appear. After the MCU needs to be powered off, power on again to see the LED flashing. This means that a power-on reset is required to run the user code.
2. Analyze the problem
Let me review the reference manual RM0091 of STM32F091 for the description of Empty Check:
It is now possible to analyze what is currently happening:
When the blank is connected to ST-Link through SWD for programming, since the chip detection detects that the chip is empty and the check flag is set, the system starts the bootloader from System memory and starts running. The chip is programmed via a simple SWD interface. The “Reset afterprogramming†check will generate a reset signal on the RESET pin after programming, but unfortunately this reset does not clear the check flag, resulting in a reset after the reset. Bootloader is started in System memory without running user code, which is what we encountered before.
In general, we can generate a POR by re-powering to clear the empty flag and start the running user code from MainFlash memory. However, the customer's current special needs will cause some trouble. Another application is also more troublesome, that is, a product using a lithium battery, and the battery is directly soldered to the user's board, and it is not easy to power off. At this point, if the empty piece is soldered on the board for online programming, then the problem is coming. After the empty chip is programmed, it is inconvenient to power off, and the POR action cannot be completed. An Option byte loader reset cannot be implemented without running the user code. The check mark cannot be cleared, and the program operation will lock up the Bootloader in System memory.
3. Problem solving
There are of course many problems with this kind of problem. Let's explore it in the following:
1) Solved from the production: the chip is programmed separately on the programmer, and then on the board, avoiding the POR after the blank is programmed.
2) Solve from the hardware: use a jumper, or use other methods, such as finding a way on the fixture, to achieve a POR by manually powering off and then connecting to the power. Need to be reserved on the PCB. You can choose your own countermeasures. But this will increase production troubles and reduce efficiency.
3) Solve from the programming method: do not use ST-Link for programming, use the Bootloader to upgrade the serial port directly, and then upgrade to Main Flashmemory to run the user code. The code that maps MainFlash memory to 0x0000 0000 needs to be added to the user code.
4) In the first few ways, everyone will understand if they solve it. However, if you must use ST-Link to write through SWD, it is another matter. Let's explore this method below.
Generally see this kind of problem, the intuitive thinking is to think whether there is a way, you can jump to the user code to run the user code through a certain ST-LINK command after ST-LINK programming. The method seems feasible, but it is a bit complicated. First, STM32 ST-LINKUtility does not provide similar functions, users need to use the ST-LINK_CLI command by themselves; second, you need to add the code in the user code that does not forget to map MainFlash memory to 0x0000 0000. Third, because of the empty check The flag is not cleared. You need to worry about the unexpected reset signal or interference. After the reset, you will run back to SystemMemory. You also need to add “In the user code to determine whether it is OptionBytes Loader reset. If not, execute Option BytesLoader reset directly. To clear the check mark."
Our intuitive thinking is to solve problems, but after reading the above description, such a solution is really a bit troublesome. Is there any other simple way? The answer is yes. We should not stop thinking in solving problems, but how to avoid problems. Here's a rationale:
The root of this problem lies in the existence of the empty mark, so what needs to be considered is how to avoid the impact of the empty mark?
Let's take a look at the conditions under which the check mark is generated :
b) BOOT0 pin is low, the boot area points to MainFlash memory;
c) Read the value of 0x0800 0000 address 0xFFFF FFFF;
Because it is a blank chip programming, the third condition is definitely true; due to the hardware design, the level of the BOOT0 pin is not easy to change; therefore, the focus needs to be placed on the first condition - " The BOOT0 pin is used." Due to the characteristics of the STM32F091, there is a chance to use the option byte instead of the BOOT0 pin, so the solution is available.
Proceed as follows:
i. Open the STM32 ST-LINK Utility and click the “Connect to the target†button to connect;
ii. From the menu "Target → Option Bytes" dialog box to bring up the option byte
If you want to do it more easily, you can use ST-LINK_CLI to write a batch file that contains the following actions:
in conclusion
Due to the scavenging detection mechanism, after the STM32F091 blank is used for ST-LINK programming, the reset will return to System Memory when the power is off, and the main flash memory cannot be entered to run the user code. Therefore, in special applications, if it is impossible to power off and then power on, it is necessary to use a method to destroy this mechanism.
deal with
The Boot startup configuration to be controlled by the option byte, instead of using Boot0 pin, in order to undermine the influence blank check mechanism.
Suggest
For the solution of the problem, generally think in two directions: First, there is a problem and then find a solution to the problem; Second, how to avoid problems. Many times, due to the inertia of thinking, many engineers may prefer to think directly from the first direction; however, in fact, if you can think from the second direction and stop the problem, that is the best way.