2026-04-01 2:04 AM
Hello,
I have a custom PCB that uses an STM32L4R9 controller and runs two applications. One is a bootloader and other is the actual app. The files are called boot.hex and app.hex.
The boards are made via a vendor and programmed with these files during ICT. We use ST Link programmer and SWD connection to program the devices.
I use the ST Link V2 Utility CLI to do this.
A batch file is made that runs all the commands that program the option bytes, erase chip, and programs the above files.
The problem I am facing is, the batch file fails to execute the first time. It fails after the option bytes step. The second time however, it always executes and programs the applications successfully.
I did wonder whether this is because the Read Out protection for STM32L4R is set to 1 from the factory? But again, I tried to revert the option bytes to factory after which I cannot read the flash memory again before setting it back to Level 0.
But if I connect a new and unprogrammed board to the ST Link V2 Utility it shows me all the memory content. I also saw that the reset value of the FLASH_OPTR register for the readout protection level is set to 0xAA.
However, datasheet also says that default value, I am assuming factory programmed is Level 1.
Here is my batch file. Version 1
Could anyone help me with what I am missing in these steps?
@Echo off
setlocal EnableDelayedExpansion
echo This file programs the bootloader and application to blink debug LEDs on PCBA
REM Cater for ST-Link installation on 64 and 32 bit machines
set EXE="%ProgramFiles(x86)%\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe"
if not defined ProgramFiles(x86) set EXE="%PROGRAMFILES%\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe"
echo Programming micro controller...
echo Erasing flash
%EXE% -c -ME
if %errorlevel% neq 0 goto error
echo Programming Option bytes
%EXE% -c -ob BOR_LEV=3 DBANK=1 -rOB
if %errorlevel% neq 0 goto error
echo Programming application...
%EXE% -c -P "app.hex" -V
if %errorlevel% neq 0 goto error
echo Programming bootloader...
%EXE% -c -P "boot.hex" -V
if %errorlevel% neq 0 goto error
goto ok
:error
echo An error occurred!
goto end
:ok
echo Completed successfully, recommend power cycling the UUT.
:end
REM Show status long enough to be read, then close
timeout /t 10
Another version I had tried was this: VERSION 2
@Echo off
REM Cater for ST-Link installation on 64 and 32 bit machines
set EXE="%ProgramFiles(x86)%\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe"
if not defined ProgramFiles(x86) set EXE="%PROGRAMFILES%\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe"
echo Erasing flash and setting option bytes...
%EXE% -ME -OB BOR_LEV=3 DBANK=1 -rOB -Q RDP=0xAA
if %errorlevel% neq 0 goto error
echo Programming bootloader application...
%EXE% -P "app.hex" -V -Q RDP=0xAA
if %errorlevel% neq 0 goto error
echo Programming base...
%EXE% -P "boot.hex" -V -Q RDP=0xAA
if %errorlevel% neq 0 goto error
goto ok
:error
echo An error occurred!
goto end
:ok
echo Completed successfully, recommend power cycling the UUT.
:end
REM Show status long enough to be read, then close
timeout /t 10
This also does not seem to work.
A follow up question, after twice running the batch file and programming the boards, no matter what I do on the option bytes, I can always program the boards thereon. Is there a way for me to reset the micro to it's factory state so I can test updated batch files as I am now running about of boards to test.
Thank you in advance for your help.
Makarand
2026-04-09 7:06 AM
Hello @Makarand;
it mentioned, in RM0432 User and read protection option bytes paragraph : Flash memory address: 0x1FFF 7800 (corresponding to OPTR option register) ST production value: 0xFFEF F8AA. Therefore the RDP programmed by the fab is 0xAA meaning RDP level 0.
For FLASH_OPTR reset value, I will check internally.
CDM0061548 (This is an internal tracking number and is not accessible or usable by customers).
Thank you for your contribution in STCommunity.
Kaouthar
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2026-04-09 7:15 AM
@Makarand wrote:I use the ST Link V2 Utility CLI to do this.
That's long been obsolete!
Have you looked at the STM32CubeProgrammer CLI ?
See also: Your Voice Matters : Help Us Improve STM32CubeProgrammer Usability & Error Messages
2026-04-09 11:14 PM
Thank you very much both, for your replies.
Yes, I have understood that the old utility is now obsolete and will start using the STM32CubeProgrammer CLI. Will update on the results.
Unfortunately I do not have any unprogrammed boards left to try this out with. Is it possible to set the controller back into the state how it came from factory? I have tried setting option bits to look like a new controller but with that my old batch file also works so I am not sure whether the I have set the controller in the factory state.
Thank you in advance!
Makarand