@@ -66,6 +66,7 @@ impl Mode {
6666 }
6767}
6868
69+ /// Helper trait to ensure that the correct I2C pins are used for the corresponding interface
6970pub trait Pins < I2C > {
7071 const REMAP : bool ;
7172}
@@ -90,6 +91,7 @@ pub struct I2c<I2C, PINS> {
9091 pclk1 : u32 ,
9192}
9293
94+ /// embedded-hal compatible blocking I2C implementation
9395pub struct BlockingI2c < I2C , PINS > {
9496 nb : I2c < I2C , PINS > ,
9597 start_timeout : u32 ,
@@ -99,6 +101,7 @@ pub struct BlockingI2c<I2C, PINS> {
99101}
100102
101103impl < PINS > I2c < I2C1 , PINS > {
104+ /// Creates a generic I2C1 object on pins PB6 and PB7 or PB8 and PB9 (if remapped)
102105 pub fn i2c1 (
103106 i2c : I2C1 ,
104107 pins : PINS ,
@@ -116,6 +119,7 @@ impl<PINS> I2c<I2C1, PINS> {
116119}
117120
118121impl < PINS > BlockingI2c < I2C1 , PINS > {
122+ /// Creates a blocking I2C1 object on pins PB6 and PB7 or PB8 and PB9 using the embedded-hal `BlockingI2c` trait.
119123 pub fn i2c1 (
120124 i2c : I2C1 ,
121125 pins : PINS ,
@@ -147,6 +151,7 @@ impl<PINS> BlockingI2c<I2C1, PINS> {
147151}
148152
149153impl < PINS > I2c < I2C2 , PINS > {
154+ /// Creates a generic I2C2 object on pins PB10 and PB11 using the embedded-hal `BlockingI2c` trait.
150155 pub fn i2c2 (
151156 i2c : I2C2 ,
152157 pins : PINS ,
@@ -162,6 +167,7 @@ impl<PINS> I2c<I2C2, PINS> {
162167}
163168
164169impl < PINS > BlockingI2c < I2C2 , PINS > {
170+ /// Creates a blocking I2C2 object on pins PB10 and PB1
165171 pub fn i2c2 (
166172 i2c : I2C2 ,
167173 pins : PINS ,
@@ -190,7 +196,8 @@ impl<PINS> BlockingI2c<I2C2, PINS> {
190196 }
191197}
192198
193- pub fn blocking_i2c < I2C , PINS > (
199+ /// Generates a blocking I2C instance from a universal I2C object
200+ fn blocking_i2c < I2C , PINS > (
194201 i2c : I2c < I2C , PINS > ,
195202 clocks : Clocks ,
196203 start_timeout_us : u32 ,
@@ -253,12 +260,13 @@ macro_rules! busy_wait_cycles {
253260 } } ;
254261}
255262
263+ // Generate the same code for both I2Cs
256264macro_rules! hal {
257265 ( $( $I2CX: ident: ( $i2cX: ident) , ) +) => {
258266 $(
259267 impl <PINS > I2c <$I2CX, PINS > {
260268 /// Configures the I2C peripheral to work in master mode
261- pub fn $i2cX(
269+ fn $i2cX(
262270 i2c: $I2CX,
263271 pins: PINS ,
264272 mode: Mode ,
@@ -277,6 +285,8 @@ macro_rules! hal {
277285 i2c
278286 }
279287
288+ /// Initializes I2C. Configures the `I2C_TRISE`, `I2C_CRX`, and `I2C_CCR` registers
289+ /// according to the system frequency and I2C mode.
280290 fn init( & mut self ) {
281291 let freq = self . mode. get_frequency( ) ;
282292 let pclk1_mhz = ( self . pclk1 / 1000000 ) as u16 ;
@@ -316,20 +326,28 @@ macro_rules! hal {
316326 self . i2c. cr1. modify( |_, w| w. pe( ) . set_bit( ) ) ;
317327 }
318328
329+ /// Perform an I2C software reset
319330 fn reset( & mut self ) {
320331 self . i2c. cr1. write( |w| w. pe( ) . set_bit( ) . swrst( ) . set_bit( ) ) ;
321332 self . i2c. cr1. reset( ) ;
322333 self . init( ) ;
323334 }
324335
336+ /// Generate START condition
325337 fn send_start( & mut self ) {
326338 self . i2c. cr1. modify( |_, w| w. start( ) . set_bit( ) ) ;
327339 }
328340
341+ /// Check if START condition is generated. If the condition is not generated, this
342+ /// method returns `WouldBlock` so the program can act accordingly
343+ /// (busy wait, async, ...)
329344 fn wait_after_sent_start( & mut self ) -> NbResult <( ) , Error > {
330345 wait_for_flag!( self . i2c, sb)
331346 }
332347
348+ /// Check if STOP condition is generated. If the condition is not generated, this
349+ /// method returns `WouldBlock` so the program can act accordingly
350+ /// (busy wait, async, ...)
333351 fn wait_for_stop( & mut self ) -> NbResult <( ) , Error > {
334352 if self . i2c. cr1. read( ) . stop( ) . is_no_stop( ) {
335353 Ok ( ( ) )
@@ -338,10 +356,13 @@ macro_rules! hal {
338356 }
339357 }
340358
359+ /// Sends the (7-Bit) address on the I2C bus. The 8th bit on the bus is set
360+ /// depending on wether it is a read or write transfer.
341361 fn send_addr( & self , addr: u8 , read: bool ) {
342362 self . i2c. dr. write( |w| { w. dr( ) . bits( addr << 1 | ( if read { 1 } else { 0 } ) ) } ) ;
343363 }
344364
365+ /// Generate STOP condition
345366 fn send_stop( & self ) {
346367 self . i2c. cr1. modify( |_, w| w. stop( ) . set_bit( ) ) ;
347368 }
@@ -353,7 +374,7 @@ macro_rules! hal {
353374 }
354375
355376 impl <PINS > BlockingI2c <$I2CX, PINS > {
356- pub fn $i2cX(
377+ fn $i2cX(
357378 i2c: $I2CX,
358379 pins: PINS ,
359380 mode: Mode ,
@@ -371,7 +392,7 @@ macro_rules! hal {
371392
372393 fn send_start_and_wait( & mut self ) -> NbResult <( ) , Error > {
373394 // According to http://www.st.com/content/ccc/resource/technical/document/errata_sheet/f5/50/c9/46/56/db/4a/f6/CD00197763.pdf/files/CD00197763.pdf/jcr:content/translations/en.CD00197763.pdf
374- // 2.14.4 Wrong behavior of I2C peripheral in master mode after a misplaced Stop
395+ // 2.14.4 Wrong behavior of I2C peripheral in master mode after a misplaced STOP
375396 let mut retries_left = self . start_retries;
376397 let mut last_ret: NbResult <( ) , Error > = Err ( WouldBlock ) ;
377398 while retries_left > 0 {
0 commit comments