Add f7 computation to hal common and add tests

This commit is contained in:
Rasmus Melchior Jacobsen 2023-03-25 06:25:12 +01:00
parent 7edd72f8f5
commit 99c4346579
3 changed files with 99 additions and 42 deletions

View file

@ -1,62 +1,60 @@
pub mod f4 { const FLASH_BASE: u32 = 0x0800_0000;
const FLASH_BASE: u32 = 0x08_00_00_00; pub(crate) const SMALL_SECTOR_SIZE: u32 = 16 * 1024;
pub(crate) const SMALL_SECTOR_SIZE: u32 = 16 * 1024; pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024; pub(crate) const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
pub(crate) const LARGE_SECTOR_SIZE: u32 = 128 * 1024; pub const SECOND_BANK_SECTOR_OFFSET: u8 = 12;
pub const SECOND_BANK_SECTOR_OFFSET: u8 = 12;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct FlashSector { pub struct FlashSector {
pub index: u8, pub index: u8,
pub size: u32, pub size: u32,
} }
pub fn get_sector(addr: u32, dual_bank: bool, flash_size: u32) -> FlashSector { pub fn get_sector(address: u32, dual_bank: bool, flash_size: u32) -> FlashSector {
let offset = addr - FLASH_BASE; let offset = address - FLASH_BASE;
if !dual_bank { if !dual_bank {
get_single_bank_sector(offset)
} else {
let bank_size = flash_size / 2;
if offset < bank_size {
get_single_bank_sector(offset) get_single_bank_sector(offset)
} else { } else {
let bank_size = flash_size / 2; let sector = get_single_bank_sector(offset - bank_size);
if offset < bank_size { FlashSector {
get_single_bank_sector(offset) index: SECOND_BANK_SECTOR_OFFSET + sector.index,
} else { ..sector
let sector = get_single_bank_sector(offset - bank_size);
FlashSector {
index: SECOND_BANK_SECTOR_OFFSET + sector.index,
..sector
}
} }
} }
} }
}
fn get_single_bank_sector(offset: u32) -> FlashSector { fn get_single_bank_sector(offset: u32) -> FlashSector {
// First 4 sectors are 16KB, then one 64KB, and rest are 128KB // First 4 sectors are 16KB, then one 64KB, and rest are 128KB
match offset / LARGE_SECTOR_SIZE { match offset / LARGE_SECTOR_SIZE {
0 => { 0 => {
if offset < 4 * SMALL_SECTOR_SIZE { if offset < 4 * SMALL_SECTOR_SIZE {
FlashSector { FlashSector {
index: (offset / SMALL_SECTOR_SIZE) as u8, index: (offset / SMALL_SECTOR_SIZE) as u8,
size: SMALL_SECTOR_SIZE, size: SMALL_SECTOR_SIZE,
} }
} else { } else {
FlashSector { FlashSector {
index: 4, index: 4,
size: MEDIUM_SECTOR_SIZE, size: MEDIUM_SECTOR_SIZE,
}
} }
} }
i => FlashSector {
index: 4 + i as u8,
size: LARGE_SECTOR_SIZE,
},
} }
i => FlashSector {
index: 4 + i as u8,
size: LARGE_SECTOR_SIZE,
},
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::f4::*; use super::*;
#[test] #[test]
fn can_get_sector_single_bank() { fn can_get_sector_single_bank() {

View file

@ -0,0 +1,57 @@
const FLASH_BASE: u32 = 0x0800_0000;
pub(crate) const SMALL_SECTOR_SIZE: u32 = 32 * 1024;
pub(crate) const MEDIUM_SECTOR_SIZE: u32 = 128 * 1024;
pub(crate) const LARGE_SECTOR_SIZE: u32 = 256 * 1024;
#[derive(Debug, PartialEq)]
pub struct FlashSector {
pub index: u8,
pub size: u32,
}
pub fn get_sector(address: u32) -> FlashSector {
// First 4 sectors are 32KB, then one 128KB, and rest are 256KB
let offset = address - FLASH_BASE;
match offset / LARGE_SECTOR_SIZE {
0 => {
if offset < 4 * SMALL_SECTOR_SIZE {
FlashSector {
index: (offset / SMALL_SECTOR_SIZE) as u8,
size: SMALL_SECTOR_SIZE,
}
} else {
FlashSector {
index: 4,
size: MEDIUM_SECTOR_SIZE,
}
}
}
i => FlashSector {
index: 4 + i as u8,
size: LARGE_SECTOR_SIZE,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_get_sector() {
let assert_sector = |index: u8, size: u32, addr: u32| assert_eq!(FlashSector { index, size }, get_sector(addr));
assert_sector(0, SMALL_SECTOR_SIZE, 0x0800_0000);
assert_sector(0, SMALL_SECTOR_SIZE, 0x0800_7FFF);
assert_sector(3, SMALL_SECTOR_SIZE, 0x0801_8000);
assert_sector(3, SMALL_SECTOR_SIZE, 0x0801_FFFF);
assert_sector(4, MEDIUM_SECTOR_SIZE, 0x0802_0000);
assert_sector(4, MEDIUM_SECTOR_SIZE, 0x0803_FFFF);
assert_sector(5, LARGE_SECTOR_SIZE, 0x0804_0000);
assert_sector(5, LARGE_SECTOR_SIZE, 0x0807_FFFF);
assert_sector(7, LARGE_SECTOR_SIZE, 0x080C_0000);
assert_sector(7, LARGE_SECTOR_SIZE, 0x080F_FFFF);
}
}

View file

@ -0,0 +1,2 @@
pub mod f4;
pub mod f7;