@@ -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;
@@ -508,25 +507,27 @@ impl<I2C: Instance> I2c<I2C> {
508
507
pub fn transaction < ' a > (
509
508
& mut self ,
510
509
addr : u8 ,
511
- mut ops : impl Iterator < Item = Operation < ' a > > ,
510
+ mut ops : impl Iterator < Item = Hal1Operation < ' a > > ,
512
511
) -> Result < ( ) , Error > {
513
512
if let Some ( mut prev_op) = ops. next ( ) {
514
513
// 1. Generate Start for operation
515
514
match & prev_op {
516
- Operation :: Read ( _) => self . prepare_read ( addr) ?,
517
- Operation :: Write ( _) => self . prepare_write ( addr) ?,
515
+ Hal1Operation :: Read ( _) => self . prepare_read ( addr) ?,
516
+ Hal1Operation :: Write ( _) => self . prepare_write ( addr) ?,
518
517
} ;
519
518
520
519
for op in ops {
521
520
// 2. Execute previous operations.
522
521
match & mut prev_op {
523
- Operation :: Read ( rb) => self . read_bytes ( rb) ?,
524
- Operation :: Write ( wb) => self . write_bytes ( wb. iter ( ) . cloned ( ) ) ?,
522
+ Hal1Operation :: Read ( rb) => self . read_bytes ( rb) ?,
523
+ Hal1Operation :: Write ( wb) => self . write_bytes ( wb. iter ( ) . cloned ( ) ) ?,
525
524
} ;
526
525
// 3. If operation changes type we must generate new start
527
526
match ( & prev_op, & op) {
528
- ( Operation :: Read ( _) , Operation :: Write ( _) ) => self . prepare_write ( addr) ?,
529
- ( Operation :: Write ( _) , Operation :: Read ( _) ) => self . prepare_read ( addr) ?,
527
+ ( Hal1Operation :: Read ( _) , Hal1Operation :: Write ( _) ) => {
528
+ self . prepare_write ( addr) ?
529
+ }
530
+ ( Hal1Operation :: Write ( _) , Hal1Operation :: Read ( _) ) => self . prepare_read ( addr) ?,
530
531
_ => { } // No changes if operation have not changed
531
532
}
532
533
@@ -535,8 +536,8 @@ impl<I2C: Instance> I2c<I2C> {
535
536
536
537
// 4. Now, prev_op is last command use methods variations that will generate stop
537
538
match prev_op {
538
- Operation :: Read ( rb) => self . read_wo_prepare ( rb) ?,
539
- Operation :: Write ( wb) => self . write_wo_prepare ( wb) ?,
539
+ Hal1Operation :: Read ( rb) => self . read_wo_prepare ( rb) ?,
540
+ Hal1Operation :: Write ( wb) => self . write_wo_prepare ( wb) ?,
540
541
} ;
541
542
}
542
543
@@ -547,27 +548,47 @@ impl<I2C: Instance> I2c<I2C> {
547
548
pub fn transaction_slice (
548
549
& mut self ,
549
550
addr : u8 ,
550
- ops_slice : & mut [ Operation < ' _ > ] ,
551
+ ops_slice : & mut [ Hal1Operation < ' _ > ] ,
551
552
) -> Result < ( ) , Error > {
552
- let mut ops = ops_slice. iter_mut ( ) ;
553
+ transaction_impl ! ( self , addr, ops_slice, Hal1Operation ) ;
554
+ // Fallthrough is success
555
+ Ok ( ( ) )
556
+ }
557
+
558
+ fn transaction_slice_hal_02 (
559
+ & mut self ,
560
+ addr : u8 ,
561
+ ops_slice : & mut [ Hal02Operation < ' _ > ] ,
562
+ ) -> Result < ( ) , Error > {
563
+ transaction_impl ! ( self , addr, ops_slice, Hal02Operation ) ;
564
+ // Fallthrough is success
565
+ Ok ( ( ) )
566
+ }
567
+ }
568
+
569
+ macro_rules! transaction_impl {
570
+ ( $self: ident, $addr: ident, $ops_slice: ident, $Operation: ident) => {
571
+ let i2c = $self;
572
+ let addr = $addr;
573
+ let mut ops = $ops_slice. iter_mut( ) ;
553
574
554
575
if let Some ( mut prev_op) = ops. next( ) {
555
576
// 1. Generate Start for operation
556
577
match & prev_op {
557
- Operation :: Read ( _) => self . prepare_read ( addr) ?,
558
- Operation :: Write ( _) => self . prepare_write ( addr) ?,
578
+ $ Operation:: Read ( _) => i2c . prepare_read( addr) ?,
579
+ $ Operation:: Write ( _) => i2c . prepare_write( addr) ?,
559
580
} ;
560
581
561
582
for op in ops {
562
583
// 2. Execute previous operations.
563
584
match & mut prev_op {
564
- Operation :: Read ( rb) => self . read_bytes ( rb) ?,
565
- Operation :: Write ( wb) => self . write_bytes ( wb. iter ( ) . cloned ( ) ) ?,
585
+ $ Operation:: Read ( rb) => i2c . read_bytes( rb) ?,
586
+ $ Operation:: Write ( wb) => i2c . write_bytes( wb. iter( ) . cloned( ) ) ?,
566
587
} ;
567
588
// 3. If operation changes type we must generate new start
568
589
match ( & prev_op, & op) {
569
- ( Operation :: Read ( _) , Operation :: Write ( _) ) => self . prepare_write ( addr) ?,
570
- ( Operation :: Write ( _) , Operation :: Read ( _) ) => self . prepare_read ( addr) ?,
590
+ ( $ Operation:: Read ( _) , $ Operation:: Write ( _) ) => i2c . prepare_write( addr) ?,
591
+ ( $ Operation:: Write ( _) , $ Operation:: Read ( _) ) => i2c . prepare_read( addr) ?,
571
592
_ => { } // No changes if operation have not changed
572
593
}
573
594
@@ -576,12 +597,13 @@ impl<I2C: Instance> I2c<I2C> {
576
597
577
598
// 4. Now, prev_op is last command use methods variations that will generate stop
578
599
match prev_op {
579
- Operation :: Read ( rb) => self . read_wo_prepare ( rb) ?,
580
- Operation :: Write ( wb) => self . write_wo_prepare ( wb) ?,
600
+ $ Operation:: Read ( rb) => i2c . read_wo_prepare( rb) ?,
601
+ $ Operation:: Write ( wb) => i2c . write_wo_prepare( wb) ?,
581
602
} ;
582
603
}
583
-
584
- // Fallthrough is success
585
- Ok ( ( ) )
586
- }
604
+ } ;
587
605
}
606
+ use transaction_impl;
607
+
608
+ type Hal1Operation < ' a > = embedded_hal_one:: i2c:: Operation < ' a > ;
609
+ type Hal02Operation < ' a > = embedded_hal:: blocking:: i2c:: Operation < ' a > ;
0 commit comments