cancel
Showing results for 
Search instead for 
Did you mean: 

How to write option bytes in production?

EStei.3
Associate III

Hi,

i'm currently trying to find a suitable way to programm options bytes in production.

The boards are flashed using a J-Link and we would not like to switch to ST-Link.

Is it possible to simply add the option bytes to our binary?

I guess at least some script would be necessary for unlocking the the write access for the option bytes.

Any help is appreciated!

BR

6 REPLIES 6
Florian LR
ST Employee

Hi @EStei.3​,

You can program your option bytes within the code that you flash on your boards.

In order to do that, you have to create first the programming option structure :

FLASH_OBProgramInitTypeDef OBInitStruct;

You are right about the need to unlock the write access for the option bytes.

You need to unlock the flash, and then the option bytes using :

HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();

Then you can fill your OBInitStruct with the options you need (you can find the detail of the possible options and the values you can attribute in the stm32XXxx_hal_flash.h or stm32XXxx_hal_flash_ex.h file, depending on what product you are using.

You can now program the selected option bytes using :

HAL_FLASHEx_OBProgram(&OBInitStruct);

Then you lock what you unlocked earlier using HAL_FLASH_OB_Lock()  and HAL_FLASH_Lock().

Finally, you start the option bytes load operation using :

HAL_FLASH_OB_Launch(); 

This should help you program your option bytes directly into your code.

Best regards,

Florian LR

Pavel A.
Evangelist III

Ask on the Segger J-Flash forum, perhaps they have some utility for STM32 option bytes.

Thanks for the response!

Unfortunately our board won't boot on power up unless the option bytes are set correctly because there is a pull up on the boot pin.

I could probably add the code as you described and somehow start the firmware using the debugger but I was looking for a way to directly set the option bytes using the debugger.

Are there any suggestions on how to set the option bytes with any generic debugger?

The STM32 model isn't mentioned

If you have serial port connectivity, perhaps use the system loader, see AN2606 for pins, AN3155 for protocol

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks for the response!

I didn't mention the STM32 model on purpose because the problem is generic and independent to the exact controller.

Unfortunately there is no serial port connectivity on the board.

In future designs perhaps consider getting some connectivity per AN2606 so you can establish test station connectivity with a low cost, low complexity interface. Pins for a UART, NRST, BOOT0 can be facilitated by Test Points, Edge Castellations, or break-off sections of board when separated from panel.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..