2023-05-22 07:38 AM
I was converting from using the ST-LINK Utility command line interface (executable) to using the STM32 Programmer CLI (STM32_Programmer_CLI.exe) for production programming of some STM32G070x MCUs. The reason for that was that the ST-LINK Utility CLI didn't want to reboot the STM32 MCUs if it was the first time they were ever programmed, which caused problems with subsequent programming of an ESP32 on the same PCB. It will reboot the STM32 MCU every time they are programmed after that, but not the first time, for some reason.
I'd read that the STM32 Programmer CLI might do a better job of being able to reboot them after their first programming, and that it's replacing the older ST-LINK Utility CLI anyway, so that seemed worth trying.
I get a weird problem when trying to use the connect command in a Windows batch file, though.
I was using the same approach that I'd used with the previous ST-LINK Utility CLI, of using a `list` command, to check there was only one ST-LINK attached, and getting the ST-LINK programmer's serial number, which is used in the later programming command. Then I’d do a `connect` command, to check that there was a valid, powered STM32 MCU attached to the programmer, and get some information on it, to confirm it was of the correct type (correct flash memory size, etc.). Then I'd erase the STM32 MCU flash memory and program it.
I find that I can run the STM32 Programmer CLI commands in a command window, e.g.:
"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe" --list
or
"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe" --connect port=SWD
Both work fine.
I can also run the `list` command in the batch file without any issues, so (if I cut it down to the minimum to show what I mean), I can do this:
@echo off
setlocal EnableDelayedExpansion
set "stm32_programmer_filepath=C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
for /F "delims=" %%f in ('"!stm32_programmer_filepath!" --list') do (
echo %%f
)
pause
that produces something like this:
-------------------------------------------------------------------
STM32CubeProgrammer v2.6.0
-------------------------------------------------------------------
===== DFU Interface =====
No STM32 device in DFU mode connected
===== STLink Interface =====
-------- Connected ST-LINK Probes List --------
ST-Link Probe 0 :
ST-LINK SN : 066EFF313736504157095133
ST-LINK FW : V2J41M27
-----------------------------------------------
===== UART Interface =====
Total number of serial ports available: 2
Port: COM6
Location: \\.\COM6
Description: STMicroelectronics STLink Virtual COM Port
Manufacturer: STMicroelectronics
Port: COM9
Location: \\.\COM9
Description: USB Serial Port
Manufacturer: FTDI
Press any key to continue . . .
and I can then (rather than just echoing the output) retrieve, examine and use those lines of output from the CLI, to inform the user and create an appropriate MCU programming command.
But when it comes to doing the `connect` command, if I simply place the command in a batch file, e.g.:
@echo off
setlocal EnableDelayedExpansion
set "stm32_programmer_filepath=C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
"!stm32_programmer_filepath!" --connect port=SWD
pause
then the command runs fine and produces an output like this:
-------------------------------------------------------------------
STM32CubeProgrammer v2.6.0
-------------------------------------------------------------------
ST-LINK SN : 066EFF313736504157095133
ST-LINK FW : V2J41M27
Board : NUCLEO-G070RB
Voltage : 0.00V
SWD freq : 4000 KHz
Connect mode: Normal
Reset mode : Software reset
Device ID : 0x460
Revision ID : Rev B
Device name : STM32G07x/STM32G08x
Flash size : 128 KBytes
Device type : MCU
Device CPU : Cortex-M0+
Press any key to continue . . .
but if I instead try and do something with the output (even just echoing the lines back, as a precursor to parsing it and extracting information from the lines), e.g.:
@echo off
setlocal EnableDelayedExpansion
set "stm32_programmer_filepath=C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
for /F "delims=" %%f in ('"!stm32_programmer_filepath!" --connect port=SWD') do (
echo %%f
)
pause
I get this error:
-------------------------------------------------------------------
STM32CubeProgrammer v2.6.0
-------------------------------------------------------------------
Error: The connect command you trying to perform is missing the port name value. Please refer to the Help to see how to use it.
Press any key to continue . . .
despite the command being the same as when entered outside of the `for` loop.
Can anyone see what's going on here? It's driving me mad! Also - fun grammar mistake in the error it gives!
Thank you for any help you can give.