9. Embedded Flash memory

The Flash controller implements the erase and programs Flash memory operation. The Flash controller also implements the read and write protection.

9.1 Flash main features

Flash memory features:

Flash controller features:

9.2 Description

The flash memory is organized as follows:

Erasing the whole flash results in every bit cell of the flash memory.

In parallel, the flash controller manages 1 Kbyte of OTP (one-time programmable) memory for user data. The OTP data cannot be erased. The OTP data area can no longer be written only as soon as the last word (address 0x1000_1BFC) is different from 0xFFFF_FFFF and a system reset occurred.

The flash memory is mapped on the AHB-Lite bus with the range described below.

Table 19. Flash memory section address

SectionFlash AHB start addressFlash AHB end address
Main flash memory0x1004_00000x1007_FFFF (1)
User OTP0x1000_18000x1000_1BFF

1. Depends on Flash size. See Table 22. Flash size information.

9.3 Flash controller register map

Refer to Table 3. STM32WB07xC and STM32WB06xC memory map and peripheral register boundary addresses for the flash controller base address location in the STM32WB07xC and STM32WB06xC.

Table 20. Flash APB registers

Address offsetNameWidthRW (1)ResetDescription
0x00COMMAND8RW0x0000_0000Commands for the module. See Section 9.4.1: Command register (COMMAND) .
0x04CONFIG2RW0x0000_0018Configure the wrapper. See Section 9.4.2: Configuration register (CONFIG) .
0x08IRQSTAT5RC0x0000_0000Flash status interrupts (masked). See Section 9.4.3: Interrupt status register (IRQSTAT) .
0x0CIRQMASK5RW0x0000_003FMask for interrupts. See Section 9.4.4: Interrupt mask register (IRQMASK) .
0x10IRQRAW5RC0x0000_0001Status interrupts (unmasked). See Section 9.4.5: Raw status register (IRQRAW) .
0x14FLASH_SIZE16RO0x0000_----Indicates the last usable address of the main Flash and the RAM size. See Section 9.4.6: SIZE register .
0x18ADDRESS14RW0x0000_0000Address for programming Flash, auto-increments. See Section 9.4.7: Address register (ADDRESS) .
0x24LFSRVAL32RO0xFFFF_FFFFLFSR register needed for check after MASS READ command. See Section 9.4.8: Linear feedback shift register (LFSRVAL) .
0x34PAGEPROT032RW0x0000_0000Write/page erase protection management register. See Section 9.4.9: Main flash page protection registers (PAGEPROTx) (PAGEPRT0) .
0x38PAGEPROT132RW0x0000_0000Write/page erase protection management register. See Section 9.4.9: Main flash page protection registers (PAGEPROTx) (PAGEPRT1) .
0x3CRESERVED32RW0x0000_0000UNUSED
0x40DATA032RW0xFFFF_FFFFProgram cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) .
0x44DATA132RW0xFFFF_FFFFProgram cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) .
0x48DATA232RW0xFFFF_FFFFProgram cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) .
0x4CDATA332RW0xFFFF_FFFFProgram cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) .

1. Acronym meaning: RW: read and write RC: read and write to clear RO: read only.

9.4 Flash controller registers

9.4.1 Command register (COMMAND)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.COMMAND
rw
Bits 31:8Reserved, must be kept at reset value.
Bit 7:0COMMAND: Command opcode to launch any operation on Flash memory. Refer to Table 20. Command list available for customer.

Table 21. Command list available for customer

CommandFlash sectorDescriptionCommand Opcode
ERASEMain memoryErase page defined by register ADDRESS.0x11
MASSERASEMain memoryMass erase (main flash memory is completely erased).0x22
WRITE (1)Main memoryProgram one location (defined by registers DATA and ADDRESS).0x33
MASSREADMain memoryRead all locations and compare with DATA register value or produce LFSR signature.0x55
SLEEPWhole memoryPut Flash in sleep mode.
Warning: Once this command is launched, no access (read) nor action (any command except WAKE) on the Flash component is possible until the WAKE command is requested (and associated CMDDONE status is returned).
0xAA
WAKEUPWhole memoryGet Flash out of sleep mode.0xBB
BURSTWRITEMain memoryProgram 4 locations (ADDRESS → ADDRESS+3) with data in DATA0-DATA3 registers.
Warning: Bursts always start (=DATA0 is always written) on a 16-byte aligned address, even if ADDRESS is not 16-byte aligned (register bit field ADDRESS[1:0] is always considered as 0 at flash controller level).
0xCC
OTPWRITEUser OTPOne time writable 1 Kbyte for customer.
No erase nor second programming is possible.
0xEE
KEYWRITEMain memoryWrite the customer key used to protect the main Flash.0xFF

1. Each address can be programmed only twice without erase operation in between.

Status bit behavior versus commands:

The sequences to use the different commands are described in Section 9.5: Programmer model .

9.4.2 Configuration register (CONFIG)

Address offset: 0x04

Reset value: 0x0000 0018

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.WAIT_STATESRes.Res.DIS_GROUP_WRITEREMAPRes.
rwrwrwrw
Bits 31:6Reserved, must be kept at reset value.
Bits 5:4WAIT_STATES: Number of wait states to be inserted on flash read (AHB accesses).
The flash memory embedded in the STM32WB07xC and STM32WB06xC devices require 1 wait_state when system clock frequency is 64 MHz.
Bit 3Reserved, must be kept at reset value.
Bit 2DIS_GROUP_WRITE:
  • 0: Burst write operations are allowed/enabled
  • 1: Burst write operations are blocked and result on a single write
Note: If this bit is set during an on-going burst write operation, the flash controller stops the write operation at the end of the current word writing even if some words are still to be written.
Bit 1REMAP: Bit to redirect boot area on SRAM0.
Bit 0Reserved, must be kept at reset value.

The flash memory can be read in one system clock cycle (the best for power consumption) when the system clock is 32 MHz maximum.

9.4.3 Interrupt status register (IRQSTAT)

The interrupt status register shows the masked version of the interrupt raw register.

Address offset: 0x08

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOK_MISILLCMD_MISCMDERR_MISCMDSTART_MISCMDDONE_MIS
rc_w1rc_w1rc_w1rc_w1rc_w1
Bits 31:5Reserved, must be kept at reset value.
Bit 4READOK_MIS: Mass read OK masked interrupt status.
This bit is set at the end of a MASSREAD operation if all the words read in the memory match the DATA0 register value.
Cleared by writing 1.
Bit 3ILLCMD_MIS: Illegal command masked interrupt status.
This bit is set when a bad opcode command is written in the COMMAND register. Cleared by writing 1.
Bit 2CMDERR_MIS: Command error masked interrupt status.
This bit is set if a command opcode is written in COMMAND register while the Flash is busy. Cleared by writing 1.
Bit 1CMDSTART_MIS: Command started masked interrupt status.
This bit is set once the requested command execution has started. Cleared by writing 1.
Bit 0CMDDONE_MIS: Command done masked interrupt status.
This it is set once the requested command execution is completed. Cleared by writing 1.

The CMDDONE and CMDSTART bits are updated a few clock cycles after the requested command has been started by writing to the COMMAND register.

Note: Clearing a bit by writing in IRQSTAT (respectively IRQRAW) register also cleared the same bit in IRQRAW (respectively IRQSTAT) register as they are referring to a common condition/event.

9.4.4 Interrupt mask register (IRQMASK)

The mask bit in IRQMASK masks the condition in the status register IRQSTAT and prevents the generation from the interrupt.

Address offset: 0x0C

Reset value: 0x0000 003F

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOK MILLCMD MCMDERRMCMDSTART MCMDDONE M
rwrwrwrwrw
Bits 31:5Reserved, must be kept at reset value.
Bit 4READOKM: Mass read OK mask.
  • 0: Enable interrupt on “Mass read OK” event
  • 1: Disable interrupt on “Mass read OK” event
Bit 3ILLCMDM: Illegal command mask.
  • 0: Enable interrupt on “illegal command” event
  • 1: Disable interrupt on “illegal command” event
Bit 2CMDERRM: Command error mask.
  • 0: Enable interrupt on “command error” event
  • 1: Disable interrupt on “command error” event
Bit 1CMDSTARTM: Command started mask.
  • 0: Enable interrupt on “command started” event
  • 1: Disable interrupt on “command started” event
Bit 0CMDDONEM: Command done mask.
  • 0: Enable interrupt on “command done” event
  • 1: Disable interrupt on “command done” event

9.4.5 Raw status register (IRQRAW)

The raw status register shows the unmasked condition of interrupt events.

Address offset: 0x10

Reset value: 0x0000 0001

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOK_RISILLCMD_RISCMDERR_RISCMDSTART_RISCMDDONE_RIS
rc_w1rc_w1rc_w1rc_w1rc_w1
Bits 31:5Reserved, must be kept at reset value.
Bit 4READOK_RIS: Mass read OK raw/unmasked interrupt status.
This bit is set at the end of a MASSREAD operation if all the words read in the memory match the DATA0 register value.
Cleared by writing 1.
Bit 3ILLCMD_RIS: Illegal command raw/unmasked interrupt status.
This bit is set when a bad opcode command is written in the COMMAND register. Cleared by writing 1.
Bit 2CMDERR_RIS: Command error raw/unmasked interrupt status.
This bit is set if a command opcode is written in COMMAND register while the Flash is busy. Cleared by writing 1.
Bit 1CMDSTART_RIS: Command started raw/unmasked interrupt status. This bit is set once the requested command execution has started.
Cleared by writing 1.
Bit 0CMDDONE_RIS: Command done raw/unmasked interrupt status. This bit is set once the requested command execution is completed. Cleared by writing 1.

The CMDDONE and CMDSTART bits are updated a few clock cycles after the requested command has been started by writing to the COMMAND register.

9.4.6 SIZE register

Address offset: 0x14

Reset value: 0x000- ---- (depends on the device)

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.SWD_DISABLEFLASH_SECURERAM_SIZE[1:0]Res.
r
1514131211109876543210
FLASH_SIZE[15:0]
r
Bits 31:21Reserved, must be kept at reset value.
Bit 20SWD_DISABLE: Flash+SWD protection:
  • 0: No SWD protection (refer to FLASH_SECURE)
  • 1: Flash and SWD protected
Bit 19FLASH_SECURE: Flash memory protection:
  • 0: The main FLASH is not protected
  • 1: The main FLASH is protected through a customer key
Bits 18:17RAM_SIZE: Indicates the size of RAM available in the device:
  • 00: 32 Kbytes of RAM available (RAM0 and RAM1 banks)
  • 01: 32 Kbytes of RAM available (RAM0 and RAM1 banks)
  • 10: 48 Kbytes of RAM available (RAM0, RAM1 and RAM2 banks)
  • 11: 64 Kbytes of RAM available (RAM0, RAM1, RAM2 and RAM3 banks)
Bit 16Reserved, must be kept at reset value.
Bits 15:0FLASH_SIZE: Indicates the last usable address of the Flash using memory component address format. See Table 22. Flash size information for relation between address at Flash component level and AHB address mapping.
  • 0x7FFF: 128 Kbytes of main flash are available on this device
  • 0xBFFF: 192 Kbytes of main flash are available on this device
  • 0xFFFF: 256 Kbytes of main flash are available on this device

Table 22. Flash size information

Main flash sizeHighest usable address at Flash level (1)Highest usable address at AHB level
128 Kbytes0x7FFF0x1005_FFFC
192 Kbytes0xBFFF0x1006_FFFC
256 Kbytes0xFFFF0x1007_FFFC

1. Value seen in FLASH_SIZE bit field.

9.4.7 Address register (ADDRESS)

Address offset: 0x18

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
XADDR[9:0]YADDR[5:0]
rwrw
Bits 31:16Reserved, must be kept at reset value.
Bits 15:6XADDR[9:0]:
  • XADDR[9:3]: Page number (from 0 to 127)
  • XADDR[2:0]: Row number (from 0 to 7)
Bits 5:0YADDR[5:0]: Word number inside the selected row (from 0 to 63)

Address to provide to the Flash is not the AHB device mapping address but the address respecting Flash component format.

The main Flash is composed of 128 pages containing 8 rows each with 64 words = 256 bytes by row.

To program the ADDRESS register, the formula is the following:

Example 1: To program a word (32-bit) at AHB address 0x1005_0454:

9.4.8 Linear feedback shift register (LFSRVAL)

The LFSRVAL register contains the signature issued by a MASSREAD command.

The LFSRVAL register is initialized with all ones when the MASS READ command is written to the COMMAND register. Then every read value is put through the LFSR.

The final signature can be read in this register once the CMDDONE information is set.

Address offset: 0x24

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
LFSRVAL[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
LFSRVAL[15:0]
rrrrrrrrrrrrrrrr
Bits 31:0LSFRVAL: Signature after a MASSREAD command, generated through a linear feedback. Shift Register block.

9.4.9 Main flash page protection registers (PAGEPROTx)

The PAGEPROTx register allows protecting from accidental write a contiguous set of pages called segment in the following description. A maximum of four segments can be defined.

An example of usage is available in Section 9.5.7: Write page protection example.

PAGEPROT0

Address offset: 0x34

Reset value: 0x0000 0000

31302928272625242322212019181716
SEG1[15:0]
rw
1514131211109876543210
SEG0[15:0]
rw
Bits 31:16SEG1: Second segment definition. See SEG0 description for details on SEG1[31:16] content.
Bit 15:0SEG0: First segment definition.
A segment SEGx is built as follows:
  • SEGx[15]: Reserved
  • SEGx[14:8] = OFFSET: Page number to start the write protection (value between 0 and 0x7F)
  • SEGx[7]: Reserved
  • SEGx[6:0] = SIZE: number of 2 Kbytes pages to protect including the starting page (provided in SEGx[14:8])
Note:
  • SIZE=0 means no segment defined so if all segments have SIZE=0, then no write protection is applied on the whole FLASH.
  • The segments can overlap, the protection on a page is guaranteed if at least one segment covers this page.
  • If OFFSET + SIZE > 127d so exceeds the maximum size of the FLASH, the end of the segment is positioned on the maximum allowed address.

PAGEPROT01

Address offset: 0x38

Reset value: 0x0000 0000

31302928272625242322212019181716
SEG3[15:0]
rw
1514131211109876543210
SEG2[15:0]
rw
Bits 31:16SEG3: Fourth segment definition. See PAGEPROT0 SEG0 description for details on SEG3[15:0] content.
Bit 15:0SEG2: Third segment definition. See PAGEPROT0 SEG0 description for details on SEG2[15:0] content.

9.4.10 Data registers (DATA0-DATA3)

The DATA0 register needs to be written with:

The DATA1-DATA3 registers need to be written only for burst write.

DATA0

Address offset: 0x40

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
DATA0[31:16]
rw
1514131211109876543210
DATA0[15:0]
rw
Bits 31:0

DATA0: This register has several uses:

  • • Data to write in Flash in single write mode
  • • First data to be written in Flash on a burst write
  • • Compared value for a MASSREAD command (useful only if Flash is fully written with the same word)

Note: In this last case, the flag READOK indicates whether there was a match or not at the end of the mass read.

DATA1

Address offset: 0x44

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
DATA1[31:16]
rw
1514131211109876543210
DATA1[15:0]
rw
Bits 31:0

DATA1: Data that are written at ADDRESS+1 during a BURSTWRITE command.

Note: This register is used only on burst write.

DATA2

Address offset: 0x48

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
DATA2[31:16]
rw
1514131211109876543210
DATA2[15:0]
rw
Bits 31:0

DATA2: Data that are written at ADDRESS+2 during a BURSTWRITE command.

Note: This register is used only on burst write.

DATA3

Address offset: 0x4C

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
DATA3[31:16]
rw
1514131211109876543210
DATA3[15:0]
rw
Bits 31:0DATA3: Data that are written at ADDRESS+3 during a BURSTWRITE command.
Note: This register is used only on burst write.

9.5 Programmer model

The STM32WB07xC and STM32WB06xC embed up to 256 Kbytes (65536 x 32-bit) of internal flash memory. A flash interface implements instruction access and data access based on the AHB protocol. It implements the logic necessary to carry out the flash memory operations (program/erase) controlled through the flash registers.

9.5.1 General information

Writing to flash memory only allows clearing bits from '1' to '0'. This means any write from '0' to '1' implies erasing before performing a write.

Flash memory is composed of 128 pages containing 8 rows of 64 words (128 x 8 x 64 = 65536 words). Each word is 32-bit = 4 bytes long which means 256 Kbytes of flash memory.

The address inside the the Flash controller ADDRESS register is built as follows: ADDRESS[15:0] = XADR[9:0] & YADR[5:0] with:

Note: One specific address can be written only twice between two erase actions even if each writing only clears bit 1.

9.5.2 Read function examples

There are two possible read accesses:

There are two ways of using MASSREAD:

MASSREAD sequence:

9.5.3 Erase function examples

The flash memory controller allows the erasing of one page.

ERASE sequence (erase one page):

MASSERASE sequence (erase whole main flash memory):

9.5.4 Write function examples

The Flash controller allows writing one word (WRITE), up to 4 words (BURSWRITE) or the full main Flash memory (with a single fixed word).

Note: As a write can only program to '0' on bits already set to '1', it is necessary to erase the page and request that the bits are set to '1' (instead of '0') in order to re-write to '0'.

WRITE sequence:

BURSTWRITE sequence:

9.5.5 Enabling protection example

The device offers three levels of protection to prevent application cloning and/or altering of application code. The different levels are described in Table 23. System memory protection . It is important to note that disabling of SWD access is an irreversible operation.

Table 23. System memory protection

ProtectionUserkey[DATA0]Userkey[DATA1]Disable protectionDescription
None0xFFFFFFFF0xFFFFFFFFNot applicableDebugger can access and modify Flash memory and RAM content. This is the default configuration. All the bootloader commands are available.
Readout0xAAAAAAAA0xAAAAAAAAPerform mass eraseDebugger cannot read or modify both Flash memory and RAM content. The bootloader commands READ_MEMORY, WRITE_MEMORY and GO are disabled and MASS ERASE is available.
SWD0xABACABAD0xABACABADThis selection is irreversibleDebugger connection is not possible. There is no possibility to access to the device via SWD and all the bootloader commands are disabled.
Not specifiedAny other valueAny other valueNot applicableThis configuration is forbidden and can lead to unrecoverable damage of the device.

In order to activate the desired level of protection, the following KEYWRITE sequence should be used:

The keys are activated (and Flash and RAM banks are protected) after a reset.

Note: The secure bootloader capability offers a further security level for preventing unwanted control from external users.

When the secure bootloader capability is enabled, the flash and RAM contents are readable. The GO command of the bootloader is disabled and all the other bootloader commands are available. Only signed firmware can be executed.

For more information about the secure bootloader capability refer to the AN6140: How to use the secure bootloader on STM32WB0 MCUs.

9.5.6 OTP function example

OTPWRITE sequence:

Note: The OTP locations are following Flash memory rules, that is, a second write only flips bit from 1 to 0. If the user wishes to lock the OTP values and prevent any further modification in the OTP area, they must write the last OTP word (address 0x10001BFC) with a value different from 0xFFFFFFFF and perform system reset. The operation of locking the OTP area is irreversible.

9.5.7 Write page protection example

Example to write protect against accidental programming knowing the Flash starts at address 0x1004_0000 and contains 128 pages of 2 Kbytes (pages 0 to 127)

Starting page: 0xC000 / 0x800 = 0x18

Number of pages: (0x10000 - 0xC000) / 0x800 = 0x8

Starting page: 0x1E000 / 0x800 = 0x3C

Number of pages: (0x20000 - 0x1E000) / 0x800 = 0x4

Conclusion: program the PAGEPROT0 = 0x3C041808.