Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

Revision:
7:e269411568c9
Parent:
0:7a00359e701e
Child:
9:6fa3e7b17c27
--- a/akmakd.cpp	Fri May 13 23:52:37 2016 +0000
+++ b/akmakd.cpp	Thu May 26 21:56:45 2016 +0000
@@ -29,18 +29,17 @@
     subId = subid;
     
     AkmECompass::SensorType type;
-
-    drdy = new InterruptIn(DRDY);    
-    drdy->rise(0);
+    AK099XX* ak099xx;
+    AK8963* ak8963;
 
     if(primaryId == AKM_PRIMARY_ID_AKD_I2C){
 
+        drdy = new InterruptIn(I2C_DRDY);    
+        drdy->rise(0);
+
         I2C* i2c = new I2C(I2C_SDA,I2C_SCL);
         i2c->frequency(I2C_SPEED_100KHZ);
 
-        AK099XX* ak099xx;
-        AK8963* ak8963;
-
         switch(subid){
             case AkmAkd::SUB_ID_AK8963N:
             case AkmAkd::SUB_ID_AK8963C:
@@ -96,19 +95,71 @@
                 break;
             }
         }
-        if(foundSensor != true) return AkmSensor::ERROR;
+        if(foundSensor != true){
+            return AkmSensor::ERROR;
+        }
         
         // read a data to reset DRDY 
         AkmECompass::MagneticVector mag;
         compass->getMagneticVector(&mag);
         
     }else if(primaryId == AKM_PRIMARY_ID_AKD_SPI){
-        MSG("#Error: SPI doesn't support.\n"); 
+        
+        drdy = new InterruptIn(SPI_DRDY);    
+        drdy->rise(0);
 
-        return AkmSensor::ERROR_DOESNT_SUPPORT;
-//        spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK);
-//        spi->format(8,3);    // 8bit, Mode=3
-//        spi->frequency(100000);
+        SPI* spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK);
+        spi->format(8,3);    // 8bit, Mode=3
+        spi->frequency(1000000);
+        
+        DigitalOut* cs = new DigitalOut(SPI_CS);
+        
+        switch(subid){
+            case AkmAkd::SUB_ID_AK8963N:
+            case AkmAkd::SUB_ID_AK8963C:
+                type = AkmECompass::AK8963;
+                ak8963 = new AK8963();
+                compass = ak8963;
+                AkmAkd::sensorName = "AK8963";
+                break;          
+            case AkmAkd::SUB_ID_AK09911:
+                type = AkmECompass::AK09911;
+                AkmAkd::sensorName = "AK09911";
+                return AkmSensor::ERROR;
+            case AkmAkd::SUB_ID_AK09912:
+                type = AkmECompass::AK09912;
+                ak099xx = new AK099XX();
+                compass = ak099xx;
+                AkmAkd::sensorName = "AK09912";
+                break;
+            case AkmAkd::SUB_ID_AK09915:
+                type = AkmECompass::AK09915;
+                ak099xx = new AK099XX();
+                compass = ak099xx;
+                AkmAkd::sensorName = "AK09915";
+                break;
+            case AkmAkd::SUB_ID_AK09916C:
+                type = AkmECompass::AK09916C;
+                AkmAkd::sensorName = "AK09916C";
+                // doesn't support SPI
+                return AkmSensor::ERROR;
+            default:
+                return AkmSensor::ERROR;
+//                break;
+        }
+        
+        bool foundSensor = false;
+        compass->init(spi, cs, type);
+        if(compass->checkConnection() == AkmECompass::SUCCESS) {
+            foundSensor = true;
+        }
+        if(foundSensor != true){
+            MSG("#failed checkConnetion(SPI). %s\n",AkmAkd::sensorName);
+            return AkmSensor::ERROR;
+        }
+        // read a data to reset DRDY 
+        AkmECompass::MagneticVector mag;
+        compass->getMagneticVector(&mag);
     }
     
     MSG("#%s detected.\n", AkmAkd::sensorName);
@@ -136,10 +187,11 @@
         return AkmSensor::ERROR;
     }
     
-    // DRDY interrupt enable for AK8963/AK09912/AK09915
-    if(compass->getSensorType() == AkmECompass::AK8963 ||
-       compass->getSensorType() == AkmECompass::AK09912 ||
-       compass->getSensorType() == AkmECompass::AK09915 ){       
+    // DRDY interrupt enable for AK8963/AK09912/AK09915 with I2C interface
+    if(primaryId == AKM_PRIMARY_ID_AKD_I2C && 
+        (compass->getSensorType() == AkmECompass::AK8963 ||
+         compass->getSensorType() == AkmECompass::AK09912 ||
+         compass->getSensorType() == AkmECompass::AK09915) ){       
         drdy->rise(this, &AkmAkd::detectDRDY);
     }
     else{