11use core:: ops:: Deref ;
22
3- use crate :: gpio:: { Const , OpenDrain , PinA , SetAlternate } ;
4- use crate :: i2c:: { Error , Scl , Sda } ;
3+ use crate :: i2c:: { Error , Pins } ;
54use crate :: pac:: { fmpi2c1, FMPI2C1 , RCC } ;
65use crate :: rcc:: { Enable , Reset } ;
76use crate :: time:: { Hertz , U32Ext } ;
@@ -66,12 +65,11 @@ where
6665 }
6766}
6867
69- impl < SCL , SDA , const SCLA : u8 , const SDAA : u8 > FMPI2c < FMPI2C1 , ( SCL , SDA ) >
68+ impl < PINS > FMPI2c < FMPI2C1 , PINS >
7069where
71- SCL : PinA < Scl , FMPI2C1 , A = Const < SCLA > > + SetAlternate < OpenDrain , SCLA > ,
72- SDA : PinA < Sda , FMPI2C1 , A = Const < SDAA > > + SetAlternate < OpenDrain , SDAA > ,
70+ PINS : Pins < FMPI2C1 > ,
7371{
74- pub fn new < M : Into < FmpMode > > ( i2c : FMPI2C1 , mut pins : ( SCL , SDA ) , mode : M ) -> Self {
72+ pub fn new < M : Into < FmpMode > > ( i2c : FMPI2C1 , mut pins : PINS , mode : M ) -> Self {
7573 unsafe {
7674 // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
7775 let rcc = & ( * RCC :: ptr ( ) ) ;
@@ -83,17 +81,15 @@ where
8381 rcc. dckcfgr2 . modify ( |_, w| w. fmpi2c1sel ( ) . hsi ( ) ) ;
8482 }
8583
86- pins. 0 . set_alt_mode ( ) ;
87- pins. 1 . set_alt_mode ( ) ;
84+ pins. set_alt_mode ( ) ;
8885
8986 let i2c = FMPI2c { i2c, pins } ;
9087 i2c. i2c_init ( mode) ;
9188 i2c
9289 }
9390
94- pub fn release ( mut self ) -> ( FMPI2C1 , ( SCL , SDA ) ) {
95- self . pins . 0 . restore_mode ( ) ;
96- self . pins . 1 . restore_mode ( ) ;
91+ pub fn release ( mut self ) -> ( FMPI2C1 , PINS ) {
92+ self . pins . restore_mode ( ) ;
9793
9894 ( self . i2c , self . pins )
9995 }
@@ -180,21 +176,24 @@ where
180176 // Wait until we're ready for sending
181177 while {
182178 let isr = self . i2c . isr . read ( ) ;
183- self . check_and_clear_error_flags ( & isr) ?;
179+ self . check_and_clear_error_flags ( & isr)
180+ . map_err ( Error :: nack_addr) ?;
184181 isr. txis ( ) . bit_is_clear ( )
185182 } { }
186183
187184 // Push out a byte of data
188185 self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
189186
190- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
187+ self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) )
188+ . map_err ( Error :: nack_data) ?;
191189 Ok ( ( ) )
192190 }
193191
194192 fn recv_byte ( & self ) -> Result < u8 , Error > {
195193 while {
196194 let isr = self . i2c . isr . read ( ) ;
197- self . check_and_clear_error_flags ( & isr) ?;
195+ self . check_and_clear_error_flags ( & isr)
196+ . map_err ( Error :: nack_data) ?;
198197 isr. rxne ( ) . bit_is_clear ( )
199198 } { }
200199
0 commit comments