NUCLEO-L031K6 and ST-LINK SWD. What on earth did I do?

ST has a nice family of ultralow power microcontrollers (or MCU for short) that can be used in applications with energy harvesting power supplies. My objective was to use one of these MCUs to test the capabilities of an energy harvester based on Peltier cells to power an application to acquire signals from a sensor. I chose an STM32L031K6 MCU because I could easily find a unit with a LQFP32 (low profile quad flat package) easy to solder on a bare breadboard such as:

easily found on ebay. And, most importantly, I had a development board based on the same MCUs available from ST, NUCLEO-L031K6. I could test my program on the NUCLEO board and then flash it in the stand alone chip to know how much power consumption did I have. The power consumption can be estimated in the NUCLEO board but the process is awkward and probably not reliable since there are LED everywhere and currents are drained in unexpected ways.

My problem was how to flash that stand alone STM32L031K6 MCU. I knew from the Arduino side that it is possible to program an MCU via a serial connection with Arduino UNO but it was difficult to find information about how to do it with STM32 products. The main reason is because most people use ST-LINK and do not need anything else. Well, I didn’t have an ST-LINK programming device (also found in ebay) and I wanted to accept the challenge of finding another way with the tools I had. The answer to a question I posted on the mbed discussion forum gave me some clues as how to proceed. There seemed to be an alternative to the serial port and it was to use the SWD (serial wire debugging) port with an external flash device. The ST-LINK uses the SWD to flash the target processor on a nucleo or discovery board and I could use the ST-LINK of any nucleo to flash an off-the-shelf processor as Wim kindly told me. I had a nucleo board! and… I also had a discovery board! mine was a discovery kit with STM32L476VG MCU, so I decided to give it a try.

I soon found that there was very few information about how to use the ST-LINK in the 32L476GDISCOVERY kit. Apart from some lines in section 7.1.4 of the user’s manual I couldn’t find anything else so the process should be pretty straightforward. I needed to remove two jumpers from connector 3 (CN3) and connect the SWD port to the target processor. Jumper 5 (JP5) must be set to OFF and jumper 6 (JP6) to 3V3. Additionally, JP3 must be ON if CN4 pin 5 (NRST) is used in the external application board. Since I didn’t know if NRST was used or not I tested both cases.

 

The connections are summarized in the Table. Bear in mind that pin 1 receives the voltage connected to the external MCU. It is not used as a voltage supply for the target device. I didn’t connect pin 6 SWO.

My wiring was as shown in the next Figure:

Then I used the STM32 ST-LINK utility to program the external MCU but alas, the application couldn’t find it. This was the recurrent error no matter what I tried:

ST-Link/V2 device detected
Target voltage detected: 3.221551
Error getting target IDCODE: if SWD, check SWD connection
Error (4) while initializing ST-Link in SWD mode

The good news was that the ST-LINK of the discovery board was found and the voltage was correct but the target MCU didn’t respond with its IDCODE. I still don’t know what did I do wrong but I suspect it has something to do with the need of an external oscillator connected to the target device. This is something I would need to ask.

The next step was to circumvent this error and go directly to an external MCU ready to be programmed: the MCU in my NUCLEO board. I decided that it could be a good idea to use the pins marked as reserved for an SWD connector (CN2) in the board.

I cunningly guessed what pins corresponded to which in the board and connected them to the SMD port in the 32L476GDISCOVERY board. This did the trick and now, the ST-LINK utility found an MCU without problems. I indeed noticed that the MCU was an STM32F103 instead of an STM32L031 but I didn’t pay much attention since… what the heck! the software had found an MCU after hours trying with the stand alone one that I had had soldered!

I swiftly compiled a dumb blinky program to drive an LED in the NUCLEO board, flashed and verified it with the ST-LINK utility and it worked. I had the program in the memory of the MCU and the LED was truly blinking. Strangely, the blink sequence was different from what I had selected in the program so I suspected something was amiss. I changed the ON and OFF times again and uploaded the program to obtain the same blinking as before. Definitely, something was very wrong. I disconnected the NUCLEO from the SWD port and connected it to a PC with the USB port, and, Windows couldn’t find it… Damn! I had erased the ST-LINK firmware of the NUCLEO and programmed it with the dumbest program on earth. This was pretty clear from the begining, there were many clues that I was doing something wrong such as finding a different MCU with the ST-LINK utility or Figure 11 in the reference manual of the NUCLEO-L031K6 where it is clearly stated that CN2 corresponds to the ST-LINK part of the NUCLEO, but you don’t heed these warnings when you are eager to program.

I hope I can recover it somehow. I have some ideas and I expect not to spoil anything else.

In any case, I finally managed to program the MCU in this post.