Dependents:   IMU DataLogger AcceptanceTest P4_IMU

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ITG3200.h Source File

ITG3200.h

00001 /**
00002  * @section LICENSE
00003  *
00004  * Copyright (c) 2010 ARM Limited
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  *
00024  * @section DESCRIPTION
00025  *
00026  * ITG-3200 triple axis, digital interface, gyroscope.
00027  *
00028  * Datasheet:
00029  *
00030  * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
00031  */
00032 
00033 #ifndef ITG3200_H
00034 #define ITG3200_H
00035 
00036 /**
00037  * Includes
00038  */
00039 #include "mbed.h"
00040 
00041 /**
00042  * Defines
00043  */
00044 #define ITG3200_I2C_ADDRESS 0x68 //7-bit address.
00045 
00046 //-----------
00047 // Registers
00048 //-----------
00049 #define WHO_AM_I_REG    0x00
00050 #define SMPLRT_DIV_REG  0x15
00051 #define DLPF_FS_REG     0x16
00052 #define INT_CFG_REG     0x17
00053 #define INT_STATUS      0x1A
00054 #define TEMP_OUT_H_REG  0x1B
00055 #define TEMP_OUT_L_REG  0x1C
00056 #define GYRO_XOUT_H_REG 0x1D
00057 #define GYRO_XOUT_L_REG 0x1E
00058 #define GYRO_YOUT_H_REG 0x1F
00059 #define GYRO_YOUT_L_REG 0x20
00060 #define GYRO_ZOUT_H_REG 0x21
00061 #define GYRO_ZOUT_L_REG 0x22
00062 #define PWR_MGM_REG     0x3E
00063 
00064 //----------------------------
00065 // Low Pass Filter Bandwidths
00066 //----------------------------
00067 #define LPFBW_256HZ 0x00
00068 #define LPFBW_188HZ 0x01
00069 #define LPFBW_98HZ  0x02
00070 #define LPFBW_42HZ  0x03
00071 #define LPFBW_20HZ  0x04
00072 #define LPFBW_10HZ  0x05
00073 #define LPFBW_5HZ   0x06
00074 
00075 /**
00076  * ITG-3200 triple axis digital gyroscope.
00077  */
00078 class ITG3200 {
00079 
00080 public:
00081 
00082     /**
00083      * Constructor.
00084      *
00085      * Sets FS_SEL to 0x03 for proper opertaion.
00086      *
00087      * @param sda - mbed pin to use for the SDA I2C line.
00088      * @param scl - mbed pin to use for the SCL I2C line.
00089      */
00090     ITG3200(PinName sda, PinName scl);
00091 
00092     /**
00093      * Get the identity of the device.
00094      *
00095      * @return The contents of the Who Am I register which contains the I2C
00096      *         address of the device.
00097      */
00098     char getWhoAmI(void);
00099 
00100     /**
00101      * Set the address of the device.
00102      *
00103      * @param address The I2C slave address to write to the Who Am I register
00104      *        on the device.
00105      */
00106     void setWhoAmI(char address);
00107 
00108     /**
00109      * Get the sample rate divider.
00110      *
00111      * @return The sample rate divider as a number from 0-255.
00112      */
00113     char getSampleRateDivider(void);
00114 
00115     /**
00116      * Set the sample rate divider.
00117      *
00118      * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz,
00119      * as decidied by the DLPF_FS register.
00120      *
00121      * @param The sample rate divider as a number from 0-255.
00122      */
00123     void setSampleRateDivider(char divider);
00124 
00125     /**
00126      * Get the internal sample rate.
00127      *
00128      * @return The internal sample rate in kHz - either 1 or 8.
00129      */
00130     int getInternalSampleRate(void);
00131 
00132     /**
00133      * Set the low pass filter bandwidth.
00134      *
00135      * Also used to set the internal sample rate.
00136      * Pass the #define bandwidth codes as a parameter.
00137      *
00138      * 256Hz -> 8kHz internal sample rate.
00139      * Everything else -> 1kHz internal rate.
00140      *
00141      * @param bandwidth Low pass filter bandwidth code
00142      */
00143     void setLpBandwidth(char bandwidth);
00144 
00145     /**
00146      * Get the interrupt configuration.
00147      *
00148      * See datasheet for register contents details.
00149      *
00150      *    7      6           5                 4
00151      * +------+------+--------------+------------------+
00152      * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
00153      * +------+------+--------------+------------------+
00154      *
00155      *   3        2            1       0
00156      * +---+------------+------------+---+
00157      * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
00158      * +---+------------+------------+---+
00159      *
00160      * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
00161      * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
00162      * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
00163      *                          0 = 50us pulse.
00164      * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
00165      *                                      0 = status register read only.
00166      * ITG_RDY_EN Enable interrupt when device is ready,
00167      *            (PLL ready after changing clock source).
00168      * RAW_RDY_EN Enable interrupt when data is available.
00169      * 0 Bits 1 and 3 of the INT_CFG register should be zero.
00170      *
00171      * @return the contents of the INT_CFG register.
00172      */
00173     char getInterruptConfiguration(void);
00174 
00175     /**
00176      * Set the interrupt configuration.
00177      *
00178      * See datasheet for configuration byte details.
00179      *
00180      *    7      6           5                 4
00181      * +------+------+--------------+------------------+
00182      * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
00183      * +------+------+--------------+------------------+
00184      *
00185      *   3        2            1       0
00186      * +---+------------+------------+---+
00187      * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
00188      * +---+------------+------------+---+
00189      *
00190      * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
00191      * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
00192      * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
00193      *                          0 = 50us pulse.
00194      * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
00195      *                                      0 = status register read only.
00196      * ITG_RDY_EN Enable interrupt when device is ready,
00197      *            (PLL ready after changing clock source).
00198      * RAW_RDY_EN Enable interrupt when data is available.
00199      * 0 Bits 1 and 3 of the INT_CFG register should be zero.
00200      *
00201      * @param config Configuration byte to write to INT_CFG register.
00202      */
00203     void setInterruptConfiguration(char config);
00204 
00205     /**
00206      * Check the ITG_RDY bit of the INT_STATUS register.
00207      *
00208      * @return True if the ITG_RDY bit is set, corresponding to PLL ready,
00209      *         false if the ITG_RDY bit is not set, corresponding to PLL not
00210      *         ready.
00211      */
00212     bool isPllReady(void);
00213 
00214     /**
00215      * Check the RAW_DATA_RDY bit of the INT_STATUS register.
00216      *
00217      * @return True if the RAW_DATA_RDY bit is set, corresponding to new data
00218      *         in the sensor registers, false if the RAW_DATA_RDY bit is not
00219      *         set, corresponding to no new data yet in the sensor registers.
00220      */
00221     bool isRawDataReady(void);
00222 
00223     /**
00224      * Get the temperature of the device.
00225      *
00226      * @return The temperature in degrees celsius.
00227      */
00228     float getTemperature(void);
00229 
00230     /**
00231      * Get the output for the x-axis gyroscope.
00232      *
00233      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00234      *
00235      * @return The output on the x-axis in raw ADC counts.
00236      */
00237     int getGyroX(void);
00238 
00239     /**
00240      * Get the output for the y-axis gyroscope.
00241      *
00242      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00243      *
00244      * @return The output on the y-axis in raw ADC counts.
00245      */
00246     int getGyroY(void);
00247 
00248     /**
00249      * Get the output on the z-axis gyroscope.
00250      *
00251      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00252      * 
00253      * @return The output on the z-axis in raw ADC counts.
00254      */
00255     int getGyroZ(void);
00256 
00257     /**
00258      * Get the power management configuration.
00259      *
00260      * See the datasheet for register contents details.
00261      *
00262      *     7        6        5         4
00263      * +---------+-------+---------+---------+
00264      * | H_RESET | SLEEP | STBY_XG | STBY_YG |
00265      * +---------+-------+---------+---------+
00266      *
00267      *      3          2         1          0
00268      * +---------+----------+----------+----------+
00269      * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
00270      * +---------+----------+----------+----------+
00271      *
00272      * H_RESET Reset device and internal registers to the power-up-default settings.
00273      * SLEEP Enable low power sleep mode.
00274      * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
00275      * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
00276      * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
00277      * CLK_SEL Select device clock source:
00278      *
00279      * CLK_SEL | Clock Source
00280      * --------+--------------
00281      *    0      Internal oscillator
00282      *    1      PLL with X Gyro reference
00283      *    2      PLL with Y Gyro reference
00284      *    3      PLL with Z Gyro reference
00285      *    4      PLL with external 32.768kHz reference
00286      *    5      PLL with external 19.2MHz reference
00287      *    6      Reserved
00288      *    7      Reserved
00289      *
00290      * @return The contents of the PWR_MGM register.
00291      */
00292     char getPowerManagement(void);
00293 
00294     /**
00295      * Set power management configuration.
00296      *
00297      * See the datasheet for configuration byte details
00298      *
00299      *      7        6        5         4
00300      * +---------+-------+---------+---------+
00301      * | H_RESET | SLEEP | STBY_XG | STBY_YG |
00302      * +---------+-------+---------+---------+
00303      *
00304      *      3          2         1          0
00305      * +---------+----------+----------+----------+
00306      * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
00307      * +---------+----------+----------+----------+
00308      *
00309      * H_RESET Reset device and internal registers to the power-up-default settings.
00310      * SLEEP Enable low power sleep mode.
00311      * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
00312      * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
00313      * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
00314      * CLK_SEL Select device clock source:
00315      *
00316      * CLK_SEL | Clock Source
00317      * --------+--------------
00318      *    0      Internal oscillator
00319      *    1      PLL with X Gyro reference
00320      *    2      PLL with Y Gyro reference
00321      *    3      PLL with Z Gyro reference
00322      *    4      PLL with external 32.768kHz reference
00323      *    5      PLL with external 19.2MHz reference
00324      *    6      Reserved
00325      *    7      Reserved
00326      *
00327      * @param config The configuration byte to write to the PWR_MGM register.
00328      */
00329     void setPowerManagement(char config);
00330 
00331 private:
00332 
00333     I2C i2c_;
00334 
00335 };
00336 
00337 #endif /* ITG3200_H */