PM0075 Reading/programming the embedded Flash memory
Doc ID 17863 Rev 1 17/31
The sequence is as follows:
● Check that no Flash memory operation is ongoing by checking the BSY bit in the
FLASH_SR register.
● Unlock the OPTWRE bit in the FLASH_CR register.
● Set the OPTPG bit in the FLASH_CR register
● Write the data (half-word) to the desired address
● Wait for the BSY bit to be reset.
● Read the programmed value and verify.
When the Flash memory read protection option is changed from protected to unprotected, a
Mass Erase of the main Flash memory is performed before reprogramming the read
protection option. If the user wants to change an option other than the read protection
option, then the mass erase is not performed. The erased state of the read protection option
byte protects the Flash memory.
Erase procedure
The option byte erase sequence (OPTERASE) is as follows:
● Check that no Flash memory operation is ongoing by reading the BSY bit in the
FLASH_SR register
● Unlock the OPTWRE bit in the FLASH_CR register
● Set the OPTER bit in the FLASH_CR register
● Set the STRT bit in the FLASH_CR register
● Wait for BSY to reset
● Read the erased option bytes and verify
2.4 Protections
The user area of the Flash memory can be protected against read by untrusted code. The
pages of the Flash memory can also be protected against unwanted write due to loss of
program counter contexts. The write-protection granularity is then of:
● four pages for low- and medium-density devices
● two pages for high-density and connectivity line devices.
2.4.1 Read protection
The read protection is activated by setting the RDP option byte and then, by applying a
system reset to reload the new RDP option byte.
Note: If the read protection is set while the debugger is still connected through JTAG/SWD, apply a
POR (power-on reset) instead of a system reset (without debugger connection).
Once the protection byte has been programmed:
● Main Flash memory read access is not allowed except for the user code (when booting
from main Flash memory itself with the debug mode not active).