aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/flash
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2022-04-26 18:33:09 +0200
committerUlf Lilleengen <[email protected]>2022-04-27 15:17:18 +0200
commitda61611f8f57410a87106961efd24d80e6a8f63e (patch)
tree81bf5f96a052be8cc74fa4f513592adf1f4bb1db /embassy-stm32/src/flash
parent484e0acc638c27366e19275c32db9c8487ea8fba (diff)
Add bootloader to CI
Diffstat (limited to 'embassy-stm32/src/flash')
-rw-r--r--embassy-stm32/src/flash/mod.rs84
1 files changed, 21 insertions, 63 deletions
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 7a282497f..cff3119fd 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -10,59 +10,12 @@ use embedded_storage::nor_flash::{
10 ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash, 10 ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
11}; 11};
12 12
13const FLASH_BASE: usize = 0x8000000; 13pub use crate::pac::ERASE_SIZE;
14 14pub use crate::pac::ERASE_VALUE;
15#[cfg(flash_l4)] 15pub use crate::pac::FLASH_BASE;
16mod config { 16pub use crate::pac::FLASH_SIZE;
17 use super::*; 17pub use crate::pac::WRITE_SIZE;
18 pub(crate) const FLASH_SIZE: usize = 0x100000; 18const FLASH_END: usize = FLASH_BASE + FLASH_SIZE;
19 pub(crate) const FLASH_START: usize = FLASH_BASE;
20 pub(crate) const FLASH_END: usize = FLASH_START + FLASH_SIZE;
21 pub(crate) const PAGE_SIZE: usize = 2048;
22 pub(crate) const WORD_SIZE: usize = 8;
23}
24
25#[cfg(flash_wb)]
26mod config {
27 use super::*;
28 pub(crate) const FLASH_SIZE: usize = 0x100000;
29 pub(crate) const FLASH_START: usize = FLASH_BASE;
30 pub(crate) const FLASH_END: usize = FLASH_START + FLASH_SIZE;
31 pub(crate) const PAGE_SIZE: usize = 4096;
32 pub(crate) const WORD_SIZE: usize = 8;
33}
34
35#[cfg(flash_wl)]
36mod config {
37 use super::*;
38 pub(crate) const FLASH_SIZE: usize = 0x40000;
39 pub(crate) const FLASH_START: usize = FLASH_BASE;
40 pub(crate) const FLASH_END: usize = FLASH_START + FLASH_SIZE;
41 pub(crate) const PAGE_SIZE: usize = 2048;
42 pub(crate) const WORD_SIZE: usize = 8;
43}
44
45#[cfg(flash_l0)]
46mod config {
47 use super::*;
48 pub(crate) const FLASH_SIZE: usize = 0x30000;
49 pub(crate) const FLASH_START: usize = FLASH_BASE;
50 pub(crate) const FLASH_END: usize = FLASH_START + FLASH_SIZE;
51 pub(crate) const PAGE_SIZE: usize = 128;
52 pub(crate) const WORD_SIZE: usize = 4;
53}
54
55#[cfg(flash_l1)]
56mod config {
57 use super::*;
58 pub(crate) const FLASH_SIZE: usize = 0x80000;
59 pub(crate) const FLASH_START: usize = FLASH_BASE;
60 pub(crate) const FLASH_END: usize = FLASH_START + FLASH_SIZE;
61 pub(crate) const PAGE_SIZE: usize = 256;
62 pub(crate) const WORD_SIZE: usize = 4;
63}
64
65use config::*;
66 19
67pub struct Flash<'d> { 20pub struct Flash<'d> {
68 _inner: FLASH, 21 _inner: FLASH,
@@ -114,6 +67,7 @@ impl<'d> Flash<'d> {
114 } 67 }
115 68
116 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { 69 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
70 let offset = FLASH_BASE as u32 + offset;
117 if offset as usize >= FLASH_END || offset as usize + bytes.len() > FLASH_END { 71 if offset as usize >= FLASH_END || offset as usize + bytes.len() > FLASH_END {
118 return Err(Error::Size); 72 return Err(Error::Size);
119 } 73 }
@@ -124,23 +78,25 @@ impl<'d> Flash<'d> {
124 } 78 }
125 79
126 pub fn blocking_write(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> { 80 pub fn blocking_write(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
81 let offset = FLASH_BASE as u32 + offset;
127 if offset as usize + buf.len() > FLASH_END { 82 if offset as usize + buf.len() > FLASH_END {
128 return Err(Error::Size); 83 return Err(Error::Size);
129 } 84 }
130 if offset as usize % WORD_SIZE != 0 || buf.len() as usize % WORD_SIZE != 0 { 85 if offset as usize % WRITE_SIZE != 0 || buf.len() as usize % WRITE_SIZE != 0 {
131 return Err(Error::Unaligned); 86 return Err(Error::Unaligned);
132 } 87 }
88 trace!("Writing {} bytes at 0x{:x}", buf.len(), offset);
133 89
134 self.clear_all_err(); 90 self.clear_all_err();
135 91
136 #[cfg(any(flash_wl, flash_wb, flash_l4))] 92 #[cfg(any(flash_wl, flash_wb, flash_l4))]
137 unsafe { 93 unsafe {
138 pac::FLASH.cr().write(|w| w.set_pg(true)); 94 pac::FLASH.cr().write(|w| w.set_pg(true))
139 } 95 }
140 96
141 let mut ret: Result<(), Error> = Ok(()); 97 let mut ret: Result<(), Error> = Ok(());
142 let mut offset = offset; 98 let mut offset = offset;
143 for chunk in buf.chunks(WORD_SIZE) { 99 for chunk in buf.chunks(WRITE_SIZE) {
144 for val in chunk.chunks(4) { 100 for val in chunk.chunks(4) {
145 unsafe { 101 unsafe {
146 write_volatile( 102 write_volatile(
@@ -159,23 +115,25 @@ impl<'d> Flash<'d> {
159 115
160 #[cfg(any(flash_wl, flash_wb, flash_l4))] 116 #[cfg(any(flash_wl, flash_wb, flash_l4))]
161 unsafe { 117 unsafe {
162 pac::FLASH.cr().write(|w| w.set_pg(false)); 118 pac::FLASH.cr().write(|w| w.set_pg(false))
163 } 119 }
164 120
165 ret 121 ret
166 } 122 }
167 123
168 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { 124 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
125 let from = FLASH_BASE as u32 + from;
126 let to = FLASH_BASE as u32 + to;
169 if to < from || to as usize > FLASH_END { 127 if to < from || to as usize > FLASH_END {
170 return Err(Error::Size); 128 return Err(Error::Size);
171 } 129 }
172 if from as usize % PAGE_SIZE != 0 || to as usize % PAGE_SIZE != 0 { 130 if from as usize % ERASE_SIZE != 0 || to as usize % ERASE_SIZE != 0 {
173 return Err(Error::Unaligned); 131 return Err(Error::Unaligned);
174 } 132 }
175 133
176 self.clear_all_err(); 134 self.clear_all_err();
177 135
178 for page in (from..to).step_by(PAGE_SIZE) { 136 for page in (from..to).step_by(ERASE_SIZE) {
179 #[cfg(any(flash_l0, flash_l1))] 137 #[cfg(any(flash_l0, flash_l1))]
180 unsafe { 138 unsafe {
181 pac::FLASH.pecr().modify(|w| { 139 pac::FLASH.pecr().modify(|w| {
@@ -188,7 +146,7 @@ impl<'d> Flash<'d> {
188 146
189 #[cfg(any(flash_wl, flash_wb, flash_l4))] 147 #[cfg(any(flash_wl, flash_wb, flash_l4))]
190 unsafe { 148 unsafe {
191 let idx = page / PAGE_SIZE as u32; 149 let idx = page / ERASE_SIZE as u32;
192 150
193 pac::FLASH.cr().modify(|w| { 151 pac::FLASH.cr().modify(|w| {
194 w.set_per(true); 152 w.set_per(true);
@@ -333,7 +291,7 @@ impl NorFlashError for Error {
333} 291}
334 292
335impl<'d> ReadNorFlash for Flash<'d> { 293impl<'d> ReadNorFlash for Flash<'d> {
336 const READ_SIZE: usize = WORD_SIZE; 294 const READ_SIZE: usize = WRITE_SIZE;
337 295
338 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 296 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
339 self.blocking_read(offset, bytes) 297 self.blocking_read(offset, bytes)
@@ -345,8 +303,8 @@ impl<'d> ReadNorFlash for Flash<'d> {
345} 303}
346 304
347impl<'d> NorFlash for Flash<'d> { 305impl<'d> NorFlash for Flash<'d> {
348 const WRITE_SIZE: usize = WORD_SIZE; 306 const WRITE_SIZE: usize = WRITE_SIZE;
349 const ERASE_SIZE: usize = PAGE_SIZE; 307 const ERASE_SIZE: usize = ERASE_SIZE;
350 308
351 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { 309 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
352 self.blocking_erase(from, to) 310 self.blocking_erase(from, to)