@@ -945,166 +945,93 @@ unsafe impl<SPI, STREAM, const CHANNEL: u8> DMASet<STREAM, CHANNEL, MemoryToPeri
945945{
946946}
947947
948- impl < SPI : Instance , const BIDI : bool , W : FrameSize > Spi < SPI , BIDI , W > {
949- pub fn read_nonblocking ( & mut self ) -> nb:: Result < W , Error > {
950- if BIDI {
951- self . bidi_input ( ) ;
952- }
953- self . check_read ( )
954- }
955-
956- pub fn write_nonblocking ( & mut self , byte : W ) -> nb:: Result < ( ) , Error > {
957- if BIDI {
958- self . bidi_output ( ) ;
959- }
960- self . check_send ( byte)
961- }
948+ macro_rules! spi_transfer {
949+ ( $Spi: ident) => {
950+ impl <SPI : Instance , const BIDI : bool , W : FrameSize > $Spi<SPI , BIDI , W > {
951+ pub fn read_nonblocking( & mut self ) -> nb:: Result <W , Error > {
952+ if BIDI {
953+ self . bidi_input( ) ;
954+ }
955+ self . check_read( )
956+ }
962957
963- pub fn transfer_in_place ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
964- for word in words {
965- nb:: block!( self . write_nonblocking( * word) ) ?;
966- * word = nb:: block!( self . read_nonblocking( ) ) ?;
967- }
958+ pub fn write_nonblocking( & mut self , byte: W ) -> nb:: Result <( ) , Error > {
959+ if BIDI {
960+ self . bidi_output( ) ;
961+ }
962+ self . check_send( byte)
963+ }
968964
969- Ok ( ( ) )
970- }
965+ pub fn transfer_in_place( & mut self , words: & mut [ W ] ) -> Result <( ) , Error > {
966+ for word in words {
967+ nb:: block!( self . write_nonblocking( * word) ) ?;
968+ * word = nb:: block!( self . read_nonblocking( ) ) ?;
969+ }
971970
972- pub fn transfer ( & mut self , buff : & mut [ W ] , data : & [ W ] ) -> Result < ( ) , Error > {
973- if data. len ( ) == buff. len ( ) {
974- for ( d, b) in data. iter ( ) . cloned ( ) . zip ( buff. iter_mut ( ) ) {
975- nb:: block!( self . write_nonblocking( d) ) ?;
976- * b = nb:: block!( self . read_nonblocking( ) ) ?;
971+ Ok ( ( ) )
977972 }
978- } else {
979- let mut iter_r = buff. iter_mut ( ) ;
980- let mut iter_w = data. iter ( ) . cloned ( ) ;
981- loop {
982- match ( iter_r. next ( ) , iter_w. next ( ) ) {
983- ( Some ( r) , Some ( w) ) => {
984- nb:: block!( self . write_nonblocking( w) ) ?;
985- * r = nb:: block!( self . read_nonblocking( ) ) ?;
986- }
987- ( Some ( r) , None ) => {
988- nb:: block!( self . write_nonblocking( W :: default ( ) ) ) ?;
989- * r = nb:: block!( self . read_nonblocking( ) ) ?;
973+
974+ pub fn transfer( & mut self , buff: & mut [ W ] , data: & [ W ] ) -> Result <( ) , Error > {
975+ if data. len( ) == buff. len( ) {
976+ for ( d, b) in data. iter( ) . cloned( ) . zip( buff. iter_mut( ) ) {
977+ nb:: block!( self . write_nonblocking( d) ) ?;
978+ * b = nb:: block!( self . read_nonblocking( ) ) ?;
990979 }
991- ( None , Some ( w) ) => {
992- nb:: block!( self . write_nonblocking( w) ) ?;
993- let _ = nb:: block!( self . read_nonblocking( ) ) ?;
980+ } else {
981+ let mut iter_r = buff. iter_mut( ) ;
982+ let mut iter_w = data. iter( ) . cloned( ) ;
983+ loop {
984+ match ( iter_r. next( ) , iter_w. next( ) ) {
985+ ( Some ( r) , Some ( w) ) => {
986+ nb:: block!( self . write_nonblocking( w) ) ?;
987+ * r = nb:: block!( self . read_nonblocking( ) ) ?;
988+ }
989+ ( Some ( r) , None ) => {
990+ nb:: block!( self . write_nonblocking( W :: default ( ) ) ) ?;
991+ * r = nb:: block!( self . read_nonblocking( ) ) ?;
992+ }
993+ ( None , Some ( w) ) => {
994+ nb:: block!( self . write_nonblocking( w) ) ?;
995+ let _ = nb:: block!( self . read_nonblocking( ) ) ?;
996+ }
997+ ( None , None ) => break ,
998+ }
994999 }
995- ( None , None ) => break ,
9961000 }
997- }
998- }
999-
1000- Ok ( ( ) )
1001- }
10021001
1003- pub fn flush ( & mut self ) -> Result < ( ) , Error > {
1004- Ok ( ( ) )
1005- }
1006-
1007- pub fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Error > {
1008- self . spi_write :: < BIDI , W > ( words. iter ( ) . copied ( ) )
1009- }
1010-
1011- pub fn write_iter ( & mut self , words : impl IntoIterator < Item = W > ) -> Result < ( ) , Error > {
1012- self . spi_write :: < BIDI , W > ( words)
1013- }
1014-
1015- pub fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
1016- if BIDI {
1017- self . bidi_input ( ) ;
1018- for word in words {
1019- * word = nb:: block!( self . check_read( ) ) ?;
1002+ Ok ( ( ) )
10201003 }
1021- } else {
1022- for word in words {
1023- nb:: block!( self . check_send( W :: default ( ) ) ) ?;
1024- * word = nb:: block!( self . check_read( ) ) ?;
1025- }
1026- }
1027-
1028- Ok ( ( ) )
1029- }
1030- }
1031-
1032- impl < SPI : Instance , const BIDI : bool , W : FrameSize > SpiSlave < SPI , BIDI , W > {
1033- pub fn read_nonblocking ( & mut self ) -> nb:: Result < W , Error > {
1034- if BIDI {
1035- self . bidi_input ( ) ;
1036- }
1037- self . check_read ( )
1038- }
10391004
1040- pub fn write_nonblocking ( & mut self , byte : W ) -> nb:: Result < ( ) , Error > {
1041- if BIDI {
1042- self . bidi_output ( ) ;
1043- }
1044- self . check_send ( byte)
1045- }
1046-
1047- pub fn transfer_in_place ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
1048- for word in words {
1049- nb:: block!( self . write_nonblocking( * word) ) ?;
1050- * word = nb:: block!( self . read_nonblocking( ) ) ?;
1051- }
1005+ pub fn flush( & mut self ) -> Result <( ) , Error > {
1006+ Ok ( ( ) )
1007+ }
10521008
1053- Ok ( ( ) )
1054- }
1009+ pub fn write( & mut self , words: & [ W ] ) -> Result <( ) , Error > {
1010+ self . spi_write:: <BIDI , W >( words. iter( ) . copied( ) )
1011+ }
10551012
1056- pub fn transfer ( & mut self , buff : & mut [ W ] , data : & [ W ] ) -> Result < ( ) , Error > {
1057- if data. len ( ) == buff. len ( ) {
1058- for ( d, b) in data. iter ( ) . cloned ( ) . zip ( buff. iter_mut ( ) ) {
1059- nb:: block!( self . write_nonblocking( d) ) ?;
1060- * b = nb:: block!( self . read_nonblocking( ) ) ?;
1013+ pub fn write_iter( & mut self , words: impl IntoIterator <Item = W >) -> Result <( ) , Error > {
1014+ self . spi_write:: <BIDI , W >( words)
10611015 }
1062- } else {
1063- let mut iter_r = buff. iter_mut ( ) ;
1064- let mut iter_w = data. iter ( ) . cloned ( ) ;
1065- loop {
1066- match ( iter_r. next ( ) , iter_w. next ( ) ) {
1067- ( Some ( r) , Some ( w) ) => {
1068- nb:: block!( self . write_nonblocking( w) ) ?;
1069- * r = nb:: block!( self . read_nonblocking( ) ) ?;
1070- }
1071- ( Some ( r) , None ) => {
1072- nb:: block!( self . write_nonblocking( W :: default ( ) ) ) ?;
1073- * r = nb:: block!( self . read_nonblocking( ) ) ?;
1016+
1017+ pub fn read( & mut self , words: & mut [ W ] ) -> Result <( ) , Error > {
1018+ if BIDI {
1019+ self . bidi_input( ) ;
1020+ for word in words {
1021+ * word = nb:: block!( self . check_read( ) ) ?;
10741022 }
1075- ( None , Some ( w) ) => {
1076- nb:: block!( self . write_nonblocking( w) ) ?;
1077- let _ = nb:: block!( self . read_nonblocking( ) ) ?;
1023+ } else {
1024+ for word in words {
1025+ nb:: block!( self . check_send( W :: default ( ) ) ) ?;
1026+ * word = nb:: block!( self . check_read( ) ) ?;
10781027 }
1079- ( None , None ) => break ,
10801028 }
1081- }
1082- }
1083-
1084- Ok ( ( ) )
1085- }
10861029
1087- pub fn flush ( & mut self ) -> Result < ( ) , Error > {
1088- Ok ( ( ) )
1089- }
1090-
1091- pub fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Error > {
1092- self . spi_write :: < BIDI , W > ( words. iter ( ) . copied ( ) )
1093- }
1094-
1095- pub fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
1096- if BIDI {
1097- self . bidi_input ( ) ;
1098- for word in words {
1099- * word = nb:: block!( self . check_read( ) ) ?;
1100- }
1101- } else {
1102- for word in words {
1103- nb:: block!( self . check_send( W :: default ( ) ) ) ?;
1104- * word = nb:: block!( self . check_read( ) ) ?;
1030+ Ok ( ( ) )
11051031 }
11061032 }
1107-
1108- Ok ( ( ) )
1109- }
1033+ } ;
11101034}
1035+
1036+ spi_transfer ! ( Spi ) ;
1037+ spi_transfer ! ( SpiSlave ) ;
0 commit comments