Skip to content

Commit fca0b1f

Browse files
plaesnewAM
authored andcommitted
examples: Add spi-eh1-loopback example
Add embedded-hal-1 traits usage example for SpiBus.
1 parent 6e10b5d commit fca0b1f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

examples/spi-eh1-loopback.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! This is a loopback example for embedded-hal-1 traits:
2+
//! * SpiBus
3+
//! * SpiDevice (TODO)
4+
//! Pin setup:
5+
//! * D1 <-> D2 (connect SDO with SDI)
6+
//! Leave other pins unconnected.
7+
use ftdi_embedded_hal as hal;
8+
use std::thread::sleep;
9+
use std::time::Duration;
10+
11+
fn main() {
12+
cfg_if::cfg_if! {
13+
if #[cfg(feature = "ftdi")] {
14+
let device = ftdi::find_by_vid_pid(0x0403, 0x6014)
15+
.interface(ftdi::Interface::A)
16+
.open()
17+
.unwrap();
18+
} else if #[cfg(feature = "libftd2xx")] {
19+
let device = libftd2xx::Ft232h::with_description("Single RS232-HS").unwrap();
20+
} else {
21+
compile_error!("one of features 'ftdi' and 'libftd2xx' shall be enabled");
22+
}
23+
}
24+
25+
let hal = hal::FtHal::init_freq(device, 1_000_000).unwrap();
26+
let mut spi = hal.spi().unwrap();
27+
28+
let delay = Duration::from_millis(250);
29+
30+
{
31+
use hal::eh1::spi::SpiBus;
32+
33+
println!("=== SpiBus example with embedded-hal-1 traits ===");
34+
35+
// --- Symmetric transfer (Read as much as we write) ---
36+
print!("Starting symmetric transfer...");
37+
let write = [0xde, 0xad, 0xbe, 0xef];
38+
let mut read: [u8; 4] = [0x00u8; 4];
39+
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Symmetric transfer failed");
40+
assert_eq!(write, read);
41+
println!(" SUCCESS");
42+
43+
// XXX: After we do an asymmetric transfer, we still have two leftover
44+
// bytes are still in the read buffer, which breaks tests afterwards.
45+
// Spi::flush(&mut spi) doesn't help either
46+
47+
/*
48+
// --- Asymmetric transfer (Read more than we write) ---
49+
print!("Starting asymetric transfer (read > write)...");
50+
let mut read: [u8; 4] = [0x00; 4];
51+
52+
SpiBus::transfer(&mut spi, &mut read[0..2], &write[..])
53+
.expect("Asymmetric transfer failed");
54+
assert_eq!(write[0], read[0]);
55+
assert_eq!(read[2], 0x00u8);
56+
println!(" SUCCESS");
57+
sleep(delay);
58+
*/
59+
60+
// --- Symmetric transfer with huge buffer ---
61+
// Only your RAM is the limit!
62+
print!("Starting huge transfer...");
63+
let mut write = [0x55u8; 4096];
64+
for byte in 0..write.len() {
65+
write[byte] = byte as u8;
66+
}
67+
let mut read = [0x00u8; 4096];
68+
sleep(delay);
69+
70+
SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Huge transfer failed");
71+
assert_eq!(write, read);
72+
println!(" SUCCESS");
73+
sleep(delay);
74+
75+
// --- Symmetric transfer with huge buffer in-place (No additional allocation
76+
// needed) ---
77+
print!("Starting huge transfer (in-place)...");
78+
let mut write = [0x55u8; 4096];
79+
for byte in 0..write.len() {
80+
write[byte] = byte as u8;
81+
}
82+
83+
SpiBus::transfer_in_place(&mut spi, &mut write[..]).expect("Huge transfer failed");
84+
for byte in 0..write.len() {
85+
assert_eq!(write[byte], byte as u8);
86+
}
87+
println!(" SUCCESS");
88+
sleep(delay);
89+
}
90+
91+
// TODO: eh1 SpiDevice
92+
}

0 commit comments

Comments
 (0)