Browse Source

TWI timeout

Alex Voinea 2 years ago
parent
commit
6351d29d70
4 changed files with 10 additions and 12 deletions
  1. 0 0
      Firmware/pat9125.cpp
  2. 0 11
      Firmware/pat9125.h
  3. 8 1
      Firmware/twi.c
  4. 2 0
      Firmware/twi.h

Firmware/pat9125.c → Firmware/pat9125.cpp


+ 0 - 11
Firmware/pat9125.h

@@ -4,12 +4,6 @@
 
 #include <inttypes.h>
 
-
-#if defined(__cplusplus)
-extern "C" {
-#endif //defined(__cplusplus)
-
-
 extern uint8_t pat9125_PID1;
 extern uint8_t pat9125_PID2;
 
@@ -24,9 +18,4 @@ extern uint8_t pat9125_update(void);    // update all sensor data
 extern uint8_t pat9125_update_y(void);  // update _y only
 extern uint8_t pat9125_update_bs(void); // update _b/_s only
 
-
-#if defined(__cplusplus)
-}
-#endif //defined(__cplusplus)
-
 #endif //PAT9125_H

+ 8 - 1
Firmware/twi.c

@@ -24,6 +24,7 @@
 #include "config.h"
 #include "fastio.h"
 #include "twi.h"
+#include "Timer.h"
 
 
 void twi_init(void)
@@ -58,7 +59,13 @@ static void twi_stop()
 
 static uint8_t twi_wait(uint8_t status)
 {
-  while(!(TWCR & _BV(TWINT)));
+  static ShortTimer timmy;
+  timmy.start();
+  while(!(TWCR & _BV(TWINT))) {
+    if (timmy.expired(TWI_TIMEOUT_MS)) {
+      return 2;
+    }
+  }
   if(TW_STATUS != status)
   {
       twi_stop();

+ 2 - 0
Firmware/twi.h

@@ -26,6 +26,8 @@
 #define TWI_FREQ 400000L
 #endif
 
+#define TWI_TIMEOUT_MS 100
+
 /*
  * Function twi_init
  * Desc     readys twi pins and sets twi bitrate