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 , Pins } ;
5
4
use crate :: pac:: { fmpi2c1, FMPI2C1 , RCC } ;
6
5
use crate :: rcc:: { Enable , Reset } ;
7
6
use crate :: time:: { Hertz , U32Ext } ;
@@ -66,12 +65,11 @@ where
66
65
}
67
66
}
68
67
69
- impl < SCL , SDA , const SCLA : u8 , const SDAA : u8 > FMPI2c < FMPI2C1 , ( SCL , SDA ) >
68
+ impl < PINS > FMPI2c < FMPI2C1 , PINS >
70
69
where
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 > ,
73
71
{
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 {
75
73
unsafe {
76
74
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
77
75
let rcc = & ( * RCC :: ptr ( ) ) ;
@@ -83,17 +81,15 @@ where
83
81
rcc. dckcfgr2 . modify ( |_, w| w. fmpi2c1sel ( ) . hsi ( ) ) ;
84
82
}
85
83
86
- pins. 0 . set_alt_mode ( ) ;
87
- pins. 1 . set_alt_mode ( ) ;
84
+ pins. set_alt_mode ( ) ;
88
85
89
86
let i2c = FMPI2c { i2c, pins } ;
90
87
i2c. i2c_init ( mode) ;
91
88
i2c
92
89
}
93
90
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 ( ) ;
97
93
98
94
( self . i2c , self . pins )
99
95
}
@@ -180,21 +176,24 @@ where
180
176
// Wait until we're ready for sending
181
177
while {
182
178
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) ?;
184
181
isr. txis ( ) . bit_is_clear ( )
185
182
} { }
186
183
187
184
// Push out a byte of data
188
185
self . i2c . txdr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
189
186
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) ?;
191
189
Ok ( ( ) )
192
190
}
193
191
194
192
fn recv_byte ( & self ) -> Result < u8 , Error > {
195
193
while {
196
194
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) ?;
198
197
isr. rxne ( ) . bit_is_clear ( )
199
198
} { }
200
199
0 commit comments