From 8a20585db4eb35d6e811f2ea486b72312cd83667 Mon Sep 17 00:00:00 2001
From: Federico Terraneo <fede.tft@miosix.org>
Date: Mon, 1 May 2017 23:42:18 +0200
Subject: [PATCH] Fixed intermittent MAX21105 bug

---
 src/shared/boards/AnakinBoard.cpp | 9 ++++++---
 src/shared/sensors/MAX21105.h     | 1 +
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/shared/boards/AnakinBoard.cpp b/src/shared/boards/AnakinBoard.cpp
index f7f8e9bc0..2c0fe8f9f 100644
--- a/src/shared/boards/AnakinBoard.cpp
+++ b/src/shared/boards/AnakinBoard.cpp
@@ -28,6 +28,8 @@ bool AnakinBoard::init()
     spiMS580301BA07::init();
 
     mInited = true;
+    mS_MAX21105  = new max21105_t   (max21105_t::ACC_FS_16G, 
+                                     max21105_t::GYRO_FS_250);
     mS_MPU9250   = new mpu_t        (mpu_t::ACC_FS_16G, 
                                      mpu_t::GYRO_FS_250);
     mS_INEMO     = new inemo_t      (inemo_t::ACC_FS_16G, 
@@ -35,15 +37,16 @@ bool AnakinBoard::init()
                                      inemo_t::COMPASS_FS_2);
     mS_FXAS      = new fxas_t       (fxas_t::DPS500);
     mS_LPS331AP  = new lps331ap_t   (lps331ap_t::SS_25HZ);
-    mS_MAX21105  = new max21105_t   (max21105_t::ACC_FS_16G, 
-                                     max21105_t::GYRO_FS_250);
     mS_MS580     = new ms580_t();
 
+    //The MAX21105 has to be initialized first as it is prone to listening
+    //to the initialization of the other sensors and switch by mistake to
+    //I2C mode
+    INIT_AND_CHECK(mS_MAX21105);
     INIT_AND_CHECK(mS_MPU9250);
     INIT_AND_CHECK(mS_INEMO);
     INIT_AND_CHECK(mS_FXAS);
     INIT_AND_CHECK(mS_LPS331AP);
-    INIT_AND_CHECK(mS_MAX21105);
     INIT_AND_CHECK(mS_MS580);
 
     AddSensor(MPU9250_ACCEL,    DATA_VEC3,     mS_MPU9250->accelDataPtr());
diff --git a/src/shared/sensors/MAX21105.h b/src/shared/sensors/MAX21105.h
index e78b09469..fc7417583 100644
--- a/src/shared/sensors/MAX21105.h
+++ b/src/shared/sensors/MAX21105.h
@@ -66,6 +66,7 @@ public:
         // Init this sensor
         uint8_t init_data[][2] =
         {
+            {MIF_CFG,      0b00101001},  // SPI 4 wire, I2C OFF (important!)
             {EXT_STATUS,         0x00},  // Choose the bank 0
             {SET_PWR,            0x00},  // Power down
 
-- 
GitLab