1
1
use core:: ops:: Deref ;
2
2
3
- use crate :: gpio:: { Const , OpenDrain , PinA , SetAlternate } ;
4
- use crate :: i2c:: { Error , Scl , Sda } ;
3
+ use crate :: i2c:: { Error , NoAcknowledgeSource , Pins } ;
5
4
use crate :: pac:: { fmpi2c1, FMPI2C1 , RCC } ;
6
5
use crate :: rcc:: { Enable , Reset } ;
7
6
use crate :: time:: { Hertz , U32Ext } ;
8
7
9
8
mod hal_02;
9
+ mod hal_1;
10
10
11
11
/// I2C FastMode+ abstraction
12
12
pub struct FMPI2c < I2C , PINS > {
@@ -67,12 +67,11 @@ where
67
67
}
68
68
}
69
69
70
- impl < SCL , SDA , const SCLA : u8 , const SDAA : u8 > FMPI2c < FMPI2C1 , ( SCL , SDA ) >
70
+ impl < PINS > FMPI2c < FMPI2C1 , PINS >
71
71
where
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 > ,
74
73
{
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 {
76
75
unsafe {
77
76
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
78
77
let rcc = & ( * RCC :: ptr ( ) ) ;
@@ -84,17 +83,15 @@ where
84
83
rcc. dckcfgr2 . modify ( |_, w| w. fmpi2c1sel ( ) . hsi ( ) ) ;
85
84
}
86
85
87
- pins. 0 . set_alt_mode ( ) ;
88
- pins. 1 . set_alt_mode ( ) ;
86
+ pins. set_alt_mode ( ) ;
89
87
90
88
let i2c = FMPI2c { i2c, pins } ;
91
89
i2c. i2c_init ( mode) ;
92
90
i2c
93
91
}
94
92
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 ( ) ;
98
95
99
96
( self . i2c , self . pins )
100
97
}
@@ -171,31 +168,39 @@ where
171
168
self . i2c
172
169
. icr
173
170
. write ( |w| w. stopcf ( ) . set_bit ( ) . nackcf ( ) . set_bit ( ) ) ;
174
- return Err ( Error :: NACK ) ;
171
+ return Err ( Error :: NoAcknowledge ( NoAcknowledgeSource :: Unknown ) ) ;
175
172
}
176
173
177
174
Ok ( ( ) )
178
175
}
179
176
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
+
180
184
fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
181
185
// Wait until we're ready for sending
182
186
while {
183
187
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) ?;
185
190
isr. txis ( ) . bit_is_clear ( )
186
191
} { }
187
192
188
193
// Push out a byte of data
189
194
self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
190
195
191
- self . check_and_clear_error_flags ( & self . i2c . isr . read ( ) ) ?;
192
- Ok ( ( ) )
196
+ self . end_transaction ( )
193
197
}
194
198
195
199
fn recv_byte ( & self ) -> Result < u8 , Error > {
196
200
while {
197
201
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) ?;
199
204
isr. rxne ( ) . bit_is_clear ( )
200
205
} { }
201
206
@@ -225,10 +230,7 @@ where
225
230
* c = self . recv_byte ( ) ?;
226
231
}
227
232
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 ( )
232
234
}
233
235
234
236
pub fn write ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
@@ -252,10 +254,7 @@ where
252
254
self . send_byte ( * c) ?;
253
255
}
254
256
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 ( )
259
258
}
260
259
261
260
pub fn write_read ( & mut self , addr : u8 , bytes : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) , Error > {
@@ -277,7 +276,8 @@ where
277
276
// Wait until the transmit buffer is empty and there hasn't been any error condition
278
277
while {
279
278
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) ?;
281
281
isr. txis ( ) . bit_is_clear ( ) && isr. tc ( ) . bit_is_clear ( )
282
282
} { }
283
283
@@ -289,7 +289,8 @@ where
289
289
// Wait until data was sent
290
290
while {
291
291
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) ?;
293
294
isr. tc ( ) . bit_is_clear ( )
294
295
} { }
295
296
@@ -314,9 +315,6 @@ where
314
315
* c = self . recv_byte ( ) ?;
315
316
}
316
317
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 ( )
321
319
}
322
320
}
0 commit comments