-
Notifications
You must be signed in to change notification settings - Fork 2
/
spi.rs
83 lines (67 loc) · 1.91 KB
/
spi.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
use core::fmt;
use core::pin;
use core::task;
pub mod complete;
pub trait Spi<'t, 'a>: fmt::Debug {
type Error;
type Transaction: SpiTransaction<'t, 'a, Error = Self::Error>;
fn transaction(&'t mut self) -> Self::Transaction;
}
pub trait SpiTransaction<'t, 'a>: fmt::Debug {
type Error;
type Transfer: SpiTransfer<'a, Error = Self::Error>;
type TransferSplit: SpiTransfer<'a, Error = Self::Error>;
fn transfer(&'a mut self, buffer: &'a mut [u8]) -> Result<Self::Transfer, Self::Error>;
fn transfer_split(
&'a mut self,
tx_buffer: &'a [u8],
rx_buffer: &'a mut [u8],
) -> Result<Self::TransferSplit, Self::Error>;
}
pub trait SpiTransfer<'a> {
type Error;
fn poll_complete(
self: pin::Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> task::Poll<Result<(), Self::Error>>;
}
pub trait SpiTransferExt<'a>: SpiTransfer<'a> {
fn complete(&'a mut self) -> complete::Complete<'a, Self>
where
Self: Unpin,
{
complete::complete(self)
}
}
impl<'a, T> SpiTransferExt<'a> for T where T: SpiTransfer<'a> {}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Polarity {
IdleLow,
IdleHigh,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Phase {
CaptureOnFirstTransition,
CaptureOnSecondTransition,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Mode {
pub polarity: Polarity,
pub phase: Phase,
}
pub const MODE_0: Mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};
pub const MODE_1: Mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnSecondTransition,
};
pub const MODE_2: Mode = Mode {
polarity: Polarity::IdleHigh,
phase: Phase::CaptureOnFirstTransition,
};
pub const MODE_3: Mode = Mode {
polarity: Polarity::IdleHigh,
phase: Phase::CaptureOnSecondTransition,
};