Skip to content

Commit ff0f619

Browse files
Scale the app partition based on flash size. (#151)
* Scale the app partition based on flash size. The pre-defined `app_sizes` for the various `Esp32Param` assume a flash size which is not necessarily correct, e.g. there are esp32-c3 chips with 2MB. * Fix formatting.
1 parent 369dffd commit ff0f619

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

espflash/src/chip/esp32/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ pub struct Esp32Params {
2626
}
2727

2828
impl Esp32Params {
29-
pub fn default_partition_table(&self) -> PartitionTable {
29+
/// Generates a default partition table.
30+
/// `flash_size` is used to scale app partition when present, otherwise the param defaults are used.
31+
pub fn default_partition_table(&self, flash_size: Option<u32>) -> PartitionTable {
3032
PartitionTable::basic(
3133
self.nvs_addr,
3234
self.nvs_size,
3335
self.phy_init_data_addr,
3436
self.phy_init_data_size,
3537
self.app_addr,
36-
self.app_size,
38+
flash_size.map_or(self.app_size, |size| size - self.app_addr),
3739
)
3840
}
3941
}

espflash/src/flasher.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ impl FlashSize {
6565
_ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))),
6666
}
6767
}
68+
69+
/// Returns the flash size in bytes
70+
pub fn size(self) -> u32 {
71+
match self {
72+
FlashSize::Flash256Kb => 0x0040000,
73+
FlashSize::Flash512Kb => 0x0080000,
74+
FlashSize::Flash1Mb => 0x0100000,
75+
FlashSize::Flash2Mb => 0x0200000,
76+
FlashSize::Flash4Mb => 0x0400000,
77+
FlashSize::Flash8Mb => 0x0800000,
78+
FlashSize::Flash16Mb => 0x1000000,
79+
FlashSize::Flash32Mb => 0x2000000,
80+
FlashSize::Flash64Mb => 0x4000000,
81+
FlashSize::Flash128Mb => 0x8000000,
82+
}
83+
}
6884
}
6985

7086
impl FromStr for FlashSize {

espflash/src/image_format/esp32bootloader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ impl<'a> Esp32BootloaderFormat<'a> {
3232
partition_table: Option<PartitionTable>,
3333
bootloader: Option<Vec<u8>>,
3434
) -> Result<Self, Error> {
35-
let partition_table = partition_table.unwrap_or_else(|| params.default_partition_table());
35+
let partition_table = partition_table
36+
.unwrap_or_else(|| params.default_partition_table(image.flash_size.map(|v| v.size())));
3637
let mut bootloader = if let Some(bytes) = bootloader {
3738
Cow::Owned(bytes)
3839
} else {

0 commit comments

Comments
 (0)