L3GD20 & L3G4200D / STMicroelectronics / MEMS motion sensor, three-axis gyroscope library

Dependents:   GR-PEACH_test_wo_rtos GR-PEACH_test_on_rtos_works_well

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers L3GD20.h Source File

L3GD20.h

00001 /*
00002  * mbed library program
00003  *  L3GD20 MEMS motion sensor: 3-axis digital gyroscope, made by STMicroelectronics
00004  *      http://www.st.com/web/catalog/sense_power/FM89/SC1288
00005  *          /PF252443?sc=internet/analog/product/252443.jsp
00006  *  L3G4200D MEMS motion sensor: three-axis digital output gyroscope, made by STMicroelectronics
00007  *      http://www.st.com/web/catalog/sense_power/FM89/SC1288
00008  *          /PF250373?sc=internet/analog/product/250373.jsp
00009  *
00010  * Copyright (c) 2014,'15,'17 Kenji Arai / JH1PJL
00011  *  http://www.page.sannet.ne.jp/kenjia/index.html
00012  *  http://mbed.org/users/kenjiArai/
00013  *      Created: July       13th, 2014
00014  *      Revised: August     23rd, 2017
00015  */
00016 
00017 #ifndef L3GD20_GYRO_H
00018 #define L3GD20_GYRO_H
00019 
00020 #include "mbed.h"
00021 
00022 //  L3G4200DMEMS Address
00023 //  7bit address = 0b110100x(0x68 or 0x69 depends on SA0/SDO)
00024 #define L3G4200D_G_CHIP_ADDR (0x68 << 1)    // SA0(=SDO pin) = Ground
00025 #define L3G4200D_V_CHIP_ADDR (0x69 << 1)    // SA0(=SDO pin) = Vdd
00026 //  L3GD20MEMS Address
00027 //  7bit address = 0b110101x(0x6a or 0x6b depends on SA0/SDO)
00028 #define L3GD20_G_CHIP_ADDR   (0x6a << 1)    // SA0(=SDO pin) = Ground
00029 #define L3GD20_V_CHIP_ADDR   (0x6b << 1)    // SA0(=SDO pin) = Vdd
00030 
00031 //  L3G4200DMEMS ID
00032 #define I_AM_L3G4200D        0xd3
00033 //  L3GD20MEMS ID
00034 #define I_AM_L3GD20          0xd4
00035 
00036 //  Register's definition
00037 #define L3GX_WHO_AM_I        0x0f
00038 #define L3GX_CTRL_REG1       0x20
00039 #define L3GX_CTRL_REG2       0x21
00040 #define L3GX_CTRL_REG3       0x22
00041 #define L3GX_CTRL_REG4       0x23
00042 #define L3GX_CTRL_REG5       0x24
00043 #define L3GX_REFERENCE       0x25
00044 #define L3GX_OUT_TEMP        0x26
00045 #define L3GX_STATUS_REG      0x27
00046 #define L3GX_OUT_X_L         0x28
00047 #define L3GX_OUT_X_H         0x29
00048 #define L3GX_OUT_Y_L         0x2a
00049 #define L3GX_OUT_Y_H         0x2b
00050 #define L3GX_OUT_Z_L         0x2c
00051 #define L3GX_OUT_Z_H         0x2d
00052 #define L3GX_FIFO_CTRL_REG   0x2e
00053 #define L3GX_FIFO_SRC_REG    0x2f
00054 #define L3GX_INT1_CFG        0x30
00055 #define L3GX_INT1_SRC        0x31
00056 #define L3GX_INT1_TSH_XH     0x32
00057 #define L3GX_INT1_TSH_XL     0x33
00058 #define L3GX_INT1_TSH_YH     0x34
00059 #define L3GX_INT1_TSH_YL     0x35
00060 #define L3GX_INT1_TSH_ZH     0x36
00061 #define L3GX_INT1_TSH_ZL     0x37
00062 #define L3GX_INT1_DURATION   0x38
00063 
00064 // Output Data Rate (ODR)
00065 //      L3G4200DMEMS
00066 #define L3GX_DR_100HZ        0
00067 #define L3GX_DR_200HZ        1
00068 #define L3GX_DR_400HZ        2
00069 #define L3GX_DR_800HZ        3
00070 //      L3GD20MEMS
00071 #define L3GX_DR_95HZ         0
00072 #define L3GX_DR_190HZ        1
00073 #define L3GX_DR_380HZ        2
00074 #define L3GX_DR_760HZ        3
00075 
00076 // Bandwidth (Low pass)
00077 #define L3GX_BW_LOW          0
00078 #define L3GX_BW_M_LOW        1
00079 #define L3GX_BW_M_HI         2
00080 #define L3GX_BW_HI           3
00081 
00082 // Power-down mode enable/disable
00083 #define L3GX_PD_EN           0
00084 #define L3GX_PD_DIS          1
00085 
00086 // Axis control
00087 #define L3GX_X_EN            1
00088 #define L3GX_X_DIS           0
00089 #define L3GX_Y_EN            1
00090 #define L3GX_Y_DIS           0
00091 #define L3GX_Z_EN            1
00092 #define L3GX_Z_DIS           0
00093 
00094 // Full Scale
00095 #define L3GX_FS_250DPS       0
00096 #define L3GX_FS_500DPS       1
00097 #define L3GX_FS_2000DPS      2
00098 
00099 //Convert from degrees to radians.
00100 #define toRadians(x) (x * 0.01745329252)
00101 //Convert from radians to degrees.
00102 #define toDegrees(x) (x * 57.2957795)
00103 
00104 /** Interface for STMicronics MEMS motion sensor: 3-axis digital gyroscope
00105  *      Chip: L3GD20 (new one) and L3G4200 (old one)
00106  *
00107  * @code
00108  * #include "mbed.h"
00109  *
00110  * // I2C Communication
00111  * L3GX_GYRO gyro(p_sda, p_scl, chip_addr, datarate, bandwidth, fullscale);
00112  * // If you connected I2C line not only this device but also other devices,
00113  * //     you need to declare following method.
00114  * I2C i2c(dp5,dp27);              // SDA, SCL
00115  * L3GX_GYRO gyro(i2c, chip_addr, datarate, bandwidth, fullscale);
00116  *
00117  * int main() {
00118  * float f[3];
00119  * int8_t t;
00120  *
00121  *   if (gyro.read_id() == I_AM_L3G4200D){
00122  *      t = gyro.read_temp();
00123  *      gyro.read_data(f);
00124  *   }
00125  * }
00126  * @endcode
00127  */
00128 
00129 class L3GX_GYRO
00130 {
00131 public:
00132     /** Configure data pin
00133       * @param data SDA and SCL pins
00134       * @param device address L3G4200D(SA0=0 or 1) or L3GD20(SA0=0 or 1)
00135       * @param ->L3G4200D_G_CHIP_ADDR to L3GD20_V_CHIP_ADDR
00136       * @param output data rate selection, DR_100HZ/DR_95HZ to DR_800HZ/DR_760HZ
00137       * @param bandwidth selection, BW_LOW to BW_HI
00138       * @param full scale selection, FS_250DPS, FS_500DPS, FS_2000DPS
00139       */
00140     L3GX_GYRO(PinName p_sda, PinName p_scl,
00141               uint8_t addr, uint8_t data_rate, uint8_t bandwidth, uint8_t fullscale);
00142 
00143     /** Configure data pin
00144       * @param data SDA and SCL pins
00145       * @param device address L3G4200D(SA0=0 or 1) or L3GD20(SA0=0 or 1)
00146       * @param ->L3G4200D_G_CHIP_ADDR to L3GD20_V_CHIP_ADDR
00147       * @default output data rate selection = DR_100HZ/DR_95HZ
00148       * @default bandwidth selection = BW_HI
00149       * @default full scale selection = FS_250DPS
00150       */
00151     L3GX_GYRO(PinName p_sda, PinName p_scl, uint8_t addr);
00152 
00153     /** Configure data pin (with other devices on I2C line)
00154       * @param I2C previous definition
00155       * @param other parameters -> please see L3GX_GYRO(PinName p_sda, PinName p_scl,...)
00156       */
00157     L3GX_GYRO(I2C& p_i2c,
00158               uint8_t addr, uint8_t data_rate, uint8_t bandwidth, uint8_t fullscale);
00159 
00160     /** Configure data pin (with other devices on I2C line)
00161       * @param I2C previous definition
00162       * @param other parameters -> please see L3GX_GYRO(PinName p_sda, PinName p_scl,...)
00163       * @default output data rate selection = DR_100HZ/DR_95HZ
00164       * @default bandwidth selection = BW_HI
00165       * @default full scale selection = FS_250DPS
00166       */
00167     L3GX_GYRO(I2C& p_i2c, uint8_t addr);
00168 
00169     /** Read a tow's complemet type data from Gyro
00170       * @param none
00171       * @return temperature unit:degreeC(Celsius)
00172       */
00173     int8_t read_temp();
00174 
00175     /** Read a float type data from Gyro
00176       * @param float type of three arry's address, e.g. float dt_usr[3];
00177       * @return Gyro motion data unit in param array:dps(degree per second)
00178       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00179       */
00180     void read_data(float *dt_usr);
00181 
00182     /** Read a Gyro ID number
00183       * @param none
00184       * @return if STM MEMS Gyro, it should be I_AM_L3G4200D(0xd3) or I_AM_L3GD20(0xd4)
00185       */
00186     uint8_t read_id();
00187 
00188     /** Read Data Ready flag
00189       * @param none
00190       * @return 1 = Ready
00191       */
00192     uint8_t data_ready();
00193 
00194     /** Set I2C clock frequency
00195       * @param freq.
00196       * @return none
00197       */
00198     void frequency(int hz);
00199 
00200     /** Read register (general purpose)
00201       * @param register's address
00202       * @return register data
00203       */
00204     uint8_t read_reg(uint8_t addr);
00205 
00206     /** Write register (general purpose)
00207       * @param register's address
00208       * @param data
00209       * @return none
00210       */
00211     void write_reg(uint8_t addr, uint8_t data);
00212 
00213 protected:
00214     void initialize(uint8_t, uint8_t, uint8_t, uint8_t);
00215 
00216     I2C *_i2c_p;
00217     I2C &_i2c;
00218 
00219 private:
00220     float   fs_factor;  // full scale factor
00221     char    dt[2];      // working buffer
00222     uint8_t gyro_addr;  // gyro sensor address
00223     uint8_t gyro_id;    // gyro ID
00224     uint8_t gyro_ready; // gyro is on I2C line = 1, not = 0
00225 };
00226 
00227 #endif      // L3GD20_GYRO_H
00228