|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+use std::collections::BTreeSet;
|
|
|
2
|
+use std::io::{BufWriter, Write};
|
|
|
3
|
+
|
|
|
4
|
+use super::mcu::{Mcu, Pin};
|
|
|
5
|
+
|
|
|
6
|
+pub fn draw(mcu: &Mcu, shown_peripherals: &BTreeSet<String>) -> Vec<u8> {
|
|
|
7
|
+ let mut output = Vec::new();
|
|
|
8
|
+ let mut writer = BufWriter::new(&mut output);
|
|
|
9
|
+ let package = match Package::from_str(&mcu.package) {
|
|
|
10
|
+ Some(package) => package,
|
|
|
11
|
+ None => panic!("Package {} not yet supported.", mcu.package),
|
|
|
12
|
+ };
|
|
|
13
|
+
|
|
|
14
|
+ let mut peripherals = BTreeSet::new();
|
|
|
15
|
+ for pin in mcu.pins.iter() {
|
|
|
16
|
+ for function in pin.functions.iter() {
|
|
|
17
|
+ let peripheral = Pin::split_function(function).0;
|
|
|
18
|
+ if shown_peripherals.len() != 0 && !shown_peripherals.contains(peripheral) {
|
|
|
19
|
+ continue;
|
|
|
20
|
+ }
|
|
|
21
|
+ if !peripherals.contains(peripheral) {
|
|
|
22
|
+ peripherals.insert(peripheral.to_owned());
|
|
|
23
|
+ }
|
|
|
24
|
+ }
|
|
|
25
|
+ }
|
|
|
26
|
+ println!("{} peripherals:", peripherals.len());
|
|
|
27
|
+ for peripheral in peripherals.iter() {
|
|
|
28
|
+ println!("{}", peripheral);
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ begin_document(&mut writer, &peripherals);
|
|
|
32
|
+ draw_package_body(&mut writer, package);
|
|
|
33
|
+ draw_legend(&mut writer, package, &peripherals);
|
|
|
34
|
+ for pin in mcu.pins.iter() {
|
|
|
35
|
+ draw_pin_label(&mut writer, package, pin, &peripherals);
|
|
|
36
|
+ }
|
|
|
37
|
+ end_document(&mut writer);
|
|
|
38
|
+
|
|
|
39
|
+ drop(writer);
|
|
|
40
|
+ output
|
|
|
41
|
+}
|
|
|
42
|
+
|
|
|
43
|
+fn begin_document(writer: &mut BufWriter<&mut Vec<u8>>, peripherals: &BTreeSet<String>) {
|
|
|
44
|
+ write!(
|
|
|
45
|
+ writer,
|
|
|
46
|
+ "{}",
|
|
|
47
|
+ r#"\documentclass[crop,tikz]{standalone}
|
|
|
48
|
+\usepackage{pgf}
|
|
|
49
|
+\fontsize{10}{12}\selectfont
|
|
|
50
|
+\usetikzlibrary{calc, positioning}
|
|
|
51
|
+\pgfmathsetseed{12345}
|
|
|
52
|
+"#
|
|
|
53
|
+ )
|
|
|
54
|
+ .unwrap();
|
|
|
55
|
+ // Randomly generate colors for all peripherals.
|
|
|
56
|
+ for peripheral in peripherals {
|
|
|
57
|
+ write!(
|
|
|
58
|
+ writer,
|
|
|
59
|
+ "\\pgfmathsetmacro{{\\R}}{{random(0,10000)/10000}}
|
|
|
60
|
+\\pgfmathsetmacro{{\\G}}{{random(0,10000)/10000}}
|
|
|
61
|
+\\pgfmathsetmacro{{\\B}}{{random(0,10000)/10000}}
|
|
|
62
|
+\\definecolor{{color{name}}}{{rgb}}{{\\R,\\G,\\B}}
|
|
|
63
|
+",
|
|
|
64
|
+ name = peripheral
|
|
|
65
|
+ )
|
|
|
66
|
+ .unwrap()
|
|
|
67
|
+ }
|
|
|
68
|
+ write!(
|
|
|
69
|
+ writer,
|
|
|
70
|
+ "{}",
|
|
|
71
|
+ r#"\begin{document}
|
|
|
72
|
+\begin{tikzpicture}[x=12pt,y=12pt]
|
|
|
73
|
+"#
|
|
|
74
|
+ )
|
|
|
75
|
+ .unwrap();
|
|
|
76
|
+}
|
|
|
77
|
+
|
|
|
78
|
+fn end_document(writer: &mut BufWriter<&mut Vec<u8>>) {
|
|
|
79
|
+ write!(
|
|
|
80
|
+ writer,
|
|
|
81
|
+ "{}",
|
|
|
82
|
+ r#"\end{tikzpicture}
|
|
|
83
|
+\end{document}
|
|
|
84
|
+"#
|
|
|
85
|
+ )
|
|
|
86
|
+ .unwrap();
|
|
|
87
|
+}
|
|
|
88
|
+
|
|
|
89
|
+fn draw_package_body(writer: &mut BufWriter<&mut Vec<u8>>, package: Package) {
|
|
|
90
|
+ if let Package::Qfp(pins) = package {
|
|
|
91
|
+ let pins_per_side = package.pins_per_side();
|
|
|
92
|
+ let edge = pins_per_side + 3;
|
|
|
93
|
+ write!(writer, "\\draw[thick] (0, 0) --\n").unwrap();
|
|
|
94
|
+ for i in 0..pins_per_side {
|
|
|
95
|
+ write!(
|
|
|
96
|
+ writer,
|
|
|
97
|
+ " coordinate[pos={pos}] (P{pin})\n",
|
|
|
98
|
+ pos = (i + 2) as f64 / edge as f64,
|
|
|
99
|
+ pin = i + 1,
|
|
|
100
|
+ )
|
|
|
101
|
+ .unwrap();
|
|
|
102
|
+ }
|
|
|
103
|
+ write!(writer, " ({size}, 0);\n", size = edge).unwrap();
|
|
|
104
|
+ write!(writer, "\\draw[thick] ({size}, 0) --\n", size = edge).unwrap();
|
|
|
105
|
+ for i in 0..pins_per_side {
|
|
|
106
|
+ write!(
|
|
|
107
|
+ writer,
|
|
|
108
|
+ " coordinate[pos={pos}] (P{pin})\n",
|
|
|
109
|
+ pos = (i + 2) as f64 / edge as f64,
|
|
|
110
|
+ pin = i + pins_per_side + 1,
|
|
|
111
|
+ )
|
|
|
112
|
+ .unwrap();
|
|
|
113
|
+ }
|
|
|
114
|
+ write!(writer, " ({size}, {size});\n", size = edge).unwrap();
|
|
|
115
|
+ write!(writer, "\\draw[thick] ({size}, {size}) --\n", size = edge).unwrap();
|
|
|
116
|
+ for i in 0..pins_per_side {
|
|
|
117
|
+ write!(
|
|
|
118
|
+ writer,
|
|
|
119
|
+ " coordinate[pos={pos}] (P{pin})\n",
|
|
|
120
|
+ pos = (i + 2) as f64 / edge as f64,
|
|
|
121
|
+ pin = i + pins_per_side * 2 + 1,
|
|
|
122
|
+ )
|
|
|
123
|
+ .unwrap();
|
|
|
124
|
+ }
|
|
|
125
|
+ write!(writer, " (0, {size});\n", size = edge).unwrap();
|
|
|
126
|
+ write!(writer, "\\draw[thick] (0, {size}) --\n", size = edge).unwrap();
|
|
|
127
|
+ for i in 0..pins_per_side {
|
|
|
128
|
+ write!(
|
|
|
129
|
+ writer,
|
|
|
130
|
+ " coordinate[pos={pos}] (P{pin})\n",
|
|
|
131
|
+ pos = (i + 2) as f64 / edge as f64,
|
|
|
132
|
+ pin = i + pins_per_side * 3 + 1,
|
|
|
133
|
+ )
|
|
|
134
|
+ .unwrap();
|
|
|
135
|
+ }
|
|
|
136
|
+ write!(writer, " (0, 0);\n").unwrap();
|
|
|
137
|
+
|
|
|
138
|
+ // Draw pin numbers.
|
|
|
139
|
+ for i in 0..pins {
|
|
|
140
|
+ let side = Side(i / (pins / 4));
|
|
|
141
|
+ write!(
|
|
|
142
|
+ writer,
|
|
|
143
|
+ "\\node[{side} = 0.1 of P{pin}, rotate={rotate}] {{{pin}}};\n",
|
|
|
144
|
+ side = side.inside_position(),
|
|
|
145
|
+ rotate = side.rotate(),
|
|
|
146
|
+ pin = i + 1,
|
|
|
147
|
+ )
|
|
|
148
|
+ .unwrap();
|
|
|
149
|
+ }
|
|
|
150
|
+ } else {
|
|
|
151
|
+ panic!("Not yet implemented.");
|
|
|
152
|
+ }
|
|
|
153
|
+}
|
|
|
154
|
+
|
|
|
155
|
+fn draw_legend(
|
|
|
156
|
+ writer: &mut BufWriter<&mut Vec<u8>>,
|
|
|
157
|
+ package: Package,
|
|
|
158
|
+ peripherals: &BTreeSet<String>,
|
|
|
159
|
+) {
|
|
|
160
|
+ let rows = (package.pins_per_side() as usize - 4) * 2 / 3;
|
|
|
161
|
+ let pos_x = 3;
|
|
|
162
|
+ let pos_y = package.pins_per_side() as f64 - 2.0;
|
|
|
163
|
+ for (i, peripheral) in peripherals.iter().enumerate() {
|
|
|
164
|
+ let column = i / rows;
|
|
|
165
|
+ let row = i % rows;
|
|
|
166
|
+ write!(
|
|
|
167
|
+ writer,
|
|
|
168
|
+ "\\node[draw, fill=color{rawname}!30, anchor=west] at ({x}, {y}) {{{name}}};\n",
|
|
|
169
|
+ x = pos_x + column * 5,
|
|
|
170
|
+ y = pos_y - row as f64 * 1.5,
|
|
|
171
|
+ rawname = peripheral,
|
|
|
172
|
+ name = latex_escape(peripheral),
|
|
|
173
|
+ )
|
|
|
174
|
+ .unwrap();
|
|
|
175
|
+ }
|
|
|
176
|
+}
|
|
|
177
|
+
|
|
|
178
|
+fn draw_pin_label(
|
|
|
179
|
+ writer: &mut BufWriter<&mut Vec<u8>>,
|
|
|
180
|
+ package: Package,
|
|
|
181
|
+ pin: &Pin,
|
|
|
182
|
+ peripherals: &BTreeSet<String>,
|
|
|
183
|
+) {
|
|
|
184
|
+ if let Package::Qfp(pins) = package {
|
|
|
185
|
+ let position = pin.position.parse::<u32>().expect("invalid pin position");
|
|
|
186
|
+ let side = Side((position - 1) / (pins / 4));
|
|
|
187
|
+ write!(
|
|
|
188
|
+ writer,
|
|
|
189
|
+ "\\node[{side} = 0.1 of P{pin}, rotate={rotate}] (p{pin}label0) {{{name}}};\n",
|
|
|
190
|
+ side = side.outside_position(),
|
|
|
191
|
+ rotate = side.rotate(),
|
|
|
192
|
+ //anchor = side.outside_anchor(),
|
|
|
193
|
+ pin = position,
|
|
|
194
|
+ name = latex_escape(&pin.name),
|
|
|
195
|
+ )
|
|
|
196
|
+ .unwrap();
|
|
|
197
|
+
|
|
|
198
|
+ // Draw the functions as well.
|
|
|
199
|
+ // TODO: Ideally, the functions would be grouped together.
|
|
|
200
|
+ let mut label_count = 0;
|
|
|
201
|
+ for function in pin.functions.iter() {
|
|
|
202
|
+ let (peripheral, signal) = Pin::split_function(function);
|
|
|
203
|
+ if !peripherals.contains(peripheral) {
|
|
|
204
|
+ continue;
|
|
|
205
|
+ }
|
|
|
206
|
+ write!(
|
|
|
207
|
+ writer,
|
|
|
208
|
+ "\\node[draw, fill=color{peripheral}!30, {side} = 0.1 of p{pin}label{label_count}, rotate={rotate}] (p{pin}label{label_count_p_1}) {{{name}}};\n",
|
|
|
209
|
+ side = side.outside_position(),
|
|
|
210
|
+ label_count = label_count,
|
|
|
211
|
+ label_count_p_1 = label_count + 1,
|
|
|
212
|
+ rotate = side.rotate(),
|
|
|
213
|
+ //anchor = side.outside_anchor(),
|
|
|
214
|
+ pin = position,
|
|
|
215
|
+ peripheral = peripheral,
|
|
|
216
|
+ name = latex_escape(signal),
|
|
|
217
|
+ )
|
|
|
218
|
+ .unwrap();
|
|
|
219
|
+ label_count += 1;
|
|
|
220
|
+ }
|
|
|
221
|
+ } else {
|
|
|
222
|
+ panic!("Not yet implemented.");
|
|
|
223
|
+ }
|
|
|
224
|
+}
|
|
|
225
|
+
|
|
|
226
|
+#[derive(Copy, Clone)]
|
|
|
227
|
+struct Side(u32);
|
|
|
228
|
+
|
|
|
229
|
+impl Side {
|
|
|
230
|
+ fn inside_anchor(self) -> &'static str {
|
|
|
231
|
+ match self.0 {
|
|
|
232
|
+ 0 => "west",
|
|
|
233
|
+ 1 => "east",
|
|
|
234
|
+ 2 => "east",
|
|
|
235
|
+ 3 => "west",
|
|
|
236
|
+ _ => panic!("invalid side"),
|
|
|
237
|
+ }
|
|
|
238
|
+ }
|
|
|
239
|
+ fn outside_anchor(self) -> &'static str {
|
|
|
240
|
+ Side((self.0 + 2) % 4).inside_anchor()
|
|
|
241
|
+ }
|
|
|
242
|
+ fn rotate(self) -> u32 {
|
|
|
243
|
+ match self.0 {
|
|
|
244
|
+ 0 => 90,
|
|
|
245
|
+ 1 => 0,
|
|
|
246
|
+ 2 => 90,
|
|
|
247
|
+ 3 => 0,
|
|
|
248
|
+ _ => panic!("invalid side"),
|
|
|
249
|
+ }
|
|
|
250
|
+ }
|
|
|
251
|
+ fn inside_position(self) -> &'static str {
|
|
|
252
|
+ match self.0 {
|
|
|
253
|
+ 0 => "right",
|
|
|
254
|
+ 1 => "left",
|
|
|
255
|
+ 2 => "left",
|
|
|
256
|
+ 3 => "right",
|
|
|
257
|
+ _ => panic!("invalid side"),
|
|
|
258
|
+ }
|
|
|
259
|
+ }
|
|
|
260
|
+ fn outside_position(self) -> &'static str {
|
|
|
261
|
+ Side((self.0 + 2) % 4).inside_position()
|
|
|
262
|
+ }
|
|
|
263
|
+}
|
|
|
264
|
+
|
|
|
265
|
+fn latex_escape(s: &str) -> String {
|
|
|
266
|
+ s.replace("_", "\\_")
|
|
|
267
|
+}
|
|
|
268
|
+
|
|
|
269
|
+#[derive(Clone, Copy)]
|
|
|
270
|
+enum Package {
|
|
|
271
|
+ Qfp(u32),
|
|
|
272
|
+ Bga(u32),
|
|
|
273
|
+}
|
|
|
274
|
+
|
|
|
275
|
+impl Package {
|
|
|
276
|
+ fn from_str(s: &str) -> Option<Package> {
|
|
|
277
|
+ if s.starts_with("LQFP") {
|
|
|
278
|
+ let pins = s[4..].parse().ok()?;
|
|
|
279
|
+ Some(Package::Qfp(pins))
|
|
|
280
|
+ } else {
|
|
|
281
|
+ None
|
|
|
282
|
+ }
|
|
|
283
|
+ }
|
|
|
284
|
+
|
|
|
285
|
+ fn pins_per_side(&self) -> u32 {
|
|
|
286
|
+ match self {
|
|
|
287
|
+ Package::Qfp(pins) => pins / 4,
|
|
|
288
|
+ _ => panic!("Not yet implemented."),
|
|
|
289
|
+ }
|
|
|
290
|
+ }
|
|
|
291
|
+ fn pins(&self) -> u32 {
|
|
|
292
|
+ match self {
|
|
|
293
|
+ Package::Qfp(pins) => *pins,
|
|
|
294
|
+ _ => panic!("Not yet implemented."),
|
|
|
295
|
+ }
|
|
|
296
|
+ }
|
|
|
297
|
+}
|