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 , NoAcknowledgeSource , Pins } ;
54use crate :: pac:: { fmpi2c1, FMPI2C1 , RCC } ;
65use crate :: rcc:: { Enable , Reset } ;
76use crate :: time:: { Hertz , U32Ext } ;
87
98mod hal_02;
9+ mod hal_1;
1010
1111/// I2C FastMode+ abstraction
1212pub struct FMPI2c < I2C , PINS > {
@@ -67,12 +67,11 @@ where
6767 }
6868}
6969
70- impl < SCL , SDA , const SCLA : u8 , const SDAA : u8 > FMPI2c < FMPI2C1 , ( SCL , SDA ) >
70+ impl < PINS > FMPI2c < FMPI2C1 , PINS >
7171where
72- SCL : PinA < Scl , FMPI2C1 , A = Const < SCLA > > + SetAlternate < OpenDrain , SCLA > ,
73- SDA : PinA < Sda , FMPI2C1 , A = Const < SDAA > > + SetAlternate < OpenDrain , SDAA > ,
72+ PINS : Pins < FMPI2C1 > ,
7473{
75- pub fn new < M : Into < FmpMode > > ( i2c : FMPI2C1 , mut pins : ( SCL , SDA ) , mode : M ) -> Self {
74+ pub fn new < M : Into < FmpMode > > ( i2c : FMPI2C1 , mut pins : PINS , mode : M ) -> Self {
7675 unsafe {
7776 // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
7877 let rcc = & ( * RCC :: ptr ( ) ) ;
@@ -84,17 +83,15 @@ where
8483 rcc. dckcfgr2 . modify ( |_, w| w. fmpi2c1sel ( ) . hsi ( ) ) ;
8584 }
8685
87- pins. 0 . set_alt_mode ( ) ;
88- pins. 1 . set_alt_mode ( ) ;
86+ pins. set_alt_mode ( ) ;
8987
9088 let i2c = FMPI2c { i2c, pins } ;
9189 i2c. i2c_init ( mode) ;
9290 i2c
9391 }
9492
95- pub fn release ( mut self ) -> ( FMPI2C1 , ( SCL , SDA ) ) {
96- self . pins . 0 . restore_mode ( ) ;
97- self . pins . 1 . restore_mode ( ) ;
93+ pub fn release ( mut self ) -> ( FMPI2C1 , PINS ) {
94+ self . pins . restore_mode ( ) ;
9895
9996 ( self . i2c , self . pins )
10097 }
@@ -171,31 +168,39 @@ where
171168 self . i2c
172169 . icr
173170 . write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
174- return Err ( Error :: NACK ) ;
171+ return Err ( Error :: NoAcknowledge ( NoAcknowledgeSource :: Unknown ) ) ;
175172 }
176173
177174 Ok ( ( ) )
178175 }
179176
177+ fn end_transaction ( & self ) -> Result < ( ) , Error > {
178+ // Check and clear flags if they somehow ended up set
179+ self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) )
180+ . map_err ( Error :: nack_data) ?;
181+ Ok ( ( ) )
182+ }
183+
180184 fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
181185 // Wait until we're ready for sending
182186 while {
183187 let isr = self . i2c . isr . read ( ) ;
184- self . check_and_clear_error_flags ( & isr) ?;
188+ self . check_and_clear_error_flags ( & isr)
189+ . map_err ( Error :: nack_addr) ?;
185190 isr. txis ( ) . bit_is_clear ( )
186191 } { }
187192
188193 // Push out a byte of data
189194 self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
190195
191- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
192- Ok ( ( ) )
196+ self . end_transaction ( )
193197 }
194198
195199 fn recv_byte ( & self ) -> Result < u8 , Error > {
196200 while {
197201 let isr = self . i2c . isr . read ( ) ;
198- self . check_and_clear_error_flags ( & isr) ?;
202+ self . check_and_clear_error_flags ( & isr)
203+ . map_err ( Error :: nack_data) ?;
199204 isr. rxne ( ) . bit_is_clear ( )
200205 } { }
201206
@@ -225,10 +230,7 @@ where
225230 * c = self . recv_byte ( ) ?;
226231 }
227232
228- // Check and clear flags if they somehow ended up set
229- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
230-
231- Ok ( ( ) )
233+ self . end_transaction ( )
232234 }
233235
234236 pub fn write ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
@@ -252,10 +254,7 @@ where
252254 self . send_byte ( * c) ?;
253255 }
254256
255- // Check and clear flags if they somehow ended up set
256- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
257-
258- Ok ( ( ) )
257+ self . end_transaction ( )
259258 }
260259
261260 pub fn write_read ( & mut self , addr : u8 , bytes : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) , Error > {
@@ -277,7 +276,8 @@ where
277276 // Wait until the transmit buffer is empty and there hasn't been any error condition
278277 while {
279278 let isr = self . i2c . isr . read ( ) ;
280- self . check_and_clear_error_flags ( & isr) ?;
279+ self . check_and_clear_error_flags ( & isr)
280+ . map_err ( Error :: nack_addr) ?;
281281 isr. txis ( ) . bit_is_clear ( ) && isr. tc ( ) . bit_is_clear ( )
282282 } { }
283283
@@ -289,7 +289,8 @@ where
289289 // Wait until data was sent
290290 while {
291291 let isr = self . i2c . isr . read ( ) ;
292- self . check_and_clear_error_flags ( & isr) ?;
292+ self . check_and_clear_error_flags ( & isr)
293+ . map_err ( Error :: nack_data) ?;
293294 isr. tc ( ) . bit_is_clear ( )
294295 } { }
295296
@@ -314,9 +315,6 @@ where
314315 * c = self . recv_byte ( ) ?;
315316 }
316317
317- // Check and clear flags if they somehow ended up set
318- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
319-
320- Ok ( ( ) )
318+ self . end_transaction ( )
321319 }
322320}
0 commit comments