Merge #755
755: Add support for flash and bootloader for F3, F7 and H7 r=matoushybl a=matoushybl Co-authored-by: Matous Hybl <hyblmatous@gmail.com>
This commit is contained in:
commit
7e774ff830
@ -14,7 +14,7 @@ The bootloader supports both internal and external flash by relying on the `embe
|
||||
The bootloader supports
|
||||
|
||||
* nRF52 with and without softdevice
|
||||
* STM32 L4, WB, WL, L1 and L0
|
||||
* STM32 L4, WB, WL, L1, L0, F3, F7 and H7
|
||||
|
||||
In general, the bootloader works on any platform that implements the `embedded-storage` traits for its internal flash, but may require custom initialization code to work.
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
authors = [
|
||||
"Ulf Lilleengen <lulf@redhat.com>",
|
||||
]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32"
|
||||
version = "0.1.0"
|
||||
description = "Bootloader for STM32 chips"
|
||||
|
@ -67,7 +67,7 @@ impl<const PAGE_SIZE: usize> BootLoader<PAGE_SIZE> {
|
||||
[(); <<F as FlashProvider>::ACTIVE as FlashConfig>::FLASH::ERASE_SIZE]:,
|
||||
{
|
||||
match self.boot.prepare_boot(flash) {
|
||||
Ok(_) => self.boot.boot_address(),
|
||||
Ok(_) => embassy_stm32::flash::FLASH_BASE + self.boot.boot_address(),
|
||||
Err(_) => panic!("boot prepare error!"),
|
||||
}
|
||||
}
|
||||
|
104
embassy-stm32/src/flash/f3.rs
Normal file
104
embassy-stm32/src/flash/f3.rs
Normal file
@ -0,0 +1,104 @@
|
||||
use core::convert::TryInto;
|
||||
use core::ptr::write_volatile;
|
||||
|
||||
use crate::flash::Error;
|
||||
use crate::pac;
|
||||
|
||||
pub(crate) unsafe fn lock() {
|
||||
pac::FLASH.cr().modify(|w| w.set_lock(true));
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unlock() {
|
||||
pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123));
|
||||
pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB));
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> {
|
||||
pac::FLASH.cr().write(|w| w.set_pg(true));
|
||||
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(2) {
|
||||
write_volatile(
|
||||
offset as *mut u16,
|
||||
u16::from_le_bytes(chunk[0..2].try_into().unwrap()),
|
||||
);
|
||||
offset += chunk.len() as u32;
|
||||
|
||||
ret = blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
pac::FLASH.cr().write(|w| w.set_pg(false));
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
|
||||
for page in (from..to).step_by(super::ERASE_SIZE) {
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_per(true);
|
||||
});
|
||||
|
||||
pac::FLASH.ar().write(|w| w.set_far(page));
|
||||
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_strt(true);
|
||||
});
|
||||
|
||||
let mut ret: Result<(), Error> = blocking_wait_ready();
|
||||
|
||||
if !pac::FLASH.sr().read().eop() {
|
||||
trace!("FLASH: EOP not set");
|
||||
ret = Err(Error::Prog);
|
||||
} else {
|
||||
pac::FLASH.sr().write(|w| w.set_eop(true));
|
||||
}
|
||||
|
||||
pac::FLASH.cr().modify(|w| w.set_per(false));
|
||||
|
||||
clear_all_err();
|
||||
if ret.is_err() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn clear_all_err() {
|
||||
pac::FLASH.sr().modify(|w| {
|
||||
if w.pgerr() {
|
||||
w.set_pgerr(true);
|
||||
}
|
||||
if w.wrprterr() {
|
||||
w.set_wrprterr(true);
|
||||
}
|
||||
if w.eop() {
|
||||
w.set_eop(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_wait_ready() -> Result<(), Error> {
|
||||
loop {
|
||||
let sr = pac::FLASH.sr().read();
|
||||
|
||||
if !sr.bsy() {
|
||||
if sr.wrprterr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
if sr.pgerr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
138
embassy-stm32/src/flash/f7.rs
Normal file
138
embassy-stm32/src/flash/f7.rs
Normal file
@ -0,0 +1,138 @@
|
||||
use core::convert::TryInto;
|
||||
use core::ptr::write_volatile;
|
||||
|
||||
use atomic_polyfill::{fence, Ordering};
|
||||
|
||||
use crate::flash::Error;
|
||||
use crate::pac;
|
||||
|
||||
pub(crate) unsafe fn lock() {
|
||||
pac::FLASH.cr().modify(|w| w.set_lock(true));
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unlock() {
|
||||
pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123));
|
||||
pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB));
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> {
|
||||
pac::FLASH.cr().write(|w| {
|
||||
w.set_pg(true);
|
||||
w.set_psize(pac::flash::vals::Psize::PSIZE32);
|
||||
});
|
||||
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(super::WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
);
|
||||
offset += val.len() as u32;
|
||||
|
||||
// prevents parallelism errors
|
||||
fence(Ordering::SeqCst);
|
||||
}
|
||||
|
||||
ret = blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
pac::FLASH.cr().write(|w| w.set_pg(false));
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
|
||||
let start_sector = if from >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 {
|
||||
4 + (from - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32
|
||||
} else {
|
||||
(from - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8)
|
||||
};
|
||||
|
||||
let end_sector = if to >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 {
|
||||
4 + (to - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32
|
||||
} else {
|
||||
(to - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8)
|
||||
};
|
||||
|
||||
for sector in start_sector..end_sector {
|
||||
let ret = erase_sector(sector as u8);
|
||||
if ret.is_err() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn erase_sector(sector: u8) -> Result<(), Error> {
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_ser(true);
|
||||
w.set_snb(sector)
|
||||
});
|
||||
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_strt(true);
|
||||
});
|
||||
|
||||
let ret: Result<(), Error> = blocking_wait_ready();
|
||||
|
||||
pac::FLASH.cr().modify(|w| w.set_ser(false));
|
||||
|
||||
clear_all_err();
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn clear_all_err() {
|
||||
pac::FLASH.sr().modify(|w| {
|
||||
if w.erserr() {
|
||||
w.set_erserr(true);
|
||||
}
|
||||
if w.pgperr() {
|
||||
w.set_pgperr(true);
|
||||
}
|
||||
if w.pgaerr() {
|
||||
w.set_pgaerr(true);
|
||||
}
|
||||
if w.wrperr() {
|
||||
w.set_wrperr(true);
|
||||
}
|
||||
if w.eop() {
|
||||
w.set_eop(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_wait_ready() -> Result<(), Error> {
|
||||
loop {
|
||||
let sr = pac::FLASH.sr().read();
|
||||
|
||||
if !sr.bsy() {
|
||||
if sr.erserr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
|
||||
if sr.pgperr() {
|
||||
return Err(Error::Parallelism);
|
||||
}
|
||||
|
||||
if sr.pgaerr() {
|
||||
return Err(Error::Unaligned);
|
||||
}
|
||||
|
||||
if sr.wrperr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
202
embassy-stm32/src/flash/h7.rs
Normal file
202
embassy-stm32/src/flash/h7.rs
Normal file
@ -0,0 +1,202 @@
|
||||
use core::convert::TryInto;
|
||||
use core::ptr::write_volatile;
|
||||
|
||||
use crate::flash::Error;
|
||||
use crate::pac;
|
||||
|
||||
const SECOND_BANK_OFFSET: usize = 0x0010_0000;
|
||||
|
||||
const fn is_dual_bank() -> bool {
|
||||
super::FLASH_SIZE / 2 > super::ERASE_SIZE
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn lock() {
|
||||
pac::FLASH.bank(0).cr().modify(|w| w.set_lock(true));
|
||||
if is_dual_bank() {
|
||||
pac::FLASH.bank(1).cr().modify(|w| w.set_lock(true));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unlock() {
|
||||
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0x4567_0123));
|
||||
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
||||
|
||||
if is_dual_bank() {
|
||||
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0x4567_0123));
|
||||
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> {
|
||||
let bank = if !is_dual_bank() || (offset - super::FLASH_BASE as u32) < SECOND_BANK_OFFSET as u32
|
||||
{
|
||||
pac::FLASH.bank(0)
|
||||
} else {
|
||||
pac::FLASH.bank(1)
|
||||
};
|
||||
|
||||
bank.cr().write(|w| {
|
||||
w.set_pg(true);
|
||||
w.set_psize(2); // 32 bits at once
|
||||
});
|
||||
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
'outer: for chunk in buf.chunks(super::WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
trace!("Writing at {:x}", offset);
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
);
|
||||
offset += val.len() as u32;
|
||||
|
||||
ret = blocking_wait_ready(bank);
|
||||
bank.sr().modify(|w| {
|
||||
if w.eop() {
|
||||
w.set_eop(true);
|
||||
}
|
||||
});
|
||||
if ret.is_err() {
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
bank.cr().write(|w| w.set_pg(false));
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
|
||||
let from = from - super::FLASH_BASE as u32;
|
||||
let to = to - super::FLASH_BASE as u32;
|
||||
|
||||
let bank_size = (super::FLASH_SIZE / 2) as u32;
|
||||
|
||||
let (bank, start, end) = if to <= bank_size {
|
||||
let start_sector = from / super::ERASE_SIZE as u32;
|
||||
let end_sector = to / super::ERASE_SIZE as u32;
|
||||
(0, start_sector, end_sector)
|
||||
} else if from >= SECOND_BANK_OFFSET as u32 && to <= (SECOND_BANK_OFFSET as u32 + bank_size) {
|
||||
let start_sector = (from - SECOND_BANK_OFFSET as u32) / super::ERASE_SIZE as u32;
|
||||
let end_sector = (to - SECOND_BANK_OFFSET as u32) / super::ERASE_SIZE as u32;
|
||||
(1, start_sector, end_sector)
|
||||
} else {
|
||||
error!("Attempting to write outside of defined sectors");
|
||||
return Err(Error::Unaligned);
|
||||
};
|
||||
|
||||
trace!("Erasing bank {}, sectors from {} to {}", bank, start, end);
|
||||
for sector in start..end {
|
||||
let ret = erase_sector(pac::FLASH.bank(bank), sector as u8);
|
||||
if ret.is_err() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn erase_sector(bank: pac::flash::Bank, sector: u8) -> Result<(), Error> {
|
||||
bank.cr().modify(|w| {
|
||||
w.set_ser(true);
|
||||
w.set_snb(sector)
|
||||
});
|
||||
|
||||
bank.cr().modify(|w| {
|
||||
w.set_start(true);
|
||||
});
|
||||
|
||||
let ret: Result<(), Error> = blocking_wait_ready(bank);
|
||||
|
||||
bank.cr().modify(|w| w.set_ser(false));
|
||||
|
||||
bank_clear_all_err(bank);
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn clear_all_err() {
|
||||
bank_clear_all_err(pac::FLASH.bank(0));
|
||||
bank_clear_all_err(pac::FLASH.bank(1));
|
||||
}
|
||||
|
||||
unsafe fn bank_clear_all_err(bank: pac::flash::Bank) {
|
||||
bank.sr().modify(|w| {
|
||||
if w.wrperr() {
|
||||
w.set_wrperr(true);
|
||||
}
|
||||
if w.pgserr() {
|
||||
w.set_pgserr(true);
|
||||
}
|
||||
if w.strberr() {
|
||||
// single address was written multiple times, can be ignored
|
||||
w.set_strberr(true);
|
||||
}
|
||||
if w.incerr() {
|
||||
// writing to a different address when programming 256 bit word was not finished
|
||||
w.set_incerr(true);
|
||||
}
|
||||
if w.operr() {
|
||||
w.set_operr(true);
|
||||
}
|
||||
if w.sneccerr1() {
|
||||
// single ECC error
|
||||
w.set_sneccerr1(true);
|
||||
}
|
||||
if w.dbeccerr() {
|
||||
// double ECC error
|
||||
w.set_dbeccerr(true);
|
||||
}
|
||||
if w.rdperr() {
|
||||
w.set_rdperr(true);
|
||||
}
|
||||
if w.rdserr() {
|
||||
w.set_rdserr(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_wait_ready(bank: pac::flash::Bank) -> Result<(), Error> {
|
||||
loop {
|
||||
let sr = bank.sr().read();
|
||||
|
||||
if !sr.bsy() && !sr.qw() {
|
||||
if sr.wrperr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
if sr.pgserr() {
|
||||
error!("pgserr");
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
if sr.incerr() {
|
||||
// writing to a different address when programming 256 bit word was not finished
|
||||
error!("incerr");
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
if sr.operr() {
|
||||
return Err(Error::Prog);
|
||||
}
|
||||
if sr.sneccerr1() {
|
||||
// single ECC error
|
||||
return Err(Error::Prog);
|
||||
}
|
||||
if sr.dbeccerr() {
|
||||
// double ECC error
|
||||
return Err(Error::Prog);
|
||||
}
|
||||
if sr.rdperr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
if sr.rdserr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
185
embassy-stm32/src/flash/l.rs
Normal file
185
embassy-stm32/src/flash/l.rs
Normal file
@ -0,0 +1,185 @@
|
||||
use core::convert::TryInto;
|
||||
use core::ptr::write_volatile;
|
||||
|
||||
use crate::flash::Error;
|
||||
use crate::pac;
|
||||
|
||||
pub(crate) unsafe fn lock() {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
pac::FLASH.cr().modify(|w| w.set_lock(true));
|
||||
|
||||
#[cfg(any(flash_l0))]
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_optlock(true);
|
||||
w.set_prglock(true);
|
||||
w.set_pelock(true);
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unlock() {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
{
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123));
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
||||
}
|
||||
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
{
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x89ABCDEF));
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x02030405));
|
||||
|
||||
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x8C9DAEBF));
|
||||
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x13141516));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
pac::FLASH.cr().write(|w| w.set_pg(true));
|
||||
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(super::WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
);
|
||||
offset += val.len() as u32;
|
||||
}
|
||||
|
||||
ret = blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
pac::FLASH.cr().write(|w| w.set_pg(false));
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
|
||||
for page in (from..to).step_by(super::ERASE_SIZE) {
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
{
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_erase(true);
|
||||
w.set_prog(true);
|
||||
});
|
||||
|
||||
write_volatile(page as *mut u32, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
{
|
||||
let idx = page / super::ERASE_SIZE as u32;
|
||||
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_per(true);
|
||||
w.set_pnb(idx as u8);
|
||||
#[cfg(any(flash_wl, flash_wb))]
|
||||
w.set_strt(true);
|
||||
#[cfg(any(flash_l4))]
|
||||
w.set_start(true);
|
||||
});
|
||||
}
|
||||
|
||||
let ret: Result<(), Error> = blocking_wait_ready();
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
pac::FLASH.cr().modify(|w| w.set_per(false));
|
||||
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_erase(false);
|
||||
w.set_prog(false);
|
||||
});
|
||||
|
||||
clear_all_err();
|
||||
if ret.is_err() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn clear_all_err() {
|
||||
pac::FLASH.sr().modify(|w| {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_l0))]
|
||||
if w.rderr() {
|
||||
w.set_rderr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.fasterr() {
|
||||
w.set_fasterr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.miserr() {
|
||||
w.set_miserr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.pgserr() {
|
||||
w.set_pgserr(true);
|
||||
}
|
||||
if w.sizerr() {
|
||||
w.set_sizerr(true);
|
||||
}
|
||||
if w.pgaerr() {
|
||||
w.set_pgaerr(true);
|
||||
}
|
||||
if w.wrperr() {
|
||||
w.set_wrperr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.progerr() {
|
||||
w.set_progerr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.operr() {
|
||||
w.set_operr(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn blocking_wait_ready() -> Result<(), Error> {
|
||||
loop {
|
||||
let sr = pac::FLASH.sr().read();
|
||||
|
||||
if !sr.bsy() {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.progerr() {
|
||||
return Err(Error::Prog);
|
||||
}
|
||||
|
||||
if sr.wrperr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
if sr.pgaerr() {
|
||||
return Err(Error::Unaligned);
|
||||
}
|
||||
|
||||
if sr.sizerr() {
|
||||
return Err(Error::Size);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.miserr() {
|
||||
return Err(Error::Miss);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.pgserr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,5 @@
|
||||
use crate::pac;
|
||||
use crate::peripherals::FLASH;
|
||||
use core::convert::TryInto;
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr::write_volatile;
|
||||
use embassy::util::Unborrow;
|
||||
use embassy_hal_common::unborrow;
|
||||
|
||||
@ -17,6 +14,12 @@ pub use crate::pac::FLASH_SIZE;
|
||||
pub use crate::pac::WRITE_SIZE;
|
||||
const FLASH_END: usize = FLASH_BASE + FLASH_SIZE;
|
||||
|
||||
#[cfg_attr(any(flash_wl, flash_wb, flash_l0, flash_l1, flash_l4), path = "l.rs")]
|
||||
#[cfg_attr(flash_f3, path = "f3.rs")]
|
||||
#[cfg_attr(flash_f7, path = "f7.rs")]
|
||||
#[cfg_attr(flash_h7, path = "h7.rs")]
|
||||
mod family;
|
||||
|
||||
pub struct Flash<'d> {
|
||||
_inner: FLASH,
|
||||
_phantom: PhantomData<&'d mut FLASH>,
|
||||
@ -33,37 +36,13 @@ impl<'d> Flash<'d> {
|
||||
|
||||
pub fn unlock(p: impl Unborrow<Target = FLASH>) -> Self {
|
||||
let flash = Self::new(p);
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123));
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
||||
}
|
||||
|
||||
#[cfg(any(flash_l0))]
|
||||
unsafe {
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x89ABCDEF));
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x02030405));
|
||||
|
||||
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x8C9DAEBF));
|
||||
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x13141516));
|
||||
}
|
||||
unsafe { family::unlock() };
|
||||
flash
|
||||
}
|
||||
|
||||
pub fn lock(&mut self) {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().modify(|w| w.set_lock(true));
|
||||
}
|
||||
|
||||
#[cfg(any(flash_l0))]
|
||||
unsafe {
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_optlock(true);
|
||||
w.set_prglock(true);
|
||||
w.set_pelock(true);
|
||||
});
|
||||
}
|
||||
unsafe { family::lock() };
|
||||
}
|
||||
|
||||
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
@ -89,36 +68,7 @@ impl<'d> Flash<'d> {
|
||||
|
||||
self.clear_all_err();
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().write(|w| w.set_pg(true))
|
||||
}
|
||||
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
unsafe {
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
);
|
||||
}
|
||||
offset += val.len() as u32;
|
||||
}
|
||||
|
||||
ret = self.blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().write(|w| w.set_pg(false))
|
||||
}
|
||||
|
||||
ret
|
||||
unsafe { family::blocking_write(offset, buf) }
|
||||
}
|
||||
|
||||
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
||||
@ -127,135 +77,17 @@ impl<'d> Flash<'d> {
|
||||
if to < from || to as usize > FLASH_END {
|
||||
return Err(Error::Size);
|
||||
}
|
||||
if from as usize % ERASE_SIZE != 0 || to as usize % ERASE_SIZE != 0 {
|
||||
if ((to - from) as usize % ERASE_SIZE) != 0 {
|
||||
return Err(Error::Unaligned);
|
||||
}
|
||||
|
||||
self.clear_all_err();
|
||||
|
||||
for page in (from..to).step_by(ERASE_SIZE) {
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
unsafe {
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_erase(true);
|
||||
w.set_prog(true);
|
||||
});
|
||||
|
||||
write_volatile(page as *mut u32, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
let idx = page / ERASE_SIZE as u32;
|
||||
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_per(true);
|
||||
w.set_pnb(idx as u8);
|
||||
#[cfg(any(flash_wl, flash_wb))]
|
||||
w.set_strt(true);
|
||||
#[cfg(any(flash_l4))]
|
||||
w.set_start(true);
|
||||
});
|
||||
}
|
||||
|
||||
let ret: Result<(), Error> = self.blocking_wait_ready();
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().modify(|w| w.set_per(false));
|
||||
}
|
||||
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
unsafe {
|
||||
pac::FLASH.pecr().modify(|w| {
|
||||
w.set_erase(false);
|
||||
w.set_prog(false);
|
||||
});
|
||||
}
|
||||
|
||||
self.clear_all_err();
|
||||
if ret.is_err() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn blocking_wait_ready(&self) -> Result<(), Error> {
|
||||
loop {
|
||||
let sr = unsafe { pac::FLASH.sr().read() };
|
||||
|
||||
if !sr.bsy() {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.progerr() {
|
||||
return Err(Error::Prog);
|
||||
}
|
||||
|
||||
if sr.wrperr() {
|
||||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
if sr.pgaerr() {
|
||||
return Err(Error::Unaligned);
|
||||
}
|
||||
|
||||
if sr.sizerr() {
|
||||
return Err(Error::Size);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.miserr() {
|
||||
return Err(Error::Miss);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if sr.pgserr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
unsafe { family::blocking_erase(from, to) }
|
||||
}
|
||||
|
||||
fn clear_all_err(&mut self) {
|
||||
unsafe {
|
||||
pac::FLASH.sr().modify(|w| {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_l0))]
|
||||
if w.rderr() {
|
||||
w.set_rderr(false);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.fasterr() {
|
||||
w.set_fasterr(false);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.miserr() {
|
||||
w.set_miserr(false);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.pgserr() {
|
||||
w.set_pgserr(false);
|
||||
}
|
||||
if w.sizerr() {
|
||||
w.set_sizerr(false);
|
||||
}
|
||||
if w.pgaerr() {
|
||||
w.set_pgaerr(false);
|
||||
}
|
||||
if w.wrperr() {
|
||||
w.set_wrperr(false);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.progerr() {
|
||||
w.set_progerr(false);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.operr() {
|
||||
w.set_operr(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
unsafe { family::clear_all_err() };
|
||||
}
|
||||
}
|
||||
|
||||
@ -274,6 +106,7 @@ pub enum Error {
|
||||
Seq,
|
||||
Protected,
|
||||
Unaligned,
|
||||
Parallelism,
|
||||
}
|
||||
|
||||
impl<'d> ErrorType for Flash<'d> {
|
||||
|
@ -50,7 +50,9 @@ pub mod i2c;
|
||||
|
||||
#[cfg(crc)]
|
||||
pub mod crc;
|
||||
#[cfg(any(flash_l0, flash_l1, flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(
|
||||
flash_l0, flash_l1, flash_wl, flash_wb, flash_l4, flash_f3, flash_f7, flash_h7
|
||||
))]
|
||||
pub mod flash;
|
||||
pub mod pwm;
|
||||
#[cfg(rng)]
|
||||
|
6
examples/boot/stm32f3/.cargo/config.toml
Normal file
6
examples/boot/stm32f3/.cargo/config.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
|
||||
runner = "probe-run --chip STM32F303VCTx"
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabihf"
|
26
examples/boot/stm32f3/Cargo.toml
Normal file
26
examples/boot/stm32f3/Cargo.toml
Normal file
@ -0,0 +1,26 @@
|
||||
[package]
|
||||
authors = ["Ulf Lilleengen <lulf@redhat.com>"]
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32f3-examples"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f303re", "time-driver-any", "exti"] }
|
||||
embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" }
|
||||
embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" }
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
defmt-rtt = { version = "0.3", optional = true }
|
||||
panic-reset = { version = "0.1.1" }
|
||||
embedded-hal = { version = "0.2.6" }
|
||||
|
||||
cortex-m = "0.7.3"
|
||||
cortex-m-rt = "0.7.0"
|
||||
|
||||
[features]
|
||||
defmt = [
|
||||
"dep:defmt",
|
||||
"embassy-stm32/defmt",
|
||||
"embassy-boot-stm32/defmt",
|
||||
]
|
29
examples/boot/stm32f3/README.md
Normal file
29
examples/boot/stm32f3/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Examples using bootloader
|
||||
|
||||
Example for STM32F3 demonstrating the bootloader. The example consists of application binaries, 'a'
|
||||
which allows you to press a button to start the DFU process, and 'b' which is the updated
|
||||
application.
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* `cargo-binutils`
|
||||
* `cargo-flash`
|
||||
* `embassy-boot-stm32`
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
# Flash bootloader
|
||||
cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f303re --chip STM32F303RETx
|
||||
# Build 'b'
|
||||
cargo build --release --bin b
|
||||
# Generate binary for 'b'
|
||||
cargo objcopy --release --bin b -- -O binary b.bin
|
||||
```
|
||||
|
||||
# Flash `a` (which includes b.bin)
|
||||
|
||||
```
|
||||
cargo flash --release --bin a --chip STM32F303RETx
|
||||
```
|
37
examples/boot/stm32f3/build.rs
Normal file
37
examples/boot/stm32f3/build.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
if env::var("CARGO_FEATURE_DEFMT").is_ok() {
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
}
|
||||
}
|
15
examples/boot/stm32f3/memory.x
Normal file
15
examples/boot/stm32f3/memory.x
Normal file
@ -0,0 +1,15 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K
|
||||
FLASH : ORIGIN = 0x08008000, LENGTH = 32K
|
||||
DFU : ORIGIN = 0x08010000, LENGTH = 36K
|
||||
RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER);
|
44
examples/boot/stm32f3/src/bin/a.rs
Normal file
44
examples/boot/stm32f3/src/bin/a.rs
Normal file
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_boot_stm32::FirmwareUpdater;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::adapter::BlockingAsync;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let flash = Flash::unlock(p.FLASH);
|
||||
let mut flash = BlockingAsync::new(flash);
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Up);
|
||||
let mut button = ExtiInput::new(button, p.EXTI13);
|
||||
|
||||
let mut led = Output::new(p.PA5, Level::Low, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
let mut updater = FirmwareUpdater::default();
|
||||
button.wait_for_falling_edge().await;
|
||||
let mut offset = 0;
|
||||
for chunk in APP_B.chunks(2048) {
|
||||
let mut buf: [u8; 2048] = [0; 2048];
|
||||
buf[..chunk.len()].copy_from_slice(chunk);
|
||||
updater
|
||||
.write_firmware(offset, &buf, &mut flash, 2048)
|
||||
.await
|
||||
.unwrap();
|
||||
offset += chunk.len();
|
||||
}
|
||||
updater.update(&mut flash).await.unwrap();
|
||||
led.set_low();
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
25
examples/boot/stm32f3/src/bin/b.rs
Normal file
25
examples/boot/stm32f3/src/bin/b.rs
Normal file
@ -0,0 +1,25 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let mut led = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
6
examples/boot/stm32f7/.cargo/config.toml
Normal file
6
examples/boot/stm32f7/.cargo/config.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
|
||||
runner = "probe-run --chip STM32F767ZITx -v"
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabihf"
|
26
examples/boot/stm32f7/Cargo.toml
Normal file
26
examples/boot/stm32f7/Cargo.toml
Normal file
@ -0,0 +1,26 @@
|
||||
[package]
|
||||
authors = ["Ulf Lilleengen <lulf@redhat.com>"]
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32f7-examples"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f767zi", "time-driver-any", "exti"] }
|
||||
embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" }
|
||||
embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" }
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
defmt-rtt = { version = "0.3", optional = true }
|
||||
panic-reset = { version = "0.1.1" }
|
||||
embedded-hal = { version = "0.2.6" }
|
||||
|
||||
cortex-m = "0.7.3"
|
||||
cortex-m-rt = "0.7.0"
|
||||
|
||||
[features]
|
||||
defmt = [
|
||||
"dep:defmt",
|
||||
"embassy-stm32/defmt",
|
||||
"embassy-boot-stm32/defmt",
|
||||
]
|
29
examples/boot/stm32f7/README.md
Normal file
29
examples/boot/stm32f7/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Examples using bootloader
|
||||
|
||||
Example for STM32F7 demonstrating the bootloader. The example consists of application binaries, 'a'
|
||||
which allows you to press a button to start the DFU process, and 'b' which is the updated
|
||||
application.
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* `cargo-binutils`
|
||||
* `cargo-flash`
|
||||
* `embassy-boot-stm32`
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
# Flash bootloader
|
||||
./flash-boot.sh
|
||||
# Build 'b'
|
||||
cargo build --release --bin b
|
||||
# Generate binary for 'b'
|
||||
cargo objcopy --release --bin b -- -O binary b.bin
|
||||
```
|
||||
|
||||
# Flash `a` (which includes b.bin)
|
||||
|
||||
```
|
||||
cargo flash --release --bin a --chip STM32F767ZITx
|
||||
```
|
37
examples/boot/stm32f7/build.rs
Normal file
37
examples/boot/stm32f7/build.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
if env::var("CARGO_FEATURE_DEFMT").is_ok() {
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
}
|
||||
}
|
8
examples/boot/stm32f7/flash-boot.sh
Executable file
8
examples/boot/stm32f7/flash-boot.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
mv ../../../embassy-boot/stm32/memory.x ../../../embassy-boot/stm32/memory-old.x
|
||||
cp memory-bl.x ../../../embassy-boot/stm32/memory.x
|
||||
|
||||
cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32F767ZITx --target thumbv7em-none-eabihf
|
||||
|
||||
rm ../../../embassy-boot/stm32/memory.x
|
||||
mv ../../../embassy-boot/stm32/memory-old.x ../../../embassy-boot/stm32/memory.x
|
18
examples/boot/stm32f7/memory-bl.x
Normal file
18
examples/boot/stm32f7/memory-bl.x
Normal file
@ -0,0 +1,18 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
FLASH : ORIGIN = 0x08000000, LENGTH = 256K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K
|
||||
ACTIVE : ORIGIN = 0x08080000, LENGTH = 256K
|
||||
DFU : ORIGIN = 0x080c0000, LENGTH = 512K
|
||||
RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 368K + 16K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH);
|
||||
|
||||
__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH);
|
||||
__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(FLASH);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(FLASH);
|
15
examples/boot/stm32f7/memory.x
Normal file
15
examples/boot/stm32f7/memory.x
Normal file
@ -0,0 +1,15 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 256K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K
|
||||
FLASH : ORIGIN = 0x08080000, LENGTH = 256K
|
||||
DFU : ORIGIN = 0x080c0000, LENGTH = 512K
|
||||
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 368K + 16K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER);
|
44
examples/boot/stm32f7/src/bin/a.rs
Normal file
44
examples/boot/stm32f7/src/bin/a.rs
Normal file
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_boot_stm32::FirmwareUpdater;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::adapter::BlockingAsync;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let flash = Flash::unlock(p.FLASH);
|
||||
let mut flash = BlockingAsync::new(flash);
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Down);
|
||||
let mut button = ExtiInput::new(button, p.EXTI13);
|
||||
|
||||
let mut led = Output::new(p.PB7, Level::Low, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
let mut updater = FirmwareUpdater::default();
|
||||
button.wait_for_rising_edge().await;
|
||||
let mut offset = 0;
|
||||
let mut buf: [u8; 256 * 1024] = [0; 256 * 1024];
|
||||
for chunk in APP_B.chunks(256 * 1024) {
|
||||
buf[..chunk.len()].copy_from_slice(chunk);
|
||||
updater
|
||||
.write_firmware(offset, &buf, &mut flash, 2048)
|
||||
.await
|
||||
.unwrap();
|
||||
offset += chunk.len();
|
||||
}
|
||||
updater.update(&mut flash).await.unwrap();
|
||||
led.set_low();
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
27
examples/boot/stm32f7/src/bin/b.rs
Normal file
27
examples/boot/stm32f7/src/bin/b.rs
Normal file
@ -0,0 +1,27 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
let mut led = Output::new(p.PB7, Level::High, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
6
examples/boot/stm32h7/.cargo/config.toml
Normal file
6
examples/boot/stm32h7/.cargo/config.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
|
||||
runner = "probe-run --chip STM32H743ZITx"
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabihf"
|
26
examples/boot/stm32h7/Cargo.toml
Normal file
26
examples/boot/stm32h7/Cargo.toml
Normal file
@ -0,0 +1,26 @@
|
||||
[package]
|
||||
authors = ["Ulf Lilleengen <lulf@redhat.com>"]
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32f7-examples"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32h743zi", "time-driver-any", "exti"] }
|
||||
embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" }
|
||||
embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" }
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
defmt-rtt = { version = "0.3", optional = true }
|
||||
panic-reset = { version = "0.1.1" }
|
||||
embedded-hal = { version = "0.2.6" }
|
||||
|
||||
cortex-m = "0.7.3"
|
||||
cortex-m-rt = "0.7.0"
|
||||
|
||||
[features]
|
||||
defmt = [
|
||||
"dep:defmt",
|
||||
"embassy-stm32/defmt",
|
||||
"embassy-boot-stm32/defmt",
|
||||
]
|
29
examples/boot/stm32h7/README.md
Normal file
29
examples/boot/stm32h7/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Examples using bootloader
|
||||
|
||||
Example for STM32H7 demonstrating the bootloader. The example consists of application binaries, 'a'
|
||||
which allows you to press a button to start the DFU process, and 'b' which is the updated
|
||||
application.
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* `cargo-binutils`
|
||||
* `cargo-flash`
|
||||
* `embassy-boot-stm32`
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
# Flash bootloader
|
||||
./flash-boot.sh
|
||||
# Build 'b'
|
||||
cargo build --release --bin b
|
||||
# Generate binary for 'b'
|
||||
cargo objcopy --release --bin b -- -O binary b.bin
|
||||
```
|
||||
|
||||
# Flash `a` (which includes b.bin)
|
||||
|
||||
```
|
||||
cargo flash --release --bin a --chip STM32H743ZITx
|
||||
```
|
37
examples/boot/stm32h7/build.rs
Normal file
37
examples/boot/stm32h7/build.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
if env::var("CARGO_FEATURE_DEFMT").is_ok() {
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
}
|
||||
}
|
8
examples/boot/stm32h7/flash-boot.sh
Executable file
8
examples/boot/stm32h7/flash-boot.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
mv ../../../embassy-boot/stm32/memory.x ../../../embassy-boot/stm32/memory-old.x
|
||||
cp memory-bl.x ../../../embassy-boot/stm32/memory.x
|
||||
|
||||
cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32H743ZITx --target thumbv7em-none-eabihf
|
||||
|
||||
rm ../../../embassy-boot/stm32/memory.x
|
||||
mv ../../../embassy-boot/stm32/memory-old.x ../../../embassy-boot/stm32/memory.x
|
18
examples/boot/stm32h7/memory-bl.x
Normal file
18
examples/boot/stm32h7/memory-bl.x
Normal file
@ -0,0 +1,18 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
FLASH : ORIGIN = 0x08000000, LENGTH = 128K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K
|
||||
ACTIVE : ORIGIN = 0x08040000, LENGTH = 128K
|
||||
DFU : ORIGIN = 0x08100000, LENGTH = 512K
|
||||
RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH);
|
||||
|
||||
__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH);
|
||||
__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(FLASH);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(FLASH);
|
15
examples/boot/stm32h7/memory.x
Normal file
15
examples/boot/stm32h7/memory.x
Normal file
@ -0,0 +1,15 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 128K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K
|
||||
FLASH : ORIGIN = 0x08040000, LENGTH = 256K
|
||||
DFU : ORIGIN = 0x08100000, LENGTH = 512K
|
||||
RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER);
|
44
examples/boot/stm32h7/src/bin/a.rs
Normal file
44
examples/boot/stm32h7/src/bin/a.rs
Normal file
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_boot_stm32::FirmwareUpdater;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::adapter::BlockingAsync;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let flash = Flash::unlock(p.FLASH);
|
||||
let mut flash = BlockingAsync::new(flash);
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Down);
|
||||
let mut button = ExtiInput::new(button, p.EXTI13);
|
||||
|
||||
let mut led = Output::new(p.PB14, Level::Low, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
let mut updater = FirmwareUpdater::default();
|
||||
button.wait_for_rising_edge().await;
|
||||
let mut offset = 0;
|
||||
let mut buf: [u8; 128 * 1024] = [0; 128 * 1024];
|
||||
for chunk in APP_B.chunks(128 * 1024) {
|
||||
buf[..chunk.len()].copy_from_slice(chunk);
|
||||
updater
|
||||
.write_firmware(offset, &buf, &mut flash, 2048)
|
||||
.await
|
||||
.unwrap();
|
||||
offset += chunk.len();
|
||||
}
|
||||
updater.update(&mut flash).await.unwrap();
|
||||
led.set_low();
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
27
examples/boot/stm32h7/src/bin/b.rs
Normal file
27
examples/boot/stm32h7/src/bin/b.rs
Normal file
@ -0,0 +1,27 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
let mut led = Output::new(p.PB14, Level::High, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
@ -19,3 +19,4 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
heapless = { version = "0.7.5", default-features = false }
|
||||
nb = "1.0.0"
|
||||
embedded-storage = "0.3.0"
|
||||
|
@ -15,7 +15,7 @@ use panic_probe as _;
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello World!");
|
||||
|
||||
let mut led = Output::new(p.PE12, Level::High, Speed::Low);
|
||||
let mut led = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
43
examples/stm32f3/src/bin/flash.rs
Normal file
43
examples/stm32f3/src/bin/flash.rs
Normal file
@ -0,0 +1,43 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::{info, unwrap};
|
||||
use embassy::executor::Spawner;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use panic_probe as _;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello Flash!");
|
||||
|
||||
const ADDR: u32 = 0x26000;
|
||||
|
||||
let mut f = Flash::unlock(p.FLASH);
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
|
||||
info!("Erasing...");
|
||||
unwrap!(f.erase(ADDR, ADDR + 2048));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read after erase: {=[u8]:x}", buf);
|
||||
|
||||
info!("Writing...");
|
||||
unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8]));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
}
|
@ -22,6 +22,7 @@ heapless = { version = "0.7.5", default-features = false }
|
||||
nb = "1.0.0"
|
||||
rand_core = "0.6.3"
|
||||
critical-section = "0.2.3"
|
||||
embedded-storage = "0.3.0"
|
||||
|
||||
[dependencies.smoltcp]
|
||||
version = "0.8.0"
|
||||
|
59
examples/stm32f7/src/bin/flash.rs
Normal file
59
examples/stm32f7/src/bin/flash.rs
Normal file
@ -0,0 +1,59 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::{info, unwrap};
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use panic_probe as _;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello Flash!");
|
||||
|
||||
const ADDR: u32 = 0x8_0000;
|
||||
|
||||
// wait a bit before accessing the flash
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
|
||||
let mut f = Flash::unlock(p.FLASH);
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
|
||||
info!("Erasing...");
|
||||
unwrap!(f.erase(ADDR, ADDR + 256 * 1024));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read after erase: {=[u8]:x}", buf);
|
||||
|
||||
info!("Writing...");
|
||||
unwrap!(f.write(
|
||||
ADDR,
|
||||
&[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 26, 27, 28, 29, 30, 31, 32
|
||||
]
|
||||
));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
assert_eq!(
|
||||
&buf[..],
|
||||
&[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 26, 27, 28, 29, 30, 31, 32
|
||||
]
|
||||
);
|
||||
}
|
@ -27,6 +27,7 @@ rand_core = "0.6.3"
|
||||
critical-section = "0.2.5"
|
||||
micromath = "2.0.0"
|
||||
stm32-fmc = "0.2.4"
|
||||
embedded-storage = "0.3.0"
|
||||
|
||||
[dependencies.smoltcp]
|
||||
version = "0.8.0"
|
||||
|
58
examples/stm32h7/src/bin/flash.rs
Normal file
58
examples/stm32h7/src/bin/flash.rs
Normal file
@ -0,0 +1,58 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::{info, unwrap};
|
||||
use defmt_rtt as _; // global logger
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||
use panic_probe as _;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello Flash!");
|
||||
|
||||
const ADDR: u32 = 0x08_0000;
|
||||
|
||||
// wait a bit before accessing the flash
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
|
||||
let mut f = Flash::unlock(p.FLASH);
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
|
||||
info!("Erasing...");
|
||||
unwrap!(f.erase(ADDR, ADDR + 128 * 1024));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read after erase: {=[u8]:x}", buf);
|
||||
|
||||
info!("Writing...");
|
||||
unwrap!(f.write(
|
||||
ADDR,
|
||||
&[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 26, 27, 28, 29, 30, 31, 32
|
||||
]
|
||||
));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 32];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
assert_eq!(
|
||||
&buf[..],
|
||||
&[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 26, 27, 28, 29, 30, 31, 32
|
||||
]
|
||||
);
|
||||
}
|
@ -1 +1 @@
|
||||
Subproject commit 9abfa9d2b51e6071fdc7e680b4a171e4fa20c2fb
|
||||
Subproject commit b2d7a9f5de7dc3ae17c87c1ff94e13a822d18e74
|
Loading…
Reference in New Issue
Block a user