@@ -12,7 +12,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, RccBus, Reset};
1212 feature = "stm32g484"
1313) ) ]
1414use crate :: stm32:: I2C4 ;
15- use crate :: stm32:: { I2C1 , I2C2 , I2C3 , RCC } ;
15+ use crate :: stm32:: { I2C1 , I2C2 , I2C3 } ;
1616use crate :: time:: Hertz ;
1717use core:: cmp;
1818use core:: convert:: TryInto ;
@@ -29,12 +29,9 @@ pub struct Config {
2929
3030impl Config {
3131 /// 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 {
3633 Config {
37- speed : Some ( speed. into ( ) ) ,
34+ speed : Some ( speed) ,
3835 timing : None ,
3936 analog_filter : true ,
4037 digital_filter : 0 ,
@@ -98,16 +95,17 @@ impl Config {
9895 ( psc, scll, sclh, sdadel, scldel)
9996 } ;
10097
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)
111109 }
112110}
113111
@@ -149,7 +147,13 @@ impl embedded_hal::i2c::Error for Error {
149147}
150148
151149pub 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 >
153157 where
154158 SDA : SDAPin < I2C > ,
155159 SCL : SCLPin < I2C > ;
@@ -216,7 +220,7 @@ macro_rules! i2c {
216220 self ,
217221 sda: SDA ,
218222 scl: SCL ,
219- config: Config ,
223+ config: impl Into < Config > ,
220224 rcc: & mut Rcc ,
221225 ) -> I2c <$I2CX, SDA , SCL >
222226 where
@@ -232,17 +236,15 @@ macro_rules! i2c {
232236 SCL : SCLPin <$I2CX>
233237 {
234238 /// 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
236240 where
237241 SDA : SDAPin <$I2CX>,
238242 SCL : SCLPin <$I2CX>,
239243 {
244+ let config = config. into( ) ;
240245 // 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) ;
246248
247249 // Make sure the I2C unit is disabled so we can configure it
248250 i2c. cr1( ) . modify( |_, w| w. pe( ) . clear_bit( ) ) ;
@@ -252,12 +254,9 @@ macro_rules! i2c {
252254
253255 // Enable the I2C processing
254256 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)
261260 } ) ;
262261
263262 I2c { i2c, sda, scl }
@@ -267,9 +266,8 @@ macro_rules! i2c {
267266 pub fn release( self ) -> ( $I2CX, SDA , SCL ) {
268267 // Disable I2C.
269268 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( ) ;
273271 }
274272
275273 ( self . i2c, self . sda, self . scl)
0 commit comments