Dependencies:   mbed

Committer:
higedura
Date:
Thu Mar 15 10:41:22 2012 +0000
Revision:
11:b32b3d6be8d2
Parent:
0:b61f317f452e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
higedura 0:b61f317f452e 1 /**
higedura 0:b61f317f452e 2 * @author Aaron Berk
higedura 0:b61f317f452e 3 *
higedura 0:b61f317f452e 4 * @section LICENSE
higedura 0:b61f317f452e 5 *
higedura 0:b61f317f452e 6 * Copyright (c) 2010 ARM Limited
higedura 0:b61f317f452e 7 *
higedura 0:b61f317f452e 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
higedura 0:b61f317f452e 9 * of this software and associated documentation files (the "Software"), to deal
higedura 0:b61f317f452e 10 * in the Software without restriction, including without limitation the rights
higedura 0:b61f317f452e 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
higedura 0:b61f317f452e 12 * copies of the Software, and to permit persons to whom the Software is
higedura 0:b61f317f452e 13 * furnished to do so, subject to the following conditions:
higedura 0:b61f317f452e 14 *
higedura 0:b61f317f452e 15 * The above copyright notice and this permission notice shall be included in
higedura 0:b61f317f452e 16 * all copies or substantial portions of the Software.
higedura 0:b61f317f452e 17 *
higedura 0:b61f317f452e 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
higedura 0:b61f317f452e 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
higedura 0:b61f317f452e 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
higedura 0:b61f317f452e 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
higedura 0:b61f317f452e 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
higedura 0:b61f317f452e 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
higedura 0:b61f317f452e 24 * THE SOFTWARE.
higedura 0:b61f317f452e 25 *
higedura 0:b61f317f452e 26 * @section DESCRIPTION
higedura 0:b61f317f452e 27 *
higedura 0:b61f317f452e 28 * ITG-3200 triple axis, digital interface, gyroscope.
higedura 0:b61f317f452e 29 *
higedura 0:b61f317f452e 30 * Datasheet:
higedura 0:b61f317f452e 31 *
higedura 0:b61f317f452e 32 * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
higedura 0:b61f317f452e 33 */
higedura 0:b61f317f452e 34
higedura 0:b61f317f452e 35 /**
higedura 0:b61f317f452e 36 * Includes
higedura 0:b61f317f452e 37 */
higedura 0:b61f317f452e 38 #include "ITG3200.h"
higedura 0:b61f317f452e 39
higedura 0:b61f317f452e 40 ITG3200::ITG3200(PinName sda, PinName scl) : i2c_(sda, scl) {
higedura 0:b61f317f452e 41
higedura 0:b61f317f452e 42 //400kHz, fast mode.
higedura 0:b61f317f452e 43 i2c_.frequency(400000);
higedura 0:b61f317f452e 44
higedura 0:b61f317f452e 45 //Set FS_SEL to 0x03 for proper operation.
higedura 0:b61f317f452e 46 //See datasheet for details.
higedura 0:b61f317f452e 47 char tx[2];
higedura 0:b61f317f452e 48 tx[0] = DLPF_FS_REG;
higedura 0:b61f317f452e 49 //FS_SEL bits sit in bits 4 and 3 of DLPF_FS register.
higedura 0:b61f317f452e 50 tx[1] = 0x03 << 3;
higedura 0:b61f317f452e 51
higedura 0:b61f317f452e 52 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 53
higedura 0:b61f317f452e 54 }
higedura 0:b61f317f452e 55
higedura 0:b61f317f452e 56 char ITG3200::getWhoAmI(void){
higedura 0:b61f317f452e 57
higedura 0:b61f317f452e 58 //WhoAmI Register address.
higedura 0:b61f317f452e 59 char tx = WHO_AM_I_REG;
higedura 0:b61f317f452e 60 char rx;
higedura 0:b61f317f452e 61
higedura 0:b61f317f452e 62 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 63
higedura 0:b61f317f452e 64 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 65
higedura 0:b61f317f452e 66 return rx;
higedura 0:b61f317f452e 67
higedura 0:b61f317f452e 68 }
higedura 0:b61f317f452e 69
higedura 0:b61f317f452e 70 void ITG3200::setWhoAmI(char address){
higedura 0:b61f317f452e 71
higedura 0:b61f317f452e 72 char tx[2];
higedura 0:b61f317f452e 73 tx[0] = WHO_AM_I_REG;
higedura 0:b61f317f452e 74 tx[1] = address;
higedura 0:b61f317f452e 75
higedura 0:b61f317f452e 76 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 77
higedura 0:b61f317f452e 78 }
higedura 0:b61f317f452e 79
higedura 0:b61f317f452e 80 char ITG3200::getSampleRateDivider(void){
higedura 0:b61f317f452e 81
higedura 0:b61f317f452e 82 char tx = SMPLRT_DIV_REG;
higedura 0:b61f317f452e 83 char rx;
higedura 0:b61f317f452e 84
higedura 0:b61f317f452e 85 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 86
higedura 0:b61f317f452e 87 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 88
higedura 0:b61f317f452e 89 return rx;
higedura 0:b61f317f452e 90
higedura 0:b61f317f452e 91 }
higedura 0:b61f317f452e 92
higedura 0:b61f317f452e 93 void ITG3200::setSampleRateDivider(char divider){
higedura 0:b61f317f452e 94
higedura 0:b61f317f452e 95 char tx[2];
higedura 0:b61f317f452e 96 tx[0] = SMPLRT_DIV_REG;
higedura 0:b61f317f452e 97 tx[1] = divider;
higedura 0:b61f317f452e 98
higedura 0:b61f317f452e 99 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 100
higedura 0:b61f317f452e 101 }
higedura 0:b61f317f452e 102
higedura 0:b61f317f452e 103 int ITG3200::getInternalSampleRate(void){
higedura 0:b61f317f452e 104
higedura 0:b61f317f452e 105 char tx = DLPF_FS_REG;
higedura 0:b61f317f452e 106 char rx;
higedura 0:b61f317f452e 107
higedura 0:b61f317f452e 108 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 109
higedura 0:b61f317f452e 110 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 111
higedura 0:b61f317f452e 112 //DLPF_CFG == 0 -> sample rate = 8kHz.
higedura 0:b61f317f452e 113 if(rx == 0){
higedura 0:b61f317f452e 114 return 8;
higedura 0:b61f317f452e 115 }
higedura 0:b61f317f452e 116 //DLPF_CFG = 1..7 -> sample rate = 1kHz.
higedura 0:b61f317f452e 117 else if(rx >= 1 && rx <= 7){
higedura 0:b61f317f452e 118 return 1;
higedura 0:b61f317f452e 119 }
higedura 0:b61f317f452e 120 //DLPF_CFG = anything else -> something's wrong!
higedura 0:b61f317f452e 121 else{
higedura 0:b61f317f452e 122 return -1;
higedura 0:b61f317f452e 123 }
higedura 0:b61f317f452e 124
higedura 0:b61f317f452e 125 }
higedura 0:b61f317f452e 126
higedura 0:b61f317f452e 127 void ITG3200::setLpBandwidth(char bandwidth){
higedura 0:b61f317f452e 128
higedura 0:b61f317f452e 129 char tx[2];
higedura 0:b61f317f452e 130 tx[0] = DLPF_FS_REG;
higedura 0:b61f317f452e 131 //Bits 4,3 are required to be 0x03 for proper operation.
higedura 0:b61f317f452e 132 tx[1] = bandwidth | (0x03 << 3);
higedura 0:b61f317f452e 133
higedura 0:b61f317f452e 134 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 135
higedura 0:b61f317f452e 136 }
higedura 0:b61f317f452e 137
higedura 0:b61f317f452e 138 char ITG3200::getInterruptConfiguration(void){
higedura 0:b61f317f452e 139
higedura 0:b61f317f452e 140 char tx = INT_CFG_REG;
higedura 0:b61f317f452e 141 char rx;
higedura 0:b61f317f452e 142
higedura 0:b61f317f452e 143 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 144
higedura 0:b61f317f452e 145 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 146
higedura 0:b61f317f452e 147 return rx;
higedura 0:b61f317f452e 148
higedura 0:b61f317f452e 149 }
higedura 0:b61f317f452e 150
higedura 0:b61f317f452e 151 void ITG3200::setInterruptConfiguration(char config){
higedura 0:b61f317f452e 152
higedura 0:b61f317f452e 153 char tx[2];
higedura 0:b61f317f452e 154 tx[0] = INT_CFG_REG;
higedura 0:b61f317f452e 155 tx[1] = config;
higedura 0:b61f317f452e 156
higedura 0:b61f317f452e 157 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 158
higedura 0:b61f317f452e 159 }
higedura 0:b61f317f452e 160
higedura 0:b61f317f452e 161 bool ITG3200::isPllReady(void){
higedura 0:b61f317f452e 162
higedura 0:b61f317f452e 163 char tx = INT_STATUS;
higedura 0:b61f317f452e 164 char rx;
higedura 0:b61f317f452e 165
higedura 0:b61f317f452e 166 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 167
higedura 0:b61f317f452e 168 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 169
higedura 0:b61f317f452e 170 //ITG_RDY bit is bit 4 of INT_STATUS register.
higedura 0:b61f317f452e 171 if(rx & 0x04){
higedura 0:b61f317f452e 172 return true;
higedura 0:b61f317f452e 173 }
higedura 0:b61f317f452e 174 else{
higedura 0:b61f317f452e 175 return false;
higedura 0:b61f317f452e 176 }
higedura 0:b61f317f452e 177
higedura 0:b61f317f452e 178 }
higedura 0:b61f317f452e 179
higedura 0:b61f317f452e 180 bool ITG3200::isRawDataReady(void){
higedura 0:b61f317f452e 181
higedura 0:b61f317f452e 182 char tx = INT_STATUS;
higedura 0:b61f317f452e 183 char rx;
higedura 0:b61f317f452e 184
higedura 0:b61f317f452e 185 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 186
higedura 0:b61f317f452e 187 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 188
higedura 0:b61f317f452e 189 //RAW_DATA_RDY bit is bit 1 of INT_STATUS register.
higedura 0:b61f317f452e 190 if(rx & 0x01){
higedura 0:b61f317f452e 191 return true;
higedura 0:b61f317f452e 192 }
higedura 0:b61f317f452e 193 else{
higedura 0:b61f317f452e 194 return false;
higedura 0:b61f317f452e 195 }
higedura 0:b61f317f452e 196
higedura 0:b61f317f452e 197 }
higedura 0:b61f317f452e 198
higedura 0:b61f317f452e 199 float ITG3200::getTemperature(void){
higedura 0:b61f317f452e 200
higedura 0:b61f317f452e 201 char tx = TEMP_OUT_H_REG;
higedura 0:b61f317f452e 202 char rx[2];
higedura 0:b61f317f452e 203
higedura 0:b61f317f452e 204 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 205
higedura 0:b61f317f452e 206 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
higedura 0:b61f317f452e 207
higedura 0:b61f317f452e 208 int16_t temperature = ((int) rx[0] << 8) | ((int) rx[1]);
higedura 0:b61f317f452e 209 //Offset = -35 degrees, 13200 counts. 280 counts/degrees C.
higedura 0:b61f317f452e 210 return 35.0 + ((temperature + 13200)/280.0);
higedura 0:b61f317f452e 211
higedura 0:b61f317f452e 212 }
higedura 0:b61f317f452e 213
higedura 0:b61f317f452e 214 int ITG3200::getGyroX(void){
higedura 0:b61f317f452e 215
higedura 0:b61f317f452e 216 char tx = GYRO_XOUT_H_REG;
higedura 0:b61f317f452e 217 char rx[2];
higedura 0:b61f317f452e 218
higedura 0:b61f317f452e 219 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 220
higedura 0:b61f317f452e 221 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
higedura 0:b61f317f452e 222
higedura 0:b61f317f452e 223 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
higedura 0:b61f317f452e 224
higedura 0:b61f317f452e 225 return output;
higedura 0:b61f317f452e 226
higedura 0:b61f317f452e 227 }
higedura 0:b61f317f452e 228
higedura 0:b61f317f452e 229 int ITG3200::getGyroY(void){
higedura 0:b61f317f452e 230
higedura 0:b61f317f452e 231 char tx = GYRO_YOUT_H_REG;
higedura 0:b61f317f452e 232 char rx[2];
higedura 0:b61f317f452e 233
higedura 0:b61f317f452e 234 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 235
higedura 0:b61f317f452e 236 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
higedura 0:b61f317f452e 237
higedura 0:b61f317f452e 238 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
higedura 0:b61f317f452e 239
higedura 0:b61f317f452e 240 return output;
higedura 0:b61f317f452e 241
higedura 0:b61f317f452e 242 }
higedura 0:b61f317f452e 243
higedura 0:b61f317f452e 244 int ITG3200::getGyroZ(void){
higedura 0:b61f317f452e 245
higedura 0:b61f317f452e 246 char tx = GYRO_ZOUT_H_REG;
higedura 0:b61f317f452e 247 char rx[2];
higedura 0:b61f317f452e 248
higedura 0:b61f317f452e 249 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 250
higedura 0:b61f317f452e 251 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, rx, 2);
higedura 0:b61f317f452e 252
higedura 0:b61f317f452e 253 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
higedura 0:b61f317f452e 254
higedura 0:b61f317f452e 255 return output;
higedura 0:b61f317f452e 256
higedura 0:b61f317f452e 257 }
higedura 0:b61f317f452e 258
higedura 0:b61f317f452e 259 char ITG3200::getPowerManagement(void){
higedura 0:b61f317f452e 260
higedura 0:b61f317f452e 261 char tx = PWR_MGM_REG;
higedura 0:b61f317f452e 262 char rx;
higedura 0:b61f317f452e 263
higedura 0:b61f317f452e 264 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
higedura 0:b61f317f452e 265
higedura 0:b61f317f452e 266 i2c_.read((ITG3200_I2C_ADDRESS << 1) | 0x01, &rx, 1);
higedura 0:b61f317f452e 267
higedura 0:b61f317f452e 268 return rx;
higedura 0:b61f317f452e 269
higedura 0:b61f317f452e 270 }
higedura 0:b61f317f452e 271
higedura 0:b61f317f452e 272 void ITG3200::setPowerManagement(char config){
higedura 0:b61f317f452e 273
higedura 0:b61f317f452e 274 char tx[2];
higedura 0:b61f317f452e 275 tx[0] = PWR_MGM_REG;
higedura 0:b61f317f452e 276 tx[1] = config;
higedura 0:b61f317f452e 277
higedura 0:b61f317f452e 278 i2c_.write((ITG3200_I2C_ADDRESS << 1) & 0xFE, tx, 2);
higedura 0:b61f317f452e 279
higedura 0:b61f317f452e 280 }