Felix Rüdiger / MPU6050_lib

Dependents:   BLE_Nano_MPU6050Service

Files at this revision

API Documentation at this revision

Comitter:
fruediger
Date:
Tue Sep 08 13:42:30 2015 +0000
Parent:
5:5739a7a6d0e9
Child:
7:960ec35011dd
Commit message:
mostly complete commit; too many minor changes to list them all...

Changed in this revision

MPU6050.cpp Show annotated file Show diff for this revision Revisions of this file
MPU6050.h Show annotated file Show diff for this revision Revisions of this file
--- a/MPU6050.cpp	Wed Jul 22 16:02:20 2015 +0000
+++ b/MPU6050.cpp	Tue Sep 08 13:42:30 2015 +0000
@@ -6,16 +6,10 @@
     
     uint8_t tmp = reg;    
     
-    while (!(i2c.write(baseAddress, (char*)&tmp, 1, true) == 0))
+    while ((i2c.write(baseAddress & BYTE(11111110), (char*)&tmp, 1, true) != 0) || (i2c.read(baseAddress | BYTE(00000001), (char*)data, length) != 0))
         if ((t += retryDelay_secs) > timeout_secs)
             return false;
-        else               
-            wait(retryDelay_secs);
-    
-    while (!(i2c.read(baseAddress, (char*)data, length) == 0))
-        if ((t += retryDelay_secs) > timeout_secs)
-            return false;
-        else               
+        else
             wait(retryDelay_secs);
             
     return true;
@@ -28,11 +22,13 @@
 
 bool MPU6050::write(Register reg, uint8_t *data, size_t length, float timeout_secs)
 {
+    float t = 0.0f;
+    
     uint8_t tmp[length + 1];
     tmp[0] = reg;    
     memcpy(&tmp[1], data, length);    
     
-    for (float t = 0.0f; !(i2c.write(baseAddress, (char*)&tmp[0], length + 1) == 0);)
+    while (i2c.write(baseAddress & BYTE(11111110), (char*)&tmp[0], length + 1) != 0)
         if ((t += retryDelay_secs) > timeout_secs)
             return false;
         else               
@@ -43,7 +39,17 @@
 
 inline bool MPU6050::write(Register reg, uint8_t data, float timeout_secs)
 {
-    return this->write(reg, &data, 1, timeout_secs);
+    float t = 0.0f;
+    
+    uint8_t tmp[] = { (uint8_t)reg, data };
+    
+    while (i2c.write(baseAddress & BYTE(11111110), (char*)&tmp[0], 2) != 0)
+        if ((t += retryDelay_secs) > timeout_secs)
+            return false;
+        else               
+            wait(retryDelay_secs);
+    
+    return true;
 }
  
 bool MPU6050::getAuxVDDIOLevel(AuxVDDIOLevel *level, float timeout_secs)
@@ -798,9 +804,9 @@
     uint8_t tmp[6];
     if (this->read(REG_ACCEL_XOUT_H, tmp, 6, timeout_secs))
     {
-        *ax = (((int16_t)tmp[0]) << 8) | tmp[1];
-        *ay = (((int16_t)tmp[2]) << 8) | tmp[3];
-        *az = (((int16_t)tmp[4]) << 8) | tmp[5];
+        *ax = (int16_t)((tmp[0] << 8) | tmp[1]);
+        *ay = (int16_t)((tmp[2] << 8) | tmp[3]);
+        *az = (int16_t)((tmp[4] << 8) | tmp[5]);
         
         return true;
     }
@@ -813,7 +819,7 @@
     uint8_t tmp[2];
     if (this->read(REG_ACCEL_XOUT_H, tmp, 2, timeout_secs))
     {
-        *ax = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *ax = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -825,7 +831,7 @@
     uint8_t tmp[2];
     if (this->read(REG_ACCEL_YOUT_H, tmp, 2, timeout_secs))
     {
-        *ay = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *ay = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -837,7 +843,7 @@
     uint8_t tmp[2];
     if (this->read(REG_ACCEL_ZOUT_H, tmp, 2, timeout_secs))
     {
-        *az = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *az = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -849,7 +855,7 @@
     uint8_t tmp[2];
     if (this->read(REG_TEMP_OUT_H, tmp, 2, timeout_secs))
     {
-        *temp = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *temp = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -861,9 +867,9 @@
     uint8_t tmp[6];
     if (this->read(REG_GYRO_XOUT_H, tmp, 6, timeout_secs))
     {
-        *ox = (((int16_t)tmp[0]) << 8) | tmp[1];
-        *oy = (((int16_t)tmp[2]) << 8) | tmp[3];
-        *oz = (((int16_t)tmp[4]) << 8) | tmp[5];
+        *ox = (int16_t)((tmp[0] << 8) | tmp[1]);
+        *oy = (int16_t)((tmp[2] << 8) | tmp[3]);
+        *oz = (int16_t)((tmp[4] << 8) | tmp[5]);
         
         return true;
     }
@@ -876,7 +882,7 @@
     uint8_t tmp[2];
     if (this->read(REG_GYRO_XOUT_H, tmp, 2, timeout_secs))
     {
-        *ox = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *ox = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -888,7 +894,7 @@
     uint8_t tmp[2];
     if (this->read(REG_GYRO_YOUT_H, tmp, 2, timeout_secs))
     {
-        *oy = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *oy = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
@@ -900,25 +906,25 @@
     uint8_t tmp[2];
     if (this->read(REG_GYRO_ZOUT_H, tmp, 2, timeout_secs))
     {
-        *oz = (((int16_t)tmp[0]) << 8) | tmp[1];        
+        *oz = (int16_t)((tmp[0] << 8) | tmp[1]);        
         return true;
     }
     
     return false;
 }
 
-bool MPU6050::getMotionAndTemperature(int16_t *ax, int16_t *ay, int16_t *az, int16_t *ox, int16_t *oy, int16_t *oz, int16_t *temp, float timeout_secs)
+bool MPU6050::getMotionAndTemperature(int16_t *ax, int16_t *ay, int16_t *az, int16_t *temp, int16_t *ox, int16_t *oy, int16_t *oz, float timeout_secs)
 {
     uint8_t tmp[14];
-    if (this->read(REG_GYRO_XOUT_H, tmp, 14, timeout_secs))
+    if (this->read(REG_ACCEL_XOUT_H, &tmp[0], 14, timeout_secs))
     {
-        *ax = (((int16_t)tmp[0]) << 8) | tmp[1];
-        *ay = (((int16_t)tmp[2]) << 8) | tmp[3];
-        *az = (((int16_t)tmp[4]) << 8) | tmp[5];
-        *temp = (((int16_t)tmp[6]) << 8) | tmp[7];
-        *ox = (((int16_t)tmp[8]) << 8) | tmp[9];
-        *oy = (((int16_t)tmp[10]) << 8) | tmp[11];
-        *oz = (((int16_t)tmp[12]) << 8) | tmp[13];
+        *ax = (int16_t)(((uint16_t)tmp[0] << 8) | tmp[1]);
+        *ay = (int16_t)(((uint16_t)tmp[2] << 8) | tmp[3]);
+        *az = (int16_t)(((uint16_t)tmp[4] << 8) | tmp[5]);
+        *temp = (int16_t)(((uint16_t)tmp[6] << 8) | tmp[7]);
+        *ox = (int16_t)(((uint16_t)tmp[8] << 8) | tmp[9]);
+        *oy = (int16_t)(((uint16_t)tmp[10] << 8) | tmp[11]);
+        *oz = (int16_t)(((uint16_t)tmp[12] << 8) | tmp[13]);
         
         return true;
     }
@@ -931,7 +937,7 @@
     uint8_t tmp;
     #define SWAP(a, b) tmp = a; a = b; b = tmp;
     
-    if (this->read(REG_GYRO_XOUT_H, (uint8_t*)&converter, sizeof(converter), timeout_secs))
+    if (this->read(REG_ACCEL_XOUT_H, (uint8_t*)&converter, sizeof(converter), timeout_secs))
     {
         SWAP(converter.reg.ax_h,    converter.reg.ax_l);
         SWAP(converter.reg.ay_h,    converter.reg.ay_l);
@@ -1437,8 +1443,7 @@
 }
 
 bool MPU6050::getDeviceId(uint8_t *id, float timeout_secs)
-{
-    
+{    
     uint8_t tmp;
     if (this->read(REG_WHO_AM_I, &tmp, timeout_secs))
     {
--- a/MPU6050.h	Wed Jul 22 16:02:20 2015 +0000
+++ b/MPU6050.h	Tue Sep 08 13:42:30 2015 +0000
@@ -337,7 +337,7 @@
         
         static const uint8_t DEVICE_ID_MASK         = BYTE(01111110);
         
-        static const uint8_t MPU6050_ID             = BYTE(00110100);   // should be 0x34
+        static const uint8_t MPU6050_ID             = BYTE(01101000);   // should be 0x34 << 1
         
         /**
          * basic members