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 - Bad LengthTooLong check #101

Open
usbalbin opened this issue Dec 15, 2023 · 0 comments
Open

Flash - Bad LengthTooLong check #101

usbalbin opened this issue Dec 15, 2023 · 0 comments

Comments

@usbalbin
Copy link
Contributor

usbalbin commented Dec 15, 2023

    /// Retrieve a slice of data from `FLASH_START + offset`
    pub fn read(&self, offset: u32, length: usize) -> Result<&[u8]> {
        self.valid_address(offset)?;

        if offset + length as u32 > self.flash_sz.kbytes() {
            return Err(Error::LengthTooLong);
        }

I do not believe that check takes dual bank flash into account (RM)

From what I can tell from the table in the RM, the address offset 0x0004_0000 (Page 0 on Bank 2) should be valid for all devices supporting dual bank and having it enabled no matter 128, 256 or 512kB. However trying to read from that on an 256kB device gives Err(Error::LengthTooLong)

#![no_main]
#![no_std]

use cortex_m_rt::entry;
use hal::prelude::*;
use hal::stm32;
use stm32g4xx_hal as hal;
use hal::flash::FlashSize;
use hal::flash::FlashExt;
extern crate cortex_m_rt as rt;

#[macro_use]
mod utils;

use utils::logger::println;

#[entry]
fn main() -> ! {
    utils::logger::init();

    let dp = stm32::Peripherals::take().expect("cannot take peripherals");

    let mut flash = dp.FLASH.constrain();
    let is_dual_bank = flash.is_dual_bank();

    println!("Is dual bank: {}", is_dual_bank);

    let flash_writer = flash.writer::<2048>(FlashSize::Sz256K);

    let bytes = flash_writer.read(0x0000_0000, 4).unwrap();
    println!("Bank 1 first 4 bytes: {:?}", bytes);

    if is_dual_bank {
        let bytes = flash_writer.read(0x0004_0000, 4).unwrap(); // BOOM <-- Error::LengthTooLong 
        println!("Bank 2 first 4 bytes: {:?}", bytes);
    }

    loop {
        cortex_m::asm::nop()
    }
}
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