Dependents:   IMU DataLogger AcceptanceTest P4_IMU

Committer:
atommota
Date:
Fri Nov 12 23:40:58 2010 +0000
Revision:
0:03dd788f437f
updated version of ITG3200 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 0:03dd788f437f 1 /**
atommota 0:03dd788f437f 2 * @section LICENSE
atommota 0:03dd788f437f 3 *
atommota 0:03dd788f437f 4 * Copyright (c) 2010 ARM Limited
atommota 0:03dd788f437f 5 *
atommota 0:03dd788f437f 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
atommota 0:03dd788f437f 7 * of this software and associated documentation files (the "Software"), to deal
atommota 0:03dd788f437f 8 * in the Software without restriction, including without limitation the rights
atommota 0:03dd788f437f 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
atommota 0:03dd788f437f 10 * copies of the Software, and to permit persons to whom the Software is
atommota 0:03dd788f437f 11 * furnished to do so, subject to the following conditions:
atommota 0:03dd788f437f 12 *
atommota 0:03dd788f437f 13 * The above copyright notice and this permission notice shall be included in
atommota 0:03dd788f437f 14 * all copies or substantial portions of the Software.
atommota 0:03dd788f437f 15 *
atommota 0:03dd788f437f 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
atommota 0:03dd788f437f 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
atommota 0:03dd788f437f 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
atommota 0:03dd788f437f 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
atommota 0:03dd788f437f 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
atommota 0:03dd788f437f 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
atommota 0:03dd788f437f 22 * THE SOFTWARE.
atommota 0:03dd788f437f 23 *
atommota 0:03dd788f437f 24 * @section DESCRIPTION
atommota 0:03dd788f437f 25 *
atommota 0:03dd788f437f 26 * ITG-3200 triple axis, digital interface, gyroscope.
atommota 0:03dd788f437f 27 *
atommota 0:03dd788f437f 28 * Datasheet:
atommota 0:03dd788f437f 29 *
atommota 0:03dd788f437f 30 * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
atommota 0:03dd788f437f 31 */
atommota 0:03dd788f437f 32
atommota 0:03dd788f437f 33 #ifndef ITG3200_H
atommota 0:03dd788f437f 34 #define ITG3200_H
atommota 0:03dd788f437f 35
atommota 0:03dd788f437f 36 /**
atommota 0:03dd788f437f 37 * Includes
atommota 0:03dd788f437f 38 */
atommota 0:03dd788f437f 39 #include "mbed.h"
atommota 0:03dd788f437f 40
atommota 0:03dd788f437f 41 /**
atommota 0:03dd788f437f 42 * Defines
atommota 0:03dd788f437f 43 */
atommota 0:03dd788f437f 44 #define ITG3200_I2C_ADDRESS 0x68 //7-bit address.
atommota 0:03dd788f437f 45
atommota 0:03dd788f437f 46 //-----------
atommota 0:03dd788f437f 47 // Registers
atommota 0:03dd788f437f 48 //-----------
atommota 0:03dd788f437f 49 #define WHO_AM_I_REG 0x00
atommota 0:03dd788f437f 50 #define SMPLRT_DIV_REG 0x15
atommota 0:03dd788f437f 51 #define DLPF_FS_REG 0x16
atommota 0:03dd788f437f 52 #define INT_CFG_REG 0x17
atommota 0:03dd788f437f 53 #define INT_STATUS 0x1A
atommota 0:03dd788f437f 54 #define TEMP_OUT_H_REG 0x1B
atommota 0:03dd788f437f 55 #define TEMP_OUT_L_REG 0x1C
atommota 0:03dd788f437f 56 #define GYRO_XOUT_H_REG 0x1D
atommota 0:03dd788f437f 57 #define GYRO_XOUT_L_REG 0x1E
atommota 0:03dd788f437f 58 #define GYRO_YOUT_H_REG 0x1F
atommota 0:03dd788f437f 59 #define GYRO_YOUT_L_REG 0x20
atommota 0:03dd788f437f 60 #define GYRO_ZOUT_H_REG 0x21
atommota 0:03dd788f437f 61 #define GYRO_ZOUT_L_REG 0x22
atommota 0:03dd788f437f 62 #define PWR_MGM_REG 0x3E
atommota 0:03dd788f437f 63
atommota 0:03dd788f437f 64 //----------------------------
atommota 0:03dd788f437f 65 // Low Pass Filter Bandwidths
atommota 0:03dd788f437f 66 //----------------------------
atommota 0:03dd788f437f 67 #define LPFBW_256HZ 0x00
atommota 0:03dd788f437f 68 #define LPFBW_188HZ 0x01
atommota 0:03dd788f437f 69 #define LPFBW_98HZ 0x02
atommota 0:03dd788f437f 70 #define LPFBW_42HZ 0x03
atommota 0:03dd788f437f 71 #define LPFBW_20HZ 0x04
atommota 0:03dd788f437f 72 #define LPFBW_10HZ 0x05
atommota 0:03dd788f437f 73 #define LPFBW_5HZ 0x06
atommota 0:03dd788f437f 74
atommota 0:03dd788f437f 75 /**
atommota 0:03dd788f437f 76 * ITG-3200 triple axis digital gyroscope.
atommota 0:03dd788f437f 77 */
atommota 0:03dd788f437f 78 class ITG3200 {
atommota 0:03dd788f437f 79
atommota 0:03dd788f437f 80 public:
atommota 0:03dd788f437f 81
atommota 0:03dd788f437f 82 /**
atommota 0:03dd788f437f 83 * Constructor.
atommota 0:03dd788f437f 84 *
atommota 0:03dd788f437f 85 * Sets FS_SEL to 0x03 for proper opertaion.
atommota 0:03dd788f437f 86 *
atommota 0:03dd788f437f 87 * @param sda - mbed pin to use for the SDA I2C line.
atommota 0:03dd788f437f 88 * @param scl - mbed pin to use for the SCL I2C line.
atommota 0:03dd788f437f 89 */
atommota 0:03dd788f437f 90 ITG3200(PinName sda, PinName scl);
atommota 0:03dd788f437f 91
atommota 0:03dd788f437f 92 /**
atommota 0:03dd788f437f 93 * Get the identity of the device.
atommota 0:03dd788f437f 94 *
atommota 0:03dd788f437f 95 * @return The contents of the Who Am I register which contains the I2C
atommota 0:03dd788f437f 96 * address of the device.
atommota 0:03dd788f437f 97 */
atommota 0:03dd788f437f 98 char getWhoAmI(void);
atommota 0:03dd788f437f 99
atommota 0:03dd788f437f 100 /**
atommota 0:03dd788f437f 101 * Set the address of the device.
atommota 0:03dd788f437f 102 *
atommota 0:03dd788f437f 103 * @param address The I2C slave address to write to the Who Am I register
atommota 0:03dd788f437f 104 * on the device.
atommota 0:03dd788f437f 105 */
atommota 0:03dd788f437f 106 void setWhoAmI(char address);
atommota 0:03dd788f437f 107
atommota 0:03dd788f437f 108 /**
atommota 0:03dd788f437f 109 * Get the sample rate divider.
atommota 0:03dd788f437f 110 *
atommota 0:03dd788f437f 111 * @return The sample rate divider as a number from 0-255.
atommota 0:03dd788f437f 112 */
atommota 0:03dd788f437f 113 char getSampleRateDivider(void);
atommota 0:03dd788f437f 114
atommota 0:03dd788f437f 115 /**
atommota 0:03dd788f437f 116 * Set the sample rate divider.
atommota 0:03dd788f437f 117 *
atommota 0:03dd788f437f 118 * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz,
atommota 0:03dd788f437f 119 * as decidied by the DLPF_FS register.
atommota 0:03dd788f437f 120 *
atommota 0:03dd788f437f 121 * @param The sample rate divider as a number from 0-255.
atommota 0:03dd788f437f 122 */
atommota 0:03dd788f437f 123 void setSampleRateDivider(char divider);
atommota 0:03dd788f437f 124
atommota 0:03dd788f437f 125 /**
atommota 0:03dd788f437f 126 * Get the internal sample rate.
atommota 0:03dd788f437f 127 *
atommota 0:03dd788f437f 128 * @return The internal sample rate in kHz - either 1 or 8.
atommota 0:03dd788f437f 129 */
atommota 0:03dd788f437f 130 int getInternalSampleRate(void);
atommota 0:03dd788f437f 131
atommota 0:03dd788f437f 132 /**
atommota 0:03dd788f437f 133 * Set the low pass filter bandwidth.
atommota 0:03dd788f437f 134 *
atommota 0:03dd788f437f 135 * Also used to set the internal sample rate.
atommota 0:03dd788f437f 136 * Pass the #define bandwidth codes as a parameter.
atommota 0:03dd788f437f 137 *
atommota 0:03dd788f437f 138 * 256Hz -> 8kHz internal sample rate.
atommota 0:03dd788f437f 139 * Everything else -> 1kHz internal rate.
atommota 0:03dd788f437f 140 *
atommota 0:03dd788f437f 141 * @param bandwidth Low pass filter bandwidth code
atommota 0:03dd788f437f 142 */
atommota 0:03dd788f437f 143 void setLpBandwidth(char bandwidth);
atommota 0:03dd788f437f 144
atommota 0:03dd788f437f 145 /**
atommota 0:03dd788f437f 146 * Get the interrupt configuration.
atommota 0:03dd788f437f 147 *
atommota 0:03dd788f437f 148 * See datasheet for register contents details.
atommota 0:03dd788f437f 149 *
atommota 0:03dd788f437f 150 * 7 6 5 4
atommota 0:03dd788f437f 151 * +------+------+--------------+------------------+
atommota 0:03dd788f437f 152 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
atommota 0:03dd788f437f 153 * +------+------+--------------+------------------+
atommota 0:03dd788f437f 154 *
atommota 0:03dd788f437f 155 * 3 2 1 0
atommota 0:03dd788f437f 156 * +---+------------+------------+---+
atommota 0:03dd788f437f 157 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
atommota 0:03dd788f437f 158 * +---+------------+------------+---+
atommota 0:03dd788f437f 159 *
atommota 0:03dd788f437f 160 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
atommota 0:03dd788f437f 161 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
atommota 0:03dd788f437f 162 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
atommota 0:03dd788f437f 163 * 0 = 50us pulse.
atommota 0:03dd788f437f 164 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
atommota 0:03dd788f437f 165 * 0 = status register read only.
atommota 0:03dd788f437f 166 * ITG_RDY_EN Enable interrupt when device is ready,
atommota 0:03dd788f437f 167 * (PLL ready after changing clock source).
atommota 0:03dd788f437f 168 * RAW_RDY_EN Enable interrupt when data is available.
atommota 0:03dd788f437f 169 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
atommota 0:03dd788f437f 170 *
atommota 0:03dd788f437f 171 * @return the contents of the INT_CFG register.
atommota 0:03dd788f437f 172 */
atommota 0:03dd788f437f 173 char getInterruptConfiguration(void);
atommota 0:03dd788f437f 174
atommota 0:03dd788f437f 175 /**
atommota 0:03dd788f437f 176 * Set the interrupt configuration.
atommota 0:03dd788f437f 177 *
atommota 0:03dd788f437f 178 * See datasheet for configuration byte details.
atommota 0:03dd788f437f 179 *
atommota 0:03dd788f437f 180 * 7 6 5 4
atommota 0:03dd788f437f 181 * +------+------+--------------+------------------+
atommota 0:03dd788f437f 182 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
atommota 0:03dd788f437f 183 * +------+------+--------------+------------------+
atommota 0:03dd788f437f 184 *
atommota 0:03dd788f437f 185 * 3 2 1 0
atommota 0:03dd788f437f 186 * +---+------------+------------+---+
atommota 0:03dd788f437f 187 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
atommota 0:03dd788f437f 188 * +---+------------+------------+---+
atommota 0:03dd788f437f 189 *
atommota 0:03dd788f437f 190 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
atommota 0:03dd788f437f 191 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
atommota 0:03dd788f437f 192 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
atommota 0:03dd788f437f 193 * 0 = 50us pulse.
atommota 0:03dd788f437f 194 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
atommota 0:03dd788f437f 195 * 0 = status register read only.
atommota 0:03dd788f437f 196 * ITG_RDY_EN Enable interrupt when device is ready,
atommota 0:03dd788f437f 197 * (PLL ready after changing clock source).
atommota 0:03dd788f437f 198 * RAW_RDY_EN Enable interrupt when data is available.
atommota 0:03dd788f437f 199 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
atommota 0:03dd788f437f 200 *
atommota 0:03dd788f437f 201 * @param config Configuration byte to write to INT_CFG register.
atommota 0:03dd788f437f 202 */
atommota 0:03dd788f437f 203 void setInterruptConfiguration(char config);
atommota 0:03dd788f437f 204
atommota 0:03dd788f437f 205 /**
atommota 0:03dd788f437f 206 * Check the ITG_RDY bit of the INT_STATUS register.
atommota 0:03dd788f437f 207 *
atommota 0:03dd788f437f 208 * @return True if the ITG_RDY bit is set, corresponding to PLL ready,
atommota 0:03dd788f437f 209 * false if the ITG_RDY bit is not set, corresponding to PLL not
atommota 0:03dd788f437f 210 * ready.
atommota 0:03dd788f437f 211 */
atommota 0:03dd788f437f 212 bool isPllReady(void);
atommota 0:03dd788f437f 213
atommota 0:03dd788f437f 214 /**
atommota 0:03dd788f437f 215 * Check the RAW_DATA_RDY bit of the INT_STATUS register.
atommota 0:03dd788f437f 216 *
atommota 0:03dd788f437f 217 * @return True if the RAW_DATA_RDY bit is set, corresponding to new data
atommota 0:03dd788f437f 218 * in the sensor registers, false if the RAW_DATA_RDY bit is not
atommota 0:03dd788f437f 219 * set, corresponding to no new data yet in the sensor registers.
atommota 0:03dd788f437f 220 */
atommota 0:03dd788f437f 221 bool isRawDataReady(void);
atommota 0:03dd788f437f 222
atommota 0:03dd788f437f 223 /**
atommota 0:03dd788f437f 224 * Get the temperature of the device.
atommota 0:03dd788f437f 225 *
atommota 0:03dd788f437f 226 * @return The temperature in degrees celsius.
atommota 0:03dd788f437f 227 */
atommota 0:03dd788f437f 228 float getTemperature(void);
atommota 0:03dd788f437f 229
atommota 0:03dd788f437f 230 /**
atommota 0:03dd788f437f 231 * Get the output for the x-axis gyroscope.
atommota 0:03dd788f437f 232 *
atommota 0:03dd788f437f 233 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:03dd788f437f 234 *
atommota 0:03dd788f437f 235 * @return The output on the x-axis in raw ADC counts.
atommota 0:03dd788f437f 236 */
atommota 0:03dd788f437f 237 int getGyroX(void);
atommota 0:03dd788f437f 238
atommota 0:03dd788f437f 239 /**
atommota 0:03dd788f437f 240 * Get the output for the y-axis gyroscope.
atommota 0:03dd788f437f 241 *
atommota 0:03dd788f437f 242 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:03dd788f437f 243 *
atommota 0:03dd788f437f 244 * @return The output on the y-axis in raw ADC counts.
atommota 0:03dd788f437f 245 */
atommota 0:03dd788f437f 246 int getGyroY(void);
atommota 0:03dd788f437f 247
atommota 0:03dd788f437f 248 /**
atommota 0:03dd788f437f 249 * Get the output on the z-axis gyroscope.
atommota 0:03dd788f437f 250 *
atommota 0:03dd788f437f 251 * Typical sensitivity is 14.375 LSB/(degrees/sec).
atommota 0:03dd788f437f 252 *
atommota 0:03dd788f437f 253 * @return The output on the z-axis in raw ADC counts.
atommota 0:03dd788f437f 254 */
atommota 0:03dd788f437f 255 int getGyroZ(void);
atommota 0:03dd788f437f 256
atommota 0:03dd788f437f 257 /**
atommota 0:03dd788f437f 258 * Get the power management configuration.
atommota 0:03dd788f437f 259 *
atommota 0:03dd788f437f 260 * See the datasheet for register contents details.
atommota 0:03dd788f437f 261 *
atommota 0:03dd788f437f 262 * 7 6 5 4
atommota 0:03dd788f437f 263 * +---------+-------+---------+---------+
atommota 0:03dd788f437f 264 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
atommota 0:03dd788f437f 265 * +---------+-------+---------+---------+
atommota 0:03dd788f437f 266 *
atommota 0:03dd788f437f 267 * 3 2 1 0
atommota 0:03dd788f437f 268 * +---------+----------+----------+----------+
atommota 0:03dd788f437f 269 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
atommota 0:03dd788f437f 270 * +---------+----------+----------+----------+
atommota 0:03dd788f437f 271 *
atommota 0:03dd788f437f 272 * H_RESET Reset device and internal registers to the power-up-default settings.
atommota 0:03dd788f437f 273 * SLEEP Enable low power sleep mode.
atommota 0:03dd788f437f 274 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 275 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 276 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 277 * CLK_SEL Select device clock source:
atommota 0:03dd788f437f 278 *
atommota 0:03dd788f437f 279 * CLK_SEL | Clock Source
atommota 0:03dd788f437f 280 * --------+--------------
atommota 0:03dd788f437f 281 * 0 Internal oscillator
atommota 0:03dd788f437f 282 * 1 PLL with X Gyro reference
atommota 0:03dd788f437f 283 * 2 PLL with Y Gyro reference
atommota 0:03dd788f437f 284 * 3 PLL with Z Gyro reference
atommota 0:03dd788f437f 285 * 4 PLL with external 32.768kHz reference
atommota 0:03dd788f437f 286 * 5 PLL with external 19.2MHz reference
atommota 0:03dd788f437f 287 * 6 Reserved
atommota 0:03dd788f437f 288 * 7 Reserved
atommota 0:03dd788f437f 289 *
atommota 0:03dd788f437f 290 * @return The contents of the PWR_MGM register.
atommota 0:03dd788f437f 291 */
atommota 0:03dd788f437f 292 char getPowerManagement(void);
atommota 0:03dd788f437f 293
atommota 0:03dd788f437f 294 /**
atommota 0:03dd788f437f 295 * Set power management configuration.
atommota 0:03dd788f437f 296 *
atommota 0:03dd788f437f 297 * See the datasheet for configuration byte details
atommota 0:03dd788f437f 298 *
atommota 0:03dd788f437f 299 * 7 6 5 4
atommota 0:03dd788f437f 300 * +---------+-------+---------+---------+
atommota 0:03dd788f437f 301 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
atommota 0:03dd788f437f 302 * +---------+-------+---------+---------+
atommota 0:03dd788f437f 303 *
atommota 0:03dd788f437f 304 * 3 2 1 0
atommota 0:03dd788f437f 305 * +---------+----------+----------+----------+
atommota 0:03dd788f437f 306 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
atommota 0:03dd788f437f 307 * +---------+----------+----------+----------+
atommota 0:03dd788f437f 308 *
atommota 0:03dd788f437f 309 * H_RESET Reset device and internal registers to the power-up-default settings.
atommota 0:03dd788f437f 310 * SLEEP Enable low power sleep mode.
atommota 0:03dd788f437f 311 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 312 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 313 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
atommota 0:03dd788f437f 314 * CLK_SEL Select device clock source:
atommota 0:03dd788f437f 315 *
atommota 0:03dd788f437f 316 * CLK_SEL | Clock Source
atommota 0:03dd788f437f 317 * --------+--------------
atommota 0:03dd788f437f 318 * 0 Internal oscillator
atommota 0:03dd788f437f 319 * 1 PLL with X Gyro reference
atommota 0:03dd788f437f 320 * 2 PLL with Y Gyro reference
atommota 0:03dd788f437f 321 * 3 PLL with Z Gyro reference
atommota 0:03dd788f437f 322 * 4 PLL with external 32.768kHz reference
atommota 0:03dd788f437f 323 * 5 PLL with external 19.2MHz reference
atommota 0:03dd788f437f 324 * 6 Reserved
atommota 0:03dd788f437f 325 * 7 Reserved
atommota 0:03dd788f437f 326 *
atommota 0:03dd788f437f 327 * @param config The configuration byte to write to the PWR_MGM register.
atommota 0:03dd788f437f 328 */
atommota 0:03dd788f437f 329 void setPowerManagement(char config);
atommota 0:03dd788f437f 330
atommota 0:03dd788f437f 331 private:
atommota 0:03dd788f437f 332
atommota 0:03dd788f437f 333 I2C i2c_;
atommota 0:03dd788f437f 334
atommota 0:03dd788f437f 335 };
atommota 0:03dd788f437f 336
atommota 0:03dd788f437f 337 #endif /* ITG3200_H */