2023-02-01 00:48:33 +01:00
|
|
|
//! Non-Volatile Memory Controller (NVMC, AKA internal flash) driver.
|
2021-10-22 02:09:55 +02:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use core::{ptr, slice};
|
|
|
|
|
2022-07-23 15:13:47 +02:00
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2022-02-07 12:35:58 +01:00
|
|
|
use embedded_storage::nor_flash::{
|
|
|
|
ErrorType, MultiwriteNorFlash, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
|
|
|
|
};
|
2021-10-22 02:09:55 +02:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::peripherals::NVMC;
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{pac, Peripheral};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-11-10 14:37:42 +01:00
|
|
|
#[cfg(not(feature = "_nrf5340-net"))]
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Erase size of NVMC flash in bytes.
|
2021-10-24 18:23:22 +02:00
|
|
|
pub const PAGE_SIZE: usize = 4096;
|
2022-11-10 14:37:42 +01:00
|
|
|
#[cfg(feature = "_nrf5340-net")]
|
|
|
|
/// Erase size of NVMC flash in bytes.
|
|
|
|
pub const PAGE_SIZE: usize = 2048;
|
2022-08-22 10:36:33 +02:00
|
|
|
|
|
|
|
/// Size of NVMC flash in bytes.
|
2021-10-24 18:23:22 +02:00
|
|
|
pub const FLASH_SIZE: usize = crate::chip::FLASH_SIZE;
|
2021-10-22 02:09:55 +02:00
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Error type for NVMC operations.
|
2021-10-22 02:09:55 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error {
|
2023-05-08 23:25:01 +02:00
|
|
|
/// Operation using a location not in flash.
|
2021-10-22 02:09:55 +02:00
|
|
|
OutOfBounds,
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Unaligned operation or using unaligned buffers.
|
2021-10-22 02:09:55 +02:00
|
|
|
Unaligned,
|
|
|
|
}
|
|
|
|
|
2022-02-07 12:35:58 +01:00
|
|
|
impl NorFlashError for Error {
|
|
|
|
fn kind(&self) -> NorFlashErrorKind {
|
|
|
|
match self {
|
|
|
|
Self::OutOfBounds => NorFlashErrorKind::OutOfBounds,
|
|
|
|
Self::Unaligned => NorFlashErrorKind::NotAligned,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Non-Volatile Memory Controller (NVMC) that implements the `embedded-storage` traits.
|
2021-10-22 02:09:55 +02:00
|
|
|
pub struct Nvmc<'d> {
|
2022-07-23 15:13:47 +02:00
|
|
|
_p: PeripheralRef<'d, NVMC>,
|
2021-10-22 02:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> Nvmc<'d> {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Create Nvmc driver.
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(_p: impl Peripheral<P = NVMC> + 'd) -> Self {
|
|
|
|
into_ref!(_p);
|
2022-07-23 15:13:47 +02:00
|
|
|
Self { _p }
|
2021-10-22 02:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn regs() -> &'static pac::nvmc::RegisterBlock {
|
|
|
|
unsafe { &*pac::NVMC::ptr() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait_ready(&mut self) {
|
|
|
|
let p = Self::regs();
|
|
|
|
while p.ready.read().ready().is_busy() {}
|
|
|
|
}
|
2022-11-10 14:37:42 +01:00
|
|
|
|
|
|
|
#[cfg(not(any(feature = "_nrf9160", feature = "_nrf5340")))]
|
|
|
|
fn wait_ready_write(&mut self) {
|
|
|
|
self.wait_ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))]
|
|
|
|
fn wait_ready_write(&mut self) {
|
|
|
|
let p = Self::regs();
|
|
|
|
while p.readynext.read().readynext().is_busy() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(feature = "_nrf9160", feature = "_nrf5340")))]
|
2022-12-09 11:02:16 +01:00
|
|
|
fn erase_page(&mut self, page_addr: u32) {
|
|
|
|
Self::regs().erasepage().write(|w| unsafe { w.bits(page_addr) });
|
2022-11-10 14:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))]
|
2022-12-09 11:02:16 +01:00
|
|
|
fn erase_page(&mut self, page_addr: u32) {
|
|
|
|
let first_page_word = page_addr as *mut u32;
|
2022-11-10 14:37:42 +01:00
|
|
|
unsafe {
|
|
|
|
first_page_word.write_volatile(0xFFFF_FFFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_erase(&self) {
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(not(feature = "_ns"))]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().config.write(|w| w.wen().een());
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(feature = "_ns")]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().configns.write(|w| w.wen().een());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_read(&self) {
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(not(feature = "_ns"))]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().config.write(|w| w.wen().ren());
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(feature = "_ns")]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().configns.write(|w| w.wen().ren());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_write(&self) {
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(not(feature = "_ns"))]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().config.write(|w| w.wen().wen());
|
2023-01-23 01:48:35 +01:00
|
|
|
#[cfg(feature = "_ns")]
|
2022-11-10 14:37:42 +01:00
|
|
|
Self::regs().configns.write(|w| w.wen().wen());
|
|
|
|
}
|
2021-10-22 02:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> MultiwriteNorFlash for Nvmc<'d> {}
|
|
|
|
|
2022-02-07 12:35:58 +01:00
|
|
|
impl<'d> ErrorType for Nvmc<'d> {
|
2021-10-22 02:09:55 +02:00
|
|
|
type Error = Error;
|
2022-02-07 12:35:58 +01:00
|
|
|
}
|
2021-10-22 02:09:55 +02:00
|
|
|
|
2022-02-07 12:35:58 +01:00
|
|
|
impl<'d> ReadNorFlash for Nvmc<'d> {
|
2021-10-22 02:09:55 +02:00
|
|
|
const READ_SIZE: usize = 1;
|
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
if offset as usize >= FLASH_SIZE || offset as usize + bytes.len() > FLASH_SIZE {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
let flash_data = unsafe { slice::from_raw_parts(offset as *const u8, bytes.len()) };
|
|
|
|
bytes.copy_from_slice(flash_data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> NorFlash for Nvmc<'d> {
|
|
|
|
const WRITE_SIZE: usize = 4;
|
|
|
|
const ERASE_SIZE: usize = PAGE_SIZE;
|
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
if to < from || to as usize > FLASH_SIZE {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
if from as usize % PAGE_SIZE != 0 || to as usize % PAGE_SIZE != 0 {
|
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:37:42 +01:00
|
|
|
self.enable_erase();
|
2021-10-22 02:09:55 +02:00
|
|
|
self.wait_ready();
|
|
|
|
|
2022-12-09 11:02:16 +01:00
|
|
|
for page_addr in (from..to).step_by(PAGE_SIZE) {
|
|
|
|
self.erase_page(page_addr);
|
2021-10-22 02:09:55 +02:00
|
|
|
self.wait_ready();
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:37:42 +01:00
|
|
|
self.enable_read();
|
2021-10-22 02:09:55 +02:00
|
|
|
self.wait_ready();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
if offset as usize + bytes.len() > FLASH_SIZE {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
if offset as usize % 4 != 0 || bytes.len() as usize % 4 != 0 {
|
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:37:42 +01:00
|
|
|
self.enable_write();
|
2021-10-22 02:09:55 +02:00
|
|
|
self.wait_ready();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let p_src = bytes.as_ptr() as *const u32;
|
|
|
|
let p_dst = offset as *mut u32;
|
|
|
|
let words = bytes.len() / 4;
|
|
|
|
for i in 0..words {
|
|
|
|
let w = ptr::read_unaligned(p_src.add(i));
|
|
|
|
ptr::write_volatile(p_dst.add(i), w);
|
2022-11-10 14:37:42 +01:00
|
|
|
self.wait_ready_write();
|
2021-10-22 02:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:37:42 +01:00
|
|
|
self.enable_read();
|
2021-10-22 02:09:55 +02:00
|
|
|
self.wait_ready();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|