Skip to content

Flash - Bad LengthTooLong check #101

@usbalbin

Description

@usbalbin
    /// 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()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions