@@ -6,7 +6,6 @@ use crate::rcc::{Enable, Reset};
6
6
use crate :: gpio;
7
7
8
8
use crate :: rcc:: Clocks ;
9
- use embedded_hal_one:: i2c:: Operation ;
10
9
use fugit:: { HertzU32 as Hertz , RateExtU32 } ;
11
10
12
11
mod hal_02;
@@ -504,84 +503,57 @@ impl<I2C: Instance> I2c<I2C> {
504
503
self . write_bytes ( bytes. into_iter ( ) ) ?;
505
504
self . read ( addr, buffer)
506
505
}
506
+ }
507
507
508
- pub fn transaction < ' a > (
509
- & mut self ,
510
- addr : u8 ,
511
- mut ops : impl Iterator < Item = Operation < ' a > > ,
512
- ) -> Result < ( ) , Error > {
513
- if let Some ( mut prev_op) = ops. next ( ) {
514
- // 1. Generate Start for operation
515
- match & prev_op {
516
- Operation :: Read ( _) => self . prepare_read ( addr) ?,
517
- Operation :: Write ( _) => self . prepare_write ( addr) ?,
518
- } ;
519
-
520
- for op in ops {
521
- // 2. Execute previous operations.
522
- match & mut prev_op {
523
- Operation :: Read ( rb) => self . read_bytes ( rb) ?,
524
- Operation :: Write ( wb) => self . write_bytes ( wb. iter ( ) . cloned ( ) ) ?,
508
+ macro_rules! transaction_impl {
509
+ ( $vis: vis fn $function: ident: $operation: ty, $read: path, $write: path) => {
510
+ $vis fn $function(
511
+ & mut self ,
512
+ addr: u8 ,
513
+ ops_slice: & mut [ $operation] ,
514
+ ) -> Result <( ) , Error > {
515
+ let mut ops = ops_slice. iter_mut( ) ;
516
+
517
+ if let Some ( mut prev_op) = ops. next( ) {
518
+ // 1. Generate Start for operation
519
+ match & prev_op {
520
+ $read( _) => self . prepare_read( addr) ?,
521
+ $write( _) => self . prepare_write( addr) ?,
525
522
} ;
526
- // 3. If operation changes type we must generate new start
527
- match ( & prev_op, & op) {
528
- ( Operation :: Read ( _) , Operation :: Write ( _) ) => self . prepare_write ( addr) ?,
529
- ( Operation :: Write ( _) , Operation :: Read ( _) ) => self . prepare_read ( addr) ?,
530
- _ => { } // No changes if operation have not changed
531
- }
532
-
533
- prev_op = op;
534
- }
535
-
536
- // 4. Now, prev_op is last command use methods variations that will generate stop
537
- match prev_op {
538
- Operation :: Read ( rb) => self . read_wo_prepare ( rb) ?,
539
- Operation :: Write ( wb) => self . write_wo_prepare ( wb) ?,
540
- } ;
541
- }
542
523
543
- // Fallthrough is success
544
- Ok ( ( ) )
545
- }
546
-
547
- pub fn transaction_slice (
548
- & mut self ,
549
- addr : u8 ,
550
- ops_slice : & mut [ Operation < ' _ > ] ,
551
- ) -> Result < ( ) , Error > {
552
- let mut ops = ops_slice. iter_mut ( ) ;
553
-
554
- if let Some ( mut prev_op) = ops. next ( ) {
555
- // 1. Generate Start for operation
556
- match & prev_op {
557
- Operation :: Read ( _) => self . prepare_read ( addr) ?,
558
- Operation :: Write ( _) => self . prepare_write ( addr) ?,
559
- } ;
560
-
561
- for op in ops {
562
- // 2. Execute previous operations.
563
- match & mut prev_op {
564
- Operation :: Read ( rb) => self . read_bytes ( rb) ?,
565
- Operation :: Write ( wb) => self . write_bytes ( wb. iter ( ) . cloned ( ) ) ?,
566
- } ;
567
- // 3. If operation changes type we must generate new start
568
- match ( & prev_op, & op) {
569
- ( Operation :: Read ( _) , Operation :: Write ( _) ) => self . prepare_write ( addr) ?,
570
- ( Operation :: Write ( _) , Operation :: Read ( _) ) => self . prepare_read ( addr) ?,
571
- _ => { } // No changes if operation have not changed
524
+ for op in ops {
525
+ // 2. Execute previous operations.
526
+ match & mut prev_op {
527
+ $read( rb) => self . read_bytes( rb) ?,
528
+ $write( wb) => self . write_bytes( wb. iter( ) . cloned( ) ) ?,
529
+ } ;
530
+ // 3. If operation changes type we must generate new start
531
+ match ( & prev_op, & op) {
532
+ ( $read( _) , $write( _) ) => self . prepare_write( addr) ?,
533
+ ( $write( _) , $read( _) ) => self . prepare_read( addr) ?,
534
+ _ => { } // No changes if operation have not changed
535
+ }
536
+
537
+ prev_op = op;
572
538
}
573
539
574
- prev_op = op;
540
+ // 4. Now, prev_op is last command use methods variations that will generate stop
541
+ match prev_op {
542
+ $read( rb) => self . read_wo_prepare( rb) ?,
543
+ $write( wb) => self . write_wo_prepare( wb) ?,
544
+ } ;
575
545
}
576
546
577
- // 4. Now, prev_op is last command use methods variations that will generate stop
578
- match prev_op {
579
- Operation :: Read ( rb) => self . read_wo_prepare ( rb) ?,
580
- Operation :: Write ( wb) => self . write_wo_prepare ( wb) ?,
581
- } ;
547
+ // Fallthrough is success
548
+ Ok ( ( ) )
582
549
}
550
+ } ;
551
+ }
583
552
584
- // Fallthrough is success
585
- Ok ( ( ) )
586
- }
553
+ type Hal1Operation < ' a > = embedded_hal_one:: i2c:: Operation < ' a > ;
554
+ type Hal02Operation < ' a > = embedded_hal:: blocking:: i2c:: Operation < ' a > ;
555
+
556
+ impl < I2C : Instance > I2c < I2C > {
557
+ transaction_impl ! ( pub fn transaction_slice: Hal1Operation <' _>, Hal1Operation :: Read , Hal1Operation :: Write ) ;
558
+ transaction_impl ! ( fn transaction_slice_hal_02: Hal02Operation <' _>, Hal02Operation :: Read , Hal02Operation :: Write ) ;
587
559
}
0 commit comments