Best method to debug STM32 boards? - Page 1
Forum section: electronics / general

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 14, 2021, 01:40:31 am
content: I've been working with STM32 processors in my projects for about 3 years now. I typically use the Blue Pill or Black Pill boards for small projects. I put header sockets on my custom PC boards and just plug those modules into the board. For the larger projects I put a STM32F407 part on my custom board.For programming the parts I use the cheap Ebay ST-LIINK V2 knockoffs. They work perfectly well for programming the boards. I am using STM32CubeIDE for my development environment. Most of the time I do not have to use the debugger as my projects are simple.However, when I do need to use the debugger it is a real pain. It usually takes 30 minutes to an hour to get the debugger working. I just keep changing the settings in the debugger configuration menu and eventually it starts to work. There seems to be no rhyme or reason as to what the settings need to be for the debugger to work. The error messages are usually that it can't start the GDB server or it can't detect the ST-LINK programmer. I needed the debugger today and managed to get it working in about an hour.Has anyone found a 100% foolproof method of using the STM32CubeIDE debugger. If so, what hardware are you using? Should I buy the real ST-LINKV2, Mouser part number 511-ST-LINK/V2? Would I be better of with the Seeger J-Link EDU mini part number 8.08.91? Or do I need another IDE tool?I did purchase the STLINK-V3MINI but haven't hooked it up. I received some connectors this week so I could make up an adapter cable that will fit the mini so I can connect it to my boards. I am going to make the cable this weekend and give the mini a try. I don't know if that will make the debugger function properly or not. From the hundreds of posts online about people not being able to use debug mode it may be a problem with STM32CubeIDE. I did try using the ST-LINK V2 programmer on one of the official STM32407 development boards to debug my board. But it didn't work any better than the Ebay ST-LINK clone. It had no trouble programming my board.I am looking for a solution that will work right from the start without having to spend an hour to make it work. I have never been able to make the debugger work using the ST-LINK GDB server setting in STM32CubeIDE. I always have to use the OpenOCD setting.

author: langwadt
author_rank: Super Contributor
timestamp: August 14, 2021, 01:55:52 am
content: Quote from: rcbuck on August 14, 2021, 01:40:31 amdo you have rst connected to the debugger? only problem I've had is that the st-link and cube does not like usb3 ports on an usb-c connected docking station

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 14, 2021, 02:20:44 am
content: No, I do not have rst connected as rst appears not to work on the Ebay clones.My computer only has USB3 ports, no USB2. My old computer had only USB2 ports and I had the same problem with it.

author: ttt
author_rank: Regular Contributor
timestamp: August 14, 2021, 09:42:25 am
content: Quote from: rcbuck on August 14, 2021, 02:20:44 amConnecting and using NRST will likely improve your debugging experience in STM32CubeIDE, especially when your MCU is stuck in some IRQ code or hangs otherwise. I've had no issues with BluePills in the past in conjunction with a real STLinkV2 and V3. But that's after verifying that I have a real STM32 part that I am debugging and not a GD32 or CS32 clone.Quote from: rcbuck on August 14, 2021, 02:20:44 amI have never had issues using USB3 ports with STLinkV2/V3s. Avoid using a USB hub, that will cause problems in some cases. The STLinkV3 will not work at all on a hub in my experience.I find that the Segger J-Link EDU does not always work as well as a STLinkV2 in STM32CubeIDE.STLinkV2 is really well supported by openocd which is a big plus. I can program non ST parts with it (NXP, Nuvoton, ATSAM etc.).STLinkV3 support in openocd is not as good but I like the integrated VCOM interface which is useful for printf debugging.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 14, 2021, 01:14:28 pm
content: Never had any issue, debugging takes 2 minutes to set up.In cubeMx go to system core, sys, and enable serial wire (or trace async if you want to use it, remember it uses SWO pin, usually PB3).If you use SWO, go to debug configurations/debugger and enable SWV setting the cpu core clock.Compile for debug, and it should run ok.If you enabled trace, you always need to pause the core and enable SWV red button.Open it from window/show view/other/SWV/...The first timw you'll also need to enable debug port 0 (ticking it).A good habit is to always enable serial wire, even in non-debug builds, otherwise the debug interface will be disabled and you'll have issues when trying to program the stm32, requiring nRST to be connected.Of course, you shouldn't connect anything else to these pins. Common mistake... PA13 and PA14 should be free, also PB3 if you use Trace.Some cheap stlink clones are known to be incorrectly wired, I'm not sure but I think nRST was a common one.Check the nRST pin, should be connected to debuggers's stm32 pin 19 (PB0). Might be through a series resistor or not.But the problem is purely due a design error, the pin actually works. I think my last clone was wired correctly.My advice is to get a real STLINk v2 clone, the white plastic box.At least for me, it works much better than these small usb drive-like clones.If you wish, make an empty project that fails to debug, and I'll try to check it out.It'll be mostly caused by bad settings, but these debuggers can also come deffective.I had one that wouldn't work by any means, when I opened it up, half of the stm32 pins were floating over the solder.No problems with usb 2/3 ports.

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 14, 2021, 05:40:08 pm
content: ttt, have you been able to get the ST-LINK GDB server to work or do you just use the OpenODC GDB option? The Bluepill I was working on yesterday has a real STM32 part. I verified it with the Bluepill Diagnostics tool that techman-001 provides.DavidAlfa, I know the nRST line is not connected on my clones. They are over 3 years old. I've seen a couple of postings on internet how to open them and correct the issue by adding a jumper. The nRST connections is available on the STLINK-V3MINI that I am going to wire up later today.I don't use trace (SW0) as I normally have something connected to PB3. I don't use PA13 or PA14 as they are strictly for programming/debugging and don't breakout on the normal headers.QuoteDo you mean the one with Mouser part number 511-ST-LINK/V2? That is the one I was going to order.Thanks guys for all the replies and suggestions.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 15, 2021, 01:42:25 pm
content: Make notes of what you do and how you got it to work.The first time your notes probably miss some steps, or even have misleading steps, but it will help to get the debugging up quicker the next time, so it would only take 15 minutes.Iterate untill your notes are correct and complete, and then you can do it in 2 minutes.I do not do much with the STM32 processors I have (I bought a bunch of blue pills from before the fakes happened, and I've bought a bunch (around 20) of WeAct STM32F411 boards.With the good old AVR processors I did not have a real debugger.What I used to do was a two pronged approach.For generic algorithms I firs wrote a small PC program to test and debug them. Getting a debugger working on a PC is usually trivially simple.For timing related issues I very much like to use a Logic Analyser as debugger.You can easily use some un-used peripheral( SPI, UsART, or even just an I/O pin, etc) to spinkle out hints of where your uC is and what code it is execuiting at a certain time.With a LA you can also correlate this with other things that are happening on uC pins.The overhead to do this is very low (Writing a byte to a UsART register is just one or two instructions)What I really like about this method, is that you can search backwards in the logic analyser trace to find hints about what happened before your uC *&^%$#@! up.I also have some interest in the "Black Magic Probe" (Open source program, but also available as (an unfortunately relatively expensive) PCB. I have not used it myself, apparently it runs the GDB server on itself, so you have to setup a step less.

author: rstofer
author_rank: Super Contributor
timestamp: August 15, 2021, 02:56:51 pm
content: I have never had much success with hardware debuggers and I have a really short attention span. So, I use printf()...

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 15, 2021, 10:26:11 pm
content: i made up a cable for the V3Mini but it doesn't see the target board. I confirmed the connections to the ribbon cable with a meter. The mini board is recognized by both the STLink Utility and the STM32CubeProgrammer but neither one is able to see the target. So I will write the STLINK-V3MINI up as a waste of money. It is useless and the 1.27 mm connector is a joke. There is no simple affordable way to get the Mini connections out to a standard 2.54 mm connector.I modified one of the Chines clones to add the nRST connection through a 22 ohm resistor. But that was a failure also. The modified clone keeps the target in reset as soon as I connect the nRST wire to the target.I connected my other clone to the board and tried a debug session. It fails with these error messages in the output window of STM32CubeIDE:QuoteAbout 5 seconds later the "Problem Occurred" window pops up with these messages:QuoteI just placed an order to Mouser for the Real ST-LINK/V2 programmer/debugger. My guess it that the real device will solve the problems with debugging.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 16, 2021, 06:43:58 am
content: Why are you using openocd instead stlink's gdb server?For openocd, go to debugger settings and click on edit generated settings (or something like that) and set reset method to software, otherwise it might use hardware (nRST) which obviously won't work.

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 16, 2021, 03:32:05 pm
content: QuoteBecause I have never been able to make STLINK GDB server work in 3 years of trying.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 16, 2021, 03:44:58 pm
content: Might be a clone/compatible stm32 instead the real thing?What issues are you getting? Works really nice here

author: errorprone
author_rank: Contributor
timestamp: August 16, 2021, 03:52:26 pm
content: For converting between 2.54 and 1.27mm arm programming connectors, this is what I’ve been using.https://www.digikey.com/en/products/detail/adafruit-industries-llc/2094/6618718.

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 16, 2021, 05:29:05 pm
content: QuoteI verified it with the Bluepill Diagnostics tool that techman-001 provides.Trying to start STLINK GDB gives these messages:Error in final launch sequence:Failed to start GDB serverFailed to start GDB serverError in initializing ST-LINK device.Reason: (4) No device found on target.I don't know why it can't find the device. Using STM32 ST-LINK Utility to program the device I have no problem. Settings for debugger are in attachment.errorprone, the link doesn't work. However, the Adafruit 2094 device is a 2x10 2.54mm to 2x5 1.27mm adapter. The V3MINI has a 2x7 1.27mm connector. They include a 2x7 1.27mm ribbon cable that apparently you are supposed to put a 2x1.27mm male header on all your boards. The Bluepill boards only have 4 2.54mm pins. If you solder a wire to the reset pin on the board you have access to the only 5 pins you need for SWD programming.Mouser says I will have the Real ST-LINK in the white plastic case on Wednesday.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 16, 2021, 05:40:20 pm
content: Strange. What happens if you first completely erase the stm32, then try debugging?

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 16, 2021, 06:36:48 pm
content: I completely erased the chip. Then tried to start the debugger. Message I received:STMicroelectronics ST-LINK GDB server. Version 5.9.1Copyright (c) 2021, STMicroelectronics. All rights reserved.Starting server with the following options:Persistent Mode : DisabledLogging Level : 1Listen Port Number : 61234Status Refresh Delay : 15sVerbose Mode : DisabledSWD Debug : EnabledError in initializing ST-LINK device.Reason: ST-LINK: Could not verify ST device! Abort connection.So I then ran the Bluepill Diagnostics and got this message:STM32F103C8 authentication FAILED one or more tests:----------------------------------------------------FAIL - Declared flash not 65536FAIL - DBGMCU_IDCODE is readable with no SWD/Jtag connectedFAIL - Second 64KB flash block not verified or not testedSo I this board is in fact a fake. I got 5 boards from the same supplier and had ran diagnostics on a couple of boards. I assumed they were all good. (I know) Apparently this is not one of the boards I tested. So mystery for this one is solved. I don't have any of the other boards left as they are in deployed projects.Does anyone know of a supplier who has legitimate boards (preferably U.S. seller) available. That may be asking too much with the current chip shortage. Or is there a debug program available that will work with the fake boards? I will see if the fake board will run OCD with the Rea STLINK when it gets here on Wednesday. I have solved my problem with the code on the current board so debugging is not a priority now. But I'm sure I will need it at some point in the future.Thanks for all the help and sorry I did not check this board with the techman-001 tool earlier.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 16, 2021, 08:19:23 pm
content: QuoteAaaand that's called "clone".

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 16, 2021, 08:27:33 pm
content: Exactly! Now if I can find a supplier of a bluepill board with a real STM32103...............

author: Kjelt
author_rank: Super Contributor
timestamp: August 16, 2021, 09:44:40 pm
content: Quote from: DavidAlfa on August 14, 2021, 01:14:28 pmA real clone. Bladerunner anyone ?Seriously, why not a real STlink v2, they were $15 or so from Farnell.When my company moved I found 8 in the dumpster, just gave them away, they are cheap.You can even break one of from any Nucleo board and use that.Quote from: rstofer on August 15, 2021, 02:56:51 pmQuite usable if you have your own printf function and not drag the c library with it, and ofcourse still have a serial port left but yet hardly a full replacement of ICD.It also takes time to output the information which can result in a difference in behavior between debug mode and normal mode. But yes it can be handy in times, Esp. when you need to trace some spurious behavior, let it run for the weekend and check the output on monday morning.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 16, 2021, 10:13:21 pm
content: The issues are caused by the mcu, for sure.The cheap stlink's do work pretty well.I have a full stlink clone (not the mini crap), couldn't be happier por $7 incl.shipping.

author: rcbuck
author_rank: Frequent Contributor
timestamp: August 18, 2021, 10:20:43 pm
content: I received the REAL ST-Link/v2 from Mouser today. Hooked it up to my board and did a couple of tests.It will not start the ST-LINK GDB server as it sees the bluepill board as not having a real STM32F103 part on the board. Error Message:Error in initializing ST-LINK device.Reason: ST-LINK: Could not verify ST device! Abort connection.However, if I change the debugger type to ST-LINK (OpenOCD), it starts the GDB server 100% of the time. The clone ST-LINK V2 programmers will eventually start the OpenOCD debugger if you keep playing with it. You just have to keep switching options back and forth until it manages somehow to start the debugger.So the bottom line is the REAL ST-Link/v2 will debug the bluepill boards that have the fake STM32 parts if you use OpenOCD. It will not start the ST-LINK GDB server if the bluepill board has a fake part on it.I haven't found anyone who has the real STM32F103C8T6 parts in stock. Actually I did find one of the hoarders who has 968,000 of them in stock. They want you to send them a RFQ. F**K them!!

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 18, 2021, 10:41:04 pm
content: I had one like that, got much better after adding a 10uF tantalum cap inside