Skip to content

Commit 5b21032

Browse files
committed
Add support for delay transactions
1 parent 9595bab commit 5b21032

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
[Full Changelog](https://github.com/rust-embedded/rust-spidev/compare/0.5.1...HEAD)
66

7+
- Added support for delay transactions.
8+
79
## 0.5.1 / 2021-11-22
810

911
[Full Changelog](https://github.com/rust-embedded/rust-spidev/compare/0.5.0...0.5.1)

examples/spidev-bidir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ fn main() {
2121
println!("===== Multi Transfer =========");
2222
let mut rx_buf1 = [0; 10];
2323
let tx_buf2 = [0x00, 0x01, 0x02, 0x03];
24+
let delay_usecs = 10;
2425
let tx_buf3 = [0xff, 0xfe, 0xfd];
2526
let mut rx_buf3 = [0; 3];
2627
let result = {
2728
let mut transfers = vec![
2829
SpidevTransfer::read(&mut rx_buf1),
2930
SpidevTransfer::write(&tx_buf2),
31+
SpidevTransfer::delay(delay_usecs),
3032
SpidevTransfer::read_write(&tx_buf3, &mut rx_buf3),
3133
];
3234
spidev.transfer_multiple(&mut transfers)
@@ -35,6 +37,7 @@ fn main() {
3537
Ok(_) => {
3638
println!("Read {:?}", rx_buf1);
3739
println!("Wrote {:?}", tx_buf2);
40+
println!("Delayed by {} microseconds", delay_usecs);
3841
println!("Wrote {:?} and read {:?}", tx_buf3, rx_buf3);
3942
}
4043
Err(err) => println!("{:?}", err),

src/spidevioctl.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub struct spi_ioc_transfer<'a, 'b> {
7878
}
7979

8080
impl<'a, 'b> spi_ioc_transfer<'a, 'b> {
81+
/// Create a read transfer
8182
pub fn read(buff: &'b mut [u8]) -> Self {
8283
spi_ioc_transfer {
8384
rx_buf: buff.as_ptr() as *const () as usize as u64,
@@ -86,6 +87,7 @@ impl<'a, 'b> spi_ioc_transfer<'a, 'b> {
8687
}
8788
}
8889

90+
/// Create a write transfer
8991
pub fn write(buff: &'a [u8]) -> Self {
9092
spi_ioc_transfer {
9193
tx_buf: buff.as_ptr() as *const () as usize as u64,
@@ -94,7 +96,8 @@ impl<'a, 'b> spi_ioc_transfer<'a, 'b> {
9496
}
9597
}
9698

97-
/// The `tx_buf` and `rx_buf` must be the same length.
99+
/// Create a read/write transfer.
100+
/// Note that the `tx_buf` and `rx_buf` must be the same length.
98101
pub fn read_write(tx_buf: &'a [u8], rx_buf: &'b mut [u8]) -> Self {
99102
assert_eq!(tx_buf.len(), rx_buf.len());
100103
spi_ioc_transfer {
@@ -104,6 +107,15 @@ impl<'a, 'b> spi_ioc_transfer<'a, 'b> {
104107
..Default::default()
105108
}
106109
}
110+
111+
/// Create a delay transfer of a number of microseconds
112+
pub fn delay(microseconds: u16) -> Self {
113+
spi_ioc_transfer {
114+
delay_usecs: microseconds,
115+
len: 0,
116+
..Default::default()
117+
}
118+
}
107119
}
108120

109121
mod ioctl {

0 commit comments

Comments
 (0)