2025-10-28 9:07 AM
Hello Team,
To address mass Programming of Customized board , Looking help to have Batch File / Python Script which accomplishes Following Task :
1. Flash The Hex file
2. Set RDP1
3. Read UID (96-bit ) of MCU
4. Log UID and Time into CSV
I tried with batch File using STm32Cube Programmer CLI.exe , but Failed to connect with ST-Link.
yes, I am Using SWD Debug Interface.
I am not sure uf any scripting Template is alraedy ready with CubeProgrammer.
Solved! Go to Solution.
2025-11-13 10:00 PM
We treid with Both Powershell and Old command shell, But stuck at some otr other point.
Any way , we found solution by leaving batch script and moving with Python.
it worked seamlessly.
Thank you for your time.
2025-10-28 1:53 PM
Batch file with CubeProgramm CLI should work - we do it all the time. Post your batch file and whatever output/error messages you get.
2025-10-29 4:02 AM - edited 2025-10-29 4:08 AM
Hell @Bob S ,
Pls. find attached one Bat File FYR.
You can suggest related document / post already available to understand it.
1. Following is , When I run it with Windows PowerShell
2. When I run in CommandLine
3. CSV LogFile
4. Folder view on my PC , all file in same Folder
2025-10-29 10:05 AM
And what does that error message lead you to? I'm guessing the batch file turns off echo. If so, enable it so you can see all the lines that execute.
Please post your batch file here instead of attaching it. My work environment restricts ZIP file downloads.
2025-10-30 7:37 AM
Okay, Bob S
Here is the Batch Scripts , FYR. Error windows was shown in Previouse Post , 2nd Image.
@Echo off
setlocal EnableDelayedExpansion
REM ============================
REM CONFIGURATION
REM ============================
set PROGRAMMER="C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
set FIRMWARE=firmware.hex
set LOGFILE=Production_Log.csv
set UID_ADDR=0x1FFFF7AC
set UID_SIZE=12
set MAX_RETRY=3
set RETRY_DELAY=4
echo Starting Production Tool...
REM Check firmware exists
if not exist %FIRMWARE% (
echo ERROR: Firmware file not found: %FIRMWARE%
pause
exit /b
)
REM Create log if missing
if not exist %LOGFILE% (
echo Timestamp,Board,UID,Status > %LOGFILE%
)
:NEW_BOARD
echo.
set /p BOARD="Enter Board ID (or type exit): "
if /I "%BOARD%"=="exit" goto END
REM ============================
REM CONNECT WITH RETRY
REM ============================
set TRY=0
:CONNECT_RETRY
echo Connecting to target...
%PROGRAMMER% -c port=SWD >nul 2>nul
if %ERRORLEVEL% neq 0 (
set /a TRY+=1
if %TRY% geq %MAX_RETRY% (
echo Connection Failed.
echo %date% %time%,%BOARD%,NO_DEVICE,CONNECT_FAIL >> %LOGFILE%
pause
goto NEW_BOARD
)
echo No device. Waiting %RETRY_DELAY% seconds and retrying (%TRY% of %MAX_RETRY%)...
timeout /t %RETRY_DELAY% >nul
goto CONNECT_RETRY
)
echo Connected.
REM ============================
REM READ UID
REM ============================
echo Reading UID...
%PROGRAMMER% -c port=SWD -r %UID_ADDR% %UID_SIZE% > uid_tmp.txt 2>nul
set UID_HEX=
for /f "tokens=2" %%A in ('findstr ":" uid_tmp.txt') do set UID_HEX=%%A
set UID_HEX=%UID_HEX: =%
if "%UID_HEX%"=="" (
echo UID Read Failed.
echo %date% %time%,%BOARD%,NO_UID,UID_FAIL >> %LOGFILE%
pause
goto NEW_BOARD
)
echo UID = %UID_HEX%
REM ============================
REM FLASH FIRMWARE
REM ============================
echo Flashing firmware...
%PROGRAMMER% -c port=SWD -w %FIRMWARE% -v
if %ERRORLEVEL% neq 0 (
echo Flash Failed.
echo %date% %time%,%BOARD%,%UID_HEX%,FLASH_FAIL >> %LOGFILE%
pause
goto NEW_BOARD
)
echo Flash OK.
REM ============================
REM SET RDP TO LEVEL 1
REM ============================
echo Setting RDP Level 1...
%PROGRAMMER% -c port=SWD -ob RDP=0xBB
if %ERRORLEVEL% neq 0 (
echo Failed to set RDP level 1.
echo %date% %time%,%BOARD%,%UID_HEX%,RDP_FAIL >> %LOGFILE%
pause
goto NEW_BOARD
)
echo RDP set to Level 1.
REM ============================
REM LOG SUCCESS
REM ============================
echo %date% %time%,%BOARD%,%UID_HEX%,SUCCESS >> %LOGFILE%
echo Board %BOARD% Programmed Successfully.
echo Remove board and connect next one.
echo -------------------------------------
timeout /t 2 >nul
goto NEW_BOARD
:END
echo Logging complete.
echo Saved in %LOGFILE%
pause
exit /b
2025-10-30 1:36 PM
That is not the same file that generated the error messages in the 2nd image you posted. In the image, the prompt is "Enter board number", but in the file you posted it is "Enter board ID". The file you posted also has echo lines that do not appear in the image. And other differences.
So... back to my suggestion above: Remove or comment out the "@echo off" line at the top of the file and then try again. Look at exactly which line fails.
2025-10-31 5:06 AM
Hello @Bob S ,
Ok , removed Echo off and Run same code.
After Entering Board ID batch file closes immidiately. See below Commnad consol Msgs on Batch RUN :
//===========================================================================
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>setlocal EnableDelayedExpansion
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>REM ============================
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>REM CONFIGURATION
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>REM ============================
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set PROGRAMMER="C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set FIRMWARE=firmware.hex
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set LOGFILE=Production_Log.csv
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set UID_ADDR=0x1FFFF7AC
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set UID_SIZE=12
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set MAX_RETRY=3
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set RETRY_DELAY=4
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>echo Starting Production Tool...
Starting Production Tool...
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>REM Check firmware exists
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>if not exist firmware.hex (
echo ERROR: Firmware file not found: firmware.hex
pause
exit /b
)
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>REM Create log if missing
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>if not exist Production_Log.csv (echo Timestamp,Board,UID,Status 1>Production_Log.csv )
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>echo.
E:\Spark_Test_Software\SPARK_FIRMWARE\AutoProgData>set /p BOARD="Enter Board ID (or type exit): "
Enter Board ID (or type exit): 0001
//======================================================================================
2025-10-31 5:36 AM
Also, would it be possible to share Batch file Demo Code which Just reads UID from 0x1FFFF7AC and Store It to Flash From address, 0x801F3F0. Total 96 bit.
2025-11-05 9:39 AM
There is nothing obvious why your batch file should just quit/exit after entering the board ID. So debug that. Divide and conquer - try removing everything after that "set" line and just add an echo. Or try removing everything before that "set" line, see if that makes a difference (it shouldn't, but you never know until you test it).
2025-11-06 11:18 PM
Hello Bob,
Since you mentioned "Automate production programming through batch file you do all the time" , we were expecting some ready to go script / pointers if you can list out.
Considering your suggeston for breaking down code path , i have removed the Wrapper and just rewrite Short batch file step by step.
See the New Batch file as Below , Firmware Download Works and RDP1 set works fine. But, UID Read/Write Fails
//==========================================================================
:: @Echo off
:: Paths
set CLI_PATH="C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin\STM32_Programmer_CLI.exe"
set FIRMWARE=firmware.hex
set UID_ADDR=0x1FFFF7AC
set UID_SIZE=4
set FLASH_ADDR=0x0801F3F0
:: Flash firmware
echo Flashing firmware...
%CLI_PATH% -c port=SWD -w %FIRMWARE% 0x08000000 -v
if %ERRORLEVEL% neq 0 (
echo Firmware flash failed!
exit /b
)
echo Firmware flashed successfully.
echo Reading UID...
%CLI_PATH% -c port=SWD -r32 %UID_ADDR% 4 > uid.txt
for /f "tokens=2" %%A in ('find "%UID_ADDR%" uid.txt') do set UID=%%A
if "%UID%"=="" (
echo ERROR: UID not read
pause
exit /b
)
echo UID = %UID%
rem Convert Hex UID (like 002E0038) into raw binary
certutil -f -decodehex uid.txt uid.bin >nul 2>&1
echo Writing UID to flash...
%CLI_PATH% -c port=SWD -w uid.bin %FLASH_ADDR%
if %errorlevel% neq 0 (
echo ERROR: Flash write failed
pause
exit /b
)
echo DONE
::echo Setting OptionByte.
REM %CLI_PATH% -c port=SWD -ob RDP=0xBB
REM %CLI_PATH% -c port=SWD -ob displ
pause
//===================================================================
Outpu on Command windows :