@@ -4,7 +4,7 @@ use crate::gpio::{AltFunction, OpenDrain, Output};
44use crate :: i2c:: config:: Config ;
55use crate :: i2c:: { Error , I2c , I2cDirection , I2cExt , I2cResult , SCLPin , SDAPin } ;
66use crate :: rcc:: * ;
7- use crate :: stm32:: { I2C1 , I2C2 } ;
7+ use crate :: stm32:: I2C ;
88use nb:: Error :: { Other , WouldBlock } ;
99
1010pub trait I2cControl {
@@ -101,13 +101,13 @@ macro_rules! flush_rxdr {
101101
102102macro_rules! i2c {
103103 ( $I2CX: ident, $i2cx: ident,
104- sda: [ $( $PSDA: ty, ) + ] ,
105- scl: [ $( $PSCL: ty, ) + ] ,
104+ sda: [ $( ( $PSDA: ty, $AFSDA : expr ) , ) + ] ,
105+ scl: [ $( ( $PSCL: ty, $AFSCL : expr ) , ) + ] ,
106106 ) => {
107107 $(
108108 impl SDAPin <$I2CX> for $PSDA {
109109 fn setup( & self ) {
110- self . set_alt_mode( AltFunction :: AF6 )
110+ self . set_alt_mode( $AFSDA )
111111 }
112112
113113 fn release( self ) -> Self {
@@ -119,7 +119,7 @@ macro_rules! i2c {
119119 $(
120120 impl SCLPin <$I2CX> for $PSCL {
121121 fn setup( & self ) {
122- self . set_alt_mode( AltFunction :: AF6 )
122+ self . set_alt_mode( $AFSCL )
123123 }
124124
125125 fn release( self ) -> Self {
@@ -175,22 +175,11 @@ macro_rules! i2c {
175175 } ) ;
176176
177177 if config. slave_address_1 > 0 {
178- if config. address_11bits {
179- i2c. oar1. write( |w| unsafe {
180- let addr = config. slave_address_1;
181- w. oa1_0( ) . bit( addr& 0x1 == 0x1 )
182- . oa1_7_1( ) . bits( ( ( addr >> 1 ) & 0x7F ) as u8 )
183- . oa1_8_9( ) . bits( ( ( addr >> 8 ) & 0x3 ) as u8 )
184- . oa1mode( ) . set_bit( )
185- . oa1en( ) . set_bit( )
186- } ) ;
187- } else {
188- i2c. oar1. write( |w| unsafe {
189- w. oa1_7_1( ) . bits( config. slave_address_1 as u8 )
190- . oa1mode( ) . clear_bit( )
191- . oa1en( ) . set_bit( )
192- } ) ;
193- }
178+ i2c. oar1. write( |w| unsafe {
179+ w. oa1( ) . bits( config. slave_address_1)
180+ . oa1mode( ) . bit( config. address_11bits)
181+ . oa1en( ) . set_bit( )
182+ } ) ;
194183 // Enable acknowlidge control
195184 i2c. cr1. modify( |_, w| w. sbc( ) . set_bit( ) ) ;
196185 }
@@ -202,7 +191,7 @@ macro_rules! i2c {
202191 . oa2en( ) . set_bit( )
203192 } ) ;
204193 // Enable acknowlidge control
205- i2c. cr1. modify( |_, w| w. sbc( ) . set_bit( ) ) ;
194+ i2c. cr1. modify( |_, w| w. sbc( ) . set_bit( ) ) ;
206195 }
207196
208197 // Enable pins
@@ -268,11 +257,11 @@ macro_rules! i2c {
268257 self . errors += 1 ;
269258 self . watchdog = 0 ;
270259 // Disable I2C processing, resetting all hardware state machines
271- self . i2c. cr1. modify( |_, w| unsafe { w. pe( ) . clear_bit( ) } ) ;
260+ self . i2c. cr1. modify( |_, w| w. pe( ) . clear_bit( ) ) ;
272261 // force enough wait states for the pe clear
273262 let _ = self . i2c. cr1. read( ) ;
274263 // Enable the I2C processing again
275- self . i2c. cr1. modify( |_, w| unsafe { w. pe( ) . set_bit( ) } ) ;
264+ self . i2c. cr1. modify( |_, w| w. pe( ) . set_bit( ) ) ;
276265 } ,
277266 _ => { self . watchdog -= 1 } ,
278267 }
@@ -372,14 +361,14 @@ macro_rules! i2c {
372361 return Err ( WouldBlock )
373362 } else
374363 if self . index == 0 {
375- self . i2c. cr2. modify( |_, w| unsafe {
364+ self . i2c. cr2. modify( |_, w| {
376365 w. stop( ) . set_bit( )
377366 } ) ;
378367 self . errors += 1 ;
379368 return Err ( Other ( Error :: Nack ) )
380369 } else
381370 {
382- self . i2c. cr2. modify( |_, w| unsafe {
371+ self . i2c. cr2. modify( |_, w| {
383372 w. stop( ) . set_bit( )
384373 } ) ;
385374 self . errors += 1 ;
@@ -555,20 +544,19 @@ macro_rules! i2c {
555544
556545 fn set_address( & mut self , address: u16 ) {
557546 self . i2c. oar1. write( |w| unsafe {
558- w. oa1_7_1 ( ) . bits( address as u8 )
547+ w. oa1 ( ) . bits( address as _ )
559548 . oa1en( ) . clear_bit( )
560549 } ) ;
561550 // set the 7 bits address
562551 self . i2c. oar1. write( |w| unsafe {
563- w. oa1_7_1 ( ) . bits( address as u8 )
552+ w. oa1 ( ) . bits( address as _ )
564553 . oa1mode( ) . clear_bit( )
565554 . oa1en( ) . set_bit( )
566555 } ) ;
567556 }
568557
569558 fn slave_write( & mut self , bytes: & [ u8 ] ) -> Result <( ) , Error > {
570559 let buflen = bytes. len( ) ;
571- // TODO support transfers of more than 255 bytes
572560 assert!( buflen < 256 && buflen > 0 ) ;
573561
574562 self . length = buflen;
@@ -601,31 +589,18 @@ macro_rules! i2c {
601589}
602590
603591i2c ! (
604- I2C1 ,
592+ I2C ,
605593 i2c1,
606594 sda: [
607- PA10 <Output <OpenDrain >>,
608- PB7 <Output <OpenDrain >>,
609- PB9 <Output <OpenDrain >>,
610- ] ,
611- scl: [
612- PA9 <Output <OpenDrain >>,
613- PB6 <Output <OpenDrain >>,
614- PB8 <Output <OpenDrain >>,
615- ] ,
616- ) ;
617-
618- i2c ! (
619- I2C2 ,
620- i2c2,
621- sda: [
622- PA12 <Output <OpenDrain >>,
623- PB11 <Output <OpenDrain >>,
624- PB14 <Output <OpenDrain >>,
595+ ( PA10 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
596+ ( PB7 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
597+ ( PB9 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
598+ ( PC14 <Output <OpenDrain >>, AltFunction :: AF14 ) ,
625599 ] ,
626600 scl: [
627- PA11 <Output <OpenDrain >>,
628- PB10 <Output <OpenDrain >>,
629- PB13 <Output <OpenDrain >>,
601+ ( PA9 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
602+ ( PB6 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
603+ ( PB8 <Output <OpenDrain >>, AltFunction :: AF6 ) ,
604+ ( PB7 <Output <OpenDrain >>, AltFunction :: AF14 ) ,
630605 ] ,
631606) ;
0 commit comments