I want to enable write protection on the flash pages occupied by the bootloader on an STM32L4 chip. I understand I must set the WRP1AR register to 0xFFXXFFYY where XX is the end page and YY is the start page.

My question is quite simple, but the docs is not really clear. Is the end page also included in the protection or not? So if I write 0 as start and 4 end I get 5 pages protected (0, 1, 2, 3, 4) or only 4 (0, 1, 2, 3)? The way the documentation is worded makes me think it is the first case, but the second way is more usual in programming (at least in C).


I don't see how the reference manual is unclear, so my answer is basically just a quote:

The user area in Flash memory can be protected against unwanted write operations. Two write-protected (WRP) areas can be defined, with page (2 KByte) granularity. The area is defined by a start page offset and an end page offset related to the physical Flash memory base address. These offsets are defined in the WRP address registers: Flash WRP area A address register (FLASH_WRP1AR), Flash WRP area B address register (FLASH_WRP1BR).

The WRP “y” area (y=A,B) is defined from the address:

Flash memory Base address + [WRP1y_STRT x 0x800] (included) to the address: Flash memory Base address + [(WRP1y_END+1) x 0x800] (excluded).

For example, to protect by WRP from the address 0x0806 2800 (included) to the address 0x0807 07FF (included):

• if boot in flash is selected, FLASH_WRP1AR register must be programmed with:

– WRP1A_STRT = 0xC5.

– WRP1A_END = 0xE0.

WRP1B_STRT and WRP1B_END in FLASH_WRP1BR can be used instead (area “B” in Flash memory).

So the end page is included. For your example:

Start address = 0x800 0000 + 0 * 0x800 = 0x800 0000

End address = 0x800 0000 + (4+1) * 0x800 = 0x800 2800

So the flash from 0x800 0000 to 0x800 27FF will be protected.

Maybe what is confusing is the word "excluded" what is excluded is the end address as that would be the byte at the start of the next page. So their formula should probably read something like:

Flash is protected from: Flash memory Base address + [WRP1y_STRT x 0x800] to the address: Flash memory Base address + [(WRP1y_END+1) x 0x800 - 1] (included).

Would that be clearer? I don't know.

  • \$\begingroup\$ Thanks! It turns out I was reading at the wrong place and missed that paragraph altogether. It is actually very clear now \$\endgroup\$ – Bitgamma May 18 '18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.