Browse Source

Use _check instead of register read for probe()

Alex Voinea 2 years ago
parent
commit
28c527fece
4 changed files with 31 additions and 10 deletions
  1. 2 8
      Firmware/pat9125.cpp
  2. 2 2
      Firmware/swi2c.c
  3. 19 0
      Firmware/twi.cpp
  4. 8 0
      Firmware/twi.h

+ 2 - 8
Firmware/pat9125.cpp

@@ -112,16 +112,10 @@ uint8_t pat9125_probe()
   #error not implemented
 #elif defined(PAT9125_SWI2C)
     swi2c_init();
-    return swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL);
+    return swi2c_check(PAT9125_I2C_ADDR) == 0;
 #elif defined(PAT9125_I2C)
     twi_init();
-  #ifdef IR_SENSOR
-    // NOTE: this is called from the MK3S variant, so it should be kept minimal
-    uint8_t data;
-    return (twi_r8(PAT9125_I2C_ADDR,PAT9125_PID1,&data) == 0);
-  #else
-    return (pat9125_rd_reg(PAT9125_PID1) != 0);
-  #endif
+    return twi_check(PAT9125_I2C_ADDR) == 0;
 #endif
 }
 

+ 2 - 2
Firmware/swi2c.c

@@ -142,9 +142,9 @@ uint8_t swi2c_check(uint8_t dev_addr)
 {
 	swi2c_start();
 	swi2c_write((dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
-	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 1; }
 	swi2c_stop();
-	return 1;
+	return 0;
 }
 
 #ifdef SWI2C_A8 //8bit address

+ 19 - 0
Firmware/twi.cpp

@@ -101,6 +101,25 @@ static uint8_t twi_start(uint8_t address, uint8_t reg)
 }
 
 
+uint8_t twi_check(uint8_t address)
+{
+    // send start condition
+    TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
+    if(twi_wait(TW_START))
+        return 1;
+    
+      // send address
+    TWDR = TW_WRITE | (address << 1);
+    TWCR = _BV(TWEN) | _BV(TWINT);
+    if(twi_wait(TW_MT_SLA_ACK))
+        return 2;
+    
+    // send stop
+    twi_stop();
+    return 0;
+}
+
+
 uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data)
 {
   if(twi_start(address, reg))

+ 8 - 0
Firmware/twi.h

@@ -44,6 +44,14 @@ void twi_init(void);
  */
 void twi_disable(void);
 
+/*
+ * Function twi_check
+ * Desc     checks if a device exists on the bus
+ * Input    address: 7bit i2c device address
+ * Output   0 on device found at address
+ */
+uint8_t twi_check(uint8_t address);
+
 /*
  * Function twi_r8
  * Desc     read a single byte from a device