Better procedure for 'recovering'an STM32F103C8T6?
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.