AKM Development Platform. This is the D7.014 version.

Dependencies:   AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822

Fork of AKDP by Masahiko Fukasawa

Revision:
14:76205d28fea2
Parent:
12:522a22a23f8a
Child:
16:5343a852fea4
--- a/main.cpp	Fri Jul 08 22:31:05 2016 +0000
+++ b/main.cpp	Fri Jul 22 22:54:45 2016 +0000
@@ -19,8 +19,7 @@
 #define CR                              '\r'
 #define LF                              '\n'
 
-#define DEVICE_NAME                     "AKDP Rev.D7.003"
-
+#define DEVICE_NAME                     "AKDP Rev.D7.004"
 
 BLE                 ble;
 UARTService*        uartService;
@@ -94,13 +93,13 @@
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
     manager->setEventConnected();
-    MSG("#Connected\n");
+    MSG("#Connected\r\n");
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     manager->setEventDisconnected();
-    MSG("#Disconnected\n");
+    MSG("#Disconnected\r\n");
     ble.gap().startAdvertising();
 } 
 
@@ -148,12 +147,12 @@
 #ifndef REV_D
     /* Rev.C */
     AnalogIn id(pin);
-    MSG("#Voltage=%5.2f[V]\n",id*3.0);
+//    MSG("#Voltage=%5.2f[V]\r\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");
+//    MSG("#GetID\r\n");
     
     I2C i2c(I2C_SDA, I2C_SCL);
     // ADC
@@ -166,13 +165,13 @@
         ch = MCP342X::ADC_CH2;
     }
     int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT);
-    MSG("#12bit ADC Val = %d.\n", val);
+//    MSG("#12bit ADC Val = %d.\r\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) + 0.5);
-    MSG("#ID = %d.\n", value);
+//    MSG("#ID = %d.\r\n", value);
 
 #endif    
     return value;
@@ -186,19 +185,14 @@
     NRF_TWI1->POWER  = 0;
 }
 
-void initAkdpBoard(){
+bool initAkdpBoard(){
 
-    // CSN High
+    // CSN High to activate I2C_GATE
     DigitalOut _cs = DigitalOut(SPI_CS);
     _cs.write(1);
-//    DigitalOut _so = DigitalOut(SPI_MISO);
-//    _so.write(1);
-//    DigitalOut _si = DigitalOut(SPI_MOSI);
-//    _si.write(1);
-//    DigitalOut _sck = DigitalOut(SPI_SCK);
-//    _sck.write(1);
+    DigitalOut _sck = DigitalOut(SPI_SCK);
+    _sck.write(1);
     
-    MSG("#I2C GPIO Expander.\n");
     const int TIME_FOR_OE_MS = 100;
     const TCA9554A::Port PORT_OE_LVS1   = TCA9554A::PORT_7;
     const TCA9554A::Port PORT_OE_LVS2   = TCA9554A::PORT_6;
@@ -219,28 +213,29 @@
     tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
     tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::HIGH);
     tca9554a.setPortLevel(PORT_SPIN, TCA9554A::HIGH);
+    wait_ms(TIME_FOR_OE_MS);
 
-    wait_ms(TIME_FOR_OE_MS);
-    
     //  disable 1.8V level shifter to read ID
     tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
-    MSG("#LVS1 Low.\n");
-    
     wait_ms(TIME_FOR_OE_MS);
+//    MSG("#LVS1 Low.\r\n");
     
     //  read ID and subId from ADC
     id = getId(ANALOG_SENSOR_ID,4);
     uint8_t subid_bitlen = 4;
     if(id == AkmSensor::AKM_PRIMARY_ID_AKD_SPI || id == AkmSensor::AKM_PRIMARY_ID_AKD_I2C){
-        MSG("#5 bit sub ID.\n");
+//        MSG("#5 bit sub ID.\r\n");
         subid_bitlen = 5;
     }
     subId = getId(ANALOG_SENSOR_ID_SUB,subid_bitlen);
 
+    if( (id == 11 && subId == 11) || (id == 55 && subId == 55) ){
+        return true;
+    }
+
     //  enable 1.8V level shifter
     tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
-    MSG("#LVS1 High.\n");
-
+//    MSG("#LVS1 High.\r\n");
     wait_ms(TIME_FOR_OE_MS);
 
     // RSTN control
@@ -248,7 +243,7 @@
         tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::LOW);
         wait_ms(TIME_FOR_OE_MS);
         tca9554a.setPortLevel(PORT_RSV_RSTN, TCA9554A::HIGH);                
-        MSG("#Detect AKD, RSTN control.\n");
+//        MSG("#Detect AKD, RSTN control.\r\n");
     }
 
     // SPI disable/enable
@@ -256,7 +251,7 @@
         tca9554a.setPortLevel(PORT_SPIN, TCA9554A::LOW);
         // Disable 5.0V level shifter in order to ADC doesn't respond.
         tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW);
-        MSG("#Detect SPI, set SPIN low.\n");
+//        MSG("#Detect SPI, set SPIN low.\r\n");
     }
     else{
         tca9554a.setPortLevel(PORT_SPIN, TCA9554A::HIGH);
@@ -266,12 +261,16 @@
     wait_ms(TIME_FOR_OE_MS);
     
     releaseTWI();
+    
+    return false;
 }
 
 
 
 int main(void)
-{   
+{
+//    bool isFailedBoot = false;
+       
     // USB serial
     serial.baud(115200);
     
@@ -280,11 +279,14 @@
     
 #ifdef DEBUG
     Debug::setSerial(&serial);
-    MSG("#Debug Mode.\n");
+    MSG("#Debug Mode.\r\n");
 #endif
     
     // initialize AKDP board
-    initAkdpBoard();
+    if( initAkdpBoard() ){
+//        isFailedBoot = true;
+        MSG("#Error: AKDP boot failed.\r\n");
+    }
 
     // ble initialize
     bleSetup();
@@ -296,11 +298,17 @@
     manager = new AkmSensorManager(&serial, uartService);
     
     if( manager->init(id, subId) == AkmSensorManager::ERROR){
-        MSG("#Error: sensor is NULL\n");
+//        isFailedBoot = true;
+        MSG("#Error: sensor is NULL\r\n");
     }
-
-    MSG("#Connecting...\n");
     
+    MSG("#Connecting...\r\n");
+/*
+    Message msg;
+    msg.setCommand(Message::CMD_BOOT_STATUS);
+    msg.setArgument( 0, isFailedBoot ? 1 : 0  );
+    manager->throwMessage(&msg);       
+*/
     // main loop
     while(1)
     {