aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-03-25 06:25:12 +0100
committerRasmus Melchior Jacobsen <[email protected]>2023-03-25 06:25:12 +0100
commit99c4346579cd000128d6b14aca98968bc4bbad22 (patch)
treef38041aba24e4258a2ecb9fd73665fceb929103f
parent7edd72f8f542e81143d2375f1783418404d1dc58 (diff)
Add f7 computation to hal common and add tests
-rw-r--r--embassy-hal-common/src/stm32/flash/f4.rs (renamed from embassy-hal-common/src/stm32/flash.rs)86
-rw-r--r--embassy-hal-common/src/stm32/flash/f7.rs57
-rw-r--r--embassy-hal-common/src/stm32/flash/mod.rs2
3 files changed, 101 insertions, 44 deletions
diff --git a/embassy-hal-common/src/stm32/flash.rs b/embassy-hal-common/src/stm32/flash/f4.rs
index 46c42aff9..44d6c08c4 100644
--- a/embassy-hal-common/src/stm32/flash.rs
+++ b/embassy-hal-common/src/stm32/flash/f4.rs
@@ -1,62 +1,60 @@
1pub mod f4 { 1const FLASH_BASE: u32 = 0x0800_0000;
2 const FLASH_BASE: u32 = 0x08_00_00_00; 2pub(crate) const SMALL_SECTOR_SIZE: u32 = 16 * 1024;
3 pub(crate) const SMALL_SECTOR_SIZE: u32 = 16 * 1024; 3pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
4 pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024; 4pub(crate) const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
5 pub(crate) const LARGE_SECTOR_SIZE: u32 = 128 * 1024; 5pub const SECOND_BANK_SECTOR_OFFSET: u8 = 12;
6 pub const SECOND_BANK_SECTOR_OFFSET: u8 = 12; 6
7 7#[derive(Debug, PartialEq)]
8 #[derive(Debug, PartialEq)] 8pub struct FlashSector {
9 pub struct FlashSector { 9 pub index: u8,
10 pub index: u8, 10 pub size: u32,
11 pub size: u32, 11}
12 }
13 12
14 pub fn get_sector(addr: u32, dual_bank: bool, flash_size: u32) -> FlashSector { 13pub fn get_sector(address: u32, dual_bank: bool, flash_size: u32) -> FlashSector {
15 let offset = addr - FLASH_BASE; 14 let offset = address - FLASH_BASE;
16 if !dual_bank { 15 if !dual_bank {
16 get_single_bank_sector(offset)
17 } else {
18 let bank_size = flash_size / 2;
19 if offset < bank_size {
17 get_single_bank_sector(offset) 20 get_single_bank_sector(offset)
18 } else { 21 } else {
19 let bank_size = flash_size / 2; 22 let sector = get_single_bank_sector(offset - bank_size);
20 if offset < bank_size { 23 FlashSector {
21 get_single_bank_sector(offset) 24 index: SECOND_BANK_SECTOR_OFFSET + sector.index,
22 } else { 25 ..sector
23 let sector = get_single_bank_sector(offset - bank_size);
24 FlashSector {
25 index: SECOND_BANK_SECTOR_OFFSET + sector.index,
26 ..sector
27 }
28 } 26 }
29 } 27 }
30 } 28 }
29}
31 30
32 fn get_single_bank_sector(offset: u32) -> FlashSector { 31fn get_single_bank_sector(offset: u32) -> FlashSector {
33 // First 4 sectors are 16KB, then one 64KB, and rest are 128KB 32 // First 4 sectors are 16KB, then one 64KB, and rest are 128KB
34 33
35 match offset / LARGE_SECTOR_SIZE { 34 match offset / LARGE_SECTOR_SIZE {
36 0 => { 35 0 => {
37 if offset < 4 * SMALL_SECTOR_SIZE { 36 if offset < 4 * SMALL_SECTOR_SIZE {
38 FlashSector { 37 FlashSector {
39 index: (offset / SMALL_SECTOR_SIZE) as u8, 38 index: (offset / SMALL_SECTOR_SIZE) as u8,
40 size: SMALL_SECTOR_SIZE, 39 size: SMALL_SECTOR_SIZE,
41 } 40 }
42 } else { 41 } else {
43 FlashSector { 42 FlashSector {
44 index: 4, 43 index: 4,
45 size: MEDIUM_SECTOR_SIZE, 44 size: MEDIUM_SECTOR_SIZE,
46 }
47 } 45 }
48 } 46 }
49 i => FlashSector {
50 index: 4 + i as u8,
51 size: LARGE_SECTOR_SIZE,
52 },
53 } 47 }
48 i => FlashSector {
49 index: 4 + i as u8,
50 size: LARGE_SECTOR_SIZE,
51 },
54 } 52 }
55} 53}
56 54
57#[cfg(test)] 55#[cfg(test)]
58mod tests { 56mod tests {
59 use super::f4::*; 57 use super::*;
60 58
61 #[test] 59 #[test]
62 fn can_get_sector_single_bank() { 60 fn can_get_sector_single_bank() {
diff --git a/embassy-hal-common/src/stm32/flash/f7.rs b/embassy-hal-common/src/stm32/flash/f7.rs
new file mode 100644
index 000000000..90938e6ab
--- /dev/null
+++ b/embassy-hal-common/src/stm32/flash/f7.rs
@@ -0,0 +1,57 @@
1const FLASH_BASE: u32 = 0x0800_0000;
2pub(crate) const SMALL_SECTOR_SIZE: u32 = 32 * 1024;
3pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 128 * 1024;
4pub(crate) const LARGE_SECTOR_SIZE: u32 = 256 * 1024;
5
6#[derive(Debug, PartialEq)]
7pub struct FlashSector {
8 pub index: u8,
9 pub size: u32,
10}
11
12pub fn get_sector(address: u32) -> FlashSector {
13 // First 4 sectors are 32KB, then one 128KB, and rest are 256KB
14 let offset = address - FLASH_BASE;
15 match offset / LARGE_SECTOR_SIZE {
16 0 => {
17 if offset < 4 * SMALL_SECTOR_SIZE {
18 FlashSector {
19 index: (offset / SMALL_SECTOR_SIZE) as u8,
20 size: SMALL_SECTOR_SIZE,
21 }
22 } else {
23 FlashSector {
24 index: 4,
25 size: MEDIUM_SECTOR_SIZE,
26 }
27 }
28 }
29 i => FlashSector {
30 index: 4 + i as u8,
31 size: LARGE_SECTOR_SIZE,
32 },
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn can_get_sector() {
42 let assert_sector = |index: u8, size: u32, addr: u32| assert_eq!(FlashSector { index, size }, get_sector(addr));
43
44 assert_sector(0, SMALL_SECTOR_SIZE, 0x0800_0000);
45 assert_sector(0, SMALL_SECTOR_SIZE, 0x0800_7FFF);
46 assert_sector(3, SMALL_SECTOR_SIZE, 0x0801_8000);
47 assert_sector(3, SMALL_SECTOR_SIZE, 0x0801_FFFF);
48
49 assert_sector(4, MEDIUM_SECTOR_SIZE, 0x0802_0000);
50 assert_sector(4, MEDIUM_SECTOR_SIZE, 0x0803_FFFF);
51
52 assert_sector(5, LARGE_SECTOR_SIZE, 0x0804_0000);
53 assert_sector(5, LARGE_SECTOR_SIZE, 0x0807_FFFF);
54 assert_sector(7, LARGE_SECTOR_SIZE, 0x080C_0000);
55 assert_sector(7, LARGE_SECTOR_SIZE, 0x080F_FFFF);
56 }
57}
diff --git a/embassy-hal-common/src/stm32/flash/mod.rs b/embassy-hal-common/src/stm32/flash/mod.rs
new file mode 100644
index 000000000..1452b491d
--- /dev/null
+++ b/embassy-hal-common/src/stm32/flash/mod.rs
@@ -0,0 +1,2 @@
1pub mod f4;
2pub mod f7;