2022-04-20 13:49:59 +02:00
|
|
|
use crate::peripherals::FLASH;
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_hal_common::unborrow;
|
|
|
|
|
|
|
|
use embedded_storage::nor_flash::{
|
|
|
|
ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
|
|
|
|
};
|
|
|
|
|
2022-04-26 18:33:09 +02:00
|
|
|
pub use crate::pac::ERASE_SIZE;
|
|
|
|
pub use crate::pac::ERASE_VALUE;
|
|
|
|
pub use crate::pac::FLASH_BASE;
|
|
|
|
pub use crate::pac::FLASH_SIZE;
|
|
|
|
pub use crate::pac::WRITE_SIZE;
|
|
|
|
const FLASH_END: usize = FLASH_BASE + FLASH_SIZE;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
2022-05-02 15:36:02 +02:00
|
|
|
#[cfg_attr(any(flash_wl, flash_wb, flash_l0, flash_l1, flash_l4), path = "l.rs")]
|
|
|
|
#[cfg_attr(flash_f3, path = "f3.rs")]
|
2022-05-03 16:16:37 +02:00
|
|
|
#[cfg_attr(flash_f7, path = "f7.rs")]
|
2022-05-06 09:21:29 +02:00
|
|
|
#[cfg_attr(flash_h7, path = "h7.rs")]
|
2022-05-02 15:36:02 +02:00
|
|
|
mod family;
|
|
|
|
|
2022-04-20 13:49:59 +02:00
|
|
|
pub struct Flash<'d> {
|
|
|
|
_inner: FLASH,
|
|
|
|
_phantom: PhantomData<&'d mut FLASH>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> Flash<'d> {
|
|
|
|
pub fn new(p: impl Unborrow<Target = FLASH>) -> Self {
|
|
|
|
unborrow!(p);
|
|
|
|
Self {
|
|
|
|
_inner: p,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unlock(p: impl Unborrow<Target = FLASH>) -> Self {
|
|
|
|
let flash = Self::new(p);
|
|
|
|
|
2022-05-02 15:36:02 +02:00
|
|
|
unsafe { family::unlock() };
|
2022-04-20 13:49:59 +02:00
|
|
|
flash
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lock(&mut self) {
|
2022-05-02 15:36:02 +02:00
|
|
|
unsafe { family::lock() };
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2022-04-26 18:33:09 +02:00
|
|
|
let offset = FLASH_BASE as u32 + offset;
|
2022-04-20 13:49:59 +02:00
|
|
|
if offset as usize >= FLASH_END || offset as usize + bytes.len() > FLASH_END {
|
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
let flash_data = unsafe { core::slice::from_raw_parts(offset as *const u8, bytes.len()) };
|
|
|
|
bytes.copy_from_slice(flash_data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_write(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
|
2022-04-26 18:33:09 +02:00
|
|
|
let offset = FLASH_BASE as u32 + offset;
|
2022-04-20 13:49:59 +02:00
|
|
|
if offset as usize + buf.len() > FLASH_END {
|
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
2022-04-26 18:33:09 +02:00
|
|
|
if offset as usize % WRITE_SIZE != 0 || buf.len() as usize % WRITE_SIZE != 0 {
|
2022-04-20 13:49:59 +02:00
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
2022-04-26 18:33:09 +02:00
|
|
|
trace!("Writing {} bytes at 0x{:x}", buf.len(), offset);
|
2022-04-20 13:49:59 +02:00
|
|
|
|
|
|
|
self.clear_all_err();
|
|
|
|
|
2022-05-02 15:36:02 +02:00
|
|
|
unsafe { family::blocking_write(offset, buf) }
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
2022-04-26 18:33:09 +02:00
|
|
|
let from = FLASH_BASE as u32 + from;
|
|
|
|
let to = FLASH_BASE as u32 + to;
|
2022-04-20 13:49:59 +02:00
|
|
|
if to < from || to as usize > FLASH_END {
|
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
2022-05-03 16:16:37 +02:00
|
|
|
if ((to - from) as usize % ERASE_SIZE) != 0 {
|
2022-04-20 13:49:59 +02:00
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.clear_all_err();
|
|
|
|
|
2022-05-02 15:36:02 +02:00
|
|
|
unsafe { family::blocking_erase(from, to) }
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_all_err(&mut self) {
|
2022-05-02 15:36:02 +02:00
|
|
|
unsafe { family::clear_all_err() };
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Flash<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error {
|
|
|
|
Prog,
|
|
|
|
Size,
|
|
|
|
Miss,
|
|
|
|
Seq,
|
|
|
|
Protected,
|
|
|
|
Unaligned,
|
2022-05-03 16:16:37 +02:00
|
|
|
Parallelism,
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> ErrorType for Flash<'d> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NorFlashError for Error {
|
|
|
|
fn kind(&self) -> NorFlashErrorKind {
|
|
|
|
match self {
|
|
|
|
Self::Size => NorFlashErrorKind::OutOfBounds,
|
|
|
|
Self::Unaligned => NorFlashErrorKind::NotAligned,
|
|
|
|
_ => NorFlashErrorKind::Other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> ReadNorFlash for Flash<'d> {
|
2022-04-26 18:33:09 +02:00
|
|
|
const READ_SIZE: usize = WRITE_SIZE;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_read(offset, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> NorFlash for Flash<'d> {
|
2022-04-26 18:33:09 +02:00
|
|
|
const WRITE_SIZE: usize = WRITE_SIZE;
|
|
|
|
const ERASE_SIZE: usize = ERASE_SIZE;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_erase(from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_write(offset, bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(feature = "nightly")]
|
|
|
|
{
|
|
|
|
use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash};
|
|
|
|
use core::future::Future;
|
|
|
|
|
|
|
|
impl<'d> AsyncNorFlash for Flash<'d> {
|
|
|
|
const WRITE_SIZE: usize = <Self as NorFlash>::WRITE_SIZE;
|
|
|
|
const ERASE_SIZE: usize = <Self as NorFlash>::ERASE_SIZE;
|
|
|
|
|
|
|
|
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
|
|
|
fn write<'a>(&'a mut self, offset: u32, data: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
async move {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type EraseFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
|
|
|
fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a> {
|
|
|
|
async move {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> AsyncReadNorFlash for Flash<'d> {
|
|
|
|
const READ_SIZE: usize = <Self as ReadNorFlash>::READ_SIZE;
|
|
|
|
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
|
|
|
|
fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
async move {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|