3. Embedded flash memory interface

3.1 Introduction

The flash memory interface manages CPU AHB I-Code and D-Code accesses to the flash memory. It implements the erase and program flash memory operations and the read and write protection mechanisms.

The flash memory interface accelerates code execution with a system of instruction prefetch and cache lines.

3.2 Main features

Figure 2 shows the flash memory interface connection inside the system architecture.

Figure 2. Flash memory interface connection inside system architecture
(STM32F401xB/C and STM32F401xD/E)

Block diagram of the system architecture showing the connection of the Flash memory interface. The diagram includes a Cortex-M4 with FPU, DMA1/DMA2, AHB 32-bit instruction bus, AHB 32-bit data bus, AHB 32-bit system bus, Flash interface with FLITF registers, Flash memory, AHB periph1, SRAM and External memories, and AHB periph2. A legend indicates: purple lines for I-Code access, green lines for D-Code access, and blue lines for FLITF register access.

The diagram illustrates the system architecture for STM32F401xB/C and STM32F401xD/E. On the left, a 'Cortex-M4 with FPU' block contains a 'Cortex core'. It has three output buses: 'I-Code bus' (purple), 'D-Code bus' (green), and 'S bus' (blue). These buses connect to a central vertical bus structure. The 'I-Code bus' connects to the 'AHB 32-bit instruction bus'. The 'D-Code bus' connects to the 'AHB 32-bit data bus'. The 'S bus' connects to the 'AHB 32-bit system bus'. Below the Cortex core, 'DMA1' and 'DMA2' blocks are connected to the 'AHB 32-bit system bus'. The 'AHB 32-bit instruction bus' connects to the 'Flash interface' block. The 'AHB 32-bit data bus' also connects to the 'Flash interface' block. The 'Flash interface' block contains 'FLITF registers' and is connected to the 'Flash memory' block via a 'Flash memory bus 128 bits'. The 'AHB 32-bit system bus' connects to 'AHB periph1', 'SRAM and External memories', and 'AHB periph2'. A legend at the bottom left indicates: purple line for 'Access to instruction in Flash memory', green line for 'Access to data and literal pool in Flash memory', and blue line for 'FLITF register access'. The identifier 'MS31423V1' is in the bottom right corner.

Block diagram of the system architecture showing the connection of the Flash memory interface. The diagram includes a Cortex-M4 with FPU, DMA1/DMA2, AHB 32-bit instruction bus, AHB 32-bit data bus, AHB 32-bit system bus, Flash interface with FLITF registers, Flash memory, AHB periph1, SRAM and External memories, and AHB periph2. A legend indicates: purple lines for I-Code access, green lines for D-Code access, and blue lines for FLITF register access.

3.3 Embedded Flash memory in STM32F401xB/C and STM32F401xD/E

The Flash memory has the following main features:

The Flash memory is organized as follows:

The OTP area contains 16 additional bytes used to lock the corresponding OTP data block.

Table 5. Flash module organization (STM32F401xB/C and STM32F401xD/E)

BlockNameBlock base addressesSize
Main memorySector 00x0800 0000 - 0x0800 3FFF16 Kbytes
Sector 10x0800 4000 - 0x0800 7FFF16 Kbytes
Sector 20x0800 8000 - 0x0800 BFFF16 Kbytes
Sector 30x0800 C000 - 0x0800 FFFF16 Kbytes
Sector 40x0801 0000 - 0x0801 FFFF64 Kbytes
Sector 50x0802 0000 - 0x0803 FFFF128 Kbytes
Sector 60x0804 0000 - 0x0805 FFFF128 Kbytes
Sector 70x0806 0000 - 0x0807 FFFF128 Kbytes
System memory0x1FFF 0000 - 0x1FFF 77FF30 Kbytes
OTP area0x1FFF 7800 - 0x1FFF 7A0F528 bytes
Option bytes0x1FFF C000 - 0x1FFF C00F16 bytes

3.4 Read interface

3.4.1 Relation between CPU clock frequency and flash memory read time

To correctly read data from flash memory, the number of wait states (LATENCY) must be correctly programmed in the Flash access control register (FLASH_ACR) according to the frequency of the CPU clock (HCLK) and the supply voltage of the device.

The prefetch buffer must be disabled when the supply voltage is below 2.1 V. The correspondence between wait states and CPU clock frequency is given in Table 6 .

- when \( VOS[1:0] = 0x01 \) , the maximum value of \( f_{HCLK} = 60 \) MHz.

- when \( VOS[1:0] = 0x10 \) , the maximum value of \( f_{HCLK} = 84 \) MHz.

Table 6. Number of wait states according to CPU clock (HCLK) frequency

Wait states (WS)
(LATENCY)
HCLK (MHz)
Voltage range
2.7 V - 3.6 V
Voltage range
2.4 V - 2.7 V
Voltage range
2.1 V - 2.4 V
Voltage range
1.71 V - 2.1 V
0 WS (1 CPU cycle)\( 0 < HCLK \leq 30 \)\( 0 < HCLK \leq 24 \)\( 0 < HCLK \leq 18 \)\( 0 < HCLK \leq 16 \)
1 WS (2 CPU cycles)\( 30 < HCLK \leq 60 \)\( 24 < HCLK \leq 48 \)\( 18 < HCLK \leq 36 \)\( 16 < HCLK \leq 32 \)
2 WS (3 CPU cycles)\( 60 < HCLK \leq 84 \)\( 48 < HCLK \leq 72 \)\( 36 < HCLK \leq 54 \)\( 32 < HCLK \leq 48 \)
3 WS (4 CPU cycles)\( 72 < HCLK \leq 84 \)\( 54 < HCLK \leq 72 \)\( 48 < HCLK \leq 64 \)
4 WS (5 CPU cycles)--\( 72 < HCLK \leq 84 \)\( 64 < HCLK \leq 80 \)
5 WS (6 CPU cycles)---\( 80 < HCLK \leq 84 \)

After reset, the CPU clock frequency is 16 MHz and 0 wait state (WS) is configured in the FLASH_ACR register.

It is highly recommended to use the following software sequences to tune the number of wait states needed to access the flash memory with the CPU frequency.

Increasing the CPU frequency

  1. 1. Program the new number of wait states to the LATENCY bits in the FLASH_ACR register
  2. 2. Check that the new number of wait states is taken into account to access the flash memory by reading the FLASH_ACR register
  3. 3. Modify the CPU clock source by writing the SW bits in the RCC_CFGR register
  4. 4. If needed, modify the CPU clock prescaler by writing the HPRE bits in RCC_CFGR
  5. 5. Check that the new CPU clock source or/and the new CPU clock prescaler value is/are taken into account by reading the clock source status (SWS bits) or/and the AHB prescaler value (HPRE bits), respectively, in the RCC_CFGR register.

Decreasing the CPU frequency

  1. 1. Modify the CPU clock source by writing the SW bits in the RCC_CFGR register
  2. 2. If needed, modify the CPU clock prescaler by writing the HPRE bits in RCC_CFGR
  3. 3. Check that the new CPU clock source or/and the new CPU clock prescaler value is/are taken into account by reading the clock source status (SWS bits) or/and the AHB prescaler value (HPRE bits), respectively, in the RCC_CFGR register
  4. 4. Program the new number of wait states to the LATENCY bits in FLASH_ACR
  5. 5. Check that the new number of wait states is used to access the flash memory by reading the FLASH_ACR register

Note: A change in CPU clock configuration or wait state (WS) configuration may not be effective straight away. To make sure that the current CPU clock frequency is the one you have configured, you can check the AHB prescaler factor and clock source status values. To make sure that the number of WS you have programmed is effective, you can read the FLASH_ACR register.

3.4.2 Adaptive real-time memory accelerator (ART Accelerator™)

The proprietary Adaptive real-time (ART) memory accelerator is optimized for STM32 industry-standard Arm® Cortex®-M4 with FPU processors. It balances the inherent performance advantage of the Arm® Cortex®-M4 with FPU over flash memory technologies, which normally requires the processor to wait for the flash memory at higher operating frequencies.

To release the processor full performance, the accelerator implements an instruction prefetch queue and branch cache which increases program execution speed from the 128-bit flash memory. Based on CoreMark benchmark, the performance achieved thanks to the ART accelerator is equivalent to 0 wait state program execution from flash memory at a CPU frequency up to 84 MHz.

Instruction prefetch

Each flash memory read operation provides 128 bits from either four instructions of 32 bits or 8 instructions of 16 bits according to the program launched. So, in case of sequential code, at least four CPU cycles are needed to execute the previous read instruction line. Prefetch on the I-Code bus can be used to read the next sequential instruction line from the flash memory while the current instruction line is being requested by the CPU. Prefetch is enabled by setting the PRFTEN bit in the FLASH_ACR register. This feature is useful if at least one wait state is needed to access the flash memory.

Figure 3 shows the execution of sequential 32-bit instructions with and without prefetch when 3 WSs are needed to access the flash memory.

Figure 3. Sequential 32-bit instruction execution

Timing diagrams for sequential 32-bit instruction execution, comparing 'Without prefetch' and 'With prefetch' scenarios. The diagrams show the flow of instructions through the Cortex-M4 pipeline stages: Fetch (F), Decode (D), and Execute (E), with address requests (@) and wait states.

Without prefetch

The diagram shows a sequence of 8 instructions. The first instruction starts at cycle 1 with address @1, followed by a WAIT state at cycle 2. Fetch (F), Decode (D), and Execute (E) stages occur at cycles 3, 4, and 5 respectively. Subsequent instructions start at cycles 2, 3, 4, 5, 6, 7, and 8. The AHB protocol shows 'Read ins 1, 2, 3, 4' at cycle 1, 'Gives ins 1, 2, 3, 4' at cycle 2, 'Read ins 5, 6, 7, 8' at cycle 3, and 'Gives ins 5, 6, 7, 8' at cycle 4.

With prefetch

The diagram shows a sequence of 8 instructions. The first instruction starts at cycle 1 with address @1, followed by 'Wait data' at cycle 2. Fetch (F), Decode (D), and Execute (E) stages occur at cycles 3, 4, and 5 respectively. Subsequent instructions start at cycles 2, 3, 4, 5, 6, 7, and 8. The AHB protocol shows 'Read ins 1, 2, 3, 4' at cycle 1, 'Gives ins 1, 2, 3, 4' at cycle 2, 'Read ins 5, 6, 7, 8' at cycle 3, 'Gives ins 5, 6, 7, 8' at cycle 4, and 'Read ins 9, 10, ...' at cycle 5.

Cortex-M4 pipeline

@FDE

AHB protocol

@ : address requested
F: Fetch stage
D: Decode stage
E: Execute stage

MS31831V1

Timing diagrams for sequential 32-bit instruction execution, comparing 'Without prefetch' and 'With prefetch' scenarios. The diagrams show the flow of instructions through the Cortex-M4 pipeline stages: Fetch (F), Decode (D), and Execute (E), with address requests (@) and wait states.

When the code is not sequential (branch), the instruction may not be present in the currently used instruction line or in the prefetched instruction line. In this case (miss), the penalty in terms of number of cycles is at least equal to the number of wait states.

Instruction cache memory

To limit the time lost due to jumps, it is possible to retain 64 lines of 128 bits in an instruction cache memory. This feature can be enabled by setting the instruction cache enable (ICEN) bit in the FLASH_ACR register. Each time a miss occurs (requested data not present in the currently used instruction line, in the prefetched instruction line or in the instruction cache memory), the line read is copied into the instruction cache memory. If some data contained in the instruction cache memory are requested by the CPU, they are provided without inserting any delay. Once all the instruction cache memory lines have been filled, the LRU (least recently used) policy is used to determine the line to replace in the instruction memory cache. This feature is particularly useful in case of code containing loops.

Data management

Literal pools are fetched from flash memory through the D-Code bus during the execution stage of the CPU pipeline. The CPU pipeline is consequently stalled until the requested literal pool is provided. To limit the time lost due to literal pools, accesses through the AHB databus D-Code have priority over accesses through the AHB instruction bus I-Code.

If some literal pools are frequently used, the data cache memory can be enabled by setting the data cache enable (DCEN) bit in the FLASH_ACR register. This feature works like the instruction cache memory, but the retained data size is limited to 8 rows of 128 bits.

Note: Data in user configuration sector are not cacheable.

3.5 Erase and program operations

For any flash memory program operation (erase or program), the CPU clock frequency (HCLK) must be at least 1 MHz. The contents of the flash memory are not guaranteed if a device reset occurs during a flash memory operation.

Any attempt to read the flash memory on STM32F4xx while it is being written or erased, causes the bus to stall. Read operations are processed correctly once the program operation has completed. This means that code or data fetches cannot be performed while a write/erase operation is ongoing.

3.5.1 Unlocking the Flash control register

After reset, write is not allowed in the Flash control register (FLASH_CR) to protect the flash memory against possible unwanted operations due, for example, to electric disturbances. The following sequence is used to unlock this register:

  1. 1. Write KEY1 = 0x45670123 in the Flash key register (FLASH_KEYR)
  2. 2. Write KEY2 = 0xCDEF89AB in the Flash key register (FLASH_KEYR)

Any wrong sequence returns a bus error and lock up the FLASH_CR register until the next reset.

The FLASH_CR register can be locked again by software by setting the LOCK bit in the FLASH_CR register.

Note: The FLASH_CR register is not accessible in write mode when the BSY bit in the FLASH_SR register is set. Any attempt to write to it with the BSY bit set causes the AHB bus to stall until the BSY bit is cleared.

3.5.2 Program/erase parallelism

The parallelism size is configured through the PSIZE field in the FLASH_CR register. It represents the number of bytes to be programmed each time a write operation occurs to the flash memory. PSIZE is limited by the supply voltage and by whether the external V PP supply is used or not. It must therefore be correctly configured in the FLASH_CR register before any programming/erasing operation.

A flash memory erase operation can only be performed by sector, or for the whole flash memory (mass erase). The erase time depends on PSIZE programmed value. For more details on the erase time, refer to the electrical characteristics section of the device datasheet.

Table 7 provides the correct maximum PSIZE values.

Table 7. Maximum program/erase parallelism

Voltage range 2.7 - 3.6 V with External V PPVoltage range 2.7 - 3.6 VVoltage range 2.4 - 2.7 VVoltage range 2.1 - 2.4 VVoltage range 1.7 V - 2.1 V
Max. parallelism sizex64x32x16x8
PSIZE(1:0)11100100

Note: Any program or erase operation started with inconsistent program parallelism/voltage range settings may lead to unpredicted results. Even if a subsequent read operation indicates that the logical value was effectively written to the memory, this value may not be retained.

To use V PP , an external high-voltage supply (between 8 and 9 V) must be applied to the V PP pad. The external supply must be able to sustain this voltage range even if the DC consumption exceeds 10 mA. It is advised to limit the use of V PP to initial programming on the factory line. The V PP supply must not be applied for more than an hour, otherwise the flash memory might be damaged.

3.5.3 Erase

The flash memory erase operation can be performed at sector level or on the whole flash memory (Mass Erase). Mass Erase does not affect the OTP sector or the configuration sector.

Sector Erase

To erase a sector, follow the procedure below:

  1. 1. Check that no flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
  2. 2. Set the SER bit and select the sector out of the 5 sectors (for STM32F401xB/C) and out of 7 (for STM32F401xD/E) in the main memory block you wish to erase (SNB) in the FLASH_CR register
  3. 3. Set the STRT bit in the FLASH_CR register
  4. 4. Wait for the BSY bit to be cleared

Mass Erase

To perform Mass Erase, the following sequence is recommended:

  1. 1. Check that no flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
  2. 2. Set the MER bit in the FLASH_CR register
  3. 3. Set the STRT bit in the FLASH_CR register
  4. 4. Wait for the BSY bit to be cleared

Note: If MERx and SER bits are both set in the FLASH_CR register, mass erase is performed. If both MERx and SER bits are reset and the STRT bit is set, an unpredictable behavior may occur without generating any error flag. This condition should be forbidden.

3.5.4 Programming

Standard programming

The flash memory programming sequence is as follows:

  1. 1. Check that no main flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register.
  2. 2. Set the PG bit in the FLASH_CR register
  3. 3. Perform the data write operation(s) to the desired memory address (inside main memory block or OTP area):
    • – Byte access in case of x8 parallelism
    • – Half-word access in case of x16 parallelism
    • – Word access in case of x32 parallelism
    • – Double word access in case of x64 parallelism
  4. 4. Wait for the BSY bit to be cleared.

Note: Successive write operations are possible without the need of an erase operation when changing bits from '1' to '0'. Writing '1' requires a flash memory erase operation. If an erase and a program operation are requested simultaneously, the erase operation is performed first.

Programming errors

It is not allowed to program data to the flash memory that would cross the 128-bit row boundary. In such a case, the write operation is not performed and a program alignment error flag (PGAERR) is set in the FLASH_SR register.

The write access type (byte, half-word, word or double word) must correspond to the type of parallelism chosen (x8, x16, x32 or x64). If not, the write operation is not performed and a program parallelism error flag (PGPERR) is set in the FLASH_SR register.

If the standard programming sequence is not respected (for example, if there is an attempt to write to a flash memory address when the PG bit is not set), the operation is aborted and a program sequence error flag (PGSERR) is set in the FLASH_SR register.

Programming and caches

If a flash memory write access concerns some data in the data cache, the Flash write access modifies the data in the flash memory and the data in the cache.

If an erase operation in flash memory also concerns data in the data or instruction cache, you have to make sure that these data are rewritten before they are accessed during code execution. If this cannot be done safely, it is recommended to flush the caches by setting the DCRST and ICRST bits in the FLASH_CR register.

Note: The I/D cache should be flushed only when it is disabled (I/DCEN = 0).

3.5.5 Interrupts

Setting the end of operation interrupt enable bit (EOPIE) in the FLASH_CR register enables interrupt generation when an erase or program operation ends, that is when the busy bit (BSY) in the FLASH_SR register is cleared (operation completed, correctly or not). In this case, the end of operation (EOP) bit in the FLASH_SR register is set.

If an error occurs during a program, an erase, or a read operation request, one of the following error flags is set in the FLASH_SR register:

In this case, if the error interrupt enable bit (ERRIE) is set in the FLASH_CR register, an interrupt is generated and the operation error bit (OPERR) is set in the FLASH_SR register.

Note: If several successive errors are detected (for example, in case of DMA transfer to the flash memory), the error flags cannot be cleared until the end of the successive write requests.

Table 8. Flash interrupt request

Interrupt eventEvent flagEnable control bit
End of operationEOPEOPIE
Write protection errorWRPERRERRIE
Programming errorPGAERR, PGPERR, PGSERRERRIE

3.6 Option bytes

3.6.1 Description of user option bytes

The option bytes are configured by the end user depending on the application requirements. Table 9 shows the organization of these bytes inside the user configuration sector.

Table 9. Option byte organization

Address[63:16][15:0]
0x1FFF C000ReservedROP & user option bytes ( RDP & USER )
0x1FFF C008ReservedWrite protection nWRP bits for 5 (STM32F401xB/C) and for sectors 0 to 7 (STM32F401xD/E)

Table 10. Description of the option bytes

Option bytes (word, address 0x1FFF C000)
RDP: Read protection option byte.
The read protection is used to protect the software code stored in Flash memory.
Bit 15:80xAA: Level 0, no protection
0xCC: Level 2, chip protection (debug and boot from RAM features disabled)
Others: Level 1, read protection of memories (debug features limited)
USER: User option byte
This byte is used to configure the following features:
Select the watchdog event: Hardware or software
Reset event when entering the Stop mode
Reset event when entering the Standby mode
Bit 7nRST_STDBY
0: Reset generated when entering the Standby mode
1: No reset generated
Bit 6nRST_STOP
0: Reset generated when entering the Stop mode
1: No reset generated
Bit 5WDG_SW
0: Hardware watchdog
1: Software watchdog
Bit 40x0: Not used. For STM32F401xB/C do not change the default value of this bit which is "0" out of fab.
Bits 3:2BOR_LEV: BOR reset Level
These bits contain the supply level threshold that activates/releases the reset. They can be written to program a new BOR level value into Flash memory.
00: BOR Level 3 (VBOR3), brownout threshold level 3
01: BOR Level 2 (VBOR2), brownout threshold level 2
10: BOR Level 1 (VBOR1), brownout threshold level 1
11: BOR off, POR/PDR reset threshold level is applied
For full details on BOR characteristics, refer to the Electrical characteristics section of the product datasheet.
Bits 1:00x1: Not used
Option bytes (word, address 0x1FFF C008)
Bit 15SPRMOD: Selection of Protection Mode of nWPRI bits
0: nWPRI bits used for sector i write protection (Default)
1: nWPRI bits used for sector i PCROP protection (Sector)
Bits 14:6Reserved
Table 10. Description of the option bytes (continued)
nWRP: Flash memory write protection option bytes
Sectors 0 to 5 (STM32F401xB/C) and Sectors 0 to 7 (STM32F401xD/E) can be write protected.
Bits 5:0nWRPi
If SPRMOD is reset (default value) :
0: Write protection active on sector i.
1: Write protection not active on sector i.
If SPRMOD is set (active):
0: PCROP protection not active on sector i.
1: PCROP protection active on sector i.

3.6.2 Programming user option bytes

To run any operation on this sector, the option lock bit (OPTLOCK) in the Flash option control register (FLASH_OPTCR) must be cleared. To be allowed to clear this bit, you have to perform the following sequence:

  1. 1. Write OPTKEY1 = 0x0819 2A3B in the Flash option key register (FLASH_OPTKEYR)
  2. 2. Write OPTKEY2 = 0x4C5D 6E7F in the Flash option key register (FLASH_OPTKEYR)

The user option bytes can be protected against unwanted erase/program operations by setting the OPTLOCK bit by software.

Modifying user option bytes

To modify the user option value, follow the sequence below:

  1. 1. Check that no flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
  2. 2. Write the desired option value in the FLASH_OPTCR register.
  3. 3. Set the option start bit (OPTSTRT) in the FLASH_OPTCR register
  4. 4. Wait for the BSY bit to be cleared.

Note: The value of an option is automatically modified by first erasing the user configuration sector and then programming all the option bytes with the values contained in the FLASH_OPTCR register.

3.6.3 Read protection (RDP)

The user area in the flash memory can be protected against read operations by an entrusted code. Three read protection levels are defined:

When the read protection level is set to Level 0 by writing 0xAA into the read protection option byte (RDP), all read/write operations (if no write protection is set) from/to the

flash memory are possible in all boot configurations (Flash user boot, debug or boot from RAM).

It is the default read protection level after option byte erase. The read protection Level 1 is activated by writing any value (except for 0xAA and 0xCC used to set Level 0 and Level 2, respectively) into the RDP option byte. When the read protection Level 1 is set:

When Level 1 is active, programming the protection option byte (RDP) to Level 0 causes the flash memory to be mass-erased. As a result the user code area is cleared before the read protection is removed. The mass erase only erases the user code area. The other option bytes including write protections remain unchanged from before the mass-erase operation. The OTP area is not affected by mass erase and remains unchanged. Mass erase is performed only when Level 1 is active and Level 0 requested. When the protection level is increased (0->1, 1->2, 0->2) there is no mass erase.

The read protection Level 2 is activated by writing 0xCC to the RDP option byte. When the read protection Level 2 is set:

Memory read protection Level 2 is an irreversible operation. When Level 2 is activated, the level of protection cannot be decreased to Level 0 or Level 1.

Note: If the read protection is set while the debugger is still connected (or has been connected since the last power-on) through JTAG/SWD, apply a POR (power-on reset) instead of a system reset.

If the read protection is programmed by software (executing from SRAM), perform a POR to reload the option byte and clear the detected intrusion. This can be done with a transition Standby (or Shutdown) mode followed by a wake-up.

The JTAG port is permanently disabled when Level 2 is active (acting as a JTAG fuse). As a consequence, boundary scan cannot be performed. STMicroelectronics is not able to perform analysis on defective parts on which the Level 2 protection has been set.

Table 11. Access versus read protection level

Memory areaProtection LevelDebug features, Boot from RAM or from System memory bootloaderBooting from flash memory
ReadWriteEraseReadWriteErase
Main Flash MemoryLevel 1NONO (1)YES
Level 2NOYES
Option BytesLevel 1YESYES
Level 2NONO
OTPLevel 1NONAYESNA
Level 2NONAYESNA

1. The main flash memory is only erased when the RDP changes from level 1 to 0. The OTP area remains unchanged.

Figure 4 shows how to go from one RDP level to another.

Figure 4. RDP levels

Diagram illustrating RDP levels and transitions between them. Level 1 is the default (RDP != AAh, RDP != CCh). Level 2 has RDP = CCh. Level 0 has RDP = AAh. Transitions are shown with arrows indicating the required write options.

Figure 4. RDP levels

The diagram shows three RDP levels and the transitions between them:

Legend:

MS47541V1

Diagram illustrating RDP levels and transitions between them. Level 1 is the default (RDP != AAh, RDP != CCh). Level 2 has RDP = CCh. Level 0 has RDP = AAh. Transitions are shown with arrows indicating the required write options.

3.6.4 Write protections

Up to 5 (STM32F401xB/C) or up to 7 (STM32F401xD/E) user sectors in flash memory can be protected against unwanted write operations due to loss of program counter contexts. When the non-write protection nWRPi bit (0 ≤ i ≤ 7) in the FLASH_OPTCR registers is low, the corresponding sector cannot be erased or programmed. Consequently, a mass erase cannot be performed if one of the sectors is write-protected.

If an erase/program operation to a write-protected part of the flash memory is attempted (sector protected by write protection bit, OTP part locked or part of the flash memory that can never be written like the ICP), the write protection error flag (WRPERR) is set in the FLASH_SR register.

Note: When the memory read protection level is selected (RDP level = 1), it is not possible to program or erase flash memory sector i if the CPU debug features are connected (JTAG or single wire) or boot code is being executed from RAM, even if nWRPi = 1.

Write protection error flag

If an erase/program operation to a write protected area of the flash memory is performed, the Write Protection Error flag (WRPERR) is set in the FLASH_SR register.

If an erase operation is requested, the WRPERR bit is set when:

If a program operation is requested, the WRPERR bit is set when:

3.6.5 Proprietary code readout protection (PCROP)

Flash memory user sectors (0 to 5 for STM32F401xB/C) or up to 7 for STM32F401xD/E) can be protected against D-bus read accesses by using the proprietary readout protection (PCROP).

The PCROP protection is selected as follows, through the SPRMOD option bit in the FLASH_OPTCR register:

When a sector is readout protected (PCROP mode activated), it can only be accessed for code fetch through ICODE Bus on Flash interface:

Figure 5. PCROP levels

Diagram illustrating PCROP levels (Level 0, Level 1, Level 2) and their associated RDP values and write options. Level 1 is the default (RDP != 0xAA, RDP != 0xCC). Level 2 has RDP = 0xCC. Level 0 has RDP = 0xAA. Arrows show transitions between levels with specific write option constraints.

The diagram illustrates the relationship between PCROP levels and their associated Read Protection (RDP) values and write options:

Legend:

* Valid nWRPi means that none of the nWRP bits set can be reset (transition from 1 to 0)

MS30467V1

Diagram illustrating PCROP levels (Level 0, Level 1, Level 2) and their associated RDP values and write options. Level 1 is the default (RDP != 0xAA, RDP != 0xCC). Level 2 has RDP = 0xCC. Level 0 has RDP = 0xAA. Arrows show transitions between levels with specific write option constraints.

The deactivation of the SPRMOD and/or the unprotection of PCROPed user sectors can only occur when, at the same time, the RDP level changes from 1 to 0. If this condition is not respected, the user option byte modification is cancelled and the write error WRPERR flag is set. The modification of the users option bytes (BOR_LEV, RST_STDBY, ..) is allowed since none of the active nWRPi bits is reset and SPRMOD is kept active.

Note: The active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD = 1).
If SPRMOD = 1 and nWRPi = 1, then user sector i of bank 1, respectively bank 2 is read/write protected (PCROP).

3.7 One-time programmable bytes

Table 12 shows the organization of the one-time programmable (OTP) part of the OTP area.

Table 12. OTP area organization

Block[128:96][95:64][63:32][31:0]Address byte 0
0OTP0OTP0OTP0OTP00x1FFF 7800
OTP0OTP0OTP0OTP00x1FFF 7810
1OTP1OTP1OTP1OTP10x1FFF 7820
OTP1OTP1OTP1OTP10x1FFF 7830
...
...
...
15OTP15OTP15OTP15OTP150x1FFF 79E0
OTP15OTP15OTP15OTP150x1FFF 79F0
Lock blockLOCKB15 ...
LOCKB12
LOCKB11 ...
LOCKB8
LOCKB7 ...
LOCKB4
LOCKB3 ...
LOCKB0
0x1FFF 7A00

The OTP area is divided into 16 OTP data blocks of 32 bytes and one lock OTP block of 16 bytes. The OTP data and lock blocks cannot be erased. The lock block contains 16 bytes LOCKBi ( \( 0 \le i \le 15 \) ) to lock the corresponding OTP data block (blocks 0 to 15). Each OTP data block can be programmed until the value 0x00 is programmed in the corresponding OTP lock byte. The lock bytes must only contain 0x00 and 0xFF values, otherwise the OTP bytes might not be taken into account correctly.

3.8 Flash interface registers

3.8.1 Flash access control register (FLASH_ACR)

The Flash access control register is used to enable/disable the acceleration features and control the flash memory access time according to CPU frequency.

Address offset: 0x00

Reset value: 0x0000 0000

Access: no wait state, word, half-word and byte access

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedDCRSTICRSTDCENICENPRFTENReservedLATENCY
rwwrwrwrwrwrwrwrw

Bits 31:13 Reserved, must be kept cleared.

Bit 12 DCRST : Data cache reset

0: Data cache is not reset

1: Data cache is reset

This bit can be written only when the D cache is disabled.

Bit 11 ICRST : Instruction cache reset

0: Instruction cache is not reset

1: Instruction cache is reset

This bit can be written only when the I cache is disabled.

Bit 10 DCEN : Data cache enable

0: Data cache is disabled

1: Data cache is enabled

Bit 9 ICEN : Instruction cache enable

0: Instruction cache is disabled

1: Instruction cache is enabled

Bit 8 PRFTEN : Prefetch enable

0: Prefetch is disabled

1: Prefetch is enabled

Bits 7:4 Reserved, must be kept cleared.

Bits 3:0 LATENCY : Latency

These bits represent the ratio of the CPU clock period to the Flash memory access time.

0000: Zero wait state

0001: One wait state

0010: Two wait states

-

-

-

1110: Fourteen wait states

1111: Fifteen wait states

3.8.2 Flash key register (FLASH_KEYR)

The Flash key register is used to allow access to the Flash control register and so, to allow program and erase operations.

Address offset: 0x04

Reset value: 0x0000 0000

Access: no wait state, word access

31302928272625242322212019181716
KEY[31:16]
wwwwwwwwwwwwwwww
1514131211109876543210
KEY[15:0]
wwwwwwwwwwwwwwww

Bits 31:0 FKEYR[31:0] : FPEC key

The following values must be programmed consecutively to unlock the FLASH_CR register and allow programming/erasing it:

  1. KEY1 = 0x45670123
  2. KEY2 = 0xCDEF89AB

3.8.3 Flash option key register (FLASH_OPTKEYR)

The Flash option key register is used to allow program and erase operations in the user configuration sector.

Address offset: 0x08

Reset value: 0x0000 0000

Access: no wait state, word access

31302928272625242322212019181716
OPTKEYR[31:16]
wwwwwwwwwwwwwwww
1514131211109876543210
OPTKEYR[15:0]
wwwwwwwwwwwwwwww

Bits 31:0 OPTKEYR[31:0] : Option byte key

The following values must be programmed consecutively to unlock the FLASH_OPTCR register and allow programming it:

  1. OPTKEY1 = 0x08192A3B
  2. OPTKEY2 = 0x4C5D6E7F

3.8.4 Flash status register (FLASH_SR)

The Flash status register gives information on ongoing program and erase operations.

Address offset: 0x0C

Reset value: 0x0000 0000

Access: no wait state, word, half-word and byte access

31302928272625242322212019181716
ReservedBSY
r
1514131211109876543210
ReservedRDERR
rw
PGSERR
rc_w1
PGPERR
rc_w1
PGAERR
rc_w1
WRPERR
rc_w1
ReservedOPERR
rc_w1
EOP
rc_w1

Bits 31:17 Reserved, must be kept cleared.

Bit 16 BSY : Busy

This bit indicates that a flash memory operation is in progress. It is set at the beginning of a flash memory operation and cleared when the operation finishes or an error occurs.

0: no flash memory operation ongoing

1: Flash memory operation ongoing

Bits 15:9 Reserved, must be kept cleared.

Bit 8 RDERR : Read Protection Error (pcrop)

Set by hardware when an address to be read through the Dbus belongs to a read protected part of the flash.

Reset by writing 1.

Bit 7 PGSERR : Programming sequence error

Set by hardware when a write access to the flash memory is performed by the code while the control register has not been correctly configured.

Cleared by writing 1.

Bit 6 PGPERR : Programming parallelism error

Set by hardware when the size of the access (byte, half-word, word, double word) during the program sequence does not correspond to the parallelism configuration PSIZE (x8, x16, x32, x64).

Cleared by writing 1.

Bit 5 PGAERR : Programming alignment error

Set by hardware when the data to program cannot be contained in the same 128-bit flash memory row.

Cleared by writing 1.

Bit 4 WRPERR : Write protection error

Set by hardware when an address to be erased/programmed belongs to a write-protected part of the flash memory.

Cleared by writing 1.

Bits 3:2 Reserved, must be kept cleared.

Bit 1 OPERR : Operation error

Set by hardware when a flash operation (programming / erase /read) request is detected and can not be run because of parallelism, alignment, or write protection error. This bit is set only if error interrupts are enabled (ERRIE = 1).

Bit 0 EOP : End of operation

Set by hardware when one or more flash memory operations (program/erase) has/have completed successfully. It is set only if the end of operation interrupts are enabled (EOPIE = 1). Cleared by writing a 1.

3.8.5 Flash control register (FLASH_CR)

The Flash control register is used to configure and start flash memory operations.

Address offset: 0x10

Reset value: 0x8000 0000

Access: no wait state when no flash memory operation is ongoing, word, half-word and byte access.

31302928272625242322212019181716
LOCKReservedERRIEEOPIEReservedSTRT
rsrwrwrs
1514131211109876543210
ReservedPSIZE[1:0]Res.SNB[3:0]MERSERPG
rwrwrwrwrwrwrw

Bit 31 LOCK : Lock

Write to 1 only. When it is set, this bit indicates that the FLASH_CR register is locked. It is cleared by hardware after detecting the unlock sequence.

In the event of an unsuccessful unlock operation, this bit remains set until the next reset.

Bits 30:26 Reserved, must be kept cleared.

Bit 25 ERRIE : Error interrupt enable

This bit enables the interrupt generation when the OPERR bit in the FLASH_SR register is set to 1.

0: Error interrupt generation disabled

1: Error interrupt generation enabled

Bit 24 EOPIE : End of operation interrupt enable

This bit enables the interrupt generation when the EOP bit in the FLASH_SR register goes to 1.

0: Interrupt generation disabled

1: Interrupt generation enabled

Bits 23:17 Reserved, must be kept cleared.

Bit 16 STRT : Start

This bit triggers an erase operation when set. It is set only by software and cleared when the BSY bit is cleared.

Bits 15:10 Reserved, must be kept cleared.

Bits 9:8 PSIZE[1:0] : Program size

These bits select the program parallelism.

00 program x8

01 program x16

10 program x32

11 program x64

Bit 7 Reserved, must be kept cleared.

Bits 6:3 SNB[3:0] : Sector number

These bits select the sector to erase.

0000 sector 0

0001 sector 1

...

0101 sector 5

0110 sector 6 (STM32F401xD/E devices only)

0111 sector 7 (STM32F401xE devices only)

1000 not allowed

...

1011 not allowed

1100 user specific sector

1101 user configuration sector

1110 not allowed

1111 not allowed

Bit 2 MER : Mass Erase

Erase activated for all user sectors.

Bit 1 SER : Sector Erase

Sector Erase activated.

Bit 0 PG : Programming

Flash programming activated.

3.8.6 Flash option control register (FLASH_OPTCR)

The FLASH_OPTCR register is used to modify the user option bytes.

Address offset: 0x14

Reset value: 0x0FFF AAED. The option bits are loaded with values from flash memory at reset release.

Access: no wait state when no flash memory operation is ongoing, word, half-word and byte access.

31302928272625242322212019181716
SPR
MOD
ReservednWRP[7:6] (1)
rw
rwrw
1514131211109876543210
RDP[7:0]nRST
STDBY
nRST
STOP
WDG
SW
Reserve
d
BOR_LEVOPTST
RT
OPTLO
CK
rwrwrwrwrwrsrs

1. Available in STM32F401xD/E devices only

Bit 31 SPRMOD : Selection of Protection Mode of nWPRI bits

Bits 30:24 Reserved, must be kept cleared.

Bits 23:22 nWRP[7:6] : Not write protect

Bits 21:16 nWRP[5:0] : Not write protect

These bits contain the value of the write-protection option bytes of sectors after reset. They can be written to program a new write protect value into Flash memory.

These bits contain the value of the write-protection and read-protection (PCROP) option bytes for sectors 0 to 5 after reset. They can be written to program a new write-protect or PCROP value into Flash memory.

If SPRMOD is reset:

If SPRMOD is set:

Bits 15:8 RDP[7:0] : Read protect

These bits contain the value of the read-protection option level after reset. They can be written to program a new read protection value into flash memory.

Bits 7:5 USER[2:0] : User option bytes

These bits contain the value of the user option byte after reset. They can be written to program a new user option byte value into flash memory.

Note: When changing the WDG mode from hardware to software or from software to hardware, a system reset is required to make the change effective.

Bit 4 Reserved, must be kept cleared. Always read as "0".

Bits 3:2 BOR_LEV[1:0]: BOR reset Level

These bits contain the supply level threshold that activates/releases the reset. They can be written to program a new BOR level. By default, BOR is off. When the supply voltage ( \( V_{DD} \) ) drops below the selected BOR level, a device reset is generated.

00: BOR Level 3 (VBOR3), brownout threshold level 3

01: BOR Level 2 (VBOR2), brownout threshold level 2

10: BOR Level 1 (VBOR1), brownout threshold level 1

11: BOR off, POR/PDR reset threshold level is applied

Note: For full details about BOR characteristics, refer to the “Electrical characteristics” section in the device datasheet.

Bit 1 OPTSTR: Option start

This bit triggers a user option operation when set. It is set only by software and cleared when the BSY bit is cleared.

Bit 0 OPTLOCK: Option lock

Write to 1 only. When this bit is set, it indicates that the FLASH_OPTCR register is locked. This bit is cleared by hardware after detecting the unlock sequence.

In the event of an unsuccessful unlock operation, this bit remains set until the next reset.

3.8.7 Flash interface register map

Table 13. Flash register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00FLASH_ACRReservedDCRSTICRSTDCENICENPRFTENReservedLATENCY
Reset value000000 0 0 0
0x04FLASH_KEYRKEY[31:16]KEY[15:0]
Reset value0000000000000000000000000000000
0x08FLASH_OPTKEYROPTKEYR[31:16]OPTKEYR[15:0]
Reset value0000000000000000000000000000000
0x0CFLASH_SRReservedRDERRPGSERRPGPERRPGAERRWRPERRReservedOPERREOP
Reset value0000000
0x10FLASH_CRLOCKReservedERRIEEOPIEReservedSTRTReservedPSIZE[1:0]
Reset value10000 0
0x14FLASH_OPTCRSPRMODReservednRST_STDBnRST_STOPWDG_SWReservedBOR_LEVOPTSTRTOPTLOCK
Reset value01111 101