Can someone show me a simple program to read and write the option bytes on the STM32042K6 using the STM32 HAL libraries? I only want to store an 8-bit value. I'm not interested in reading or modifying the read or write protection, etc.
I've been able to write the option bytes using the ST-LINK utility software, and then successfully been able to read one of them back using the following C code (Atollic TrueSTUDIO 7.1.2):
mode = HAL_FLASHEx_OBGetUserData(OB_DATA_ADDRESS_DATA0); // from option byte 0
...and this works just fine. Now I want to be able to update the option byte in code as the application is running. All I need to store is 8 bits. I don't think I need to allocate 1K of FLASH memory as EEPROM emulation at this point. I'm porting a trivial program from an 8-bit AVR that had dedicated EEPROM on-chip.
I've looked and looked at the HAL documentation concerning the HAL_FLASHEx_OBProgram() function, and I understand that the FLASH and the option bytes need to be unlocked before erasing and writing the data, but I have no idea how to populate the FLASH_OBProgramInitTypeDef structure needed in the ...Program() function. No examples are given in the HAL documentation (UM1785).
Any suggestions or examples would be greatly appreciated.