Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash sector erase does not work properly on STM32U5 #3561

Open
williams-one opened this issue Nov 22, 2024 · 0 comments
Open

Flash sector erase does not work properly on STM32U5 #3561

williams-one opened this issue Nov 22, 2024 · 0 comments

Comments

@williams-one
Copy link
Contributor

I'm testing flash sector erase on a STM32U5G and it doesn't work properly in all conditions.
I have tried to analyze the issue and here are my findings. If you want, I'm more than happy to work on a patch 😄

I think that this could be a summary of all the concurrent causes.

First, all U5 are dual bank but there is no selection of bank register (BKER) in blocking_erase_sector

pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
#[cfg(feature = "trustzone-secure")]
pac::FLASH.seccr().modify(|w| {
w.set_per(pac::flash::vals::SeccrPer::B_0X1);
w.set_pnb(sector.index_in_bank)
});
#[cfg(not(feature = "trustzone-secure"))]
pac::FLASH.nscr().modify(|w| {
w.set_per(pac::flash::vals::NscrPer::B_0X1);
w.set_pnb(sector.index_in_bank)
});

Then there is a problem with page size. According to ST specs
image

But the erase_size in the metapac is set to 16384, while I think it should be 8192 to match the size of the page
https://github.com/embassy-rs/stm32-data-generated/blob/5373e57121b8300a3c2f03fb0408d857f6622607/stm32-metapac/src/chips/stm32u5g9zj/metadata.rs#L8-L18

This has been fixed for some variants of the U5 family but not for all
https://github.com/embassy-rs/stm32-data/blob/55b491ee982e52f80a21276a0ccbde4907982b5d/stm32-data-gen/src/memory.rs#L352-L353

Finally there is also another problem in the function for setting the PNB (page number) register here
https://github.com/embassy-rs/stm32-data-generated/blob/5373e57121b8300a3c2f03fb0408d857f6622607/stm32-metapac/src/peripherals/flash_u5.rs#L506-L516

According to ST specs PNB should be 8 bit, so I think the masking is wrong. It should probably 0xFF instead of 0x7F
image

This is not a problem for devices which has 2MB of flash or less since the maximum page index is 127, but it is a problem for devices with 4MB of flash which have a maximum page index of 255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant