STM32 uses CubeMx to configure the MDK project to only download the program solution once

1. Problem description

Recently, when using Cubemx to configure the STM32F1 project, I found that the program can only be downloaded once, and the program cannot be downloaded the second time. The Debug in the Options for Target option displays No Device Connected (no device connected), and this small ST_Link module is used.
Insert picture description hereBecause there have been cases where the cable is too long to connect to the device (a lesson of blood and tears, the downloading program cable cannot be too long when using the SW four-wire mode, it will be very hanging if it exceeds 60 cm), the first reaction this time is the cable problem, so I changed After a short line, it still doesn't work. So I changed a board, the connection was successful, and I downloaded it once but couldn't connect. So the judgment must be a matter of setting.

2. Solution

I checked it on the Internet, and it turned out that I was not the one who encountered the problem. Let me summarize the method.

  1. Click SYS under the System Core option in cubemx, and select Serial Wire as the Debug mode. As shown in the figure
    Insert picture description here, the chip pins on the right side will have more SW pin settings
    Insert picture description here
  2. Click Generate Code to see that there is an extra line in MX_GPIO_Init() in main.c
__HAL_RCC_GPIOA_CLK_ENABLE();

One more line in the HAL_MspInit() function

 __HAL_AFIO_REMAP_SWJ_NOJTAG();

It is well understood that the SW function pin (GPIOA) clock and configuration function mapping are turned on respectively. You can manually add the above two lines of code without using Cubemx to regenerate the code.

  1. After the configuration is complete, you can download it for unlimited times. Of course, it can’t be downloaded at this time. Don’t panic. Press and hold the Reset button, click in the Options for Target option, and you can see that the device is connected in the Debug section. Click download to download the code, quickly release the reset button, and you can go in. .

Guess you like

Origin blog.csdn.net/qq_30267617/article/details/109465326
Recommended