-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.rs
317 lines (270 loc) · 9.08 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
// 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 spi`
//!
//! `humility spi` can be used to read or write to attached SPI devices,
//! (as defined in the application TOML).
//!
//! The SPI peripheral (block) to be used can be specified with the
//! `--peripheral` (`-p`) option. This should be a number that matches SPI
//! peripheral number; if it is not specified (and there is only one
//! SPI-controlling task found), the peripheral associated with that task will
//! be assumed.
//!
//! Because of the full duplex nature of SPI, bytes will always be written
//! *and* read; to actually write specific bytes, the bytes to be written
//! should be specified via `--write` (`-w`). To report bytes read back,
//! `--read` (`-r`) should be specified, along with the number of bytes via
//! `--nbytes` (`-n`).
//!
//! For example, to write the byte sequence `0x1`, `0x0`, `0x0` and then read
//! 32 bytes (discarding the first three) from device 0 on SPI2:
//!
//! ```console
//! % humility spi -p 2 --nbytes 32 --write 0x1,0x0,0x0 --read --discard 3
//! humility: attached to 0483:374e:003C00174741500520383733 via ST-Link V3
//! humility: SPI master is spi2_driver
//! \/ 1 2 3 4 5 6 7 8 9 a b c d e f
//! 0x00000000 | 01 de aa 55 00 00 00 ff 06 00 00 00 1c 01 0b 00 | ...U............
//! 0x00000010 | 00 00 00 00 ff ff ff 06 12 00 00 00 06 | .............
//! ```
//!
use humility::core::Core;
use humility::hubris::*;
use humility_cmd::hiffy::*;
use humility_cmd::{Archive, Args, Attach, Command, Dumper, Validate};
use std::convert::TryInto;
use std::str;
use anyhow::{bail, Result};
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
use hif::*;
#[derive(Parser, Debug)]
#[clap(name = "spi", about = env!("CARGO_PKG_DESCRIPTION"))]
struct SpiArgs {
/// sets timeout
#[clap(
long, short = 'T', default_value = "5000", value_name = "timeout_ms",
value_parser=parse_int::parse::<u32>,
)]
timeout: u32,
/// SPI peripheral on which to operate
#[clap(long, short, value_name = "peripheral")]
peripheral: Option<u8>,
/// comma-separated bytes to write
#[clap(long, short, value_name = "bytes")]
write: Option<String>,
/// perform a read
#[clap(long, short, requires = "nbytes")]
read: bool,
/// specify number of bytes to read
#[clap(long, short, value_name = "nbytes",
value_parser=parse_int::parse::<usize>,
)]
nbytes: Option<usize>,
/// print out data read as words rather than bytes
#[clap(long, short = 'W', requires = "read")]
word: bool,
/// interpret the specified number of trailing bytes on a write as a
/// bigendian address
#[clap(
long, short = 'A', requires_all = &["read", "write", "discard"],
conflicts_with = "littleendian-address"
)]
bigendian_address: Option<usize>,
/// interpret the specified number of trailing bytes on a write as a
/// bigendian address
#[clap(
long, short = 'a', requires_all = &["read", "write", "discard"],
conflicts_with = "bigendian-address"
)]
littleendian_address: Option<usize>,
/// number of bytes to discard when printing read result
#[clap(long, short, value_name = "nbytes", requires = "read")]
discard: Option<usize>,
/// device open which to operate
#[clap(long, short = 'D', value_name = "device")]
device: Option<String>,
}
/// Looks up which Hubris task is associated with SPI (accepting a peripheral
/// hint to disambiguate).
pub fn spi_task(
hubris: &HubrisArchive,
peripheral: Option<u8>,
) -> Result<HubrisTask> {
let lookup = |peripheral| {
let spi = format!("spi{}", peripheral);
let tasks = hubris.lookup_feature(&spi)?;
match tasks.len() {
0 => Ok(None),
1 => Ok(Some(tasks[0])),
_ => {
bail!("more than one task has {}", spi);
}
}
};
let task = if let Some(peripheral) = peripheral {
match lookup(peripheral)? {
Some(task) => task,
None => {
bail!("SPI peripheral {} not found", peripheral);
}
}
} else {
let mut found = vec![];
for peripheral in 0..9 {
if let Some(task) = lookup(peripheral)? {
found.push((peripheral, task));
}
}
if found.is_empty() {
bail!("no SPI peripherals found")
}
if found.len() > 1 {
bail!(
"SPI peripheral must be specified; valid peripherals: {}",
found
.iter()
.map(|v| v.0.to_string())
.collect::<Vec<String>>()
.join(", ")
);
}
found[0].1
};
if task == HubrisTask::Kernel {
bail!("SPI task cannot be the kernel");
}
Ok(task)
}
fn spi(
hubris: &HubrisArchive,
core: &mut dyn Core,
_args: &Args,
subargs: &[String],
) -> Result<()> {
let subargs = SpiArgs::try_parse_from(subargs)?;
let mut context = HiffyContext::new(hubris, core, subargs.timeout)?;
let funcs = context.functions()?;
let spi_read = funcs.get("SpiRead", 4)?;
let spi_write = funcs.get("SpiWrite", 3)?;
let task = spi_task(hubris, subargs.peripheral)?;
let mut ops = vec![];
if let HubrisTask::Task(task) = task {
ops.push(Op::Push32(task));
} else {
bail!("SPI task cannot be the kernel");
}
if let Some(device) = subargs.device {
if let Ok(device) = parse_int::parse::<u8>(&device) {
ops.push(Op::Push(device));
} else {
bail!("illegal device {}", device);
}
} else {
// Device 0
ops.push(Op::Push(0));
}
humility::msg!("SPI master is {}", hubris.lookup_module(task)?.name);
let mut addr = 0;
let data = if let Some(ref write) = subargs.write {
let bytes: Vec<&str> = write.split(',').collect();
let mut arr = vec![];
for byte in &bytes {
if let Ok(val) = parse_int::parse::<u8>(byte) {
arr.push(val);
} else {
bail!("invalid byte {}", byte)
}
}
if let Some(size) = subargs.littleendian_address {
let l = arr.len();
if size > l {
bail!("number of address bytes cannot exceed bytes to write");
}
addr = match size {
1 => arr[l - size] as u32,
2 => u16::from_le_bytes(arr[l - size..l].try_into().unwrap())
as u32,
4 => u32::from_le_bytes(arr[l - size..l].try_into().unwrap())
as u32,
_ => {
bail!("invalid address size");
}
};
}
if let Some(size) = subargs.bigendian_address {
let l = arr.len();
if size > l {
bail!("number of address bytes cannot exceed bytes to write");
}
addr = match size {
1 => arr[l - size] as u32,
2 => u16::from_be_bytes(arr[l - size..l].try_into().unwrap())
as u32,
4 => u32::from_be_bytes(arr[l - size..l].try_into().unwrap())
as u32,
_ => {
bail!("invalid address size");
}
};
}
ops.push(Op::Push32(arr.len() as u32));
Some(arr)
} else {
if !subargs.read {
bail!("must specify read or write");
}
ops.push(Op::Push(0));
None
};
let discard = if let Some(discard) = subargs.discard {
if discard > subargs.nbytes.unwrap() {
bail!("cannot discard more than specified number of bytes");
}
discard
} else {
0
};
if subargs.read {
ops.push(Op::Push32(subargs.nbytes.unwrap() as u32));
ops.push(Op::Call(spi_read.id));
} else {
ops.push(Op::Call(spi_write.id));
}
ops.push(Op::Done);
let results = context.run(
core,
ops.as_slice(),
match data {
Some(ref data) => Some(data.as_slice()),
_ => None,
},
)?;
if subargs.read {
if let Ok(results) = &results[0] {
if results.len() < discard {
bail!("short read: {:x?}", results);
}
let mut dumper = Dumper::new();
dumper.size = if subargs.word { 4 } else { 1 };
dumper.dump(&results[discard..], addr);
return Ok(());
}
}
println!("{:x?}", results);
Ok(())
}
pub fn init() -> (Command, ClapCommand<'static>) {
(
Command::Attached {
name: "spi",
archive: Archive::Required,
attach: Attach::LiveOnly,
validate: Validate::Booted,
run: spi,
},
SpiArgs::command(),
)
}