@@ -86,6 +86,18 @@ static uint8_t stop(void);
8686static void lockUp (void );
8787
8888
89+ #define TIMEOUT_WAIT_WHILE (CONDITION ,ERROR ) \
90+ while (CONDITION) /* wait for the required condition */ \
91+ { \
92+ if (!timeOutDelay ){continue ;} \
93+ if ((millis () - startingTime ) >= timeOutDelay ) \
94+ { \
95+ lockUp (); \
96+ return (ERROR ); /* return the appropriate error code */ \
97+ } \
98+ }
99+
100+
89101////////////// Public Methods ////////////////////////////////////////
90102
91103
@@ -129,12 +141,10 @@ void I2C_end()
129141}
130142#endif
131143
132- #if 1
133144void I2C_timeOut (uint16_t _timeOut )
134145{
135146 timeOutDelay = _timeOut ;
136147}
137- #endif
138148
139149#if 0
140150void I2C_setSpeed (uint8_t _fast )
@@ -184,7 +194,6 @@ void I2C_pullup(uint8_t activate)
184194}
185195#endif
186196
187- #if 1
188197void I2C_scan ()
189198{
190199 uint16_t tempTime = timeOutDelay ;
@@ -227,7 +236,6 @@ void I2C_scan()
227236 if (!totalDevicesFound ){Serial_println_s ("No devices found" );}
228237 timeOutDelay = tempTime ;
229238}
230- #endif
231239
232240uint8_t I2C_available ()
233241{
@@ -586,6 +594,12 @@ uint8_t I2C_read_sn(uint8_t address, uint8_t registerAddress, uint8_t numberByte
586594
587595/////////////// Private Methods ////////////////////////////////////////
588596
597+ /**
598+ * send a start sequence
599+ *
600+ * in contrast to the AVR version it does not wait for start to finish for
601+ * the STM8. The timeout is handled in sendAddress().
602+ */
589603static uint8_t start (void )
590604{
591605#if 0
@@ -613,7 +627,7 @@ static uint8_t start(void)
613627 }
614628 return (TWI_STATUS );
615629#else
616- I2C -> CR2 |= I2C_CR2_START ; // I2C_GenerateSTART(enable);
630+ I2C -> CR2 |= I2C_CR2_START ; // send start sequence
617631 return 0 ;
618632#endif
619633}
@@ -708,15 +722,8 @@ uint8_t sendByte(uint8_t i2cData)
708722 unsigned long startingTime = millis (); // FIXME: uint16_t could be used
709723
710724 /* Test on EV8 */
711- while (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTING ))
712- {
713- if (!timeOutDelay ){continue ;}
714- if ((millis () - startingTime ) >= timeOutDelay )
715- {
716- lockUp ();
717- return (3 ); // no ACK received on data transmission
718- }
719- }
725+ TIMEOUT_WAIT_WHILE (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTING ), 3 );
726+ // error 3: no ACK received on data transmission
720727
721728 I2C -> DR = i2cData ;
722729 return 0 ;
@@ -730,7 +737,6 @@ static uint8_t receiveByte(uint8_t ack)
730737 if (ack )
731738 {
732739 TWCR = (1 <<TWINT ) | (1 <<TWEN ) | (1 <<TWEA );
733-
734740 }
735741 else
736742 {
@@ -776,19 +782,15 @@ static uint8_t stop(void)
776782 }
777783#else
778784 unsigned long startingTime = millis (); // FIXME: uint16_t could be used
785+
779786 /* Test on EV8_2 */
780- while (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED ))
781- {
782- if (!timeOutDelay ){continue ;}
783- if ((millis () - startingTime ) >= timeOutDelay )
784- {
785- lockUp ();
786- return (3 ); // no ACK received on data transmission
787- }
788- }
787+ TIMEOUT_WAIT_WHILE (!I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED ), 3 );
788+ // error 3: no ACK received on data transmission
789789
790790 /* Generate a STOP condition */
791791 I2C -> CR2 |= I2C_CR2_STOP ;
792+
793+ TIMEOUT_WAIT_WHILE (!(I2C -> SR1 & I2C_SR1_STOPF ), 7 );
792794#endif
793795 return (0 );
794796}
0 commit comments