-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.rs
230 lines (194 loc) · 6.16 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
// 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/.
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 = "lpc55gpio", about = "GPIO pin manipulation (lpc55 variant)")]
struct GpioArgs {
/// sets timeout
#[clap(
long, short = 'T', default_value = "5000", value_name = "timeout_ms",
value_parser=parse_int::parse::<u32>,
)]
timeout: u32,
/// toggle specified pins
#[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>,
/// configures specified pins
#[clap(long, short, requires = "pins")]
direction: Option<String>,
/// specifies GPIO pins on which to operate
#[clap(long, short, value_name = "pins")]
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", 1)?;
let gpio_set = funcs.get("GpioSet", 1)?;
let gpio_reset = funcs.get("GpioReset", 1)?;
let gpio_input = funcs.get("GpioInput", 1)?;
let gpio_configure = funcs.get("GpioConfigure", 7)?;
let gpio_direction = funcs.get("GpioDirection", 2)?;
let mut configure_args = vec![];
let mut direction_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 = ["AltFn", "Mode", "Slew", "Invert", "Digimode", "Opendrain"];
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],
1 + i,
params[i],
)?);
}
gpio_configure.id
} else if let Some(ref dir) = subargs.direction {
let params: Vec<&str> = dir.split(':').collect();
let args = ["Direction"];
if params.len() != args.len() {
bail!("expected {}", args.join(":"));
}
for i in 0..args.len() {
direction_args.push(gpio_direction.lookup_argument(
hubris,
args[i],
1 + i,
params[i],
)?);
}
gpio_direction.id
} else if subargs.input {
gpio_input.id
} else {
bail!("expected one of input, toggle, set, or reset to be specified");
};
let mut args: Vec<(u16, String)> = vec![];
if let Some(ref pins) = subargs.pins {
for pin in pins {
let pin = gpio_toggle.lookup_argument(hubris, "pin", 0, pin)?;
args.push((pin, pin.to_string()));
}
}
let mut ops = vec![];
if subargs.input {
if args.is_empty() {
let pins = gpio_input.argument_variants(hubris, 0)?;
for pin in &pins {
args.push((pin.1, pin.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));
for configure_arg in &configure_args {
ops.push(Op::Push16(*configure_arg));
}
ops.push(Op::Call(target));
ops.push(Op::DropN(7));
}
} else if subargs.direction.is_some() {
for arg in &args {
ops.push(Op::Push16(arg.0));
for direction_arg in &direction_args {
ops.push(Op::Push16(*direction_arg));
}
ops.push(Op::Call(target));
ops.push(Op::DropN(2));
}
} else {
for arg in &args {
ops.push(Op::Push16(arg.0));
ops.push(Op::Call(target));
ops.push(Op::DropN(1));
}
}
ops.push(Op::Done);
let results = context.run(core, ops.as_slice(), None)?;
if subargs.input {
for (ndx, arg) in args.iter().enumerate() {
println!(
"{} = {}",
arg.1,
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!("{}", v)
}
}
);
}
} else {
println!("{:?}", results);
}
Ok(())
}
pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Attached {
name: "lpc55gpio",
archive: Archive::Required,
attach: Attach::LiveOnly,
validate: Validate::Booted,
run: gpio,
},
GpioArgs::command(),
)
}