Electronic – Better procedure for ‘recovering’an STM32F103C8T6

debuggingeclipsestm32

I have a STM32F103C8T6 (aka blue pill) and worked with it.

Sometimes – due a reason I don't understand – after debugging it (using Eclipse), the connection is lost and I cannot start a new debug session.

The only thing to do is the following time consuming procedure:

  • Close Eclipse
  • Start ST-Link Utility
  • In the menu ST-LINK/Firmware Update, selecting button Device Connect (to see if the ST Link stick can connect).
  • In case of an error, reinsert it (Windows shows Unknown USB Device) and redo the previous step
  • In case of no error, it shows the version: V2.J27.S6 STM32+STM8 Debugger
  • Than on the STM32 I have to press and hold the reset button
  • Select the menu option Target/Erase Chip
  • Release the button (in time, otherwise repeating the previous step)
  • Than the chip is erased.
  • Than start Eclipse again and I can continue

This is already a very tedious operation… is there a better solution?
Or by what reason the debugging session causes the STM32 to lose the ability to start a new debugging session?

And even than in some cases I get the following error when trying to debug:

23:23:02 : Can not connect to target!
                  If you're trying to connect to an STM32W1xx device, please select Normal or HotPlug mode from Target->Settings menu.
                  If you're trying to connect to a low frequency application , please select a lower SWD Frequency mode from Target->Settings menu.
23:23:03 : No target connected

The Eclipse project configuration file fragment:

# use software system reset
reset_config none
set CONNECT_UNDER_RESET 0

Best Answer

I found the problem of one of the questions, so I added it for other people's reference, since it is a non-trivial default:

When using the STM32CubeMX which I do, whenever a project is made, by default the following item is selected:

  • SYS, Debug: No Debug

This causes that the following code is generated in HAL_MspInit:

__HAL_AFIO_REMAP_REMAP_SWJ_DISABLE();

As soon as the debugger passes this command (which is one of the first commands in HAL_Init which is called in main(), the debugger loses connection and even a new debug session cannot be started.

It can be easily fixed with setting to

  • SYS, Debug: Serial Wire

(which is also called SWD: Serial Wire Debug, and is supported by ST Link 2).

This causes that the following code is generated in HAL_MspInit:

__HAL_AFIO_REMAP_SWJ_NOJTAG();

And debugging works as usual.