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

Reading internal flash triggers HardFault #463

Closed
HaoboGu opened this issue Oct 11, 2023 · 7 comments
Closed

Reading internal flash triggers HardFault #463

HaoboGu opened this issue Oct 11, 2023 · 7 comments

Comments

@HaoboGu
Copy link

HaoboGu commented Oct 11, 2023

Hello, I'm using stm32h7b0 mcu and trying to emulate eeprom using mcu's flash and stm32h7xx-hal::flash. Here is my testing code for flash write/read:

        let (mut flash, _) = dp.FLASH.split();
        let mut bytes = [8u8;16];
        const FLASH_SECTOR_15_ADDR: u32 = 15 * 8192;
        let sector = flash.sector(FLASH_SECTOR_15_ADDR as usize).unwrap();
        info!("{}", sector.offset);
        if sector.contains(FLASH_SECTOR_15_ADDR as usize) {
            embedded_storage::nor_flash::ReadNorFlash::read(&mut flash, FLASH_SECTOR_15_ADDR, &mut bytes).unwrap();
        }

But the read function fails and the program jumps to HardFault
image

Here is the callstack:

image
@richardeoin
Copy link
Member

Hi! Thanks for reporting. The stm32h7b0 is a device with only 128KB of flash, divided into 16 sectors of 8KB each. You're trying to read from the last of those sectors. In theory that should be possible.

Could you confirm:

  • That you're using the --features=stm32h7b0 feature flag to build your test code.
  • If possible, the value returned by stm32h7xx_hal::signature::FlashSize::bytes() on your stm32h7b0.

@HaoboGu
Copy link
Author

HaoboGu commented Oct 12, 2023

@richardeoin

I checked my config, here is how i introduce stm32h7xx-hal in Cargo.toml:

stm32h7xx-hal = { version = "0.14.0", features = [
    "stm32h7b0",
    "rt",
    "log-rtt",
    "usb_hs",
] }

The value returned by stm32h7xx_hal::signature::FlashSize::bytes() is 131072, aka 128kb:
image

I've noticed there is a new version of stm32h7xx-hal published at crates.io, I also tried it, but my code breaks at:

note: required by a bound in `gpio::convert::<impl stm32h7xx_hal::gpio::Pin<P, N, MODE>>::into_alternate`
   --> /Users/haobogu/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/stm32h7xx-hal-0.15.0/src/gpio/convert.rs:151:15
    |
147 |     pub fn into_alternate<const A: u8>(
    |            -------------- required by a bound in this associated function
...
151 |         Self: marker::IntoAf<A>,
    |               ^^^^^^^^^^^^^^^^^ required by this bound in `gpio::convert::<impl Pin<P, N, MODE>>::into_alternate`

error[E0277]: the trait bound `stm32h7xx_hal::gpio::Pin<'A', 12>: gpio::marker::IntoAf<10>` is not satisfied
   --> boards/stm32h7/src/main.rs:83:33
    |
83  |         let usb_dp = gpioa.pa12.into_alternate();
    |                                 ^^^^^^^^^^^^^^ the trait `gpio::marker::IntoAf<10>` is not implemented for `stm32h7xx_hal::gpio::Pin<'A', 12>`

@HaoboGu
Copy link
Author

HaoboGu commented Oct 12, 2023

I tried to erase the sector using:

let (mut flash, _) = dp.FLASH.split();
let mut unlocked = flash.unlocked();
unlocked.erase_sector(15).unwrap();

The program reported the following error:

panicked at boards/stm32h7/src/main.rs:101:35:
called `Result::unwrap()` on an `Err` value: EccDoubleDetection

It seems that some of my write operations didn't complete, and then an ECC error occured.

@richardeoin Does stm32h7xx-hal crate provide any methods to fix flash ECC error?

@mlamoore
Copy link
Contributor

mlamoore commented Oct 12, 2023 via email

@HaoboGu
Copy link
Author

HaoboGu commented Oct 12, 2023

I've reviewed my code. I use the following function to write the flash:

fn write_record(&mut self, record: EepromRecord) {
        match self
            .storage
            .write(STORAGE_START_ADDR + self.pos, &record.to_bytes())
        {
            Ok(_) => self.pos += 4,
            Err(_) => error!("Failed to write record to storage"),
        }
    }

after each write, the next write addr will be addr + 4. Is this right way to use the crate?

I believe I didn't write much data(about 300 bytes). So I don't know is the flash still fine. Erasing the sector with unlocked.erase_sector(15).unwrap();, reports EccDoubleDetection error. Does it mean the flash has been broken?

@richardeoin
Copy link
Member

Your report about USB is a separate issue. I've posted a fix in #464

@HaoboGu
Copy link
Author

HaoboGu commented Oct 27, 2023

Erasing the sector fixed the ECC error, closing this issue

@HaoboGu HaoboGu closed this as completed Oct 27, 2023
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

3 participants