diff options
| -rw-r--r-- | embassy-nrf/src/chips/nrf5340_app.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf5340_net.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf9160.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-nrf/src/nvmc.rs | 67 |
5 files changed, 71 insertions, 12 deletions
diff --git a/embassy-nrf/src/chips/nrf5340_app.rs b/embassy-nrf/src/chips/nrf5340_app.rs index 39fb9a9bf..4575f09ff 100644 --- a/embassy-nrf/src/chips/nrf5340_app.rs +++ b/embassy-nrf/src/chips/nrf5340_app.rs | |||
| @@ -213,6 +213,8 @@ pub mod pac { | |||
| 213 | pub const EASY_DMA_SIZE: usize = (1 << 16) - 1; | 213 | pub const EASY_DMA_SIZE: usize = (1 << 16) - 1; |
| 214 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; | 214 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; |
| 215 | 215 | ||
| 216 | pub const FLASH_SIZE: usize = 1024 * 1024; | ||
| 217 | |||
| 216 | embassy_hal_common::peripherals! { | 218 | embassy_hal_common::peripherals! { |
| 217 | // USB | 219 | // USB |
| 218 | USBD, | 220 | USBD, |
| @@ -224,6 +226,9 @@ embassy_hal_common::peripherals! { | |||
| 224 | // WDT | 226 | // WDT |
| 225 | WDT, | 227 | WDT, |
| 226 | 228 | ||
| 229 | // NVMC | ||
| 230 | NVMC, | ||
| 231 | |||
| 227 | // UARTE, TWI & SPI | 232 | // UARTE, TWI & SPI |
| 228 | UARTETWISPI0, | 233 | UARTETWISPI0, |
| 229 | UARTETWISPI1, | 234 | UARTETWISPI1, |
diff --git a/embassy-nrf/src/chips/nrf5340_net.rs b/embassy-nrf/src/chips/nrf5340_net.rs index b8ee7745b..54827238a 100644 --- a/embassy-nrf/src/chips/nrf5340_net.rs +++ b/embassy-nrf/src/chips/nrf5340_net.rs | |||
| @@ -104,6 +104,8 @@ pub mod pac { | |||
| 104 | pub const EASY_DMA_SIZE: usize = (1 << 16) - 1; | 104 | pub const EASY_DMA_SIZE: usize = (1 << 16) - 1; |
| 105 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; | 105 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; |
| 106 | 106 | ||
| 107 | pub const FLASH_SIZE: usize = 256 * 1024; | ||
| 108 | |||
| 107 | embassy_hal_common::peripherals! { | 109 | embassy_hal_common::peripherals! { |
| 108 | // RTC | 110 | // RTC |
| 109 | RTC0, | 111 | RTC0, |
| @@ -112,6 +114,9 @@ embassy_hal_common::peripherals! { | |||
| 112 | // WDT | 114 | // WDT |
| 113 | WDT, | 115 | WDT, |
| 114 | 116 | ||
| 117 | // NVMC | ||
| 118 | NVMC, | ||
| 119 | |||
| 115 | // UARTE, TWI & SPI | 120 | // UARTE, TWI & SPI |
| 116 | UARTETWISPI0, | 121 | UARTETWISPI0, |
| 117 | UARTETWISPI1, | 122 | UARTETWISPI1, |
diff --git a/embassy-nrf/src/chips/nrf9160.rs b/embassy-nrf/src/chips/nrf9160.rs index a47c0a397..472ee6772 100644 --- a/embassy-nrf/src/chips/nrf9160.rs +++ b/embassy-nrf/src/chips/nrf9160.rs | |||
| @@ -164,6 +164,8 @@ pub mod pac { | |||
| 164 | pub const EASY_DMA_SIZE: usize = (1 << 13) - 1; | 164 | pub const EASY_DMA_SIZE: usize = (1 << 13) - 1; |
| 165 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; | 165 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; |
| 166 | 166 | ||
| 167 | pub const FLASH_SIZE: usize = 1024 * 1024; | ||
| 168 | |||
| 167 | embassy_hal_common::peripherals! { | 169 | embassy_hal_common::peripherals! { |
| 168 | // RTC | 170 | // RTC |
| 169 | RTC0, | 171 | RTC0, |
| @@ -172,6 +174,9 @@ embassy_hal_common::peripherals! { | |||
| 172 | // WDT | 174 | // WDT |
| 173 | WDT, | 175 | WDT, |
| 174 | 176 | ||
| 177 | // NVMC | ||
| 178 | NVMC, | ||
| 179 | |||
| 175 | // UARTE, TWI & SPI | 180 | // UARTE, TWI & SPI |
| 176 | UARTETWISPI0, | 181 | UARTETWISPI0, |
| 177 | UARTETWISPI1, | 182 | UARTETWISPI1, |
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 4832e143f..1dd0e7905 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -80,7 +80,6 @@ pub mod gpio; | |||
| 80 | pub mod gpiote; | 80 | pub mod gpiote; |
| 81 | #[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))] | 81 | #[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))] |
| 82 | pub mod i2s; | 82 | pub mod i2s; |
| 83 | #[cfg(not(any(feature = "_nrf5340", feature = "_nrf9160")))] | ||
| 84 | pub mod nvmc; | 83 | pub mod nvmc; |
| 85 | #[cfg(any( | 84 | #[cfg(any( |
| 86 | feature = "nrf52810", | 85 | feature = "nrf52810", |
diff --git a/embassy-nrf/src/nvmc.rs b/embassy-nrf/src/nvmc.rs index 6f66f7a78..405ea3171 100644 --- a/embassy-nrf/src/nvmc.rs +++ b/embassy-nrf/src/nvmc.rs | |||
| @@ -10,8 +10,12 @@ use embedded_storage::nor_flash::{ | |||
| 10 | use crate::peripherals::NVMC; | 10 | use crate::peripherals::NVMC; |
| 11 | use crate::{pac, Peripheral}; | 11 | use crate::{pac, Peripheral}; |
| 12 | 12 | ||
| 13 | #[cfg(not(feature = "_nrf5340-net"))] | ||
| 13 | /// Erase size of NVMC flash in bytes. | 14 | /// Erase size of NVMC flash in bytes. |
| 14 | pub const PAGE_SIZE: usize = 4096; | 15 | pub const PAGE_SIZE: usize = 4096; |
| 16 | #[cfg(feature = "_nrf5340-net")] | ||
| 17 | /// Erase size of NVMC flash in bytes. | ||
| 18 | pub const PAGE_SIZE: usize = 2048; | ||
| 15 | 19 | ||
| 16 | /// Size of NVMC flash in bytes. | 20 | /// Size of NVMC flash in bytes. |
| 17 | pub const FLASH_SIZE: usize = crate::chip::FLASH_SIZE; | 21 | pub const FLASH_SIZE: usize = crate::chip::FLASH_SIZE; |
| @@ -55,6 +59,51 @@ impl<'d> Nvmc<'d> { | |||
| 55 | let p = Self::regs(); | 59 | let p = Self::regs(); |
| 56 | while p.ready.read().ready().is_busy() {} | 60 | while p.ready.read().ready().is_busy() {} |
| 57 | } | 61 | } |
| 62 | |||
| 63 | #[cfg(not(any(feature = "_nrf9160", feature = "_nrf5340")))] | ||
| 64 | fn wait_ready_write(&mut self) { | ||
| 65 | self.wait_ready(); | ||
| 66 | } | ||
| 67 | |||
| 68 | #[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))] | ||
| 69 | fn wait_ready_write(&mut self) { | ||
| 70 | let p = Self::regs(); | ||
| 71 | while p.readynext.read().readynext().is_busy() {} | ||
| 72 | } | ||
| 73 | |||
| 74 | #[cfg(not(any(feature = "_nrf9160", feature = "_nrf5340")))] | ||
| 75 | fn erase_page(&mut self, page_addr: u32) { | ||
| 76 | Self::regs().erasepage().write(|w| unsafe { w.bits(page_addr) }); | ||
| 77 | } | ||
| 78 | |||
| 79 | #[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))] | ||
| 80 | fn erase_page(&mut self, page_addr: u32) { | ||
| 81 | let first_page_word = page_addr as *mut u32; | ||
| 82 | unsafe { | ||
| 83 | first_page_word.write_volatile(0xFFFF_FFFF); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | fn enable_erase(&self) { | ||
| 88 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 89 | Self::regs().config.write(|w| w.wen().een()); | ||
| 90 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 91 | Self::regs().configns.write(|w| w.wen().een()); | ||
| 92 | } | ||
| 93 | |||
| 94 | fn enable_read(&self) { | ||
| 95 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 96 | Self::regs().config.write(|w| w.wen().ren()); | ||
| 97 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 98 | Self::regs().configns.write(|w| w.wen().ren()); | ||
| 99 | } | ||
| 100 | |||
| 101 | fn enable_write(&self) { | ||
| 102 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 103 | Self::regs().config.write(|w| w.wen().wen()); | ||
| 104 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 105 | Self::regs().configns.write(|w| w.wen().wen()); | ||
| 106 | } | ||
| 58 | } | 107 | } |
| 59 | 108 | ||
| 60 | impl<'d> MultiwriteNorFlash for Nvmc<'d> {} | 109 | impl<'d> MultiwriteNorFlash for Nvmc<'d> {} |
| @@ -93,17 +142,15 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 93 | return Err(Error::Unaligned); | 142 | return Err(Error::Unaligned); |
| 94 | } | 143 | } |
| 95 | 144 | ||
| 96 | let p = Self::regs(); | 145 | self.enable_erase(); |
| 97 | |||
| 98 | p.config.write(|w| w.wen().een()); | ||
| 99 | self.wait_ready(); | 146 | self.wait_ready(); |
| 100 | 147 | ||
| 101 | for page in (from..to).step_by(PAGE_SIZE) { | 148 | for page_addr in (from..to).step_by(PAGE_SIZE) { |
| 102 | p.erasepage().write(|w| unsafe { w.bits(page) }); | 149 | self.erase_page(page_addr); |
| 103 | self.wait_ready(); | 150 | self.wait_ready(); |
| 104 | } | 151 | } |
| 105 | 152 | ||
| 106 | p.config.reset(); | 153 | self.enable_read(); |
| 107 | self.wait_ready(); | 154 | self.wait_ready(); |
| 108 | 155 | ||
| 109 | Ok(()) | 156 | Ok(()) |
| @@ -117,9 +164,7 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 117 | return Err(Error::Unaligned); | 164 | return Err(Error::Unaligned); |
| 118 | } | 165 | } |
| 119 | 166 | ||
| 120 | let p = Self::regs(); | 167 | self.enable_write(); |
| 121 | |||
| 122 | p.config.write(|w| w.wen().wen()); | ||
| 123 | self.wait_ready(); | 168 | self.wait_ready(); |
| 124 | 169 | ||
| 125 | unsafe { | 170 | unsafe { |
| @@ -129,11 +174,11 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 129 | for i in 0..words { | 174 | for i in 0..words { |
| 130 | let w = ptr::read_unaligned(p_src.add(i)); | 175 | let w = ptr::read_unaligned(p_src.add(i)); |
| 131 | ptr::write_volatile(p_dst.add(i), w); | 176 | ptr::write_volatile(p_dst.add(i), w); |
| 132 | self.wait_ready(); | 177 | self.wait_ready_write(); |
| 133 | } | 178 | } |
| 134 | } | 179 | } |
| 135 | 180 | ||
| 136 | p.config.reset(); | 181 | self.enable_read(); |
| 137 | self.wait_ready(); | 182 | self.wait_ready(); |
| 138 | 183 | ||
| 139 | Ok(()) | 184 | Ok(()) |
