probe-rs / flash-algorithm-template

A template for writing CMSIS-Pack flash algorithms in Rust
Apache License 2.0
10 stars 7 forks source link

Panic - "Not all sectors were erased" while the flash has been wiped? #4

Closed huming2207 closed 1 year ago

huming2207 commented 1 year ago

Hi all,

Firstly thanks for this great work! I've finally found something much more organized than CMSIS-Pack's flashing algorithm! I'm trying to implement a flash algorithm for STM32WLJC. Here's the implementation: https://github.com/huming2207/stm32wle5-flash-algo/blob/master/src/main.rs

Then I tried running RUST_BACKTRACE=1 cargo run, here's what I've got:

Test: Erasing sectorwise and writing two pages ...
Message: Init
Message: Erase sector addr:0x8000000
Message: Erase sector addr:0x8000800
thread 'main' panicked at 'Not all sectors were erased', /home/hu/.cargo/registry/src/github.com-1ecc6299db9ec823/target-gen-0.16.0/src/commands/test.rs:99:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:65:14
   2: target_gen::commands::test::cmd_test
   3: target_gen::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

However, if I use pyocd cmd and run rw 0x08000000 128, here's what I've got:

pyocd> rw 0x08000000 4096
08000000:  ffffffff ffffffff ffffffff ffffffff    |................|
08000010:  ffffffff ffffffff ffffffff ffffffff    |................|
08000020:  ffffffff ffffffff ffffffff ffffffff    |................|
08000030:  ffffffff ffffffff ffffffff ffffffff    |................|
08000040:  ffffffff ffffffff ffffffff ffffffff    |................|
08000050:  ffffffff ffffffff ffffffff ffffffff    |................|
08000060:  ffffffff ffffffff ffffffff ffffffff    |................|
08000070:  ffffffff ffffffff ffffffff ffffffff    |................|
...
08000e40:  ffffffff ffffffff ffffffff ffffffff    |................|
08000e50:  ffffffff ffffffff ffffffff ffffffff    |................|
08000e60:  ffffffff ffffffff ffffffff ffffffff    |................|
08000e70:  ffffffff ffffffff ffffffff ffffffff    |................|
08000e80:  ffffffff ffffffff ffffffff ffffffff    |................|
08000e90:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ea0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000eb0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ec0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ed0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ee0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ef0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f00:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f10:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f20:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f30:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f40:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f50:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f60:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f70:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f80:  ffffffff ffffffff ffffffff ffffffff    |................|
08000f90:  ffffffff ffffffff ffffffff ffffffff    |................|
08000fa0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000fb0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000fc0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000fd0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000fe0:  ffffffff ffffffff ffffffff ffffffff    |................|
08000ff0:  ffffffff ffffffff ffffffff ffffffff    |................|

As you see, the flash has been emptied.

I've also checked the target-gen 's code and here's something that made me a bit confused: https://github.com/probe-rs/probe-rs/blob/dc5eb11ba23a5abfd1dd734385b93c7edd60f490/target-gen/src/commands/test.rs#L98

I'm not sure why it's hardcoded to 0x0? I think it probably should be to whichever the microcontroller's flash base address is. For my STM32WLE5JC, it should be 0x08000000. May I ask, am I right? Have I misinterpreted something?

Regards, Jackson

Yatekii commented 1 year ago

Hey! Thanks for the nice words! Yes, this is indeed wrong! I was lazy in the first pass of the target-gen PR and forgot to fix it later on ... It should, as you suggested, use the base address and the correct default value for the flash :)

huming2207 commented 1 year ago

Hey! Thanks for the nice words! Yes, this is indeed wrong! I was lazy in the first pass of the target-gen PR and forgot to fix it later on ... It should, as you suggested, use the base address and the correct default value for the flash :)

Understood, thanks for your reply! I will submit a PR later tonight or tomorrow to probe-rs repo and fix this.

Yatekii commented 1 year ago

Thank you!

huming2207 commented 1 year ago

@Yatekii done, it's working:

with default (debug?) build:

Test: Erasing sectorwise and writing two pages ...
Message: Init
Message: Erase sector addr:0x8000000
Message: Erase sector addr:0x8000800
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 335.871133ms
Test: Erasing the entire chip and writing two pages ...
Message: Init
Message: Erase All
Finished erasing in 70.769179ms
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 335.360992ms
Test: Erasing sectorwise and writing two pages double buffered ...
Message: Init
Message: Erase sector addr:0x8000000
Message: Erase sector addr:0x8000800
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 143.421234ms
Finished in 7.168853657s

with release build:

Test: Erasing sectorwise and writing two pages ...
Message: Init
Message: Erase sector addr:0x8000000
Message: Erase sector addr:0x8000800
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 336.054229ms
Test: Erasing the entire chip and writing two pages ...
Message: Init
Message: Erase All
Finished erasing in 70.864407ms
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 334.405652ms
Test: Erasing sectorwise and writing two pages double buffered ...
Message: Init
Message: Erase sector addr:0x8000000
Message: Erase sector addr:0x8000800
Message: Init
Message: Program Page addr:134217728 size:1024
Message: Program Page addr:134218752 size:1024
Finished programming in 142.896715ms
Finished in 5.038007484s
huming2207 commented 1 year ago

@Yatekii please check here: https://github.com/probe-rs/probe-rs/pull/1542

I will close this issue thread for now. Feel free to open it if necessary.