test

Dependencies:   mbed

Committer:
ryosuke
Date:
Fri Jun 21 11:08:49 2013 +0000
Revision:
0:d1f69bf9cfa4
test

Who changed what in which revision?

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