#![no_main]
#![no_std]
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use log::info;
use stm32h7xx_hal::{flash::Bank, pac, prelude::*};
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let _pwrcfg = example_power!(pwr).freeze();
info!("");
info!("stm32h7xx-hal example - FLASH erase, write and read");
info!("");
let mut flash = dp.FLASH.constrain();
for i in 1..8 {
flash.erase_sector(Bank::UserBank1, i).unwrap();
}
flash.erase_bank(Bank::UserBank2).unwrap();
let mut buff = [0u8; 1024 * 15];
let mut count = 0;
for b in buff.iter_mut() {
*b = count;
count += 1;
if count >= 100 {
count = 0;
}
}
flash.write_sector(Bank::UserBank1, 1, 0, &buff).unwrap();
let mut read = [0u8; 1024 * 15];
flash.read_sector(Bank::UserBank1, 1, 0, &mut read).unwrap();
for i in 0..read.len() {
assert_eq!(read[i], buff[i]);
}
flash.write_sector(Bank::UserBank2, 0, 256, &buff).unwrap();
let mut read = [0u8; 1024 * 15];
flash
.read_sector(Bank::UserBank2, 0, 256, &mut read)
.unwrap();
for i in 0..read.len() {
assert_eq!(read[i], buff[i]);
}
info!("Successfully erased, written and read back flash data");
loop {
cortex_m::asm::nop()
}
}