0

I'm fairly new to Rust and for an internship assignment I am trying to store data in the flash memory of a NUCLEO-STM32F746ZG board with the Rust programming language. To do this I am using the stm32f7xx_hal crate and the following code (note: this code might not be optimised but I'm still trying to get data in the flash first).

#![no_std]
#![no_main]
use core::panic::PanicInfo;
use cortex_m::asm;
use stm32f7xx_hal::{self, rcc::RccExt};

#[no_mangle]
fn main() {
    for _i in 0..500000 {
        asm::nop()
    }

    // Define the address and data to write
    let start_of_address: usize = 0x080C_0000; // Start of main
    let data = "Wachtwoord".as_bytes(); // Data to write
    let sector_number = 7; // Sector in which the address lays

    // Get access to the device peripherals
    let dp = match stm32f7xx_hal::pac::Peripherals::take() {
        Some(dp) => dp,
        None => panic!("Failed to take peripherals"),
    };

    let _rcc = dp.RCC.constrain();

    let mut flash = stm32f7xx_hal::flash::Flash::new(dp.FLASH);

    flash.unlock();

    match flash.blocking_erase_sector(sector_number) {
        Ok(_) => {}
        Err(e) => panic!("Failed Block_erase_sector: {:?}", e),
    }

    match flash.blocking_program(start_of_address, data) {
        Ok(_) => {}
        Err(e) => panic!("Failed program data: {:?}", e),
    }

    flash.lock();
}

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

I would expect this to work but looking at the memory of the board, it is empty.

iteunesen
  • 1
  • 2
  • 2
    According to [the docs](https://docs.rs/stm32f7xx-hal/latest/stm32f7xx_hal/flash/struct.Flash.html#method.program), you should wait on the result of `program` (like you do for `erase_sector`) or use `blocking_program`. – Jmb Apr 11 '23 at 13:54

0 Answers0