“Cannot connect to ST-LINK!” Error Message

The STM32F4-Discovery board uses either SWD (Serial Wire Debug) or JTAG for programming, and a bootloader is in place for serial communications via the USB. And by default, programming is done via USB using SWD. By checking the user’s manual, it became clear to me that the issue was caused by re-configuring PA11 and PA12 since these two pins are dedicated as DM DP pins for USB communications. By re-configuring these pins in code we essentially interfered with the USB communication mechanism. This may be the reason why these two pins were not broken out in the first place.

Solution

Fortunately, this problem can be corrected using the STM32 ST-Link UTILITY. When the USB cable is first plugged in, hold down the reset button and upon release immediately select Target->Erase Chip. You may need to try this a few times to get the timing correctly. Once the chip is erased, the bootloader will be able to utilize the USB connection correctly and you will be able to use USB port to program your STM32F4-Discovery board again.