I just bought a STM32F103 "Blue pill" and a ST-Link V2 clone to program it. I'm having a weird issue, I can program and flash the microcontroller using Arduino IDE, and it works just fine, at least blinking LEDs and stuff, but when I try to flash it with STM32CubeIDE provided by ST, it doesn't work. It seems to know there's a microcontroller connected to the ST-Link, and when I try to flash it, if I was running the blinking code on the board, it stops, it doesn't blink anymore, like the STM32CubeIDE erases the previous program, but doesn't load the new one. This is what I tried:
- I made sure I wasn't using the pins the ST-Link uses to program and debug, and that the project had "Serial debug" configured to those pins
- I tried making the onboard LED blink and to even just turn on an output using HAL_GPIO_WritePin() and nothing
- I've read that some board have clone microcontrollers (even if mine does have the ST logo) and only work with OpenOCD debugger so I tried that too, modifying "stm32f1x.cfg" so it ignored the devices ID
- I've also read that changing the reset to "Software system reset" works so I tried it with OpenOCD and GDB Server, no luck
- I tried reinstalling the IDE
I really don't know what's going on, taking into account that with Arduino IDE it worked first try, not even an error or warning message.
This is the Consolre report when I try to use GDB Server:
Copyright (c) 2020, STMicroelectronics. All rights reserved.
Starting server with the following options:
Persistent Mode : Disabled
Logging Level : 1
Listen Port Number : 61234
Status Refresh Delay : 15s
Verbose Mode : Disabled
SWD Debug : Enabled
Waiting for debugger connection...
Debugger connected
-------------------------------------------------------------------
STM32CubeProgrammer v2.7.0-RC1
-------------------------------------------------------------------
ST-LINK SN : 4B2A18002C135737334D4E00
ST-LINK FW : V2J37S7
Board : --
Voltage : 3.19V
SWD freq : 4000 KHz
Connect mode: Under Reset
Reset mode : Hardware reset
Device ID : 0x410
Revision ID : Rev X
Device name : STM32F101/F102/F103 Medium-density
Flash size : 64 KBytes
Device type : MCU
Device CPU : Cortex-M3
Memory Programming ...
Opening and parsing file: ST-LINK_GDB_server_a10312.srec
File : ST-LINK_GDB_server_a10312.srec
Size : 6244 Bytes
Address : 0x08000000
Erasing memory corresponding to segment 0:
Erasing internal memory sectors [0 6]
Download in Progress:
File download complete
Time elapsed during download operation: 00:00:00.483
Verifying ...
Download verified successfully
Debugger connection lost.
And this is the output of OpenOCD
Open On-Chip Debugger 0.11.0-rc2+dev-00037-g4c4dbd9 (2021-02-09-13:39)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.159120
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : clock speed 4000 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x1ba01477
Info : STM32F103C8Tx.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for STM32F103C8Tx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Info : device id = 0x20036410
Info : flash size = 64kbytes
Warn : GDB connection 1 on target STM32F103C8Tx.cpu not halted
undefined debug reason 8 - target needs reset
Info : accepting 'gdb' connection on tcp/3333
Warn : GDB connection 2 on target STM32F103C8Tx.cpu not halted
undefined debug reason 8 - target needs reset
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000458 msp: 0x20005000
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000458 msp: 0x20005000
shutdown command invoked
Info : dropped 'gdb' connection
shutdown command invoked
EDIT to add more information: I tried debugging the code with the debug option. First time, I saw the microcontroller was "running" the code but was always getting stuck on the Error Handler code generated by the IDE, which is simply a loop. After more debugging, I found that for some reason it jumps to that subrutine if I use the external crystal of the board for the RTC, so I just changed it to use the internal RC, since I'm not using it for the moment. However,now the code "runs", jumping from breakpoint to breakpoint, but it doesn't actually run on the microcontroller. To explain it better, here's the main loop:
{
/* USER CODE END WHILE */
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
HAL_Delay(500);
/* USER CODE BEGIN 3 */
}
I have a breakpoin on each write calls, and if click on run, it shows that the code jumps from write to write in an infinite loop. So, in theory, it should be working, but in the microcontroller the status of the output pin doesn't change. It stays always ON, doesn't matter how many times I let the code run. It's like the debugger is emulating the microcontroller instead of running the code on the actual hardware.
I tried using Arduino IDE, and I can even make an OLED screen work with the STM board, so I'm still confused with the official IDE not working.