Added code to manage Orientation, FreeFall and Motion Detection. Data is also available via IRQ.

Dependents:   Test_FRDM_MMA8451Q AccelTest FRDM-KL46-Template KL25Z_Demo ... more

Fork of MMA8451Q by Emilio Monti

Files at this revision

API Documentation at this revision

Comitter:
clemente
Date:
Tue May 28 17:18:30 2013 +0000
Parent:
7:ba0016258d5d
Child:
9:2aa9b1668d14
Commit message:
Orientation tested. Freefall need some more tests.

Changed in this revision

MMA8451Q.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8451Q.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8451Q.cpp	Tue May 28 10:33:50 2013 +0000
+++ b/MMA8451Q.cpp	Tue May 28 17:18:30 2013 +0000
@@ -20,6 +20,7 @@
 
 #define REG_WHO_AM_I      0x0D
 #define REG_CTRL_REG_1    0x2A
+#define REG_CTRL_REG_2    0x2B
 #define REG_CTRL_REG_4    0x2D
 #define REG_CTRL_REG_5    0x2E
 #define REG_INT_SRC       0x0C
@@ -41,31 +42,36 @@
 #define UINT14_MAX        16383
 
 /** Interrupt schema
- *
- * :: The FreeFall and Motion detection share the same IRQ. 
- * 
- *   FreeFall --+                             +-- Fall_IRQ -----+
- *               \                           /                   \
- *                +-- MMA8451Q_Int2.fall ---+                     +--- user2_fptr
- *               /                           \                   /
- *   Motion ----+                             +-- Motion_IRQ ---+
- *   
- * :: The Orientation Detect use the IRQ1
- * 
- *   Orientation Detect -- MMA8451Q_Int1.fall --- Orientation_IRQ --- user1_fptr
- *
- */
-void (*user2_fptr)(void);
-void (*user1_fptr)(void);
+*
+* :: The FreeFall and Motion detection share the same IRQ2. 
+* 
+*   FreeFall --+                             +-- Fall_IRQ -----+
+*               \                           /                   \
+*                +-- MMA8451Q_Int2.fall ---+                     +--- user2_fptr
+*               /                           \                   /
+*   Motion ----+                             +-- Motion_IRQ ---+
+*   
+* :: The Orientation Detect use the IRQ1
+* 
+*   Orientation Detect -- MMA8451Q_Int1.fall --- Orientation_IRQ --- user1_fptr
+*
+*/
+void (*user2_fptr)(void);               // Pointers to user function called after
+void (*user1_fptr)(void);               // IRQ assertion.
 
 //
-InterruptIn MMA8451Q_Int1( PTA14);
-InterruptIn MMA8451Q_Int2( PTA15);
+InterruptIn MMA8451Q_Int1( PTA14);      // INT1
+InterruptIn MMA8451Q_Int2( PTA15);      // INT2
 
 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
-    // activate the peripheral
-    uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
-    writeRegs(data, 2);
+    
+    MMA8451Q_Int1.fall( NULL);
+    MMA8451Q_Int2.fall( NULL);
+    user2_fptr = NULL;
+    user1_fptr = NULL;    
+
+    Reset();    
+    Active();    
 }
 
 MMA8451Q::~MMA8451Q() 
@@ -76,8 +82,19 @@
      user1_fptr = NULL;
 }
 
-void MMA8451Q::FreFallDetection( void(*fptr)(void))
+void MMA8451Q::Reset( void)
 {
+    // Soft reset
+    uint8_t data[2] = {REG_CTRL_REG_2, 0x40};
+    writeRegs(data, 2);
+    wait( 0.1);
+}
+
+void MMA8451Q::FreeFallDetection( void(*fptr)(void))
+{
+    // Soft Reset
+    Reset();
+    
     // Example Steps for Configuring Linear Freefall Detection
     // X AND Y AND Z < 0.2g using MFF Function, 50 Hz ODR
     // Step 1: Put the device in Standby Mode: Register 0x2A CTRL_REG1
@@ -140,7 +157,9 @@
 
 void MMA8451Q::MotionDetection( void(*fptr)(void))
 {
-
+    // Soft Reset
+    Reset();
+    
     // 6.1 Example Steps for Configuring Motion Detection
     // X or Y > 3g using MFF Function 4g, 100 Hz ODR, Normal Mode
     // Step 1: Put the device into Standby Mode: Register 0x2A CTRL_REG1
@@ -156,9 +175,9 @@
 
     // Step 3: Threshold Setting Value for the Motion detection of > 2g
     // Note: The step count is 0.063g/ count
-    // • 2g/0.063g = 31.7; //Round up to 32
+    // • 1g/0.063g = 15.8; //Round up to 16
     data[0] = REG_FF_MT_THS;
-    data[1] = 0x20;
+    data[1] = 0x10;
     writeRegs(data, 2);
     
     // Step 4: Set the debounce counter to eliminate false readings for 100 Hz sample rate with a requirement
@@ -211,6 +230,13 @@
 void MMA8451Q::OrientationDetect( void(*fptr)(void), unsigned int Z_LockOut, unsigned int Z_BkFr, unsigned int PL_Thsld, unsigned int PL_Hyst)
 {
     unsigned char t;
+
+    // Soft Reset
+    Reset();
+        
+    // Reset orientation value.
+    OrientationState = 0;
+    OrientationStateUpdated = 0;
     
     // Step 1: Put the part into Standby Mode
     Standby();
@@ -320,11 +346,24 @@
     if ( (t & 0x10) == 0x10) {
         // Read the PL State from the Status Register, clear the interrupt
         readRegs( REG_PL_STATUS, &t, 1);
+        // Set the orientation state variable
+        OrientationState = t;
+        OrientationStateUpdated = 1;
         // Run the user supplied function
         user1_fptr();
     }
 }
 
+unsigned char MMA8451Q::GetOrientationState( void)
+{
+    if ( OrientationStateUpdated) {
+        OrientationStateUpdated = 0;
+        return OrientationState;
+    }
+    //
+    return 0;
+}
+
 void MMA8451Q::Active( void)
 {
     unsigned char t;
--- a/MMA8451Q.h	Tue May 28 10:33:50 2013 +0000
+++ b/MMA8451Q.h	Tue May 28 17:18:30 2013 +0000
@@ -81,6 +81,11 @@
 #define PL_HYS_17       0x05    // Set Hysteresis to ±17°
 #define PL_HYS_21       0x06    // Set Hysteresis to ±21°
 #define PL_HYS_24       0x07    // Set Hysteresis to ±24°
+// Landscape/Portrait orientation
+#define cLAPO_PU        0       // Portrait Up: Equipment standing vertically in the normal orientation
+#define cLAPO_PD        1       // Portrait Down: Equipment standing vertically in the inverted orientation
+#define cLAPO_LR        2       // Landscape Right: Equipment is in landscape mode to the right
+#define cLAPO_LL        3       // Landscape Left: Equipment is in landscape mode to the left.
 
 class MMA8451Q
 {
@@ -134,10 +139,66 @@
     */
     void getAccAllAxis(float * res);
 
-    void FreFallDetection( void(*fptr)(void));
+    /**
+    * Configure the Accelerometere for free fall detection
+    *
+    * @param pointer to the user function to execute after IRQ assertion
+    * @return none
+    */
+    void FreeFallDetection( void(*fptr)(void));
+
+    /**
+    * Configure the Accelerometere for motion detection
+    *
+    * @param pointer to the user function to execute after IRQ assertion
+    * @return none
+    */
     void MotionDetection( void(*fptr)(void));
+    
+    /**
+    * Configure the Accelerometere for orientation detection
+    *
+    * @param pointer to the user function to execute after IRQ assertion
+    * @param Z lockout value, Z Backfront, Port/Landscape Thsld, Port/Landscape Hysteresis
+    * @return none
+    */
     void OrientationDetect( void(*fptr)(void), unsigned int Z_LockOut, unsigned int Z_BkFr, unsigned int PL_Thsld, unsigned int PL_Hyst);
     void OrientationDetect( void(*fptr)(void));
+       
+    /**
+    * Get the orientation state. 
+    *
+    *    0x10: PL_STATUS Register (Read Only)
+    *    Bit 7   Bit 6   Bit 5   Bit 4   Bit 3   Bit 2   Bit 1   Bit 0
+    *    NEWLP   LO      —       —       —       LAPO[1] LAPO[0] BAFRO
+    *
+    *    NEWLP
+    *        Landscape/Portrait status change flag. Default value: 0.
+    *        0: No change, 1: BAFRO and/or LAPO and/or Z-Tilt lockout value has changed
+    *    LO
+    *        Z-Tilt Angle Lockout. Default value: 0.
+    *        0: Lockout condition has not been detected.
+    *        1: Z-Tilt lockout trip angle has been exceeded. Lockout has been detected.
+    *    LAPO[1:0](*)
+    *        Landscape/Portrait orientation. Default value: 00
+    *        00: Portrait Up: Equipment standing vertically in the normal orientation
+    *        01: Portrait Down: Equipment standing vertically in the inverted orientation
+    *        10: Landscape Right: Equipment is in landscape mode to the right
+    *        11: Landscape Left: Equipment is in landscape mode to the left.
+    *        (*) The default power up state is BAFRO = 0, LAPO = 0, and LO = 0.        
+    *    BAFRO
+    *        Back or Front orientation. Default value: 0
+    *        0: Front: Equipment is in the front facing orientation.
+    *        1: Back: Equipment is in the back facing orientation.
+    */
+    unsigned char GetOrientationState( void);
+
+    /**
+    * Soft Reset
+    * @param none
+    * @return none
+    */
+    void Reset( void);
     
 private:
     I2C m_i2c;
@@ -150,6 +211,9 @@
     void Fall_IRQ( void);
     void Motion_IRQ( void);
     void Orientation_IRQ( void);
+    //
+    unsigned char OrientationState;
+    unsigned char OrientationStateUpdated;
 };
 
 #endif