1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::{
convert::TryFrom,
ffi::OsString,
io::{Error as IoError, ErrorKind},
};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Framework {
Baremetal,
EspIdf,
}
impl Framework {
pub fn from_target<S: AsRef<str>>(target: S) -> Result<Self, IoError> {
let target = target.as_ref();
if target.ends_with("-espidf") {
Ok(Framework::EspIdf)
} else if target.ends_with("-none-elf") {
Ok(Framework::Baremetal)
} else {
Err(IoError::new(ErrorKind::InvalidInput, format!("Can't figure out framework from target '{}'", target)))
}
}
}
impl TryFrom<&str> for Framework {
type Error = IoError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"baremetal" => Ok(Framework::Baremetal),
"esp-idf" | "espidf" => Ok(Framework::EspIdf),
_ => Err(IoError::new(ErrorKind::InvalidInput, format!("'{}' is not a valid framework", value))),
}
}
}
impl Default for Framework {
fn default() -> Self {
Framework::Baremetal
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Chip {
ESP32,
ESP32S2,
ESP8266,
ESP32C3,
}
impl Chip {
pub fn from_target<S: AsRef<str>>(target: S) -> Result<Chip, IoError> {
let target = target.as_ref();
if target.contains("-esp32-") {
Ok(Chip::ESP32)
} else if target.contains("-esp32s2-") {
Ok(Chip::ESP32S2)
} else if target.contains("-esp8266-") {
Ok(Chip::ESP8266)
} else {
Err(IoError::new(ErrorKind::InvalidInput, format!("Can't figure out chip from target '{}'; try specifying the --chip option", target)))
}
}
}
impl Chip {
pub fn target(&self, framework: Framework) -> String {
let mut target = String::new();
target.push_str(match self {
Chip::ESP32C3 => "riscv32imc-",
_ => "xtensa-",
});
target.push_str(match self {
Chip::ESP32 => "esp32-",
Chip::ESP32S2 => "esp32s2-",
Chip::ESP8266 => "esp8266-",
Chip::ESP32C3 => match framework {
Framework::Baremetal => "unknown-",
Framework::EspIdf => "esp-"
}
});
target.push_str(match framework {
Framework::Baremetal => "none-elf",
Framework::EspIdf=> "espidf",
});
target
}
}
impl TryFrom<&str> for Chip {
type Error = IoError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"esp32" => Ok(Chip::ESP32),
"esp32c3" => Ok(Chip::ESP32C3),
"esp8266" => Ok(Chip::ESP8266),
_ => Err(IoError::new(ErrorKind::InvalidInput, format!("'{}' is not a valid chip", value))),
}
}
}
impl Default for Chip {
fn default() -> Self {
Chip::ESP32
}
}
#[derive(Debug)]
pub struct AppArgs {
pub serial: String,
pub chip: Chip,
pub framework: Framework,
pub speed: Option<usize>,
pub reset: bool,
pub bin: Option<OsString>,
}