UAVX Multicopter Flight Controller.

Dependencies:   mbed

Revision:
1:1e3318a30ddd
Parent:
0:62a1c91a859a
Child:
2:90292f8bd179
--- a/baro.c	Fri Feb 18 22:28:05 2011 +0000
+++ b/baro.c	Fri Feb 25 01:35:24 2011 +0000
@@ -124,7 +124,7 @@
     } else
         TxString("no rangefinder\r\n");
 
-    TxString("\r\nAmbient :\t");
+    TxString("\r\nUAVXArm Shield:\t");
     TxVal32((int32)AmbientTemperature.i16, 1, ' ');
     TxString("C\r\n");
 
@@ -193,7 +193,7 @@
 
     Comp[Alt] = AltDiffSum = AltDSum = 0;
     F.BaroAltitudeValid= true; // optimistic
-
+ 
     if ( IsFreescaleBaroActive() )
         InitFreescaleBarometer();
     else
@@ -210,6 +210,7 @@
 // Freescale ex Motorola MPX4115 Barometer with ADS7823 12bit ADC
 
 void SetFreescaleMCP4725(int16);
+boolean IdentifyMCP4725(void);
 void SetFreescaleOffset(void);
 void ReadFreescaleBaro(void);
 real32 FreescaleToDM(int24);
@@ -217,6 +218,8 @@
 boolean IsFreescaleBaroActive(void);
 void InitFreescaleBarometer(void);
 
+uint8 MCP4725_ID_Actual;
+
 void SetFreescaleMCP4725(int16 d) {
     static i16u dd;
     static uint8 r;
@@ -224,7 +227,7 @@
     dd.u16 = d << 4;                            // left align
 
     I2CBARO.start();
-    r = I2CBARO.write(MCP4725_WR) != I2C_ACK;
+    r = I2CBARO.write(MCP4725_ID_Actual) != I2C_ACK;
     r = I2CBARO.write(MCP4725_CMD) != I2C_ACK;
     r = I2CBARO.write(dd.b1) != I2C_ACK;
     r = I2CBARO.write(dd.b0) != I2C_ACK;
@@ -232,6 +235,21 @@
 
 } // SetFreescaleMCP4725
 
+boolean IdentifyMCP4725(void) {
+
+    static boolean r;
+    
+    r = true;
+    if ( I2CBAROAddressResponds( MCP4725_ID_0xC8 ) )
+        MCP4725_ID_Actual = MCP4725_ID_0xC8;
+    else 
+        if ( I2CBAROAddressResponds( MCP4725_ID_0xCC ) )
+            MCP4725_ID_Actual = MCP4725_ID_0xCC;
+        else
+            r = false;
+   return(r);    
+} // IdentifyMCP4725
+
 void SetFreescaleOffset(void) {
     // Steve Westerfeld
     // 470 Ohm, 1uF RC 0.47mS use 2mS for settling?
@@ -289,7 +307,7 @@
     if ( I2CBARO.write(ADS7823_WR) != I2C_ACK ) goto FSError;
     if ( I2CBARO.write(ADS7823_CMD) != I2C_ACK ) goto FSError;
 
-    I2CBARO.read(ADS7823_RD, B, 8);  // read block of 4 baro samples
+    I2CBARO.blockread(ADS7823_RD, B, 8);  // read block of 4 baro samples
 
     B0.b0 = B[1];
     B0.b1 = B[0];
@@ -328,9 +346,7 @@
         ReadFreescaleBaro();
         if ( F.BaroAltitudeValid ) {
             BaroPressure = (int24)BaroVal.u16; // sum of 4 samples
-
             BaroRelAltitude = FreescaleToDM(BaroPressure - OriginBaroPressure);
-
             F.NewBaroValue = F.BaroAltitudeValid;
         }
     }
@@ -339,19 +355,15 @@
 
 boolean IsFreescaleBaroActive(void) { // check for Freescale Barometer
 
-    I2CBARO.start();
-    if ( I2CBARO.write(ADS7823_ID) != I2C_ACK ) goto FreescaleInactive;
+    static boolean r;
 
-    BaroType = BaroMPX4115;
-    I2CBARO.stop();
-    
-    TrackMinI2CRate(400000);
-
-    return(true);
-
-FreescaleInactive:
-    I2CBARO.stop();
-    return(false);
+    r = I2CBAROAddressResponds( ADS7823_ID );
+    if ( r ) {
+        BaroType = BaroMPX4115;
+        r = IdentifyMCP4725();
+        TrackMinI2CRate(400000);
+    }
+    return (r);
 
 } // IsFreescaleBaroActive
 
@@ -453,18 +465,18 @@
 void ReadBoschBaro(void) {
     // Possible I2C protocol error - split read of ADC
     I2CBARO.start();
-    if ( I2CBARO.write(BOSCH_ID) != I2C_ACK ) goto RVerror;
+    if ( I2CBARO.write(BOSCH_WR) != I2C_ACK ) goto RVerror;
     if ( I2CBARO.write(BOSCH_ADC_MSB) != I2C_ACK ) goto RVerror;
     I2CBARO.start();    // restart
-    if ( I2CBARO.write(BOSCH_ID+1) != I2C_ACK ) goto RVerror;
+    if ( I2CBARO.write(BOSCH_RD) != I2C_ACK ) goto RVerror;
     BaroVal.b1 = I2CBARO.read(I2C_NACK);
     I2CBARO.stop();
 
     I2CBARO.start();
-    if ( I2CBARO.write(BOSCH_ID) != I2C_ACK ) goto RVerror;
+    if ( I2CBARO.write(BOSCH_WR) != I2C_ACK ) goto RVerror;
     if ( I2CBARO.write(BOSCH_ADC_LSB) != I2C_ACK ) goto RVerror;
     I2CBARO.start();    // restart
-    if ( I2CBARO.write(BOSCH_ID+1) != I2C_ACK ) goto RVerror;
+    if ( I2CBARO.write(BOSCH_RD) != I2C_ACK ) goto RVerror;
     BaroVal.b0 = I2CBARO.read(I2C_NACK);
     I2CBARO.stop();
 
@@ -536,10 +548,10 @@
     static uint8 r;
 
     I2CBARO.start();
-    if ( I2CBARO.write(BOSCH_ID) != I2C_ACK ) goto BoschInactive;
+    if ( I2CBARO.write(BOSCH_WR) != I2C_ACK ) goto BoschInactive;
     if ( I2CBARO.write(BOSCH_TYPE) != I2C_ACK ) goto BoschInactive;
     I2CBARO.start();    // restart
-    if ( I2CBARO.write(BOSCH_ID+1) != I2C_ACK ) goto BoschInactive;
+    if ( I2CBARO.write(BOSCH_RD) != I2C_ACK ) goto BoschInactive;
     r = I2CBARO.read(I2C_NACK);
     I2CBARO.stop();
 
@@ -547,7 +559,7 @@
         BaroType = BaroBMP085;
     else
         BaroType = BaroSMD500;
-        
+
     TrackMinI2CRate(400000);
 
     return(true);