11//! SPI bus sharing mechanisms.
22
3+ use core:: convert:: Infallible ;
4+
35use embedded_hal:: delay:: DelayNs ;
46use embedded_hal:: digital:: OutputPin ;
57use embedded_hal:: spi:: { ErrorType , Operation , SpiBus , SpiDevice } ;
@@ -10,7 +12,6 @@ use embedded_hal_async::{
1012} ;
1113
1214use super :: shared:: transaction;
13- use super :: DeviceError ;
1415
1516/// [`SpiDevice`] implementation with exclusive access to the bus (not shared).
1617///
@@ -72,15 +73,15 @@ impl<BUS, CS> ExclusiveDevice<BUS, CS, super::NoDelay> {
7273impl < BUS , CS , D > ErrorType for ExclusiveDevice < BUS , CS , D >
7374where
7475 BUS : ErrorType ,
75- CS : OutputPin ,
76+ CS : OutputPin < Error = Infallible > ,
7677{
77- type Error = DeviceError < BUS :: Error , CS :: Error > ;
78+ type Error = BUS :: Error ;
7879}
7980
8081impl < Word : Copy + ' static , BUS , CS , D > SpiDevice < Word > for ExclusiveDevice < BUS , CS , D >
8182where
8283 BUS : SpiBus < Word > ,
83- CS : OutputPin ,
84+ CS : OutputPin < Error = Infallible > ,
8485 D : DelayNs ,
8586{
8687 #[ inline]
@@ -94,15 +95,17 @@ where
9495impl < Word : Copy + ' static , BUS , CS , D > AsyncSpiDevice < Word > for ExclusiveDevice < BUS , CS , D >
9596where
9697 BUS : AsyncSpiBus < Word > ,
97- CS : OutputPin ,
98+ CS : OutputPin < Error = Infallible > ,
9899 D : AsyncDelayNs ,
99100{
100101 #[ inline]
101102 async fn transaction (
102103 & mut self ,
103104 operations : & mut [ Operation < ' _ , Word > ] ,
104105 ) -> Result < ( ) , Self :: Error > {
105- self . cs . set_low ( ) . map_err ( DeviceError :: Cs ) ?;
106+ use crate :: spi:: shared:: into_ok;
107+
108+ into_ok ( self . cs . set_low ( ) ) ;
106109
107110 let op_res = ' ops: {
108111 for op in operations {
@@ -128,12 +131,8 @@ where
128131
129132 // On failure, it's important to still flush and deassert CS.
130133 let flush_res = self . bus . flush ( ) . await ;
131- let cs_res = self . cs . set_high ( ) ;
132-
133- op_res. map_err ( DeviceError :: Spi ) ?;
134- flush_res. map_err ( DeviceError :: Spi ) ?;
135- cs_res. map_err ( DeviceError :: Cs ) ?;
134+ into_ok ( self . cs . set_high ( ) ) ;
136135
137- Ok ( ( ) )
136+ op_res . and ( flush_res )
138137 }
139138}
0 commit comments