StLink v2 problem: Wrong device detected
01 October 2019 at 8:39 pm

I just spent way too much time on not finding the correct answer to a very simple issue, so posting it for others to find. I’m currently working on a project where I use the popular STM32F103C8T6 (as used in the nice and cheap BluePill boards). After a long evening of successful compiles, I suddenly got the error “Wrong device detected”.
The complete error message was like this:
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWDadapter_nsrst_delay: 100adapter speed: 950 kHzInfo : clock speed 950 kHzInfo : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748Info : vid/pid are not identical: 0x0483/0x374B 0x0483/0x3748Info : using stlink api v2 Info : Target voltage: 3.262028Error: init mode failed (unable to connect to the target)in procedure 'init'in procedure 'ocd_bouncer'
Now that was odd? I didn’t change much or did I? After a lot of Googling I realised that I had uploaded a Firmware that did NOT include SWD support. As soon as you upload a firmware that does not feature SWD support, you obviously cannot use it any more. You deleted it after all, right? Duhh… I mistakenly thought this was a hardware feature and when you remove this support you can’t use a debugger that only uses software reset (hardware reset will still work).
So - if you use ST-Link V2 or a similar cheap JTAG debuggers, be sure to either lay out a reset pin next to your SWD pins? Or you can do like I did, temporarily hold a wire from ground to the NRST pin on the STM32, release it and trigger an upload. You basically have to do the reset manually. Once a firmware with SWD support is flashed,you can use your trusted STLINK V2 again 😊
So - when using STM32CubeMX to generate a project, always turn on Serial Wire debugging for all projects!

Other similar errors
If you get the error above, but the output looks more like this, it can be that you have swapped the SWDIO and SWCLK wires:
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWDadapter speed: 4000 kHzadapter_nsrst_delay: 100Info : clock speed 4000 kHzInfo : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748Info : using stlink api v2Info : Target voltage: 3.262028Error: init mode failed (unable to connect to the target)in procedure 'program'in procedure 'init' called at file "embedded:startup.tcl", line 495in procedure 'ocd_bouncer'** OpenOCD init failed **shutdown command invoked