Adapted nvmc so it can be used for all nrf targets

This commit is contained in:
Dion Dokter 2022-11-10 14:37:42 +01:00
parent 059610a8de
commit dbe97b4098
5 changed files with 75 additions and 11 deletions

View File

@ -213,6 +213,8 @@ pub mod pac {
pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
pub const FLASH_SIZE: usize = 1024 * 1024;
embassy_hal_common::peripherals! {
// USB
USBD,
@ -224,6 +226,9 @@ embassy_hal_common::peripherals! {
// WDT
WDT,
// NVMC
NVMC,
// UARTE, TWI & SPI
UARTETWISPI0,
UARTETWISPI1,

View File

@ -104,6 +104,8 @@ pub mod pac {
pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
pub const FLASH_SIZE: usize = 256 * 1024;
embassy_hal_common::peripherals! {
// RTC
RTC0,
@ -112,6 +114,9 @@ embassy_hal_common::peripherals! {
// WDT
WDT,
// NVMC
NVMC,
// UARTE, TWI & SPI
UARTETWISPI0,
UARTETWISPI1,

View File

@ -164,6 +164,8 @@ pub mod pac {
pub const EASY_DMA_SIZE: usize = (1 << 13) - 1;
pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
pub const FLASH_SIZE: usize = 1024 * 1024;
embassy_hal_common::peripherals! {
// RTC
RTC0,
@ -172,6 +174,9 @@ embassy_hal_common::peripherals! {
// WDT
WDT,
// NVMC
NVMC,
// UARTE, TWI & SPI
UARTETWISPI0,
UARTETWISPI1,

View File

@ -74,7 +74,6 @@ pub mod buffered_uarte;
pub mod gpio;
#[cfg(feature = "gpiote")]
pub mod gpiote;
#[cfg(not(any(feature = "_nrf5340", feature = "_nrf9160")))]
pub mod nvmc;
#[cfg(any(
feature = "nrf52810",

View File

@ -10,8 +10,12 @@ use embedded_storage::nor_flash::{
use crate::peripherals::NVMC;
use crate::{pac, Peripheral};
#[cfg(not(feature = "_nrf5340-net"))]
/// Erase size of NVMC flash in bytes.
pub const PAGE_SIZE: usize = 4096;
#[cfg(feature = "_nrf5340-net")]
/// Erase size of NVMC flash in bytes.
pub const PAGE_SIZE: usize = 2048;
/// Size of NVMC flash in bytes.
pub const FLASH_SIZE: usize = crate::chip::FLASH_SIZE;
@ -55,6 +59,56 @@ impl<'d> Nvmc<'d> {
let p = Self::regs();
while p.ready.read().ready().is_busy() {}
}
#[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")))]
fn erase_page(&mut self, page: u32) {
Self::regs().erasepage().write(|w| unsafe { w.bits(page) });
}
#[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))]
fn erase_page(&mut self, page: u32) {
#[cfg(not(feature = "_nrf5340-net"))]
const FLASH_START_ADDR: u32 = 0;
#[cfg(feature = "_nrf5340-net")]
const FLASH_START_ADDR: u32 = 0x100_0000;
let first_page_word = (FLASH_START_ADDR + page * PAGE_SIZE as u32) as *mut u32;
unsafe {
first_page_word.write_volatile(0xFFFF_FFFF);
}
}
fn enable_erase(&self) {
#[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))]
Self::regs().config.write(|w| w.wen().een());
#[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))]
Self::regs().configns.write(|w| w.wen().een());
}
fn enable_read(&self) {
#[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))]
Self::regs().config.write(|w| w.wen().ren());
#[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))]
Self::regs().configns.write(|w| w.wen().ren());
}
fn enable_write(&self) {
#[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))]
Self::regs().config.write(|w| w.wen().wen());
#[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))]
Self::regs().configns.write(|w| w.wen().wen());
}
}
impl<'d> MultiwriteNorFlash for Nvmc<'d> {}
@ -93,17 +147,15 @@ impl<'d> NorFlash for Nvmc<'d> {
return Err(Error::Unaligned);
}
let p = Self::regs();
p.config.write(|w| w.wen().een());
self.enable_erase();
self.wait_ready();
for page in (from..to).step_by(PAGE_SIZE) {
p.erasepage().write(|w| unsafe { w.bits(page) });
self.erase_page(page);
self.wait_ready();
}
p.config.reset();
self.enable_read();
self.wait_ready();
Ok(())
@ -117,9 +169,7 @@ impl<'d> NorFlash for Nvmc<'d> {
return Err(Error::Unaligned);
}
let p = Self::regs();
p.config.write(|w| w.wen().wen());
self.enable_write();
self.wait_ready();
unsafe {
@ -129,11 +179,11 @@ impl<'d> NorFlash for Nvmc<'d> {
for i in 0..words {
let w = ptr::read_unaligned(p_src.add(i));
ptr::write_volatile(p_dst.add(i), w);
self.wait_ready();
self.wait_ready_write();
}
}
p.config.reset();
self.enable_read();
self.wait_ready();
Ok(())