puuu / USIWire

USI based TWI/I2C library for Arduino
GNU Lesser General Public License v3.0
60 stars 18 forks source link

test failure #2

Open trombik opened 7 years ago

trombik commented 7 years ago

hi,

I am trying the provided tests but failed to reproduce reliable test results.

the setup:

without modification to the example code, the test keeps failing. I guessed failures were caused by timing, and inserted some delay(). after trials and errors, sometimes, the test passes, but not always. do you see anything I am missing? AUX_Callback test fails in most cases, but now always. here is the diff, failed result, and successful one.

--- ../src/TestMaster/TestMaster.ino    2017-07-16 23:03:06.650753365 +0900
+++ src/TestMaster.ino  2017-07-17 00:29:23.520245148 +0900
@@ -611,7 +611,7 @@
     Wire.write(CONTROL_ADDR);
     Wire.write(AUX_STATE_ON << CONTROL_AUX_POS | slaveSleepMode);
     assertEqual(Wire.endTransmission(), 0);
-    delay(1);
+    delay(20);
     if (slaveSleepMode) delay(2);
     assertEqual(digitalRead(AUX_PIN), HIGH);
     // AUX pin low
@@ -619,7 +619,7 @@
     Wire.write(CONTROL_ADDR);
     Wire.write(AUX_STATE_OFF << CONTROL_AUX_POS | slaveSleepMode);
     assertEqual(Wire.endTransmission(), 0);
-    delay(1);
+    delay(20);
     if (slaveSleepMode) delay(2);
     assertEqual(digitalRead(AUX_PIN), LOW);
   }
@@ -639,8 +639,8 @@
       Wire.write(i);
     }
     assertEqual(Wire.endTransmission(), 3);
-    delay(1);
-    if (slaveSleepMode) delay(2);
+    delay(10);
+    if (slaveSleepMode) delay(20);
     assertEqual(digitalRead(AUX_PIN), HIGH);
     // AUX pin low
     Wire.beginTransmission(SLAVE_ADDR);
@@ -650,8 +650,8 @@
       Wire.write(i);
     }
     assertEqual(Wire.endTransmission(), 3);
-    delay(1);
-    if (slaveSleepMode) delay(2);
+    delay(10);
+    if (slaveSleepMode) delay(20);
     assertEqual(digitalRead(AUX_PIN), LOW);
   }
 }
@@ -672,8 +672,9 @@
     assertEqual(Wire.endTransmission(false), 0); // send no Stop Condition
     assertEqual(digitalRead(AUX_PIN), LOW); // still low
     Wire.beginTransmission(SLAVE_ADDR);
+    delay(10);
     assertEqual(Wire.endTransmission(false), 0); // repeat start
-    delay(1);
+    delay(10);
     if (slaveSleepMode) delay(2);
     assertEqual(digitalRead(AUX_PIN), HIGH);
     // request AUX pin low
@@ -684,7 +685,7 @@
     assertEqual(digitalRead(AUX_PIN), HIGH); // still high
     Wire.beginTransmission(SLAVE_ADDR);
     assertEqual(Wire.endTransmission(false), 0); // repeat start
-    delay(1);
+    delay(100);
     if (slaveSleepMode) delay(2);
     assertEqual(digitalRead(AUX_PIN), LOW);
   }
@@ -717,8 +718,8 @@
     assertEqual(digitalRead(AUX_PIN), LOW); // still low
     Wire.beginTransmission(SLAVE_ADDR);
     assertEqual(Wire.endTransmission(false), 0); // repeat start
-    delay(1);
-    if (slaveSleepMode) delay(2);
+    delay(10);
+    if (slaveSleepMode) delay(20);
     assertEqual(digitalRead(AUX_PIN), HIGH);
     // request AUX pin low
     Wire.beginTransmission(SLAVE_ADDR);
@@ -730,6 +731,7 @@
     assertEqual(Wire.endTransmission(false), 3); // send no Stop Condition
     assertEqual(digitalRead(AUX_PIN), HIGH); // still high
     Wire.beginTransmission(SLAVE_ADDR);
+    delay(10);
     assertEqual(Wire.endTransmission(false), 0); // repeat start
     delay(1);
     if (slaveSleepMode) delay(2);
@@ -752,12 +754,14 @@
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   // simple read
   assertEqual(Wire.requestFrom(SLAVE_ADDR, BYTE), BYTE);
+  delay(1000);
   assertEqual(digitalRead(AUX_PIN), HIGH); // high after read event
   // simple write
   Wire.beginTransmission(SLAVE_ADDR);
   Wire.write(BYTE_ADDR0);
   Wire.write(DATA1);
   assertEqual(Wire.endTransmission(), 0);
+  delay(100);
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   // read zero
   Wire.beginTransmission(SLAVE_ADDR);
@@ -769,6 +773,7 @@
   Wire.beginTransmission(SLAVE_ADDR);
   Wire.write(ZERO_ADDR);
   assertEqual(Wire.endTransmission(), 0);
+  delay(100);
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   //repeated start, read
   Wire.beginTransmission(SLAVE_ADDR);
@@ -810,6 +815,7 @@
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   // simple read
   assertEqual(Wire.requestFrom(SLAVE_ADDR, BYTE+1), BYTE+1);
+  delay(500);
   assertEqual(digitalRead(AUX_PIN), HIGH); // high after read event
   // simple write
   Wire.beginTransmission(SLAVE_ADDR);
@@ -819,7 +825,7 @@
     Wire.write(i);
   }
   assertEqual(Wire.endTransmission(), 3);
-  delayMicroseconds(100);
+  delayMicroseconds(1000);
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   // read zero
   Wire.beginTransmission(SLAVE_ADDR);
@@ -834,7 +840,7 @@
     Wire.write(i);
   }
   assertEqual(Wire.endTransmission(), 3);
-  delayMicroseconds(100);
+  delayMicroseconds(1000);
   assertEqual(digitalRead(AUX_PIN), LOW); // low after write event
   //repeated start, read
   Wire.beginTransmission(SLAVE_ADDR);
--- Miniterm on /dev/ttyUSB0  9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Sleep mode off!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off: .................... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ...FAILED!
0x0 != 0x1
Error state!
Sleep mode off!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off: .................... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ...FAILED!
0x0 != 0x1
Error state!
Sleep mode off!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off: .................... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ...FAILED!
0x0 != 0x1
Error state!
Sleep mode off!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off: .................... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ................... SUCCESS!
Fill_Slave_RX_Buffer: ...................... SUCCESS!
AUX_On_Off_Repeated_Start_NACK: ........................................... SUCCESS!
AUX_Callback_NACK: .................. SUCCESS!
AUX_On_Off_NACK: .................... SUCCESS!
All test finished!

Idle sleep mode!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off: .................... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ................... SUCCESS!
Fill_Slave_RX_Buffer: ...................... SUCCESS!
AUX_On_Off_Repeated_Start_NACK: ........................................... SUCCESS!
AUX_Callback_NACK: .................. SUCCESS!
AUX_On_Off_NACK: .................... SUCCESS!
All test finished!

Power down sleep mode!
Initialize all tests: . SUCCESS!
SMBus_Quick_Read_Command: . SUCCESS!
SMBus_Quick_Write_Command: . SUCCESS!
SMBus_Receive_Byte: .... SUCCESS!
SMBus_Send_Byte: . SUCCESS!
SMBus_Read_Byte: ... SUCCESS!
SMBus_Read_Word: .... SUCCESS!
SMBus_Write_Byte: .... SUCCESS!
SMBus_Write_Word: ..... SUCCESS!
SMBus_Process_Call: ........ SUCCESS!
SMBus_Block_Read: ................. SUCCESS!
SMBus_Block_Write: ................. SUCCESS!
SMBus_Block_Process_Call: ........................ SUCCESS!
SMBus_Quick_Read_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Quick_Write_Command_Wrong_Addr: ..... SUCCESS!
SMBus_Read_Byte_Multiple_Times: .................. SUCCESS!
Read_Too_Much: .... SUCCESS!
Read_Too_Less: ...................... SUCCESS!
Write_Too_Much: ............ SUCCESS!
Write_Too_Less: .......... SUCCESS!
AUX_On_Off_Repeated_Start: ........................................... SUCCESS!
AUX_Callback: ................... SUCCESS!
Fill_Slave_RX_Buffer: ...................... SUCCESS!
AUX_On_Off_Repeated_Start_NACK: ........................................... SUCCESS!
AUX_Callback_NACK: .................. SUCCESS!
All test finished!
puuu commented 7 years ago

Hi, thank you for testing USIWire.

Unfortunately, I can confirm that the test not always run successfully. Tuning of the delay() may help. The current delay() values were tested with a ATTiny85 at 8 MHz. With these I can run at least 8 test in a row without any failure.

What clock you are using?

trombik commented 7 years ago

I think it is 8 Mhz. but will double-check.

it is very helpful for a library to have tests (most of them do not), but it would be nicer if the test configuration was documented, such as the chips used as master and slave in the test, wiring, breadboard or not, etc.