-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.rs
352 lines (310 loc) · 10.4 KB
/
lib.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! ## `humility gpio`
//!
//! `humility gpio` allows for GPIO pins to be set, reset, queried or
//! configured on STM32 targets. Commands:
//!
//! - `--set` (`-s`): Sets a pin (sets it high)
//! - `--reset` (`-r`): Resets a pin (sets it low)
//! - `--toggle` (`-t`): Toggles a pin (sets it high if low, low if high)
//! - `--input` (`-i`): Queries the state of a pin (or all pins if no pin
//! is specified)
//! - `--configure` (`-c`): Configures a pin
//!
//! ### Set, reset, toggle
//!
//! To change the state of a pin (or pins), specify the pin (or pins) and
//! the desired command. For example, to toggle the state on pin 14 on
//! port B:
//!
//! ```console
//! % humility gpio --toggle --pins B:14
//! humility: attached via ST-Link V3
//! [Ok([])]
//! ```
//!
//! To set pins B:0, B:14 and E:1:
//!
//! ```console
//! % humility gpio --set --pins B:0,B:14,E:1
//! humility: attached via ST-Link V3
//! [Ok([]), Ok([]), Ok([])]
//! ```
//!
//! To reset pin E:1:
//!
//! ```console
//! % humility gpio --reset --pins E:1
//! humility: attached via ST-Link V3
//! [Ok([])]
//! ```
//!
//! ### Input
//!
//! To get input values for a particular pin:
//!
//! ```console
//! % humility gpio --input --pins B:0,B:14,E:1
//! humility: attached via ST-Link V3
//! B:0 = 1
//! B:14 = 1
//! E:1 = 0
//! ```
//!
//! To get input values for all pins, leave the pin unspecified:
//!
//! ```console
//! % humility gpio --input
//! humility: attached via ST-Link V3
//! Pin 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//! -----------------------------------------------------------------------
//! Port A 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1
//! Port B 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
//! Port C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port D 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0
//! Port E 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port F 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port J 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! Port K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//! ```
//!
//! ### Configure
//!
//! To configure a pin, the configuration should be specified as a
//! colon-delimited 5-tuple consisting of:
//!
//! - Mode: `Input`, `Output`, `Alternate`, or `Analog`
//! - Output type: `PushPull` or `OpenDrain`
//! - Speed: `Low`, `Medium`, `High`, or `VeryHigh`
//! - Pull direction: `None`, `Up`, or `Down`
//! - Alternate function: one of `AF0` through `AF15`
//!
//! For example, to configure pin 5 on port A as a push-pull output:
//!
//! ```console
//! % humility gpio -c Output:PushPull:High:None:AF0 -p A:5
//! ```
//!
use humility::core::Core;
use humility::hubris::*;
use humility_cmd::hiffy::*;
use humility_cmd::{Archive, Args, Attach, Command, Validate};
use std::str;
use anyhow::{bail, Result};
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
use hif::*;
use std::convert::TryInto;
#[derive(Parser, Debug)]
#[clap(name = "gpio", about = env!("CARGO_PKG_DESCRIPTION"))]
struct GpioArgs {
/// sets timeout
#[clap(
long, short = 'T', default_value = "5000", value_name = "timeout_ms",
value_parser=parse_int::parse::<u32>,
)]
timeout: u32,
/// shows the state of an input pin (or all pins if pin is unspecified)
#[clap(
long, short,
conflicts_with_all = &["toggle", "set", "reset", "configure"]
)]
input: bool,
/// toggle specified pins
#[clap(
long, short, requires = "pins",
conflicts_with_all = &["set", "reset", "configure"]
)]
toggle: bool,
/// sets specified pins
#[clap(
long, short, requires = "pins",
conflicts_with_all = &["reset", "configure"]
)]
set: bool,
/// resets specified pins
#[clap(
long, short, requires = "pins",
conflicts_with_all = &["configure"])]
reset: bool,
/// configures specified pins
#[clap(long, short, requires = "pins")]
configure: Option<String>,
/// specifies GPIO pins on which to operate
#[clap(long, short, value_name = "pins", use_value_delimiter = true)]
pins: Option<Vec<String>>,
}
fn gpio(
hubris: &HubrisArchive,
core: &mut dyn Core,
_args: &Args,
subargs: &[String],
) -> Result<()> {
let subargs = GpioArgs::try_parse_from(subargs)?;
let mut context = HiffyContext::new(hubris, core, subargs.timeout)?;
let funcs = context.functions()?;
let gpio_toggle = funcs.get("GpioToggle", 2)?;
let gpio_set = funcs.get("GpioSet", 2)?;
let gpio_reset = funcs.get("GpioReset", 2)?;
let gpio_input = funcs.get("GpioInput", 1)?;
let gpio_configure = funcs.get("GpioConfigure", 7)?;
let mut configure_args = vec![];
let target = if subargs.toggle {
gpio_toggle.id
} else if subargs.set {
gpio_set.id
} else if subargs.reset {
gpio_reset.id
} else if let Some(ref configure) = subargs.configure {
let params: Vec<&str> = configure.split(':').collect();
let args = ["Mode", "OutputType", "Speed", "Pull", "Alternate"];
if params.len() != args.len() {
bail!("expected {}", args.join(":"));
}
for i in 0..args.len() {
configure_args.push(gpio_configure.lookup_argument(
hubris,
args[i],
2 + i,
params[i],
)?);
}
gpio_configure.id
} else if subargs.input {
gpio_input.id
} else {
bail!(
"expected one of configure, set, \
reset, toggle, or input to be specified"
);
};
let mut args: Vec<(u16, Option<u8>, String)> = vec![];
if let Some(ref pins) = subargs.pins {
for pin in pins {
let p: Vec<&str> = pin.split(':').collect();
if p.len() != 2 {
bail!("expected both a port and a pin number");
}
let port = gpio_toggle.lookup_argument(hubris, "port", 0, p[0])?;
let pin = match parse_int::parse::<u8>(p[1]) {
Ok(pin) if pin < 16 => pin,
_ => {
bail!("invalid pin {}", p[1]);
}
};
args.push((port, Some(pin), p[0].to_string()));
}
}
let mut ops = vec![];
if subargs.input {
if args.is_empty() {
let variants = gpio_input.argument_variants(hubris, 0)?;
for v in &variants {
args.push((v.1, None, v.0.clone()));
}
}
for arg in &args {
ops.push(Op::Push16(arg.0));
ops.push(Op::Call(target));
ops.push(Op::DropN(1));
}
} else if subargs.configure.is_some() {
for arg in &args {
ops.push(Op::Push16(arg.0));
ops.push(Op::Push(arg.1.unwrap()));
for configure_arg in &configure_args {
ops.push(Op::Push16(*configure_arg));
}
ops.push(Op::Call(target));
ops.push(Op::DropN(7));
}
} else {
for arg in &args {
ops.push(Op::Push16(arg.0));
ops.push(Op::Push(arg.1.unwrap()));
ops.push(Op::Call(target));
ops.push(Op::DropN(2));
}
}
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
if subargs.input {
let mut header = false;
for (ndx, arg) in args.iter().enumerate() {
match arg.1 {
Some(pin) => {
println!(
"{}:{:<2} = {}",
arg.2,
pin,
match results[ndx] {
Err(code) => {
gpio_input.strerror(code)
}
Ok(ref val) => {
let arr: &[u8; 2] = val[0..2].try_into()?;
let v = u16::from_le_bytes(*arr);
format!(
"{}",
if v & (1 << pin) != 0 { 1 } else { 0 }
)
}
}
);
}
None => {
if !header {
print!("{:7}", "Pin");
for i in 0..16 {
print!("{:4}", i);
}
println!();
print!("-------");
for _i in 0..16 {
print!("----");
}
println!();
header = true;
}
print!("Port {} ", arg.2);
match results[ndx] {
Err(code) => {
println!("{}", gpio_input.strerror(code))
}
Ok(ref val) => {
let arr: &[u8; 2] = val[0..2].try_into()?;
let v = u16::from_le_bytes(*arr);
for i in 0..16 {
print!(
"{:4}",
if v & (1 << i) != 0 { 1 } else { 0 }
)
}
println!();
}
}
}
}
}
} else {
println!("{:?}", results);
}
Ok(())
}
pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Attached {
name: "gpio",
archive: Archive::Required,
attach: Attach::LiveOnly,
validate: Validate::Booted,
run: gpio,
},
GpioArgs::command(),
)
}