aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-03-05 02:33:02 +0100
committerDario Nieuwenhuis <[email protected]>2023-03-05 02:33:02 +0100
commit8eb8ea617419726915834555266e37568b8504e0 (patch)
tree5e151f76366daab88a73b36e55d7e6b778a394bd
parent75f69803af244329ba6dd9093599be357f12ae60 (diff)
nrf/qspi: remove FLASH_SIZE const generic param.
-rw-r--r--embassy-nrf/src/qspi.rs36
-rw-r--r--examples/nrf52840/src/bin/qspi.rs2
-rw-r--r--examples/nrf52840/src/bin/qspi_lowpower.rs2
3 files changed, 22 insertions, 18 deletions
diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs
index 404438ea7..3f7f464d0 100644
--- a/embassy-nrf/src/qspi.rs
+++ b/embassy-nrf/src/qspi.rs
@@ -8,6 +8,7 @@ use core::task::Poll;
8 8
9use embassy_hal_common::drop::OnDrop; 9use embassy_hal_common::drop::OnDrop;
10use embassy_hal_common::{into_ref, PeripheralRef}; 10use embassy_hal_common::{into_ref, PeripheralRef};
11use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
11 12
12use crate::gpio::{self, Pin as GpioPin}; 13use crate::gpio::{self, Pin as GpioPin};
13use crate::interrupt::{Interrupt, InterruptExt}; 14use crate::interrupt::{Interrupt, InterruptExt};
@@ -82,6 +83,8 @@ pub struct Config {
82 pub spi_mode: SpiMode, 83 pub spi_mode: SpiMode,
83 /// Addressing mode (24-bit or 32-bit) 84 /// Addressing mode (24-bit or 32-bit)
84 pub address_mode: AddressMode, 85 pub address_mode: AddressMode,
86 /// Flash memory capacity in bytes. This is the value reported by the `embedded-storage` traits.
87 pub capacity: u32,
85} 88}
86 89
87impl Default for Config { 90impl Default for Config {
@@ -96,6 +99,7 @@ impl Default for Config {
96 sck_delay: 80, 99 sck_delay: 80,
97 spi_mode: SpiMode::MODE0, 100 spi_mode: SpiMode::MODE0,
98 address_mode: AddressMode::_24BIT, 101 address_mode: AddressMode::_24BIT,
102 capacity: 0,
99 } 103 }
100 } 104 }
101} 105}
@@ -111,12 +115,13 @@ pub enum Error {
111} 115}
112 116
113/// QSPI flash driver. 117/// QSPI flash driver.
114pub struct Qspi<'d, T: Instance, const FLASH_SIZE: u32> { 118pub struct Qspi<'d, T: Instance> {
115 irq: PeripheralRef<'d, T::Interrupt>, 119 irq: PeripheralRef<'d, T::Interrupt>,
116 dpm_enabled: bool, 120 dpm_enabled: bool,
121 capacity: u32,
117} 122}
118 123
119impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> { 124impl<'d, T: Instance> Qspi<'d, T> {
120 /// Create a new QSPI driver. 125 /// Create a new QSPI driver.
121 pub fn new( 126 pub fn new(
122 _qspi: impl Peripheral<P = T> + 'd, 127 _qspi: impl Peripheral<P = T> + 'd,
@@ -128,7 +133,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
128 io2: impl Peripheral<P = impl GpioPin> + 'd, 133 io2: impl Peripheral<P = impl GpioPin> + 'd,
129 io3: impl Peripheral<P = impl GpioPin> + 'd, 134 io3: impl Peripheral<P = impl GpioPin> + 'd,
130 config: Config, 135 config: Config,
131 ) -> Qspi<'d, T, FLASH_SIZE> { 136 ) -> Self {
132 into_ref!(irq, sck, csn, io0, io1, io2, io3); 137 into_ref!(irq, sck, csn, io0, io1, io2, io3);
133 138
134 let r = T::regs(); 139 let r = T::regs();
@@ -194,6 +199,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
194 let res = Self { 199 let res = Self {
195 dpm_enabled: config.deep_power_down.is_some(), 200 dpm_enabled: config.deep_power_down.is_some(),
196 irq, 201 irq,
202 capacity: config.capacity,
197 }; 203 };
198 204
199 r.events_ready.reset(); 205 r.events_ready.reset();
@@ -326,7 +332,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
326 assert_eq!(data.as_ptr() as u32 % 4, 0); 332 assert_eq!(data.as_ptr() as u32 % 4, 0);
327 assert_eq!(data.len() as u32 % 4, 0); 333 assert_eq!(data.len() as u32 % 4, 0);
328 assert_eq!(address % 4, 0); 334 assert_eq!(address % 4, 0);
329 if address > FLASH_SIZE { 335 if address > self.capacity {
330 return Err(Error::OutOfBounds); 336 return Err(Error::OutOfBounds);
331 } 337 }
332 338
@@ -348,7 +354,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
348 assert_eq!(data.len() as u32 % 4, 0); 354 assert_eq!(data.len() as u32 % 4, 0);
349 assert_eq!(address % 4, 0); 355 assert_eq!(address % 4, 0);
350 356
351 if address > FLASH_SIZE { 357 if address > self.capacity {
352 return Err(Error::OutOfBounds); 358 return Err(Error::OutOfBounds);
353 } 359 }
354 360
@@ -366,7 +372,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
366 372
367 fn start_erase(&mut self, address: u32) -> Result<(), Error> { 373 fn start_erase(&mut self, address: u32) -> Result<(), Error> {
368 assert_eq!(address % 4096, 0); 374 assert_eq!(address % 4096, 0);
369 if address > FLASH_SIZE { 375 if address > self.capacity {
370 return Err(Error::OutOfBounds); 376 return Err(Error::OutOfBounds);
371 } 377 }
372 378
@@ -439,7 +445,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Qspi<'d, T, FLASH_SIZE> {
439 } 445 }
440} 446}
441 447
442impl<'d, T: Instance, const FLASH_SIZE: u32> Drop for Qspi<'d, T, FLASH_SIZE> { 448impl<'d, T: Instance> Drop for Qspi<'d, T> {
443 fn drop(&mut self) { 449 fn drop(&mut self) {
444 let r = T::regs(); 450 let r = T::regs();
445 451
@@ -484,9 +490,7 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> Drop for Qspi<'d, T, FLASH_SIZE> {
484 } 490 }
485} 491}
486 492
487use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash}; 493impl<'d, T: Instance> ErrorType for Qspi<'d, T> {
488
489impl<'d, T: Instance, const FLASH_SIZE: u32> ErrorType for Qspi<'d, T, FLASH_SIZE> {
490 type Error = Error; 494 type Error = Error;
491} 495}
492 496
@@ -496,7 +500,7 @@ impl NorFlashError for Error {
496 } 500 }
497} 501}
498 502
499impl<'d, T: Instance, const FLASH_SIZE: u32> ReadNorFlash for Qspi<'d, T, FLASH_SIZE> { 503impl<'d, T: Instance> ReadNorFlash for Qspi<'d, T> {
500 const READ_SIZE: usize = 4; 504 const READ_SIZE: usize = 4;
501 505
502 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 506 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
@@ -505,11 +509,11 @@ impl<'d, T: Instance, const FLASH_SIZE: u32> ReadNorFlash for Qspi<'d, T, FLASH_
505 } 509 }
506 510
507 fn capacity(&self) -> usize { 511 fn capacity(&self) -> usize {
508 FLASH_SIZE as usize 512 self.capacity as usize
509 } 513 }
510} 514}
511 515
512impl<'d, T: Instance, const FLASH_SIZE: u32> NorFlash for Qspi<'d, T, FLASH_SIZE> { 516impl<'d, T: Instance> NorFlash for Qspi<'d, T> {
513 const WRITE_SIZE: usize = 4; 517 const WRITE_SIZE: usize = 4;
514 const ERASE_SIZE: usize = 4096; 518 const ERASE_SIZE: usize = 4096;
515 519
@@ -534,7 +538,7 @@ mod _eh1 {
534 538
535 use super::*; 539 use super::*;
536 540
537 impl<'d, T: Instance, const FLASH_SIZE: u32> AsyncNorFlash for Qspi<'d, T, FLASH_SIZE> { 541 impl<'d, T: Instance> AsyncNorFlash for Qspi<'d, T> {
538 const WRITE_SIZE: usize = <Self as NorFlash>::WRITE_SIZE; 542 const WRITE_SIZE: usize = <Self as NorFlash>::WRITE_SIZE;
539 const ERASE_SIZE: usize = <Self as NorFlash>::ERASE_SIZE; 543 const ERASE_SIZE: usize = <Self as NorFlash>::ERASE_SIZE;
540 544
@@ -554,7 +558,7 @@ mod _eh1 {
554 } 558 }
555 } 559 }
556 560
557 impl<'d, T: Instance, const FLASH_SIZE: u32> AsyncReadNorFlash for Qspi<'d, T, FLASH_SIZE> { 561 impl<'d, T: Instance> AsyncReadNorFlash for Qspi<'d, T> {
558 const READ_SIZE: usize = 4; 562 const READ_SIZE: usize = 4;
559 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 563 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
560 fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> { 564 fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
@@ -562,7 +566,7 @@ mod _eh1 {
562 } 566 }
563 567
564 fn capacity(&self) -> usize { 568 fn capacity(&self) -> usize {
565 FLASH_SIZE as usize 569 self.capacity as usize
566 } 570 }
567 } 571 }
568} 572}
diff --git a/examples/nrf52840/src/bin/qspi.rs b/examples/nrf52840/src/bin/qspi.rs
index bc55f8463..be665149a 100644
--- a/examples/nrf52840/src/bin/qspi.rs
+++ b/examples/nrf52840/src/bin/qspi.rs
@@ -24,7 +24,7 @@ async fn main(_spawner: Spawner) {
24 config.write_page_size = qspi::WritePageSize::_256BYTES; 24 config.write_page_size = qspi::WritePageSize::_256BYTES;
25 25
26 let irq = interrupt::take!(QSPI); 26 let irq = interrupt::take!(QSPI);
27 let mut q: qspi::Qspi<_, 67108864> = qspi::Qspi::new( 27 let mut q = qspi::Qspi::new(
28 p.QSPI, irq, p.P0_19, p.P0_17, p.P0_20, p.P0_21, p.P0_22, p.P0_23, config, 28 p.QSPI, irq, p.P0_19, p.P0_17, p.P0_20, p.P0_21, p.P0_22, p.P0_23, config,
29 ); 29 );
30 30
diff --git a/examples/nrf52840/src/bin/qspi_lowpower.rs b/examples/nrf52840/src/bin/qspi_lowpower.rs
index 9341a2376..5008481c1 100644
--- a/examples/nrf52840/src/bin/qspi_lowpower.rs
+++ b/examples/nrf52840/src/bin/qspi_lowpower.rs
@@ -31,7 +31,7 @@ async fn main(_p: Spawner) {
31 exit_time: 3, // tRDP = 35uS 31 exit_time: 3, // tRDP = 35uS
32 }); 32 });
33 33
34 let mut q: qspi::Qspi<_, 67108864> = qspi::Qspi::new( 34 let mut q = qspi::Qspi::new(
35 &mut p.QSPI, 35 &mut p.QSPI,
36 &mut irq, 36 &mut irq,
37 &mut p.P0_19, 37 &mut p.P0_19,