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 /embassy-nrf/src/nvmc.rs | |
| parent | 059610a8de49ff2d38311f343d3d1a6f8d90a720 (diff) | |
Adapted nvmc so it can be used for all nrf targets
Diffstat (limited to 'embassy-nrf/src/nvmc.rs')
| -rw-r--r-- | embassy-nrf/src/nvmc.rs | 70 |
1 files changed, 60 insertions, 10 deletions
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(()) |
