@@ -102,3 +102,56 @@ pub mod write_iter {
102
102
}
103
103
}
104
104
}
105
+
106
+ /// Operation for transactional SPI trait
107
+ ///
108
+ /// This allows composition of SPI operations into a single bus transaction
109
+ #[ derive( Debug , PartialEq ) ]
110
+ pub enum Operation < ' a , W : ' static > {
111
+ /// Write data from the provided buffer, discarding read data
112
+ Write ( & ' a [ W ] ) ,
113
+ /// Write data out while reading data into the provided buffer
114
+ Transfer ( & ' a mut [ W ] ) ,
115
+ }
116
+
117
+ /// Transactional trait allows multiple actions to be executed
118
+ /// as part of a single SPI transaction
119
+ pub trait Transactional < W : ' static > {
120
+ /// Associated error type
121
+ type Error ;
122
+
123
+ /// Execute the provided transactions
124
+ fn try_exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Self :: Error > ;
125
+ }
126
+
127
+ /// Blocking transactional impl over spi::Write and spi::Transfer
128
+ pub mod transactional {
129
+ use super :: { Operation , Write , Transfer } ;
130
+
131
+ /// Default implementation of `blocking::spi::Transactional<W>` for implementers of
132
+ /// `spi::Write<W>` and `spi::Transfer<W>`
133
+ pub trait Default < W , E > { }
134
+
135
+ impl < W : ' static , E , S > super :: Transactional < W > for S
136
+ where
137
+ S : Write < W , Error =E > + Transfer < W , Error =E > ,
138
+ W : Copy + Clone ,
139
+ {
140
+ type Error = E ;
141
+
142
+ fn try_exec < ' a > (
143
+ & mut self ,
144
+ operations : & mut [ super :: Operation < ' a , W > ] ,
145
+
146
+ ) -> Result < ( ) , E > {
147
+ for op in operations {
148
+ match op {
149
+ Operation :: Write ( w) => self . try_write ( w) ?,
150
+ Operation :: Transfer ( t) => self . try_transfer ( t) . map ( |_| ( ) ) ?,
151
+ }
152
+ }
153
+
154
+ Ok ( ( ) )
155
+ }
156
+ }
157
+ }
0 commit comments