diff options
| author | Dion Dokter <[email protected]> | 2022-11-10 14:37:42 +0100 |
|---|---|---|
| committer | Dion Dokter <[email protected]> | 2022-11-10 14:37:42 +0100 |
| commit | dbe97b409891ed865d07edc29cec4d8af13e55d9 (patch) | |
| tree | 60813d79e9d679508288da060e75faa52c64df12 | |
| parent | 059610a8de49ff2d38311f343d3d1a6f8d90a720 (diff) | |
Adapted nvmc so it can be used for all nrf targets
| -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 | 70 |
5 files changed, 75 insertions, 11 deletions
diff --git a/embassy-nrf/src/chips/nrf5340_app.rs b/embassy-nrf/src/chips/nrf5340_app.rs index 7845d4a8e..e20edcdf3 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 ae136e09d..8c292e52b 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 b5a53ed80..5c00b65a2 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 bc70fc2f6..cac55071c 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -74,7 +74,6 @@ pub mod buffered_uarte; | |||
| 74 | pub mod gpio; | 74 | pub mod gpio; |
| 75 | #[cfg(feature = "gpiote")] | 75 | #[cfg(feature = "gpiote")] |
| 76 | pub mod gpiote; | 76 | pub mod gpiote; |
| 77 | #[cfg(not(any(feature = "_nrf5340", feature = "_nrf9160")))] | ||
| 78 | pub mod nvmc; | 77 | pub mod nvmc; |
| 79 | #[cfg(any( | 78 | #[cfg(any( |
| 80 | feature = "nrf52810", | 79 | feature = "nrf52810", |
diff --git a/embassy-nrf/src/nvmc.rs b/embassy-nrf/src/nvmc.rs index 6f66f7a78..ba6a59129 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,56 @@ 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: u32) { | ||
| 76 | Self::regs().erasepage().write(|w| unsafe { w.bits(page) }); | ||
| 77 | } | ||
| 78 | |||
| 79 | #[cfg(any(feature = "_nrf9160", feature = "_nrf5340"))] | ||
| 80 | fn erase_page(&mut self, page: u32) { | ||
| 81 | #[cfg(not(feature = "_nrf5340-net"))] | ||
| 82 | const FLASH_START_ADDR: u32 = 0; | ||
| 83 | #[cfg(feature = "_nrf5340-net")] | ||
| 84 | const FLASH_START_ADDR: u32 = 0x100_0000; | ||
| 85 | |||
| 86 | let first_page_word = (FLASH_START_ADDR + page * PAGE_SIZE as u32) as *mut u32; | ||
| 87 | unsafe { | ||
| 88 | first_page_word.write_volatile(0xFFFF_FFFF); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | fn enable_erase(&self) { | ||
| 93 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 94 | Self::regs().config.write(|w| w.wen().een()); | ||
| 95 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 96 | Self::regs().configns.write(|w| w.wen().een()); | ||
| 97 | } | ||
| 98 | |||
| 99 | fn enable_read(&self) { | ||
| 100 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 101 | Self::regs().config.write(|w| w.wen().ren()); | ||
| 102 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 103 | Self::regs().configns.write(|w| w.wen().ren()); | ||
| 104 | } | ||
| 105 | |||
| 106 | fn enable_write(&self) { | ||
| 107 | #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] | ||
| 108 | Self::regs().config.write(|w| w.wen().wen()); | ||
| 109 | #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] | ||
| 110 | Self::regs().configns.write(|w| w.wen().wen()); | ||
| 111 | } | ||
| 58 | } | 112 | } |
| 59 | 113 | ||
| 60 | impl<'d> MultiwriteNorFlash for Nvmc<'d> {} | 114 | impl<'d> MultiwriteNorFlash for Nvmc<'d> {} |
| @@ -93,17 +147,15 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 93 | return Err(Error::Unaligned); | 147 | return Err(Error::Unaligned); |
| 94 | } | 148 | } |
| 95 | 149 | ||
| 96 | let p = Self::regs(); | 150 | self.enable_erase(); |
| 97 | |||
| 98 | p.config.write(|w| w.wen().een()); | ||
| 99 | self.wait_ready(); | 151 | self.wait_ready(); |
| 100 | 152 | ||
| 101 | for page in (from..to).step_by(PAGE_SIZE) { | 153 | for page in (from..to).step_by(PAGE_SIZE) { |
| 102 | p.erasepage().write(|w| unsafe { w.bits(page) }); | 154 | self.erase_page(page); |
| 103 | self.wait_ready(); | 155 | self.wait_ready(); |
| 104 | } | 156 | } |
| 105 | 157 | ||
| 106 | p.config.reset(); | 158 | self.enable_read(); |
| 107 | self.wait_ready(); | 159 | self.wait_ready(); |
| 108 | 160 | ||
| 109 | Ok(()) | 161 | Ok(()) |
| @@ -117,9 +169,7 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 117 | return Err(Error::Unaligned); | 169 | return Err(Error::Unaligned); |
| 118 | } | 170 | } |
| 119 | 171 | ||
| 120 | let p = Self::regs(); | 172 | self.enable_write(); |
| 121 | |||
| 122 | p.config.write(|w| w.wen().wen()); | ||
| 123 | self.wait_ready(); | 173 | self.wait_ready(); |
| 124 | 174 | ||
| 125 | unsafe { | 175 | unsafe { |
| @@ -129,11 +179,11 @@ impl<'d> NorFlash for Nvmc<'d> { | |||
| 129 | for i in 0..words { | 179 | for i in 0..words { |
| 130 | let w = ptr::read_unaligned(p_src.add(i)); | 180 | let w = ptr::read_unaligned(p_src.add(i)); |
| 131 | ptr::write_volatile(p_dst.add(i), w); | 181 | ptr::write_volatile(p_dst.add(i), w); |
| 132 | self.wait_ready(); | 182 | self.wait_ready_write(); |
| 133 | } | 183 | } |
| 134 | } | 184 | } |
| 135 | 185 | ||
| 136 | p.config.reset(); | 186 | self.enable_read(); |
| 137 | self.wait_ready(); | 187 | self.wait_ready(); |
| 138 | 188 | ||
| 139 | Ok(()) | 189 | Ok(()) |
