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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::architecture::{
arm::{memory::ADIMemoryInterface, ArmChipInfo, ArmCommunicationInterface},
riscv::communication_interface::RiscvCommunicationInterface,
};
use crate::config::{
ChipInfo, MemoryRegion, RawFlashAlgorithm, RegistryError, Target, TargetSelector,
};
use crate::core::Architecture;
use crate::{Core, CoreList, Error, Memory, MemoryList, Probe};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone)]
pub struct Session {
inner: Rc<RefCell<InnerSession>>,
}
struct InnerSession {
target: Target,
architecture_session: ArchitectureSession,
}
enum ArchitectureSession {
Arm(ArmCommunicationInterface),
Riscv(RiscvCommunicationInterface),
}
impl Session {
pub fn new(probe: Probe, target: impl Into<TargetSelector>) -> Result<Self, Error> {
let mut generic_probe = Some(probe);
let target = match target.into() {
TargetSelector::Unspecified(name) => {
match crate::config::registry::get_target_by_name(name) {
Ok(target) => target,
Err(err) => return Err(err.into()),
}
}
TargetSelector::Specified(target) => target,
TargetSelector::Auto => {
let mut found_chip = None;
if generic_probe.as_ref().unwrap().has_dap_interface() {
let mut arm_interface =
ArmCommunicationInterface::new(generic_probe.take().unwrap())?;
found_chip = match ArmChipInfo::read_from_rom_table(&mut arm_interface)
.map(|option| option.map(ChipInfo::Arm))
{
Ok(chip_info) => chip_info,
Err(e) => {
log::info!("Error during auto-detection of ARM chips: {}", e);
None
}
};
generic_probe = Some(arm_interface.close().unwrap());
} else {
log::debug!("No DAP interface available on Probe");
}
if found_chip.is_none() && generic_probe.as_ref().unwrap().has_jtag_interface() {
let riscv_interface =
RiscvCommunicationInterface::new(generic_probe.take().unwrap())?;
let idcode = riscv_interface.read_idcode();
log::debug!("ID Code read over JTAG: {:x?}", idcode);
generic_probe = Some(riscv_interface.close().unwrap());
}
if let Some(chip) = found_chip {
crate::config::registry::get_target_by_chip_info(chip)?
} else {
return Err(Error::ChipNotFound(RegistryError::ChipAutodetectFailed));
}
}
};
let session = match target.architecture() {
Architecture::ARM => {
let arm_interface = ArmCommunicationInterface::new(generic_probe.unwrap())?;
ArchitectureSession::Arm(arm_interface)
}
Architecture::RISCV => {
let riscv_interface = RiscvCommunicationInterface::new(generic_probe.unwrap())?;
ArchitectureSession::Riscv(riscv_interface)
}
};
Ok(Self {
inner: Rc::new(RefCell::new(InnerSession {
target,
architecture_session: session,
})),
})
}
pub fn list_cores(&self) -> CoreList {
CoreList::new(vec![self.inner.borrow().target.core_type])
}
pub fn attach_to_core(&self, n: usize) -> Result<Core, Error> {
let core = *self
.list_cores()
.get(n)
.ok_or_else(|| Error::CoreNotFound(n))?;
match self.inner.borrow().architecture_session {
ArchitectureSession::Arm(ref arm_interface) => core.attach_arm(arm_interface.clone()),
ArchitectureSession::Riscv(ref riscv_interface) => {
core.attach_riscv(riscv_interface.clone())
}
}
}
pub fn list_memories(&self) -> MemoryList {
MemoryList::new(vec![])
}
pub fn attach_to_memory(&self, _id: usize) -> Result<Memory, Error> {
match self.inner.borrow().architecture_session {
ArchitectureSession::Arm(ref interface) => {
if let Some(memory) = interface.dedicated_memory_interface() {
Ok(memory)
} else {
Ok(Memory::new(
ADIMemoryInterface::<ArmCommunicationInterface>::new(interface.clone(), 0),
))
}
}
ArchitectureSession::Riscv(ref _interface) => {
Ok(Memory::new_dummy())
}
}
}
pub fn flash_algorithms(&self) -> Vec<RawFlashAlgorithm> {
self.inner.borrow().target.flash_algorithms.clone()
}
pub fn memory_map(&self) -> Vec<MemoryRegion> {
self.inner.borrow().target.memory_map.clone()
}
}