@@ -12,7 +12,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, RccBus, Reset};
12
12
feature = "stm32g484"
13
13
) ) ]
14
14
use crate :: stm32:: I2C4 ;
15
- use crate :: stm32:: { I2C1 , I2C2 , I2C3 , RCC } ;
15
+ use crate :: stm32:: { I2C1 , I2C2 , I2C3 } ;
16
16
use crate :: time:: Hertz ;
17
17
use core:: cmp;
18
18
use core:: convert:: TryInto ;
@@ -29,12 +29,9 @@ pub struct Config {
29
29
30
30
impl Config {
31
31
/// Creates a default configuration for the given bus frequency.
32
- pub fn new < T > ( speed : T ) -> Self
33
- where
34
- T : Into < Hertz > ,
35
- {
32
+ pub fn new ( speed : Hertz ) -> Self {
36
33
Config {
37
- speed : Some ( speed. into ( ) ) ,
34
+ speed : Some ( speed) ,
38
35
timing : None ,
39
36
analog_filter : true ,
40
37
digital_filter : 0 ,
@@ -98,16 +95,17 @@ impl Config {
98
95
( psc, scll, sclh, sdadel, scldel)
99
96
} ;
100
97
101
- reg. presc ( )
102
- . set ( psc. try_into ( ) . unwrap ( ) )
103
- . scldel ( )
104
- . set ( scldel)
105
- . sdadel ( )
106
- . set ( sdadel)
107
- . sclh ( )
108
- . set ( sclh. try_into ( ) . unwrap ( ) )
109
- . scll ( )
110
- . set ( scll. try_into ( ) . unwrap ( ) )
98
+ reg. presc ( ) . set ( psc. try_into ( ) . unwrap ( ) ) ;
99
+ reg. scldel ( ) . set ( scldel) ;
100
+ reg. sdadel ( ) . set ( sdadel) ;
101
+ reg. sclh ( ) . set ( sclh. try_into ( ) . unwrap ( ) ) ;
102
+ reg. scll ( ) . set ( scll. try_into ( ) . unwrap ( ) )
103
+ }
104
+ }
105
+
106
+ impl From < Hertz > for Config {
107
+ fn from ( value : Hertz ) -> Self {
108
+ Self :: new ( value)
111
109
}
112
110
}
113
111
@@ -149,7 +147,13 @@ impl embedded_hal::i2c::Error for Error {
149
147
}
150
148
151
149
pub trait I2cExt < I2C > {
152
- fn i2c < SDA , SCL > ( self , sda : SDA , scl : SCL , config : Config , rcc : & mut Rcc ) -> I2c < I2C , SDA , SCL >
150
+ fn i2c < SDA , SCL > (
151
+ self ,
152
+ sda : SDA ,
153
+ scl : SCL ,
154
+ config : impl Into < Config > ,
155
+ rcc : & mut Rcc ,
156
+ ) -> I2c < I2C , SDA , SCL >
153
157
where
154
158
SDA : SDAPin < I2C > ,
155
159
SCL : SCLPin < I2C > ;
@@ -216,7 +220,7 @@ macro_rules! i2c {
216
220
self ,
217
221
sda: SDA ,
218
222
scl: SCL ,
219
- config: Config ,
223
+ config: impl Into < Config > ,
220
224
rcc: & mut Rcc ,
221
225
) -> I2c <$I2CX, SDA , SCL >
222
226
where
@@ -232,17 +236,15 @@ macro_rules! i2c {
232
236
SCL : SCLPin <$I2CX>
233
237
{
234
238
/// Initializes the I2C peripheral.
235
- pub fn $i2cx( i2c: $I2CX, sda: SDA , scl: SCL , config: Config , rcc: & mut Rcc ) -> Self
239
+ pub fn $i2cx( i2c: $I2CX, sda: SDA , scl: SCL , config: impl Into < Config > , rcc: & mut Rcc ) -> Self
236
240
where
237
241
SDA : SDAPin <$I2CX>,
238
242
SCL : SCLPin <$I2CX>,
239
243
{
244
+ let config = config. into( ) ;
240
245
// Enable and reset I2C
241
- unsafe {
242
- let rcc_ptr = & ( * RCC :: ptr( ) ) ;
243
- $I2CX:: enable( rcc_ptr) ;
244
- $I2CX:: reset( rcc_ptr) ;
245
- }
246
+ $I2CX:: enable( rcc) ;
247
+ $I2CX:: reset( rcc) ;
246
248
247
249
// Make sure the I2C unit is disabled so we can configure it
248
250
i2c. cr1( ) . modify( |_, w| w. pe( ) . clear_bit( ) ) ;
@@ -252,12 +254,9 @@ macro_rules! i2c {
252
254
253
255
// Enable the I2C processing
254
256
i2c. cr1( ) . modify( |_, w| {
255
- w. pe( )
256
- . set_bit( )
257
- . dnf( )
258
- . set( config. digital_filter)
259
- . anfoff( )
260
- . bit( !config. analog_filter)
257
+ w. pe( ) . set_bit( ) ;
258
+ w. dnf( ) . set( config. digital_filter) ;
259
+ w. anfoff( ) . bit( !config. analog_filter)
261
260
} ) ;
262
261
263
262
I2c { i2c, sda, scl }
@@ -267,9 +266,8 @@ macro_rules! i2c {
267
266
pub fn release( self ) -> ( $I2CX, SDA , SCL ) {
268
267
// Disable I2C.
269
268
unsafe {
270
- let rcc_ptr = & ( * RCC :: ptr( ) ) ;
271
- $I2CX:: reset( rcc_ptr) ;
272
- $I2CX:: disable( rcc_ptr) ;
269
+ $I2CX:: reset_unchecked( ) ;
270
+ $I2CX:: disable_unchecked( ) ;
273
271
}
274
272
275
273
( self . i2c, self . sda, self . scl)
0 commit comments