Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Diff: akmakd.cpp
- Revision:
- 19:8dcc4f323bdc
- Parent:
- 18:b7182d5ad8d5
- Child:
- 20:2fca76521680
--- a/akmakd.cpp Tue Feb 21 23:46:12 2017 +0000 +++ b/akmakd.cpp Wed Feb 22 18:47:01 2017 +0000 @@ -24,14 +24,80 @@ if (drdy) delete drdy; } +AkmSensor::Status AkmAkd::checkSensor( const uint8_t id, const uint8_t subid, AkmECompass::SensorType* type){ + AK099XX* ak099xx; + AK8963* ak8963; + + switch(subid){ + case AkmAkd::SUB_ID_AK8963N: + case AkmAkd::SUB_ID_AK8963C: + *type = AkmECompass::AK8963; + AkmAkd::sensorName = "AK8963"; + ak8963 = new AK8963(); + compass = ak8963; + break; + case AkmAkd::SUB_ID_AK09911C: + *type = AkmECompass::AK09911; + AkmAkd::sensorName = "AK09911C"; + if(primaryId == AKM_PRIMARY_ID_AKD_SPI) + return AkmSensor::ERROR; // No SPI support + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09912C: + *type = AkmECompass::AK09912; + AkmAkd::sensorName = "AK09912C"; + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09915C: + *type = AkmECompass::AK09915; + AkmAkd::sensorName = "AK09915C"; + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09915D: + *type = AkmECompass::AK09915; + AkmAkd::sensorName = "AK09915D"; + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09916C: + *type = AkmECompass::AK09916C; + AkmAkd::sensorName = "AK09916C"; + if(primaryId == AKM_PRIMARY_ID_AKD_SPI) + return AkmSensor::ERROR; // No SPI support + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09916D: + *type = AkmECompass::AK09916D; + AkmAkd::sensorName = "AK09916D"; + if(primaryId == AKM_PRIMARY_ID_AKD_SPI) + return AkmSensor::ERROR; // No SPI support + ak099xx = new AK099XX(); + compass = ak099xx; + break; + case AkmAkd::SUB_ID_AK09918: + *type = AkmECompass::AK09918; + AkmAkd::sensorName = "AK09918"; + if(primaryId == AKM_PRIMARY_ID_AKD_SPI) + return AkmSensor::ERROR; // No SPI support + ak099xx = new AK099XX(); + compass = ak099xx; + break; + default: + return AkmSensor::ERROR; + } + return AkmSensor::SUCCESS; +} + AkmSensor::Status AkmAkd::init(const uint8_t id, const uint8_t subid){ primaryId = id; subId = subid; AkmECompass::SensorType type; - AK099XX* ak099xx; - AK8963* ak8963; - + mode = AkmECompass::MODE_POWER_DOWN; nsf = AkmECompass::NSF_DISABLE; sdr = AkmECompass::SDR_LOW_POWER_DRIVE; @@ -44,63 +110,10 @@ I2C* i2c = new I2C(I2C_SDA,I2C_SCL); i2c->frequency(I2C_SPEED_100KHZ); - 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_AK09911C: - type = AkmECompass::AK09911; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09911C"; - break; - case AkmAkd::SUB_ID_AK09912C: - type = AkmECompass::AK09912; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09912C"; - break; - case AkmAkd::SUB_ID_AK09915C: - type = AkmECompass::AK09915; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09915C"; - break; - case AkmAkd::SUB_ID_AK09915D: - type = AkmECompass::AK09915; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09915D"; - break; - case AkmAkd::SUB_ID_AK09916C: - type = AkmECompass::AK09916C; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09916C"; - break; - case AkmAkd::SUB_ID_AK09916D: - type = AkmECompass::AK09916D; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09916D"; - break; - case AkmAkd::SUB_ID_AK09918: - type = AkmECompass::AK09918; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09918"; - break; - default: - return AkmSensor::ERROR; -// break; + if( checkSensor(id,subid,&type) != AkmSensor::SUCCESS ){ + return AkmSensor::ERROR; } - bool foundSensor = false; - AkmECompass::SlaveAddress slaveAddr[] = { AkmECompass::SLAVE_ADDR_1, AkmECompass::SLAVE_ADDR_2, @@ -112,19 +125,10 @@ compass->init(i2c, slaveAddr[i], type); // Checks connectivity if(compass->checkConnection() == AkmECompass::SUCCESS) { - // found - foundSensor = true; - break; + MSG("#%s detected.\r\n", AkmAkd::sensorName); + return AkmSensor::SUCCESS; } } - 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){ drdy = new InterruptIn(SPI_DRDY); @@ -135,73 +139,18 @@ 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_AK09911C: - type = AkmECompass::AK09911; - AkmAkd::sensorName = "AK09911C"; - return AkmSensor::ERROR; - case AkmAkd::SUB_ID_AK09912C: - type = AkmECompass::AK09912; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09912C"; - break; - case AkmAkd::SUB_ID_AK09915C: - type = AkmECompass::AK09915; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09915C"; - break; - case AkmAkd::SUB_ID_AK09915D: - type = AkmECompass::AK09915; - ak099xx = new AK099XX(); - compass = ak099xx; - AkmAkd::sensorName = "AK09915D"; - break; - case AkmAkd::SUB_ID_AK09916C: - type = AkmECompass::AK09916C; - AkmAkd::sensorName = "AK09916C"; - // doesn't support SPI - return AkmSensor::ERROR; - case AkmAkd::SUB_ID_AK09916D: - type = AkmECompass::AK09916D; - AkmAkd::sensorName = "AK09916D"; - // doesn't support SPI - return AkmSensor::ERROR; - case AkmAkd::SUB_ID_AK09918: - type = AkmECompass::AK09918; - AkmAkd::sensorName = "AK09918"; - // doesn't support SPI - return AkmSensor::ERROR; - default: - return AkmSensor::ERROR; -// break; + + if( checkSensor(id,subid,&type) != AkmSensor::SUCCESS ){ + return AkmSensor::ERROR; } - bool foundSensor = false; compass->init(spi, cs, type); if(compass->checkConnection() == AkmECompass::SUCCESS) { - foundSensor = true; - } - if(foundSensor != true){ - MSG("#failed checkConnetion(SPI). %s\r\n",AkmAkd::sensorName); - return AkmSensor::ERROR; + MSG("#%s detected.\r\n", AkmAkd::sensorName); + return AkmSensor::SUCCESS; } - // read a data to reset DRDY -// AkmECompass::MagneticVector mag; -// compass->getMagneticVector(&mag); } - - MSG("#%s detected.\r\n", AkmAkd::sensorName); - return AkmSensor::SUCCESS; + return AkmSensor::ERROR; } @@ -219,23 +168,41 @@ return event; } +AkmAkd::InterruptMode AkmAkd::getInterrupt(uint8_t primaryId, uint8_t subId){ + AkmAkd::InterruptMode ret = AkmAkd::INTERRUPT_DISABLED; + + if( primaryId == AKM_PRIMARY_ID_AKD_I2C && + (subId == AkmAkd::SUB_ID_AK8963N || + subId == AkmAkd::SUB_ID_AK8963C || + subId == AkmAkd::SUB_ID_AK09912C || + subId == AkmAkd::SUB_ID_AK09915C ) ){ + ret = AkmAkd::INTERRUPT_ENABLED_PP; + } + else if( primaryId == AKM_PRIMARY_ID_AKD_I2C && + (subId == AkmAkd::SUB_ID_AK09915D || + subId == AkmAkd::SUB_ID_AK09916D ) ){ + ret = AkmAkd::INTERRUPT_ENABLED_OD; + } + else{ + // No DRDY + // No DRDY use for SPI interface + } + return ret; +} + + AkmSensor::Status AkmAkd::startSensor(){ // read one data to clear DRDY AkmECompass::MagneticVector mag; compass->getMagneticVector(&mag); + AkmAkd::InterruptMode int_mode = getInterrupt(primaryId,subId); // enable interrupt - if( primaryId == AKM_PRIMARY_ID_AKD_I2C && - (subId == AkmAkd::SUB_ID_AK8963N || - subId == AkmAkd::SUB_ID_AK8963C || - subId == AkmAkd::SUB_ID_AK09912C || - subId == AkmAkd::SUB_ID_AK09915C ) ){ + if( int_mode == AkmAkd::INTERRUPT_ENABLED_PP ){ // Push-Pull DRDY drdy->rise(callback(this, &AkmAkd::detectDRDY)); } - else if( primaryId == AKM_PRIMARY_ID_AKD_I2C && - (subId == AkmAkd::SUB_ID_AK09915D || - subId == AkmAkd::SUB_ID_AK09916D ) ){ + else if( int_mode == AkmAkd::INTERRUPT_ENABLED_OD ){ // Open Drain DRDY drdy->fall(callback(this, &AkmAkd::detectDRDY)); } @@ -276,20 +243,18 @@ ticker.detach(); event = false; - // disable DRDY interrupt - if( primaryId == AKM_PRIMARY_ID_AKD_I2C && - (subId == AkmAkd::SUB_ID_AK8963N || - subId == AkmAkd::SUB_ID_AK8963C || - subId == AkmAkd::SUB_ID_AK09912C || - subId == AkmAkd::SUB_ID_AK09915C ) ){ + AkmAkd::InterruptMode int_mode = getInterrupt(primaryId,subId); + if( int_mode == AkmAkd::INTERRUPT_ENABLED_PP ){ + // Push-Pull DRDY drdy->rise(NULL); } - else if( primaryId == AKM_PRIMARY_ID_AKD_I2C && - (subId == AkmAkd::SUB_ID_AK09915D || - subId == AkmAkd::SUB_ID_AK09916D ) ){ + else if( int_mode == AkmAkd::INTERRUPT_ENABLED_OD ){ // Open Drain DRDY drdy->fall(NULL); } + else{ + // No DRDY + } // Puts the device into power down mode. if(compass->setOperationMode(AkmECompass::MODE_POWER_DOWN) != AkmECompass::SUCCESS) {