Skip to main content
YIshi.1
Associate II
Oct 22, 2026 1:35 AM UTC
Question

Erasing internal memory sectors

Title: STM32L431 debug launch fails every other time depending on odd/even number of Flash pages

Hello,

I would like to report a reproducible issue with STM32CubeIDE and an STM32L431 device.

Environment

  • MCU: STM32L431CBT6

  • STM32CubeIDE: Version 2.2.0

  • Build: 29186_20260626_0934

  • Debugger: ST-LINK / SWD

  • Flash page size: 2 KB

Symptom

For a long time, I had an intermittent problem when starting a debug session.

Approximately every second debug launch failed.

Depending on the failed attempt, I observed one of the following:

  • The program appeared not to be loaded correctly.

  • The CPU ended up somewhere in System Memory, for example at an address around 0x1FFFxxxx.

  • The program started, but did not stop at the configured breakpoint.

  • In some failed sessions, peripheral registers could no longer be read from the debugger.

Initially, I suspected BOOT0, reset timing, SWD speed, or hardware noise.

However, the same physical PCB worked reliably when I programmed another firmware project into exactly the same MCU.

This led me to compare the Flash programming size.

Reproduction result

I found a very clear correlation with the number of Flash pages erased/programmed.

When CubeIDE reported:

Erasing internal memory sectors [0 22]

this corresponds to 23 Flash pages.

With this image, the debug launch failed approximately every second time.

I also tested another build:

Erasing internal memory sectors [0 26]

This corresponds to 27 Flash pages.

Again, approximately every second debug launch failed.

However, after completely removing power and powering the board again, the application itself started normally every time. Therefore, the programmed Flash contents appeared to be valid, while the debug-start sequence immediately after programming was not.

On the other hand, with:

Erasing internal memory sectors [0 25]

which corresponds to 26 Flash pages, the problem disappeared.

Workaround

I then modified the linker script so that the Flash image is padded to a 4 KB boundary.

Since the STM32L431 Flash page size is 2 KB, this guarantees that the programmed image always occupies an even number of Flash pages.

I added the following after the .data section:

__flash_image_end = LOADADDR(.data) + SIZEOF(.data);

.flash_pad __flash_image_end (READONLY) :
{
FILL(0xFFFFFFFF)
BYTE(0x00)
. = ALIGN(0x1000);
} >FLASH

After rebuilding, CubeIDE reported:

Erasing internal memory sectors [0 27]

which corresponds to 28 Flash pages.

Since applying this workaround, every debug session has stopped correctly at the breakpoint. I have not reproduced the previous every-other-launch failure.

So far, my observations are:

23 pages (odd)  -> intermittent debug failure
26 pages (even) -> no failure
27 pages (odd) -> intermittent debug failure
28 pages (even) -> no failure

This behavior appears strongly related to the odd/even number of programmed Flash pages.

Request / suggestion

Could ST please investigate the Flash programming / debug-start sequence for STM32L4 devices, especially the handling of the Flash empty state / FLASH_SR.PEMPTY after programming?

The linker padding above is only a workaround. Ideally, this should be fixed in the Flash loader / STM32CubeProgrammer side so that application size does not affect whether the MCU starts correctly under the debugger.

As an interim workaround, would it also be possible for STM32CubeIDE to automatically generate a linker script that pads the Flash load image to an even number of Flash pages for affected STM32L4 devices?

For STM32L431, for example, aligning the final Flash image to a 4 KB boundary would ensure an even number of 2 KB Flash pages automatically.

This would prevent the problem from unexpectedly returning whenever a small source-code change causes the binary to cross from an even number of Flash pages to an odd number.

Possibly related reports

I found several existing ST Community reports that appear to describe the same or closely related PEMPTY / System Memory boot behavior:

  1. “STM32L4 only boots every other flash – CubeProgrammer PEMPTY bug with odd number of flash pages”
    This report is especially similar: the issue occurs every other programming attempt, depends on an odd/even number of Flash pages, and the failed boot enters System Memory with PEMPTY set.

  2. “Flash Loader resets State Register incorrectly.”
    This report also describes failure with an odd number of Flash sectors and points to incorrect handling of the PEMPTY bit by the Flash loader.

  3. “Possible bug report: PEMPTY Flag Issue with STM32L431CCT6 in STM32CubeIDE v1.19”
    This is particularly relevant because it involves an STM32L431 and intermittent boot into System Memory after programming.

  4. “Bizarre bootloader invocation every other debug session”
    This also describes an STM32L431 unexpectedly entering the bootloader every other debug session.

There is also an older ST Community report in which ST support identified incorrect clearing of the PEMPTY flag by Flash loaders as the cause of intermittent boot into System Memory.

Considering these previous reports and the reproducible odd/even page behavior above, this appears likely to be a Flash loader / PEMPTY handling issue rather than a hardware BOOT0 problem.

I can provide additional logs or perform further tests if required.

6 replies

ST Technical Moderator
Oct 22, 2026 9:26 AM UTC

Hi ​@YIshi.1 
Thank you for the analysis and identifying correlations with number of Flash pages. 

Could you share a minimal firmware project that reproduces the issue on your board? We can then test it on an STM32L431 reference board? Can you also check resulting section placement and image size in the map file?

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
YIshi.1
YIshi.1Author
Associate II
Oct 22, 2026 9:27 PM UTC

Hi FBL,

Thank you for your response.

Unfortunately, I cannot share the complete firmware source because it contains proprietary application code.

However, I reproduced the issue again and prepared two map-file results: one successful build and one failing build. I extracted only the section placement and Flash image size information relevant to this issue and removed application-specific symbols, source paths, function names, and variable names.

The results are:

  • Successful build:

    • Flash load image size: 53,120 bytes

    • 26 Flash pages

    • CubeIDE erase range: sectors [0 25]

  • Failing build:

    • Flash load image size: 54,248 bytes

    • 27 Flash pages

    • CubeIDE erase range: sectors [0 26]

    • Debug launch fails approximately every other attempt

The difference between the two images is 1,128 bytes (0x468), mainly in the .text section.

The vector table placement and .data size are identical in both builds.

I also confirmed that when I pad the Flash load image to a 4-KiB boundary, CubeIDE programs 28 pages ([0 27]) and the debug launch succeeds repeatedly.

I have attached a sanitized summary of the two map files containing the relevant section placement and image-size information.

If useful, I can also perform additional tests with specific image sizes or linker placements requested by ST.

YIshi.1
YIshi.1Author
Associate II
Oct 22, 2026 9:33 PM UTC

実質、AIに解析をお願いし、機密情報を削除し、という作業を依頼して出したものです。英文も全部AIがやってくれています。長年、この問題(うまく行かない、行くようになる問題)に悩まされ、2回1回は失敗するのは仕方ない、としていたのですが、去年の段階でも解決できなかったのですが、今回、AIの進歩と、うまく行く例と、行かない例がたまたまできて、その比較から出せた結論です。解析は全てAIがやってくれているので、そこはご容赦ください。
問題は、過去もoot0ピンが、と言われると、その部分をハードチェックしなくては行けなくて、製品にする際、そこが気になっていたので調べていたにすぎません。Boot0は指定通りに接続しており、問題ないことがわかってホッとしました。すみません、これは日本語です。必要に応じて、翻訳してお読みください。

Translation with Google translator:

Essentially, I tasked an AI with performing the analysis and removing any confidential information. The English text was also entirely generated by the AI. I have long struggled with this issue—where things sometimes work and sometimes don’t—and had come to accept that a 50% failure rate was simply unavoidable. Although I couldn't resolve it even as of last year, recent advancements in AI, combined with the availability of both successful and unsuccessful test cases, allowed me to draw a conclusion by comparing the two. Please note that the analysis was conducted entirely by the AI.
The concern arose because, in the past, questions were raised regarding the "BOOT0" pin, which necessitated a thorough hardware check. I investigated this specifically because it was a point of concern for the final product. I am relieved to confirm that the BOOT0 pin is connected exactly as specified, and there are no issues there. Apologies—this part is in Japanese; please feel free to translate it if necessary.

ST Technical Moderator
Oct 23, 2026 5:50 PM UTC

Hi ​@YIshi.1 

Thank you for the detailed test results. The page count is helpful, but we cannot yet confirm that odd page count or PEMPTY flag is the root cause. It seems similar to an old thread Intermittent Break at address "0x1fff51f4" with no debug... | Community

Could you please try reproducing the behavior with a minimal STM32L431 CubeL4 example, using linker padding to create 26, 27 and 28 page images while keeping the application code unchanged? 

Please send further technical information in English so we can review it efficiently.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
YIshi.1
YIshi.1Author
Associate II
Oct 29, 2026 5:55 AM UTC

I have prepared a minimal test project that reproduces the issue without including any confidential application code.

The project contains only dummy variables and simple unsigned integer calculations. The linker script fills the unused Flash area with dummy data, placing the end of the Flash image at 0x08002800, which is a five-page span from 0x08000000.

When I repeatedly start and stop debugging with programming enabled, the debug launch fails every other attempt.

The relationship between the failure and Flash page boundaries or an odd number of pages has not yet been confirmed. However, the issue is reproducible with this test project.

I am providing the project for your investigation. Please let me know if you need additional logs or information about the test environment.

ST Technical Moderator
Nov 3, 2026 7:58 PM UTC

Hi ​@YIshi.1 

An internal ticket is submitted to dedicated team : CDM0067183 (for internal reference)  

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL