Skip to content

Commit 112dad7

Browse files
committed
Ensure that FlashMode, FlashSize, and FlashFrequency can all be serialized/deserialized
1 parent db1ed13 commit 112dad7

File tree

4 files changed

+111
-24
lines changed

4 files changed

+111
-24
lines changed

espflash/src/elf.rs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,81 @@
1-
use std::borrow::Cow;
2-
use std::cmp::Ordering;
3-
4-
use crate::chip::Chip;
5-
use crate::error::{ElfError, Error};
6-
use crate::flasher::FlashSize;
7-
use std::fmt::{Debug, Formatter};
8-
use std::mem::take;
9-
use std::ops::AddAssign;
10-
use xmas_elf::program::Type;
11-
use xmas_elf::sections::{SectionData, ShType};
12-
use xmas_elf::ElfFile;
1+
use std::{
2+
borrow::Cow,
3+
cmp::Ordering,
4+
fmt::{Debug, Formatter},
5+
mem::take,
6+
ops::AddAssign,
7+
str::FromStr,
8+
};
9+
10+
use strum_macros::EnumVariantNames;
11+
use xmas_elf::{
12+
program::Type,
13+
sections::{SectionData, ShType},
14+
ElfFile,
15+
};
16+
17+
use crate::{
18+
chip::Chip,
19+
error::{ElfError, Error},
20+
flasher::FlashSize,
21+
};
1322

1423
pub const ESP_CHECKSUM_MAGIC: u8 = 0xef;
1524

16-
#[derive(Copy, Clone)]
17-
#[allow(dead_code)]
25+
#[derive(Copy, Clone, EnumVariantNames)]
26+
#[strum(serialize_all = "UPPERCASE")]
1827
pub enum FlashMode {
1928
Qio,
2029
Qout,
2130
Dio,
2231
Dout,
2332
}
2433

25-
#[derive(Copy, Clone)]
34+
impl FromStr for FlashMode {
35+
type Err = Error;
36+
37+
fn from_str(s: &str) -> Result<Self, Self::Err> {
38+
let mode = match s.to_uppercase().as_str() {
39+
"QIO" => FlashMode::Qio,
40+
"QOUT" => FlashMode::Qout,
41+
"DIO" => FlashMode::Dio,
42+
"DOUT" => FlashMode::Dout,
43+
_ => return Err(Error::InvalidFlashMode(s.to_string())),
44+
};
45+
46+
Ok(mode)
47+
}
48+
}
49+
50+
#[derive(Copy, Clone, EnumVariantNames)]
2651
#[repr(u8)]
27-
#[allow(dead_code)]
2852
pub enum FlashFrequency {
29-
Flash40M = 0,
30-
Flash26M = 1,
31-
Flash20M = 2,
53+
#[strum(serialize = "20M")]
54+
Flash20M = 0x2,
55+
#[strum(serialize = "26M")]
56+
Flash26M = 0x1,
57+
#[strum(serialize = "40M")]
58+
Flash40M = 0x0,
59+
#[strum(serialize = "80M")]
3260
Flash80M = 0xf,
3361
}
3462

63+
impl FromStr for FlashFrequency {
64+
type Err = Error;
65+
66+
fn from_str(s: &str) -> Result<Self, Self::Err> {
67+
let freq = match s.to_uppercase().as_str() {
68+
"20M" => FlashFrequency::Flash20M,
69+
"26M" => FlashFrequency::Flash26M,
70+
"40M" => FlashFrequency::Flash40M,
71+
"80M" => FlashFrequency::Flash80M,
72+
_ => return Err(Error::InvalidFlashFrequency(s.to_string())),
73+
};
74+
75+
Ok(freq)
76+
}
77+
}
78+
3579
pub struct FirmwareImage<'a> {
3680
pub entry: u32,
3781
pub elf: ElfFile<'a>,

espflash/src/error.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use thiserror::Error;
1010

1111
use crate::{
1212
command::CommandType,
13+
elf::{FlashFrequency, FlashMode},
14+
flasher::FlashSize,
1315
image_format::ImageFormatId,
1416
partition_table::{SubType, Type},
1517
Chip,
@@ -89,6 +91,24 @@ https://github.com/espressif/esp32c3-direct-boot-example"
8991
SerialNotFound(String),
9092
#[error("Canceled by user")]
9193
Canceled,
94+
#[error("The flash mode '{0}' is not valid")]
95+
#[diagnostic(
96+
code(espflash::invalid_flash_mode),
97+
help("The accepted values are: {:?}", FlashMode::VARIANTS)
98+
)]
99+
InvalidFlashMode(String),
100+
#[error("The flash frequency '{0}' is not valid")]
101+
#[diagnostic(
102+
code(espflash::invalid_flash_frequency),
103+
help("The accepted values are: {:?}", FlashFrequency::VARIANTS)
104+
)]
105+
InvalidFlashFrequency(String),
106+
#[error("The flash size '{0}' is not valid")]
107+
#[diagnostic(
108+
code(espflash::invalid_flash_size),
109+
help("The accepted values are: {:?}", FlashSize::VARIANTS)
110+
)]
111+
InvalidFlashSize(String),
92112
}
93113

94114
#[derive(Error, Debug, Diagnostic)]

espflash/src/flasher.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::{borrow::Cow, thread::sleep};
1+
use std::{borrow::Cow, str::FromStr, thread::sleep};
22

33
use bytemuck::{Pod, Zeroable, __core::time::Duration};
44
use serialport::{SerialPort, UsbPortInfo};
5-
use strum_macros::Display;
5+
use strum_macros::{Display, EnumVariantNames};
66

77
use crate::{
88
chip::Chip,
@@ -25,7 +25,7 @@ const FLASH_SECTORS_PER_BLOCK: usize = FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE;
2525
// register used for chip detect
2626
const CHIP_DETECT_MAGIC_REG_ADDR: u32 = 0x40001000;
2727

28-
#[derive(Clone, Copy, Debug, Eq, PartialEq, Display)]
28+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Display, EnumVariantNames)]
2929
#[repr(u8)]
3030
pub enum FlashSize {
3131
#[strum(serialize = "256KB")]
@@ -62,11 +62,34 @@ impl FlashSize {
6262
0x18 => Ok(FlashSize::Flash16Mb),
6363
0x19 => Ok(FlashSize::Flash32Mb),
6464
0x1a => Ok(FlashSize::Flash64Mb),
65+
0x21 => Ok(FlashSize::Flash128Mb),
6566
_ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))),
6667
}
6768
}
6869
}
6970

71+
impl FromStr for FlashSize {
72+
type Err = Error;
73+
74+
fn from_str(s: &str) -> Result<Self, Self::Err> {
75+
let size = match s.to_uppercase().as_str() {
76+
"256KB" => FlashSize::Flash256Kb,
77+
"512KB" => FlashSize::Flash512Kb,
78+
"1MB" => FlashSize::Flash1Mb,
79+
"2MB" => FlashSize::Flash2Mb,
80+
"4MB" => FlashSize::Flash4Mb,
81+
"8MB" => FlashSize::Flash8Mb,
82+
"16MB" => FlashSize::Flash16Mb,
83+
"32MB" => FlashSize::Flash32Mb,
84+
"64MB" => FlashSize::Flash64Mb,
85+
"128MB" => FlashSize::Flash128Mb,
86+
_ => return Err(Error::InvalidFlashSize(s.to_string())),
87+
};
88+
89+
Ok(size)
90+
}
91+
}
92+
7093
#[derive(Copy, Clone, Debug)]
7194
#[repr(C)]
7295
pub struct SpiAttachParams {

espflash/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pub use chip::Chip;
22
pub use cli::config::Config;
3-
pub use elf::FirmwareImage;
3+
pub use elf::{FlashFrequency, FlashMode};
44
pub use error::Error;
5-
pub use flasher::Flasher;
5+
pub use flasher::{FlashSize, Flasher};
66
pub use image_format::ImageFormatId;
77
pub use partition_table::PartitionTable;
88

0 commit comments

Comments
 (0)