-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathspi-eh1-loopback.rs
88 lines (75 loc) · 2.86 KB
/
spi-eh1-loopback.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
//! This is a loopback example for embedded-hal-1 traits:
//! * SpiBus
//! * SpiDevice (TODO)
//!
//! Pin setup:
//! * D1 <-> D2 (connect SDO with SDI)
//!
//! Leave other pins unconnected.
use ftdi_embedded_hal as hal;
use std::thread::sleep;
use std::time::Duration;
fn main() {
cfg_if::cfg_if! {
if #[cfg(feature = "ftdi")] {
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
.interface(ftdi::Interface::A)
.open()
.unwrap();
} else if #[cfg(feature = "libftd2xx")] {
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
} else {
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
}
}
let hal = hal::FtHal::init_freq(device, 1_000_000).unwrap();
let mut spi = hal.spi().unwrap();
let delay = Duration::from_millis(250);
{
use hal::eh1::spi::SpiBus;
println!("=== SpiBus example with embedded-hal-1 traits ===");
// --- Symmetric transfer (Read as much as we write) ---
print!("Starting symmetric transfer...");
let write = [0xde, 0xad, 0xbe, 0xef];
let mut read: [u8; 4] = [0x00u8; 4];
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Symmetric transfer failed");
assert_eq!(write, read);
println!(" SUCCESS");
// --- Asymmetric transfer (Read more than we write) ---
print!("Starting asymetric transfer (read > write)...");
let mut read: [u8; 4] = [0x00; 4];
SpiBus::transfer(&mut spi, &mut read[0..2], &write[..])
.expect("Asymmetric transfer failed");
assert_eq!(write[0], read[0]);
assert_eq!(read[2], 0x00u8);
println!(" SUCCESS");
sleep(delay);
// --- Symmetric transfer with huge buffer ---
// Only your RAM is the limit!
print!("Starting huge transfer...");
let mut write = [0x55u8; 4096];
for (idx, byte) in write.iter_mut().enumerate() {
*byte = idx as u8;
}
let mut read = [0x00u8; 4096];
sleep(delay);
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Huge transfer failed");
assert_eq!(write, read);
println!(" SUCCESS");
sleep(delay);
// --- Symmetric transfer with huge buffer in-place (No additional allocation
// needed) ---
print!("Starting huge transfer (in-place)...");
let mut write = [0x55u8; 4096];
for (idx, byte) in write.iter_mut().enumerate() {
*byte = idx as u8;
}
SpiBus::transfer_in_place(&mut spi, &mut write[..]).expect("Huge transfer failed");
for (idx, byte) in write.iter().enumerate() {
assert_eq!(*byte, idx as u8);
}
println!(" SUCCESS");
sleep(delay);
}
// TODO: eh1 SpiDevice
}