This is a simple device driver for the 3 axis accelerometer MMA8452 that works with mbed.

Dependents:   MMA8452_test S05APP3_routeur

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8452.h Source File

MMA8452.h

00001 // Author: Nicholas Herriot
00002 /* Copyright (c) 2013 Vodafone, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 // the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
00021 // see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf
00022 
00023 //#define SA0 1
00024 //#if SA0
00025   //#define MMA8452_ADDRESS 0x3A  // SA0 is high, 0x1C if low - it should be 0x1D, but we shift now to save shifting in the code
00026 //#else
00027   //#define MMA8452_ADDRESS 0x1C
00028 //#endif
00029 
00030 //#ifndef MBED_MMA8452
00031 
00032 //#define MBED_MMA8452
00033  
00034 #include "mbed.h"
00035  
00036 /** Accelerometer MMA8452 class 
00037  *
00038  * Example:
00039  * @code
00040  * 
00041  * #include "mbed.h"
00042  * #include "MMA8452.h"
00043  * 
00044  * 
00045  * Accelerometer_MMA8452 Acc(p28, p27);
00046  * serial pc(USBTX, USBRX);
00047  *
00048  * int main() 
00049  * {
00050  *   Acc.init(); 
00051  *      while(1)
00052  *      {
00053  *          int x=0, y=0, z=0;
00054  *          Acc.read_Tilt(&x, &y, &z);
00055  *          pc.printf("Tilt x: %2.2f degree \n", x);                    // Print the tilt orientation of the X axis
00056  *          pc.printf("Tilt y: %2.2f degree \n", y);                    // Print the tilt orientation of the Y axis
00057  *          pc.printf("Tilt z: %2.2f degree \n", z);                    // Print the tilt orientation of the Z axis
00058  *          wait(1);       
00059  *      }
00060  * }
00061  * @endcode
00062  */ 
00063 
00064 // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
00065 #define SA0 1
00066 #if SA0
00067   #define MMA8452_ADDRESS 0x1D  // SA0 is high, 0x1C if low - 
00068 #else
00069   #define MMA8452_ADDRESS 0x1C
00070 #endif
00071 
00072 // Register descriptions found in section 6 of pdf
00073 #define STATUS 0x00                         // Type 'read' : Real time status, should return 0x00
00074 #define OUT_X_MSB 0x01                      // Type 'read' : x axis - 8 most significatn bit of a 12 bit sample
00075 #define OUT_X_LSB 0x02                      // Type 'read' : x axis - 4 least significatn bit of a 12 bit sample
00076 #define OUT_Y_MSB 0x03                      // Type 'read' : y axis - 8 most significatn bit of a 12 bit sample
00077 #define OUT_Y_LSB 0x04                      // Type 'read' : y axis - 4 least significatn bit of a 12 bit sample
00078 #define OUT_Z_MSB 0x05                      // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample
00079 #define OUT_Z_LSB 0x06                      // Type 'read' : z axis - 4 least significatn bit of a 12 bit sample
00080 
00081 #define SYSMOD 0x0B                         // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP
00082 #define WHO_AM_I 0x0D                       // Type 'read' : This should return the device id of 0x2A
00083 
00084 #define PL_STATUS 0x10                      // Type 'read' : This shows portrait landscape mode orientation
00085 #define PL_CFG 0x11                         // Type 'read/write' : This allows portrait landscape configuration
00086 #define PL_COUNT 0x12                       // Type 'read' : This is the portraint landscape debounce counter
00087 #define PL_BF_ZCOMP 0x13                    // Type 'read' :
00088 #define PL_THS_REG 0x14                     // Type 'read' :
00089 
00090 #define FF_MT_CFG 0X15                      // Type 'read/write' : Freefaul motion functional block configuration
00091 #define FF_MT_SRC 0X16                      // Type 'read' : Freefaul motion event source register
00092 #define FF_MT_THS 0X17                      // Type 'read' : Freefaul motion threshold register
00093 #define FF_COUNT 0X18                       // Type 'read' : Freefaul motion debouce counter
00094 
00095 #define ASLP_COUNT 0x29                     // Type 'read/write' : Counter settings for auto sleep
00096 #define CTRL_REG_1 0x2A                     // Type 'read/write' :
00097 #define CTRL_REG_2 0x2B                     // Type 'read/write' :
00098 #define CTRL_REG_3 0x2C                     // Type 'read/write' :
00099 #define CTRL_REG_4 0x2D                     // Type 'read/write' :
00100 #define CTRL_REG_5 0x2E                     // Type 'read/write' :
00101 
00102 // Defined in table 13 of the Freescale PDF
00103 #define STANDBY 0x00                        // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
00104 #define WAKE 0x01                           // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
00105 #define SLEEP 0x02                          // State value returned after a SYSMOD request, it can be in state STANDBY, WAKE or SLEEP
00106 #define ACTIVE 0x01                         // Stage value returned and set in Control Register 1, it can be STANDBY=00, or ACTIVE=01
00107 
00108 
00109  
00110 #define TILT_STATUS 0x03        // Tilt Status (Read only)
00111 #define SRST_STATUS 0x04        // Sample Rate Status Register (Read only)
00112 #define SPCNT_STATUS 0x05       // Sleep Count Register (Read/Write)
00113 #define INTSU_STATUS 0x06       // Interrupt Setup Register
00114 #define MODE_STATUS 0x07        // Mode Register (Read/Write)
00115 #define SR_STATUS 0x08          // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
00116 #define PDET_STATUS 0x09        // Tap/Pulse Detection Register (Read/Write)
00117 #define PD_STATUS 0xA           // Tap/Pulse Debounce Count Register (Read/Write)
00118 
00119 
00120 
00121 
00122 
00123  
00124 class Accelerometer_MMA8452         
00125 {        
00126     public:
00127         
00128         
00129        /** Create an accelerometer object connected to the specified I2C object
00130         *
00131         * @param sda I2C data port
00132         * @param scl I2C8452 clock port
00133         * 
00134         */ 
00135       Accelerometer_MMA8452(PinName sda, PinName scl, int frequency);
00136       //Accelerometer_MMA8452(PinName sda, PinName scl);
00137        
00138        /** Destroys an MMA8452 object
00139         *
00140         */
00141       ~Accelerometer_MMA8452();
00142       
00143       
00144       
00145       /** Get system mode of the MMA8452 (not required)
00146         *   returns 0 for success in reading the system mode of the chip
00147         *   returns 1 for failure in reading the system mode of the chip
00148         *   -currently no retries or waiting is done, this method tries 1 time then exits.
00149         *
00150         *   This method is used to find out the system mode of the chip ACTIVE = 0x00 or STANDBY = 0x01
00151       */
00152       int get_SystemMode(int& deviceSystemMode);
00153       
00154       
00155       
00156       /** Get status of the MMA8452 (not required)
00157         *   returns 0 for success in reading the status of the chip
00158         *   returns 1 for failure in reading the status of  the chip
00159         *   -currrently no retries or waiting is done, this method tries 1 time then exits.
00160         *
00161         *   This method is used to find out the real time status of the device it shows if
00162         *   x,y and z values have been overwritten before they have been read since a change happened.
00163         *   
00164       */
00165       int get_Status(int& deviceStatus);
00166      
00167       
00168       
00169       /** Activate the MMA8452 (required)
00170         *   returns 0 for success in activating the chip
00171         *   returns 1 for failure in activating the chip
00172         *   -currrently no retries or waiting is done, this method tries 1 time the exits.
00173         *
00174         *   This will set the device 'active' even if it's already active. It's just a way to force that state.
00175       */
00176       int activate();
00177  
00178  
00179  
00180        /** Standby the MMA8452 (not required)
00181         *   returns 0 for success in activating the chip
00182         *   returns 1 for failure in activating the chip
00183         *   -currrently no retries or waiting is done, this method tries 1 time the exits.
00184         *
00185         *   This will set the device 'standby' even if it's already in standby. It's just a way to force that state.
00186       */
00187       int standby();
00188 
00189  
00190        /** get_CTRL_Reg1 the MMA8452 (not required)
00191         *   returns 0 for success in activating the chip
00192         *   returns 1 for failure in activating the chip
00193         *   -currrently no retries or waiting is done, this method tries 1 time the exits.
00194         *
00195         *   This will return the state of the control register 1. This holds and sets values for auto wake, sleep mode
00196         *   output data rate, fast read mode and active/standby. More info on 6.7 of pdf for MMA8452 Freescale doc.
00197       */
00198       int get_CTRL_Reg1(int& CTRL_Reg); 
00199       
00200       
00201        /** Initialization of device MMA8452 (required)
00202         */
00203       void init();
00204     
00205        /** Read the device ID from the accelerometer
00206         *
00207         * @param *deviceID Value of device - is should be 0x2A
00208         * return 0 for success or
00209         * return 1 for failure.
00210         */        
00211       int get_DeviceID(int& deviceID);  
00212       
00213       int read_y();
00214       
00215       int read_z();  
00216 
00217       /** Read the x register of the MMA8452
00218         *
00219         * @returns The value of x acceleration
00220         */
00221       int read_x_raw(char *xaxis);
00222       //int read_x(int& xaxisLSB);
00223       /** Read the y register of the MMA8452
00224         *
00225         * @returns The value of y acceleration
00226         */
00227       int read_y_raw(char *yaxis);
00228       
00229       /** Read the z register of the MMA8452
00230         *
00231         * @returns The value of z acceleration
00232         */
00233        int read_z_raw(char * zaxis);
00234       
00235       /** Read the x,y and z registers of the MMA8452
00236        *
00237        * takes a 2 byte char buffer to store the value
00238        * for each axis.
00239        * returns 0 for success, and 1 for failure
00240        *
00241        */ 
00242       int read_xyz(char *x, char *y, char *z); 
00243             
00244         /** Read from specified MMA8452 register
00245          *
00246          * @param addr The internal registeraddress of the MMA8452
00247          * @returns The value of the register
00248          */
00249       char read_reg(char addr);
00250         
00251         /** Write to specified MMA8452 register
00252         *
00253         * @param addr The internal registeraddress of the MMA8452
00254         * @param data New value of the register
00255         */    
00256       void write_reg(char addr, char data); 
00257       
00258    
00259     private:
00260       I2C m_i2c;
00261       int m_frequency;
00262          
00263 };
00264 
00265 //#endif