2525//! let spi_dev2 = SpiDevice::new(spi_bus, cs_pin2);
2626//! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128);
2727//! ```
28- use core:: future:: Future ;
2928
3029use embassy_sync:: blocking_mutex:: raw:: RawMutex ;
3130use embassy_sync:: mutex:: Mutex ;
3231use embedded_hal_1:: digital:: OutputPin ;
33- use embedded_hal_1:: spi:: ErrorType ;
32+ use embedded_hal_1:: spi:: Operation ;
3433use embedded_hal_async:: spi;
3534
3635use crate :: shared_bus:: SpiDeviceError ;
@@ -57,33 +56,92 @@ where
5756 type Error = SpiDeviceError < BUS :: Error , CS :: Error > ;
5857}
5958
60- unsafe impl < M , BUS , CS > spi:: SpiDevice for SpiDevice < ' _ , M , BUS , CS >
59+ impl < M , BUS , CS > spi:: SpiDeviceRead for SpiDevice < ' _ , M , BUS , CS >
6160where
62- M : RawMutex + ' static ,
63- BUS : spi:: SpiBusFlush + ' static ,
61+ M : RawMutex ,
62+ BUS : spi:: SpiBusRead ,
6463 CS : OutputPin ,
6564{
66- type Bus = BUS ;
65+ async fn read_transaction ( & mut self , operations : & mut [ & mut [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
66+ let mut bus = self . bus . lock ( ) . await ;
67+ self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
68+
69+ let op_res: Result < ( ) , BUS :: Error > = try {
70+ for buf in operations {
71+ bus. read ( buf) . await ?;
72+ }
73+ } ;
74+
75+ // On failure, it's important to still flush and deassert CS.
76+ let flush_res = bus. flush ( ) . await ;
77+ let cs_res = self . cs . set_high ( ) ;
6778
68- async fn transaction < R , F , Fut > ( & mut self , f : F ) -> Result < R , Self :: Error >
69- where
70- F : FnOnce ( * mut Self :: Bus ) -> Fut ,
71- Fut : Future < Output = Result < R , <Self :: Bus as ErrorType >:: Error > > ,
72- {
79+ let op_res = op_res. map_err ( SpiDeviceError :: Spi ) ?;
80+ flush_res. map_err ( SpiDeviceError :: Spi ) ?;
81+ cs_res. map_err ( SpiDeviceError :: Cs ) ?;
82+
83+ Ok ( op_res)
84+ }
85+ }
86+
87+ impl < M , BUS , CS > spi:: SpiDeviceWrite for SpiDevice < ' _ , M , BUS , CS >
88+ where
89+ M : RawMutex ,
90+ BUS : spi:: SpiBusWrite ,
91+ CS : OutputPin ,
92+ {
93+ async fn write_transaction ( & mut self , operations : & [ & [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
7394 let mut bus = self . bus . lock ( ) . await ;
7495 self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
7596
76- let f_res = f ( & mut * bus) . await ;
97+ let op_res: Result < ( ) , BUS :: Error > = try {
98+ for buf in operations {
99+ bus. write ( buf) . await ?;
100+ }
101+ } ;
77102
78103 // On failure, it's important to still flush and deassert CS.
79104 let flush_res = bus. flush ( ) . await ;
80105 let cs_res = self . cs . set_high ( ) ;
81106
82- let f_res = f_res . map_err ( SpiDeviceError :: Spi ) ?;
107+ let op_res = op_res . map_err ( SpiDeviceError :: Spi ) ?;
83108 flush_res. map_err ( SpiDeviceError :: Spi ) ?;
84109 cs_res. map_err ( SpiDeviceError :: Cs ) ?;
85110
86- Ok ( f_res)
111+ Ok ( op_res)
112+ }
113+ }
114+
115+ impl < M , BUS , CS > spi:: SpiDevice for SpiDevice < ' _ , M , BUS , CS >
116+ where
117+ M : RawMutex ,
118+ BUS : spi:: SpiBus ,
119+ CS : OutputPin ,
120+ {
121+ async fn transaction ( & mut self , operations : & mut [ spi:: Operation < ' _ , u8 > ] ) -> Result < ( ) , Self :: Error > {
122+ let mut bus = self . bus . lock ( ) . await ;
123+ self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
124+
125+ let op_res: Result < ( ) , BUS :: Error > = try {
126+ for op in operations {
127+ match op {
128+ Operation :: Read ( buf) => bus. read ( buf) . await ?,
129+ Operation :: Write ( buf) => bus. write ( buf) . await ?,
130+ Operation :: Transfer ( read, write) => bus. transfer ( read, write) . await ?,
131+ Operation :: TransferInPlace ( buf) => bus. transfer_in_place ( buf) . await ?,
132+ }
133+ }
134+ } ;
135+
136+ // On failure, it's important to still flush and deassert CS.
137+ let flush_res = bus. flush ( ) . await ;
138+ let cs_res = self . cs . set_high ( ) ;
139+
140+ let op_res = op_res. map_err ( SpiDeviceError :: Spi ) ?;
141+ flush_res. map_err ( SpiDeviceError :: Spi ) ?;
142+ cs_res. map_err ( SpiDeviceError :: Cs ) ?;
143+
144+ Ok ( op_res)
87145 }
88146}
89147
@@ -114,33 +172,94 @@ where
114172 type Error = SpiDeviceError < BUS :: Error , CS :: Error > ;
115173}
116174
117- unsafe impl < M , BUS , CS > spi:: SpiDevice for SpiDeviceWithConfig < ' _ , M , BUS , CS >
175+ impl < M , BUS , CS > spi:: SpiDeviceWrite for SpiDeviceWithConfig < ' _ , M , BUS , CS >
176+ where
177+ M : RawMutex ,
178+ BUS : spi:: SpiBusWrite + SetConfig ,
179+ CS : OutputPin ,
180+ {
181+ async fn write_transaction ( & mut self , operations : & [ & [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
182+ let mut bus = self . bus . lock ( ) . await ;
183+ bus. set_config ( & self . config ) ;
184+ self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
185+
186+ let op_res: Result < ( ) , BUS :: Error > = try {
187+ for buf in operations {
188+ bus. write ( buf) . await ?;
189+ }
190+ } ;
191+
192+ // On failure, it's important to still flush and deassert CS.
193+ let flush_res = bus. flush ( ) . await ;
194+ let cs_res = self . cs . set_high ( ) ;
195+
196+ let op_res = op_res. map_err ( SpiDeviceError :: Spi ) ?;
197+ flush_res. map_err ( SpiDeviceError :: Spi ) ?;
198+ cs_res. map_err ( SpiDeviceError :: Cs ) ?;
199+
200+ Ok ( op_res)
201+ }
202+ }
203+
204+ impl < M , BUS , CS > spi:: SpiDeviceRead for SpiDeviceWithConfig < ' _ , M , BUS , CS >
118205where
119- M : RawMutex + ' static ,
120- BUS : spi:: SpiBusFlush + SetConfig + ' static ,
206+ M : RawMutex ,
207+ BUS : spi:: SpiBusRead + SetConfig ,
121208 CS : OutputPin ,
122209{
123- type Bus = BUS ;
210+ async fn read_transaction ( & mut self , operations : & mut [ & mut [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
211+ let mut bus = self . bus . lock ( ) . await ;
212+ bus. set_config ( & self . config ) ;
213+ self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
214+
215+ let op_res: Result < ( ) , BUS :: Error > = try {
216+ for buf in operations {
217+ bus. read ( buf) . await ?;
218+ }
219+ } ;
220+
221+ // On failure, it's important to still flush and deassert CS.
222+ let flush_res = bus. flush ( ) . await ;
223+ let cs_res = self . cs . set_high ( ) ;
224+
225+ let op_res = op_res. map_err ( SpiDeviceError :: Spi ) ?;
226+ flush_res. map_err ( SpiDeviceError :: Spi ) ?;
227+ cs_res. map_err ( SpiDeviceError :: Cs ) ?;
228+
229+ Ok ( op_res)
230+ }
231+ }
124232
125- async fn transaction < R , F , Fut > ( & mut self , f : F ) -> Result < R , Self :: Error >
126- where
127- F : FnOnce ( * mut Self :: Bus ) -> Fut ,
128- Fut : Future < Output = Result < R , <Self :: Bus as ErrorType >:: Error > > ,
129- {
233+ impl < M , BUS , CS > spi:: SpiDevice for SpiDeviceWithConfig < ' _ , M , BUS , CS >
234+ where
235+ M : RawMutex ,
236+ BUS : spi:: SpiBus + SetConfig ,
237+ CS : OutputPin ,
238+ {
239+ async fn transaction ( & mut self , operations : & mut [ spi:: Operation < ' _ , u8 > ] ) -> Result < ( ) , Self :: Error > {
130240 let mut bus = self . bus . lock ( ) . await ;
131241 bus. set_config ( & self . config ) ;
132242 self . cs . set_low ( ) . map_err ( SpiDeviceError :: Cs ) ?;
133243
134- let f_res = f ( & mut * bus) . await ;
244+ let op_res: Result < ( ) , BUS :: Error > = try {
245+ for op in operations {
246+ match op {
247+ Operation :: Read ( buf) => bus. read ( buf) . await ?,
248+ Operation :: Write ( buf) => bus. write ( buf) . await ?,
249+ Operation :: Transfer ( read, write) => bus. transfer ( read, write) . await ?,
250+ Operation :: TransferInPlace ( buf) => bus. transfer_in_place ( buf) . await ?,
251+ }
252+ }
253+ } ;
135254
136255 // On failure, it's important to still flush and deassert CS.
137256 let flush_res = bus. flush ( ) . await ;
138257 let cs_res = self . cs . set_high ( ) ;
139258
140- let f_res = f_res . map_err ( SpiDeviceError :: Spi ) ?;
259+ let op_res = op_res . map_err ( SpiDeviceError :: Spi ) ?;
141260 flush_res. map_err ( SpiDeviceError :: Spi ) ?;
142261 cs_res. map_err ( SpiDeviceError :: Cs ) ?;
143262
144- Ok ( f_res )
263+ Ok ( op_res )
145264 }
146265}
0 commit comments