Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Diff: akmsensormanager.cpp
- Revision:
- 2:11fe67783c4c
- Parent:
- 1:b46b8653331f
- Child:
- 4:af13b985c689
--- a/akmsensormanager.cpp Thu May 05 00:19:39 2016 +0000 +++ b/akmsensormanager.cpp Thu May 05 21:11:23 2016 +0000 @@ -7,6 +7,9 @@ #include "akmakd.h" #include "debug.h" #include "Message.h" +#ifdef REV_D +#include "mcp342x.h" +#endif #define FIRMWARE_VERSION 0x02 #define MAGNETOMETER_ID 0x0A @@ -138,13 +141,56 @@ return status; } +#ifdef REV_D +int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { + const int WAIT_ADC_MS = 1; + + // Configure channel and trigger. + mcp3428->setChannel(ch); + mcp3428->setSampleSetting(s); + mcp3428->trigger(); + + // polling data (!blocking) + MCP342X::Data data; + do { + wait_ms(WAIT_ADC_MS); + mcp3428->getData(&data); + } while(data.st == MCP342X::DATA_NOT_UPDATED); + + return data.value; +} +#endif uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) { +#ifndef REV_D + /* Rev.C */ AnalogIn id(pin); MSG("#Voltage=%5.2f[V]\n",id*3.0); double s = id + 1.0/(double)(pow(2.0,bits+1)); uint8_t value = (uint8_t)(s*pow(2.0,bits)); +#else + /* Rev.D */ + MSG("GetID\n"); + I2C i2c(I2C_SDA0, I2C_SCL0); + // ADC + MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); + mcp342x.setConversionMode(MCP342X::ONE_SHOT); + MCP342X::AdcChannel ch; + if (pin == ANALOG_SENSOR_ID) { + ch = MCP342X::ADC_CH1; + } else { // pin == ANALOG_SENSOR_ID_SUB + ch = MCP342X::ADC_CH2; + } + int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); + MSG("#12bit ADC Val = %d.\n", val); + + const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V + const int16_t VAL_MIN = -2048; // Corresponds to 0V + + uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits)); + MSG("ID = %d.", value); +#endif return value; }