9. Embedded flash memory (FLASH)

The flash controller implements the erase and program flash memory operation. The flash controller also implements the read and write protection.

9.1 Flash main features

Flash memory features:

Flash controller features:

Caution: When the BORH feature is enabled in the PWR (PWR_CR1.ENBORH = 1), the flash controller stops programming activity on the flash memory as soon as the power supply voltage is below the BORH threshold (visible through the flag PWR_ISCR.BORHISC in the system controller).

9.2 Description

The flash memory is organized as follows, depending on the device:

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

In parallel, the flash controller manages 1 Kbyte OTP (One-Time Programmable) for user data. The OTP data cannot be erased. The OTP data area can no more 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 21. Flash memory section address

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

1. Depends on flash size. See Table 23: Flash size information on page 209.

9.3 Flash controller register descriptions

9.3.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:8 Reserved, must be kept at reset value.

Bit 7:0 COMMAND: Command opcode to launch any operation on flash memory. See for command list and detail.

Table 22. Command list available for customer

CommandFlash sectorDescriptionCommand Opcode
ERASEmain memoryErase page defined by register ADDRESS.0x11
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 are 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: a burst always starts (=DATA0 is always written) on a 16-bytes aligned address even if ADDRESS is not 16-bytes aligned (register bit field ADDRESS[1:0] always considered as 0 at flash controller level).
0xCC
OTPWRITEUser OTPOne time writable 1 Kbytes for customer.
No erase not second programming is possible.
0xEE

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

ST logo
ST logo

Status bit behavior versus commands:

The sequences to use the different commands are described in 9.7: Programmers model on page 216.

9.3.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.DIS_GROUP_WRITEREMAPRes.
rwrwrwrw

Bits 31:6 Reserved, must be kept at reset value.

Bits 5:4 WAIT_STATES: Number of wait states to be inserted on flash read (AHB accesses).

The flash memory embedded in STM32WL30xx/31xx/33xx devices requires 1 wait_state when system clock frequency is 64 MHz.

Bit 3 Reserved, must be kept at reset value.

Bit 2 DIS_GROUP_WRITE:

– 0: burst write operations are allowed/enabled.

– 1: burst write operations are blocked and results in 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 1 REMAP: bit to redirect boot area on SRAM0. See Table 5: Address remapping depending on the REMAP bit for details.

Bit 0 Reserved, must be kept at reset value.

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

9.3.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:5 Reserved, must be kept at reset value.

Bit 4 READOK_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 3 ILLCMD_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 2 CMDERR_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 1 CMDSTART_MIS: Command started masked interrupt status.

This bit is set once the requested command execution has started.

Cleared by writing 1.

Bit 0 CMDDONE_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.3.4 Interrupt mask register (IRQMASK)

The mask bit in IRQMASK masks the condition in the status register IRQSTAT and prevent the generation of 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
M
ILLCMD
M
CMDERR
M
CMDSTART
M
CMDDONE
M
rwrwrwrwrw

Bits 31:5 Reserved, must be kept at reset value.

Bit 4 READOKM: Mass read OK mask.

Bit 3 ILLCMDM: Illegal command mask.

Bit 2 CMDERRM: command error mask.

Bit 1 CMDSTARTM: Command started mask.

Bit 0 CMDDONEM: Command done mask.

9.3.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:5 Reserved, must be kept at reset value.

Bit 4 READOK_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 3 ILLCMD_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 2 CMDERR_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 1 CMDSTART_RIS: Command started raw/unmasked interrupt status.

This bit is set once the requested command execution has started.

Cleared by writing 1.

Bit 0 CMDDONE_RIS: Command done raw/unmasked 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.3.6 Size register (SIZE)

Address offset: 0x14

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

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.PKG_SIZE[1:0]SWD_DISABLEFLASH_SECURERes.RAM_SIZERes.
rr--r
1514131211109876543210
FLASH_SIZE[15:0]
r

Bits 31:23 Reserved, must be kept at reset value.

Bits 22:21 PKG_SIZE: indicates device package size:

Bit 20 SWD_DISABLE: indicates the SWD is disabled on the device:

Bit 19 FLASH_SECURE: indicates the main flash memory is locked by a customer key.

Bit 18 Reserved, must be kept at reset value.

Bit 17 RAM_SIZE: indicates the size of RAM available in the device:

Bit 16 Reserved, must be kept at reset value.

Bits 15:0 FLASH_SIZE: indicates the last usable address of the flash using memory component address format. See Table 23 for relation between address at flash component level and AHB address mapping.

Table 23. Flash size information

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

1. Value seen in FLASH_SIZE bit field.

9.3.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:16 Reserved, must be kept at reset value.

Bits 15:6 XADDR[9:0]:

Bits 5:0 YADDR[5:0]: word number inside the selected row (from 0 to 63).

Note: when burst write command is used, YADDR[1:0] bit field is always considered as 0 by the flash controller.

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.3.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.
customer.

Address offset: 0x24

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
LFSRVAL[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
LFSRVAL[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 LFSRVAL: signature after a MASSREAD command, generated through a Linear Feedback Shift Register block.

9.4 Main flash page protection registers 0 to 1

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

9.4.1 Page protection register 0 (PAGEPROT0)

Address offset: 0x34

Reset value: 0x0000 0000

31302928272625242322212019181716
SEG1[15:0]
rw
1514131211109876543210
SEG0[15:0]
rw

Bits 31:16 SEG1: second segment definition.

See SEG0 description for details on SEG1[15:0] content.

Bits 15:0 SEG0: First segment definition.

A segment SEGx is built as follows:

Note:

9.4.2 Page protection register 1 (PAGEPROT1)

Address offset: 0x38

Reset value: 0x0000 0000

31302928272625242322212019181716
SEG3[15:0]
rw
1514131211109876543210
SEG2[15:0]
rw

Bits 31:16 SEG3: fourth segment definition.

See PAGEPROT0.SEG0 description for details on SEG3[15:0] content.

Bits 15:0 SEG2: third segment definition.

See PAGEPROT0.SEG0 description for details on SEG2[15:0] content.

9.5 Data registers 0 to 3

The DATA0 register needs to be written with:

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

Data 0 register (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 usage:

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

9.5.1 Data 1 register (DATA1))

Address offset: 0x44

Reset value: 0xFFFF FFFF

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

Bits 31:0 DATA1: data that is written at ADDRESS+1 during a BURSTWRITE command.

Note: this register is used only on burst write.

9.5.2 Data 2 register (DATA2)

Address offset: 0x48

Reset value: 0xFFFF FFFF

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

Bits 31:0 DATA2: data that is written at ADDRESS+2 during a BURSTWRITE command.

Note: this register is used only on burst write.

9.5.3 Data 3 register (DATA3)

Address offset: 0x4C

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
DATA3[31:16]
rw
1514131211109876543210
DATA3[15:0]
rw

Bits 31:0 DATA3: data that is written at ADDRESS+3 during a BURSTWRITE command.

Note: this register is used only on burst write.

9.6 Flash controller register map

Refer to Table 3: Memory map and peripheral register boundary addresses for the flash controller base address location in the STM32WL30xx/31xx/33xx.

Table 24. Flash controller register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00COMMANDRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.COMMAND[7:0]
Reset value0000000
0x04CONFIGRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.SLEEP_SMWAIT_STATE[1:0]Res.DIS_GROUP_WRITEREMAPLONGACCESS
Reset value001001
0x08IRQSTATRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOK_MISILLCMD_MISCMDERR_MISCMDSTART_MISCMDDONE_MIS
Reset value00000
0x0CIRQMASKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOKMILLCMDMCMDERRMCMDSTARTMCMDDONEM
Reset value
0x10IRQRAWRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.READOK_RISILLCMD_RISCMDERR_RISCMDSTART_RISCMDDONE_RIS
Reset value0
0x14SIZERes.Res.Res.Res.Res.Res.Res.Res.Res.PKG_SIZE[1:0]SWD_DISABLEFLASH_SECURERes.RAM_SIZERes.FLASH_SIZE[15:0]
Reset value--------1111111111111
0x18ADDRESSRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.XADDR[9:0]YADDR[5:0]
Reset value000000000000000
0x24LFSRVALLSFRVAL[31:0]
Reset value
0x34PAGEPROT0SEG1[15:0]SEG0[15:0]
Reset value0000000000000000000000000000000

Table 24. Flash controller register map and reset values (continued)

OffsetRegister313029282726252423222120191817161514131211109876543210
0x38PAGEPROT1SEG3[15:0]SEG2[15:0]
Reset value0000000000000000000000000000000
0x3CReserved
0x40DATA0DATA0[31:0]
Reset value
0x44DATA1DATA1[31:0]
Reset value
0x48DATA2DATA2[31:0]
Reset value
0x4CDATA3DATA3[31:0]
Reset value

9.7 Programmers model

The flash memory size depends on the product:

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.7.1 General information

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

The address inside the flash controller ADDRESS register is built as follows:

ADDRESS[13: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 bits.

9.7.2 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

SEG0.OFFSET = 0x18

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

SEG0.SIZE = 0x08

Starting page: 0x1E000/0x800 = 0x3C

SEG1.OFFSET = 0x38

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

SEG0.SIZE = 0x04

Conclusion: Program the PAGEPROT0 = 0x3C041808.

9.7.3 Read function examples

There are two possible read accesses:

There are two ways of using MASSREAD:

MASSREAD sequence:

9.7.4 Erase function examples

The flash controller allows erasure of one page.

ERASE sequence (erase one page):

9.7.5 Enabling protection example

The device offers three level of protection to prevent application cloning and or altering of application code. The different levels are described in the Table 25. It is important to notice that disabling of SWD access is an irreversible operation.

Table 25. 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:

  1. 1. Write DATA0 (LSB key) and DATA1 (MSB key) registers with the value to program
  2. 2. Write 0xC7EF584D to DATA2 and 0xB3A21096 to DATA3
  3. 3. Write the KEYWRITE command (0xFF) in the flash controller COMMAND register
  4. 4. Wait for the CMDSTART flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command has been taken into account and is under execution.
  5. 5. Clear the CMDSTART flag by writing CMDSTART to '1' in the flash controller IRQSTAT register.
  6. 6. Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode), indicating that the command is completed.
  7. 7. Clear the CMDDONE flag by writing CMDDONE to '1' in the flash controller IRQSTAT register. The key are activated (and flash and RAM banks are protected) after a reset.

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

Note: If readout protection is set while the debugger is still connected through SWD, apply a power-on reset (POR) instead of a system reset.

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.

9.7.6 Write page protection example

Example to write protect against accidental programming knowing the flash starts at address 0x1004_0000 and contains 256 pages of 2 Kbytes (pages 0 to 255).

For the address range 0x1004C000 to 0x1004FFFF

And for the address ranges 0x1005E000 to 0x1005FFFF.

Conclusion: Program the PAGEPROT0 = 0x3C041808.

9.7.7 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 be set to '1' (instead of '0') in order to re-write to '0'.

WRITE sequence:

BUSRTWRITE sequence:

9.7.8 Other command usage descriptions

OTPWRITE sequence:

Note: The OTP area can be filled until the last word (address 0x1000_1BFC) is written with a value different from 0xFFFF_FFFF and a system reset occurs.