rp/flash: change naming to blocking_*, new_blocking.

- Needed for consistency with other drivers.
- Having two `new()` functions sometimes resulted in 'multiple applicable methods' errors.
This commit is contained in:
Dario Nieuwenhuis 2023-08-18 13:12:19 +02:00
parent 94fa95c699
commit b948e37769
5 changed files with 82 additions and 78 deletions

View file

@ -54,7 +54,7 @@ pub struct WatchdogFlash<'d, const SIZE: usize> {
impl<'d, const SIZE: usize> WatchdogFlash<'d, SIZE> { impl<'d, const SIZE: usize> WatchdogFlash<'d, SIZE> {
/// Start a new watchdog with a given flash and watchdog peripheral and a timeout /// Start a new watchdog with a given flash and watchdog peripheral and a timeout
pub fn start(flash: FLASH, watchdog: WATCHDOG, timeout: Duration) -> Self { pub fn start(flash: FLASH, watchdog: WATCHDOG, timeout: Duration) -> Self {
let flash = Flash::<_, Blocking, SIZE>::new(flash); let flash = Flash::<_, Blocking, SIZE>::new_blocking(flash);
let mut watchdog = Watchdog::new(watchdog); let mut watchdog = Watchdog::new(watchdog);
watchdog.start(timeout); watchdog.start(timeout);
Self { flash, watchdog } Self { flash, watchdog }
@ -71,11 +71,11 @@ impl<'d, const SIZE: usize> NorFlash for WatchdogFlash<'d, SIZE> {
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.watchdog.feed(); self.watchdog.feed();
self.flash.erase(from, to) self.flash.blocking_erase(from, to)
} }
fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
self.watchdog.feed(); self.watchdog.feed();
self.flash.write(offset, data) self.flash.blocking_write(offset, data)
} }
} }
@ -83,7 +83,7 @@ impl<'d, const SIZE: usize> ReadNorFlash for WatchdogFlash<'d, SIZE> {
const READ_SIZE: usize = <Flash<'d, FLASH, Blocking, SIZE> as ReadNorFlash>::READ_SIZE; const READ_SIZE: usize = <Flash<'d, FLASH, Blocking, SIZE> as ReadNorFlash>::READ_SIZE;
fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> { fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> {
self.watchdog.feed(); self.watchdog.feed();
self.flash.read(offset, data) self.flash.blocking_read(offset, data)
} }
fn capacity(&self) -> usize { fn capacity(&self) -> usize {
self.flash.capacity() self.flash.capacity()

View file

@ -102,7 +102,7 @@ pub struct Flash<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> {
} }
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SIZE> { impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SIZE> {
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
trace!( trace!(
"Reading from 0x{:x} to 0x{:x}", "Reading from 0x{:x} to 0x{:x}",
FLASH_BASE as u32 + offset, FLASH_BASE as u32 + offset,
@ -120,7 +120,7 @@ impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SI
FLASH_SIZE FLASH_SIZE
} }
pub fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> { pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
check_erase(self, from, to)?; check_erase(self, from, to)?;
trace!( trace!(
@ -136,7 +136,7 @@ impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SI
Ok(()) Ok(())
} }
pub fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
check_write(self, offset, bytes.len())?; check_write(self, offset, bytes.len())?;
trace!("Writing {:?} bytes to 0x{:x}", bytes.len(), FLASH_BASE as u32 + offset); trace!("Writing {:?} bytes to 0x{:x}", bytes.len(), FLASH_BASE as u32 + offset);
@ -233,13 +233,13 @@ impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SI
} }
/// Read SPI flash unique ID /// Read SPI flash unique ID
pub fn unique_id(&mut self, uid: &mut [u8]) -> Result<(), Error> { pub fn blocking_unique_id(&mut self, uid: &mut [u8]) -> Result<(), Error> {
unsafe { self.in_ram(|| ram_helpers::flash_unique_id(uid))? }; unsafe { self.in_ram(|| ram_helpers::flash_unique_id(uid))? };
Ok(()) Ok(())
} }
/// Read SPI flash JEDEC ID /// Read SPI flash JEDEC ID
pub fn jedec_id(&mut self) -> Result<u32, Error> { pub fn blocking_jedec_id(&mut self) -> Result<u32, Error> {
let mut jedec = None; let mut jedec = None;
unsafe { unsafe {
self.in_ram(|| { self.in_ram(|| {
@ -251,7 +251,7 @@ impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> Flash<'d, T, M, FLASH_SI
} }
impl<'d, T: Instance, const FLASH_SIZE: usize> Flash<'d, T, Blocking, FLASH_SIZE> { impl<'d, T: Instance, const FLASH_SIZE: usize> Flash<'d, T, Blocking, FLASH_SIZE> {
pub fn new(_flash: impl Peripheral<P = T> + 'd) -> Self { pub fn new_blocking(_flash: impl Peripheral<P = T> + 'd) -> Self {
Self { Self {
dma: None, dma: None,
phantom: PhantomData, phantom: PhantomData,
@ -310,47 +310,8 @@ impl<'d, T: Instance, const FLASH_SIZE: usize> Flash<'d, T, Async, FLASH_SIZE> {
transfer, transfer,
}) })
} }
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> ErrorType for Flash<'d, T, M, FLASH_SIZE> { pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
type Error = Error;
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> ReadNorFlash for Flash<'d, T, M, FLASH_SIZE> {
const READ_SIZE: usize = READ_SIZE;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.read(offset, bytes)
}
fn capacity(&self) -> usize {
self.capacity()
}
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> MultiwriteNorFlash for Flash<'d, T, M, FLASH_SIZE> {}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> NorFlash for Flash<'d, T, M, FLASH_SIZE> {
const WRITE_SIZE: usize = WRITE_SIZE;
const ERASE_SIZE: usize = ERASE_SIZE;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.erase(from, to)
}
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
self.write(offset, bytes)
}
}
#[cfg(feature = "nightly")]
impl<'d, T: Instance, const FLASH_SIZE: usize> embedded_storage_async::nor_flash::ReadNorFlash
for Flash<'d, T, Async, FLASH_SIZE>
{
const READ_SIZE: usize = ASYNC_READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
// Checked early to simplify address validity checks // Checked early to simplify address validity checks
@ -389,6 +350,49 @@ impl<'d, T: Instance, const FLASH_SIZE: usize> embedded_storage_async::nor_flash
Ok(()) Ok(())
} }
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> ErrorType for Flash<'d, T, M, FLASH_SIZE> {
type Error = Error;
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> ReadNorFlash for Flash<'d, T, M, FLASH_SIZE> {
const READ_SIZE: usize = READ_SIZE;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.blocking_read(offset, bytes)
}
fn capacity(&self) -> usize {
self.capacity()
}
}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> MultiwriteNorFlash for Flash<'d, T, M, FLASH_SIZE> {}
impl<'d, T: Instance, M: Mode, const FLASH_SIZE: usize> NorFlash for Flash<'d, T, M, FLASH_SIZE> {
const WRITE_SIZE: usize = WRITE_SIZE;
const ERASE_SIZE: usize = ERASE_SIZE;
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(feature = "nightly")]
impl<'d, T: Instance, const FLASH_SIZE: usize> embedded_storage_async::nor_flash::ReadNorFlash
for Flash<'d, T, Async, FLASH_SIZE>
{
const READ_SIZE: usize = ASYNC_READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.read(offset, bytes).await
}
fn capacity(&self) -> usize { fn capacity(&self) -> usize {
self.capacity() self.capacity()
@ -404,11 +408,11 @@ impl<'d, T: Instance, const FLASH_SIZE: usize> embedded_storage_async::nor_flash
const ERASE_SIZE: usize = ERASE_SIZE; const ERASE_SIZE: usize = ERASE_SIZE;
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.erase(from, to) self.blocking_erase(from, to)
} }
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
self.write(offset, bytes) self.blocking_write(offset, bytes)
} }
} }

View file

@ -7,7 +7,7 @@ use core::cell::RefCell;
use defmt_rtt as _; use defmt_rtt as _;
use embassy_boot_rp::*; use embassy_boot_rp::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_rp::flash::{self, Flash}; use embassy_rp::flash::Flash;
use embassy_rp::gpio::{Level, Output}; use embassy_rp::gpio::{Level, Output};
use embassy_rp::watchdog::Watchdog; use embassy_rp::watchdog::Watchdog;
use embassy_sync::blocking_mutex::Mutex; use embassy_sync::blocking_mutex::Mutex;
@ -34,7 +34,7 @@ async fn main(_s: Spawner) {
let mut watchdog = Watchdog::new(p.WATCHDOG); let mut watchdog = Watchdog::new(p.WATCHDOG);
watchdog.start(Duration::from_secs(8)); watchdog.start(Duration::from_secs(8));
let flash = Flash::<_, flash::Blocking, FLASH_SIZE>::new(p.FLASH); let flash = Flash::<_, _, FLASH_SIZE>::new_blocking(p.FLASH);
let flash = Mutex::new(RefCell::new(flash)); let flash = Mutex::new(RefCell::new(flash));
let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash);

View file

@ -28,12 +28,12 @@ async fn main(_spawner: Spawner) {
let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0); let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0);
// Get JEDEC id // Get JEDEC id
let jedec = flash.jedec_id().unwrap(); let jedec = flash.blocking_jedec_id().unwrap();
info!("jedec id: 0x{:x}", jedec); info!("jedec id: 0x{:x}", jedec);
// Get unique id // Get unique id
let mut uid = [0; 8]; let mut uid = [0; 8];
flash.unique_id(&mut uid).unwrap(); flash.blocking_unique_id(&mut uid).unwrap();
info!("unique id: {:?}", uid); info!("unique id: {:?}", uid);
erase_write_sector(&mut flash, 0x00); erase_write_sector(&mut flash, 0x00);
@ -48,25 +48,25 @@ async fn main(_spawner: Spawner) {
fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) { fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
info!(">>>> [multiwrite_bytes]"); info!(">>>> [multiwrite_bytes]");
let mut read_buf = [0u8; ERASE_SIZE]; let mut read_buf = [0u8; ERASE_SIZE];
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32); info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
info!("Contents start with {=[u8]}", read_buf[0..4]); info!("Contents start with {=[u8]}", read_buf[0..4]);
defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32)); defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
info!("Contents after erase starts with {=[u8]}", read_buf[0..4]); info!("Contents after erase starts with {=[u8]}", read_buf[0..4]);
if read_buf.iter().any(|x| *x != 0xFF) { if read_buf.iter().any(|x| *x != 0xFF) {
defmt::panic!("unexpected"); defmt::panic!("unexpected");
} }
defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &[0x01])); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &[0x01]));
defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 1, &[0x02])); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 1, &[0x02]));
defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 2, &[0x03])); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 2, &[0x03]));
defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 3, &[0x04])); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 3, &[0x04]));
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
info!("Contents after write starts with {=[u8]}", read_buf[0..4]); info!("Contents after write starts with {=[u8]}", read_buf[0..4]);
if &read_buf[0..4] != &[0x01, 0x02, 0x03, 0x04] { if &read_buf[0..4] != &[0x01, 0x02, 0x03, 0x04] {
defmt::panic!("unexpected"); defmt::panic!("unexpected");
@ -76,14 +76,14 @@ fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH
fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) { fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
info!(">>>> [erase_write_sector]"); info!(">>>> [erase_write_sector]");
let mut buf = [0u8; ERASE_SIZE]; let mut buf = [0u8; ERASE_SIZE];
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32); info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
info!("Contents start with {=[u8]}", buf[0..4]); info!("Contents start with {=[u8]}", buf[0..4]);
defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32)); defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
info!("Contents after erase starts with {=[u8]}", buf[0..4]); info!("Contents after erase starts with {=[u8]}", buf[0..4]);
if buf.iter().any(|x| *x != 0xFF) { if buf.iter().any(|x| *x != 0xFF) {
defmt::panic!("unexpected"); defmt::panic!("unexpected");
@ -93,9 +93,9 @@ fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLA
*b = 0xDA; *b = 0xDA;
} }
defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &buf)); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &buf));
defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
info!("Contents after write starts with {=[u8]}", buf[0..4]); info!("Contents after write starts with {=[u8]}", buf[0..4]);
if buf.iter().any(|x| *x != 0xDA) { if buf.iter().any(|x| *x != 0xDA) {
defmt::panic!("unexpected"); defmt::panic!("unexpected");
@ -111,7 +111,7 @@ async fn background_read(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async,
info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32); info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
info!("Contents start with {=u32:x}", buf[0]); info!("Contents start with {=u32:x}", buf[0]);
defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32)); defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await; defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
info!("Contents after erase starts with {=u32:x}", buf[0]); info!("Contents after erase starts with {=u32:x}", buf[0]);
@ -123,7 +123,7 @@ async fn background_read(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async,
*b = 0xDABA1234; *b = 0xDABA1234;
} }
defmt::unwrap!(flash.write(ADDR_OFFSET + offset, unsafe { defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, unsafe {
core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len() * 4) core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len() * 4)
})); }));

View file

@ -25,23 +25,23 @@ async fn main(_spawner: Spawner) {
let mut flash = embassy_rp::flash::Flash::<_, Async, { 2 * 1024 * 1024 }>::new(p.FLASH, p.DMA_CH0); let mut flash = embassy_rp::flash::Flash::<_, Async, { 2 * 1024 * 1024 }>::new(p.FLASH, p.DMA_CH0);
// Get JEDEC id // Get JEDEC id
let jedec = defmt::unwrap!(flash.jedec_id()); let jedec = defmt::unwrap!(flash.blocking_jedec_id());
info!("jedec id: 0x{:x}", jedec); info!("jedec id: 0x{:x}", jedec);
// Get unique id // Get unique id
let mut uid = [0; 8]; let mut uid = [0; 8];
defmt::unwrap!(flash.unique_id(&mut uid)); defmt::unwrap!(flash.blocking_unique_id(&mut uid));
info!("unique id: {:?}", uid); info!("unique id: {:?}", uid);
let mut buf = [0u8; ERASE_SIZE]; let mut buf = [0u8; ERASE_SIZE];
defmt::unwrap!(flash.read(ADDR_OFFSET, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));
info!("Addr of flash block is {:x}", ADDR_OFFSET + FLASH_BASE as u32); info!("Addr of flash block is {:x}", ADDR_OFFSET + FLASH_BASE as u32);
info!("Contents start with {=[u8]}", buf[0..4]); info!("Contents start with {=[u8]}", buf[0..4]);
defmt::unwrap!(flash.erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32)); defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32));
defmt::unwrap!(flash.read(ADDR_OFFSET, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));
info!("Contents after erase starts with {=[u8]}", buf[0..4]); info!("Contents after erase starts with {=[u8]}", buf[0..4]);
if buf.iter().any(|x| *x != 0xFF) { if buf.iter().any(|x| *x != 0xFF) {
defmt::panic!("unexpected"); defmt::panic!("unexpected");
@ -51,9 +51,9 @@ async fn main(_spawner: Spawner) {
*b = 0xDA; *b = 0xDA;
} }
defmt::unwrap!(flash.write(ADDR_OFFSET, &mut buf)); defmt::unwrap!(flash.blocking_write(ADDR_OFFSET, &mut buf));
defmt::unwrap!(flash.read(ADDR_OFFSET, &mut buf)); defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));
info!("Contents after write starts with {=[u8]}", buf[0..4]); info!("Contents after write starts with {=[u8]}", buf[0..4]);
if buf.iter().any(|x| *x != 0xDA) { if buf.iter().any(|x| *x != 0xDA) {
defmt::panic!("unexpected"); defmt::panic!("unexpected");