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:
- • Up to 256 Kbytes of flash memory single bank architecture.
- • Memory organization: 1 bank
- – main memory: up to 256 Kbytes
- – page size: 2 Kbytes
- • 32-bit wide data read
- • 32-bit wide data write
- • Page erase (2 Kbytes) and mass erase.
Flash controller features:
- • Flash memory read operations
- • Flash memory write operations: single data write or 4x32-bits burst write (to reduce programming time by 4)
- • Flash memory erase operations
- • Flash readout protection and disable of debug access
- • Page write protect mechanism
9.2 Description
The flash memory is organized as follows:
- • Main memory block containing 128 pages of 2 Kbytes. Each page is made of 8 rows of 256 bytes (64 words).
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
| Section | Flash AHB start address | Flash AHB end address |
|---|---|---|
| Main flash memory | 0x1004_0000 | 0x1007_FFFF (1) |
| User OTP | 0x1000_1800 | 0x1000_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 offset | Name | Width | RW (1) | Reset | Description |
|---|---|---|---|---|---|
| 0x00 | COMMAND | 8 | RW | 0x0000_0000 | Commands for the module. See Section 9.4.1: Command register (COMMAND) . |
| 0x04 | CONFIG | 2 | RW | 0x0000_0018 | Configure the wrapper. See Section 9.4.2: Configuration register (CONFIG) . |
| 0x08 | IRQSTAT | 5 | RC | 0x0000_0000 | Flash status interrupts (masked). See Section 9.4.3: Interrupt status register (IRQSTAT) . |
| 0x0C | IRQMASK | 5 | RW | 0x0000_003F | Mask for interrupts. See Section 9.4.4: Interrupt mask register (IRQMASK) . |
| 0x10 | IRQRAW | 5 | RC | 0x0000_0001 | Status interrupts (unmasked). See Section 9.4.5: Raw status register (IRQRAW) . |
| 0x14 | FLASH_SIZE | 16 | RO | 0x0000_---- | Indicates the last usable address of the main Flash and the RAM size. See Section 9.4.6: SIZE register . |
| 0x18 | ADDRESS | 14 | RW | 0x0000_0000 | Address for programming Flash, auto-increments. See Section 9.4.7: Address register (ADDRESS) . |
| 0x24 | LFSRVAL | 32 | RO | 0xFFFF_FFFF | LFSR register needed for check after MASS READ command. See Section 9.4.8: Linear feedback shift register (LFSRVAL) . |
| 0x34 | PAGEPROT0 | 32 | RW | 0x0000_0000 | Write/page erase protection management register. See Section 9.4.9: Main flash page protection registers (PAGEPROTx) (PAGEPRT0) . |
| 0x38 | PAGEPROT1 | 32 | RW | 0x0000_0000 | Write/page erase protection management register. See Section 9.4.9: Main flash page protection registers (PAGEPROTx) (PAGEPRT1) . |
| 0x3C | RESERVED | 32 | RW | 0x0000_0000 | UNUSED |
| 0x40 | DATA0 | 32 | RW | 0xFFFF_FFFF | Program cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) . |
| 0x44 | DATA1 | 32 | RW | 0xFFFF_FFFF | Program cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) . |
| 0x48 | DATA2 | 32 | RW | 0xFFFF_FFFF | Program cycle data. See Section 9.4.10: Data registers (DATA0-DATA3) . |
| 0x4C | DATA3 | 32 | RW | 0xFFFF_FFFF | Program 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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 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. Refer to Table 20. Command list available for customer. |
Table 21. Command list available for customer
| Command | Flash sector | Description | Command Opcode |
|---|---|---|---|
| ERASE | Main memory | Erase page defined by register ADDRESS. | 0x11 |
| MASSERASE | Main memory | Mass erase (main flash memory is completely erased). | 0x22 |
| WRITE (1) | Main memory | Program one location (defined by registers DATA and ADDRESS). | 0x33 |
| MASSREAD | Main memory | Read all locations and compare with DATA register value or produce LFSR signature. | 0x55 |
| SLEEP | Whole memory | Put 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 |
| WAKEUP | Whole memory | Get Flash out of sleep mode. | 0xBB |
| BURSTWRITE | Main memory | Program 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 |
| OTPWRITE | User OTP | One time writable 1 Kbyte for customer. No erase nor second programming is possible. | 0xEE |
| KEYWRITE | Main memory | Write 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:
- • Writing to the COMMAND register starts the action that is performed on the Flash
- • The CMDSTART flag goes and stays high until it is cleared
- • When the command has finished the CMDDONE flag goes high
- • When a MASS READ command was given and when CMDDONE is high, the READOK flag can be checked or the LFSRVAL register can be read (contains the signature of the mass read)
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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | WAIT_STATES | Res. | Res. | DIS_GROUP_WRITE | REMAP | Res. |
| rw | rw | rw | rw |
| 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 the STM32WB07xC and STM32WB06xC devices require 1 wait_state when system clock frequency is 64 MHz. |
| Bit 3 | Reserved, must be kept at reset value. |
| Bit 2 | DIS_GROUP_WRITE:
|
| Bit 1 | REMAP: Bit to redirect boot area on SRAM0. |
| Bit 0 | Reserved, 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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | READOK_MIS | ILLCMD_MIS | CMDERR_MIS | CMDSTART_MIS | CMDDONE_MIS |
| rc_w1 | rc_w1 | rc_w1 | rc_w1 | rc_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.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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | READOK M | ILLCMD M | CMDERRM | CMDSTART M | CMDDONE M |
| rw | rw | rw | rw | rw |
| 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.4.5 Raw status register (IRQRAW)
The raw status register shows the unmasked condition of interrupt events.
Address offset: 0x10
Reset value: 0x0000 0001
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | READOK_RIS | ILLCMD_RIS | CMDERR_RIS | CMDSTART_RIS | CMDDONE_RIS |
| rc_w1 | rc_w1 | rc_w1 | rc_w1 | rc_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 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)
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | SWD_DISABLE | FLASH_SECURE | RAM_SIZE[1:0] | Res. | |
| r | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| FLASH_SIZE[15:0] | |||||||||||||||
| r | |||||||||||||||
| Bits 31:21 | Reserved, must be kept at reset value. |
| Bit 20 | SWD_DISABLE:
Flash+SWD protection:
|
| Bit 19 | FLASH_SECURE:
Flash memory protection:
|
| Bits 18: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 22. Flash size information
for relation between address at Flash component level and AHB address mapping.
|
Table 22. Flash size information
| Main flash size | Highest usable address at Flash level (1) | Highest usable address at AHB level |
|---|---|---|
| 128 Kbytes | 0x7FFF | 0x1005_FFFC |
| 192 Kbytes | 0xBFFF | 0x1006_FFFC |
| 256 Kbytes | 0xFFFF | 0x1007_FFFC |
1. Value seen in FLASH_SIZE bit field.
9.4.7 Address register (ADDRESS)
Address offset: 0x18
Reset value: 0x0000 0000
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| XADDR[9:0] | YADDR[5:0] | ||||||||||||||
| rw | rw | ||||||||||||||
| 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) |
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:
- XADDR[9:0] = AHB address bit [17:8]
- YADDR[5:0] = AHB address bit [7:2]
Example 1: To program a word (32-bit) at AHB address 0x1005_0454:
- XADDR[9:0] = AHB address bit [17:8] = 0x104
- YADDR[5:0] = AHB address bit [7:2] = 0x15
- ADDRESS register = 0x4115
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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| LFSRVAL[31:16] | |||||||||||||||
| r | r | r | r | r | r | r | r | r | r | r | r | r | r | r | r |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| LFSRVAL[15:0] | |||||||||||||||
| r | r | r | r | r | r | r | r | r | r | r | r | r | r | r | r |
| Bits 31:0 | LSFRVAL: 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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| SEG1[15:0] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SEG0[15:0] | |||||||||||||||
| rw | |||||||||||||||
| Bits 31:16 | SEG1: Second segment definition. See SEG0 description for details on SEG1[31:16] content. |
| Bit 15:0 | SEG0:
First segment definition. A segment SEGx is built as follows:
|
PAGEPROT01
Address offset: 0x38
Reset value: 0x0000 0000
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| SEG3[15:0] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| SEG2[15:0] | |||||||||||||||
| rw | |||||||||||||||
| Bits 31:16 | SEG3: Fourth segment definition. See PAGEPROT0 SEG0 description for details on SEG3[15:0] content. |
| Bit 15:0 | SEG2: 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 desired value written to the Flash location (for single write or mass write).
- The desired compare value for a (mass) read operation, the flag READOK indicates if there was a match or not. For mass read, all read values must match for READOK.
The DATA1-DATA3 registers need to be written only for burst write.
DATA0
Address offset: 0x40
Reset value: 0xFFFF FFFF
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| DATA0[31:16] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| DATA0[15:0] | |||||||||||||||
| rw | |||||||||||||||
| Bits 31:0 | DATA0: This register has several uses:
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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| DATA1[31:16] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| DATA2[31:16] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 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
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
| DATA3[31:16] | |||||||||||||||
| rw | |||||||||||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| DATA3[15:0] | |||||||||||||||
| rw | |||||||||||||||
| Bits 31:0 | DATA3:
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:
- • XADR[9:3] = page address
- • XADR[2:0] = row address
- • YADR[5:0] = word address (one word = four bytes)
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:
- • Read one single word: simple read as if SRAM memory: read the desired Flash address and get read data on the bus
- • MASSREAD command: read the full Flash memory and compare with expected content
There are two ways of using MASSREAD:
- • Full Flash contains a fixed 32-bit pattern: indicate the expected pattern (value to be compared with each read value inside Flash) in the Flash controller DATA register and check the READOK flag in the Flash controller interrupt register once the command is completed
- • Otherwise: request a MASSREAD command without specifying any expected read value and check the LFSRVAL register once the command is completed. This LFSRVAL register contains a signature of the memory read
MASSREAD sequence:
- • Write in the Flash controller DATA register the expected value (if MASSREAD is used in combination with the READOK flag)
- • Write the MASSREAD command (0x55) in the Flash controller COMMAND register
- • 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
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the Flash controller IRQSTAT register
- • Then, wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command is completed
- • Check the READOK flag (expected high) in the IRQSTAT register or the LFSRVAL register value to ensure Flash memory content is the expected result
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the Flash controller IRQSTAT register.
9.5.3 Erase function examples
The flash memory controller allows the erasing of one page.
ERASE sequence (erase one page):
- • Write the page address to be erased by writing in the flash controller ADDRESS register the following value:
- – ADDRESS[15:9] = XADR[9:3] = page address to erase
- – ADDRESS[8:0] = 9'b0 (row and word addresses at zero)
- • Write the ERASE command (0x11) in the flash controller COMMAND register
- • Wait for the CMDSTART flag in the IRQSTAT register (polling mode or interrupt mode) indicating command is taken into account and under execution
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the flash controller IRQSTAT register
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command is completed
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the flash controller IRQSTAT register
- • After this command, the erased page contains bits set to '1' only.
MASSERASE sequence (erase whole main flash memory):
- • Write the MASSERASE command (0x22) in the flash controller COMMAND register.
- • 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.
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the flash controller IRQSTAT register.
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command is completed.
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the flash controller IRQSTAT register.
- • After this command, the full main flash contains only bits set to '1'.
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:
- • Indicate the location to write by filling the Flash controller ADDRESS register with the targeted address (page, row and word number)
- • Write the value to program in the Flash controller DATA register
- • Write the WRITE command (0x33) in the Flash controller COMMAND register
- • 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
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the Flash controller IRQSTAT register
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command is completed
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the Flash controller IRQSTAT register.
BURSTWRITE sequence:
- • Indicate the location to write by filling the Flash controller ADDRESS register with the targeted address of the first data to write (page, row and word number). DATA0 is written at ADDRESS, DATA1 at ADDRESS+1, etc.
- • Write the values to program in the Flash controller DATA0-3 registers. To write less than four words, write 0xFFFFFFFF in the unused DATA1-3 registers
- • Write the BURSTWRITE command (0xCC) in the Flash controller COMMAND register
- • 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
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the Flash controller IRQSTAT register
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode) indicating that the command is completed
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the Flash controller IRQSTAT register.
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
| Protection | Userkey[DATA0] | Userkey[DATA1] | Disable protection | Description |
|---|---|---|---|---|
| None | 0xFFFFFFFF | 0xFFFFFFFF | Not applicable | Debugger can access and modify Flash memory and RAM content. This is the default configuration. All the bootloader commands are available. |
| Readout | 0xAAAAAAAA | 0xAAAAAAAA | Perform mass erase | Debugger 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. |
| SWD | 0xABACABAD | 0xABACABAD | This selection is irreversible | Debugger connection is not possible. There is no possibility to access to the device via SWD and all the bootloader commands are disabled. |
| Not specified | Any other value | Any other value | Not applicable | This 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:
- • Write DATA0 (LSB key) and DATA1 (MSB key) registers with the value to program
- • Write 0xC7EF584D to DATA2 and 0xB3A21096 to DATA3
- • Write the KEYWRITE command (0xFF) in the Flash controller COMMAND register
- • 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
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the Flash controller IRQSTAT register
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode), indicating that the command is completed
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the Flash controller IRQSTAT register.
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:
- • Write DATA0 register with the value to program (no burst write feature is available as only few bytes to be written once only)
- • Write ADDRESS register according to the following rule:
- – ADDRESS[15:9] = do not care (page number frozen by hardware on this command)
- – ADDRESS[8:6] = 0, 1, 2 or 3
- – ADDRESS[5:0] = full area possible (from 0x00 to 0x3F)
- • Write the OTPWRITE command (0xEE) in the Flash controller COMMAND register
- • 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
- • Clear the CMDSTART flag by writing CMDSTART to '1' in the Flash controller IRQSTAT register
- • Wait for the CMDDONE flag in the IRQSTAT register (polling mode or interrupt mode), indicating that the command is completed
- • Clear the CMDDONE flag by writing CMDDONE to '1' in the FlashController IRQSTAT register.
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)
- • the address ranges 0x1004C000-0x1004FFFF
Starting page: 0xC000 / 0x800 = 0x18
- • SEG0[14:8] = 0x18 (OFFSET = 0x18)
Number of pages: (0x10000 - 0xC000) / 0x800 = 0x8
- • SEG0[6:0] = 0x8 (SIZE = 0x8)
- • and the address ranges 0x1005E000-0x1005FFFF
Starting page: 0x1E000 / 0x800 = 0x3C
- • SEG1[14:8] = 0x3C (OFFSET = 0x3C)
Number of pages: (0x20000 - 0x1E000) / 0x800 = 0x4
Conclusion: program the PAGEPROT0 = 0x3C041808.