|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +use cortex_m::Peripherals; |
| 5 | +use panic_halt as _; |
| 6 | +use stm32f0xx_hal as hal; |
| 7 | + |
| 8 | +use crate::hal::{ |
| 9 | + pac::{self, flash}, |
| 10 | + prelude::*, |
| 11 | +}; |
| 12 | + |
| 13 | +use cortex_m_rt::entry; |
| 14 | +use embedded_storage::nor_flash::NorFlash; |
| 15 | +use stm32f0xx_hal::flash::FlashExt; |
| 16 | +use stm32f0xx_hal::prelude::_stm32f0xx_hal_rcc_RccExt; |
| 17 | + |
| 18 | +#[entry] |
| 19 | +fn main() -> ! { |
| 20 | + if let Some(mut p) = pac::Peripherals::take() { |
| 21 | + let _ = p.RCC.configure().freeze(&mut p.FLASH); |
| 22 | + |
| 23 | + const OFFSET_START: u32 = 32 * 1024; |
| 24 | + const OFFSET_END: u32 = 33 * 1024; |
| 25 | + // Unlock flash before writing |
| 26 | + let mut unlocked_flash = p.FLASH.unlocked(); |
| 27 | + |
| 28 | + // All examples use the first 16K of flash for the program so we use the first page after that |
| 29 | + NorFlash::erase(&mut unlocked_flash, OFFSET_START, OFFSET_END).unwrap(); |
| 30 | + |
| 31 | + // Write some data to the start of that page |
| 32 | + let write_data = [0xC0_u8, 0xFF_u8, 0xEE_u8, 0x00_u8]; |
| 33 | + NorFlash::write(&mut unlocked_flash, OFFSET_START, &write_data).unwrap(); |
| 34 | + |
| 35 | + // Lock flash by dropping it |
| 36 | + drop(unlocked_flash); |
| 37 | + |
| 38 | + // Read back the slice from flash |
| 39 | + let read_data = unsafe { |
| 40 | + core::slice::from_raw_parts( |
| 41 | + (p.FLASH.address() + 16 * 1024) as *const u8, |
| 42 | + write_data.len(), |
| 43 | + ) |
| 44 | + }; |
| 45 | + |
| 46 | + assert_eq!(write_data, *read_data); |
| 47 | + } |
| 48 | + loop { |
| 49 | + continue; |
| 50 | + } |
| 51 | +} |
0 commit comments