TEST
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Drivers/BMI160/bmi160.cpp@1:f60eafbf009a, 2019-04-10 (annotated)
- Committer:
- gmehmet
- Date:
- Wed Apr 10 14:56:25 2019 +0300
- Revision:
- 1:f60eafbf009a
upload from local
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmehmet | 1:f60eafbf009a | 1 | /********************************************************************** |
gmehmet | 1:f60eafbf009a | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
gmehmet | 1:f60eafbf009a | 3 | * |
gmehmet | 1:f60eafbf009a | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
gmehmet | 1:f60eafbf009a | 5 | * copy of this software and associated documentation files (the "Software"), |
gmehmet | 1:f60eafbf009a | 6 | * to deal in the Software without restriction, including without limitation |
gmehmet | 1:f60eafbf009a | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
gmehmet | 1:f60eafbf009a | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
gmehmet | 1:f60eafbf009a | 9 | * Software is furnished to do so, subject to the following conditions: |
gmehmet | 1:f60eafbf009a | 10 | * |
gmehmet | 1:f60eafbf009a | 11 | * The above copyright notice and this permission notice shall be included |
gmehmet | 1:f60eafbf009a | 12 | * in all copies or substantial portions of the Software. |
gmehmet | 1:f60eafbf009a | 13 | * |
gmehmet | 1:f60eafbf009a | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
gmehmet | 1:f60eafbf009a | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
gmehmet | 1:f60eafbf009a | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
gmehmet | 1:f60eafbf009a | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
gmehmet | 1:f60eafbf009a | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
gmehmet | 1:f60eafbf009a | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
gmehmet | 1:f60eafbf009a | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
gmehmet | 1:f60eafbf009a | 21 | * |
gmehmet | 1:f60eafbf009a | 22 | * Except as contained in this notice, the name of Maxim Integrated |
gmehmet | 1:f60eafbf009a | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
gmehmet | 1:f60eafbf009a | 24 | * Products, Inc. Branding Policy. |
gmehmet | 1:f60eafbf009a | 25 | * |
gmehmet | 1:f60eafbf009a | 26 | * The mere transfer of this software does not imply any licenses |
gmehmet | 1:f60eafbf009a | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
gmehmet | 1:f60eafbf009a | 28 | * trademarks, maskwork rights, or any other form of intellectual |
gmehmet | 1:f60eafbf009a | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
gmehmet | 1:f60eafbf009a | 30 | * ownership rights. |
gmehmet | 1:f60eafbf009a | 31 | **********************************************************************/ |
gmehmet | 1:f60eafbf009a | 32 | |
gmehmet | 1:f60eafbf009a | 33 | |
gmehmet | 1:f60eafbf009a | 34 | #include "bmi160.h" |
gmehmet | 1:f60eafbf009a | 35 | |
gmehmet | 1:f60eafbf009a | 36 | |
gmehmet | 1:f60eafbf009a | 37 | const struct BMI160::AccConfig BMI160::DEFAULT_ACC_CONFIG = {SENS_2G, |
gmehmet | 1:f60eafbf009a | 38 | ACC_US_OFF, |
gmehmet | 1:f60eafbf009a | 39 | ACC_BWP_2, |
gmehmet | 1:f60eafbf009a | 40 | ACC_ODR_8}; |
gmehmet | 1:f60eafbf009a | 41 | |
gmehmet | 1:f60eafbf009a | 42 | const struct BMI160::GyroConfig BMI160::DEFAULT_GYRO_CONFIG = {DPS_2000, |
gmehmet | 1:f60eafbf009a | 43 | GYRO_BWP_2, |
gmehmet | 1:f60eafbf009a | 44 | GYRO_ODR_8}; |
gmehmet | 1:f60eafbf009a | 45 | |
gmehmet | 1:f60eafbf009a | 46 | |
gmehmet | 1:f60eafbf009a | 47 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 48 | int32_t BMI160::setSensorPowerMode(Sensors sensor, PowerModes pwrMode) |
gmehmet | 1:f60eafbf009a | 49 | { |
gmehmet | 1:f60eafbf009a | 50 | int32_t rtnVal = -1; |
gmehmet | 1:f60eafbf009a | 51 | |
gmehmet | 1:f60eafbf009a | 52 | switch(sensor) |
gmehmet | 1:f60eafbf009a | 53 | { |
gmehmet | 1:f60eafbf009a | 54 | case MAG: |
gmehmet | 1:f60eafbf009a | 55 | rtnVal = writeRegister(CMD, (MAG_SET_PMU_MODE | pwrMode)); |
gmehmet | 1:f60eafbf009a | 56 | break; |
gmehmet | 1:f60eafbf009a | 57 | |
gmehmet | 1:f60eafbf009a | 58 | case GYRO: |
gmehmet | 1:f60eafbf009a | 59 | rtnVal = writeRegister(CMD, (GYR_SET_PMU_MODE | pwrMode)); |
gmehmet | 1:f60eafbf009a | 60 | break; |
gmehmet | 1:f60eafbf009a | 61 | |
gmehmet | 1:f60eafbf009a | 62 | case ACC: |
gmehmet | 1:f60eafbf009a | 63 | rtnVal = writeRegister(CMD, (ACC_SET_PMU_MODE | pwrMode)); |
gmehmet | 1:f60eafbf009a | 64 | break; |
gmehmet | 1:f60eafbf009a | 65 | |
gmehmet | 1:f60eafbf009a | 66 | default: |
gmehmet | 1:f60eafbf009a | 67 | rtnVal = -1; |
gmehmet | 1:f60eafbf009a | 68 | break; |
gmehmet | 1:f60eafbf009a | 69 | } |
gmehmet | 1:f60eafbf009a | 70 | |
gmehmet | 1:f60eafbf009a | 71 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 72 | } |
gmehmet | 1:f60eafbf009a | 73 | |
gmehmet | 1:f60eafbf009a | 74 | |
gmehmet | 1:f60eafbf009a | 75 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 76 | int32_t BMI160::setSensorConfig(const AccConfig &config) |
gmehmet | 1:f60eafbf009a | 77 | { |
gmehmet | 1:f60eafbf009a | 78 | uint8_t data[2]; |
gmehmet | 1:f60eafbf009a | 79 | |
gmehmet | 1:f60eafbf009a | 80 | data[0] = ((config.us << ACC_US_POS) | (config.bwp << ACC_BWP_POS) | |
gmehmet | 1:f60eafbf009a | 81 | (config.odr << ACC_ODR_POS)); |
gmehmet | 1:f60eafbf009a | 82 | data[1] = config.range; |
gmehmet | 1:f60eafbf009a | 83 | |
gmehmet | 1:f60eafbf009a | 84 | return writeBlock(ACC_CONF, ACC_RANGE, data); |
gmehmet | 1:f60eafbf009a | 85 | } |
gmehmet | 1:f60eafbf009a | 86 | |
gmehmet | 1:f60eafbf009a | 87 | |
gmehmet | 1:f60eafbf009a | 88 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 89 | int32_t BMI160::setSensorConfig(const GyroConfig &config) |
gmehmet | 1:f60eafbf009a | 90 | { |
gmehmet | 1:f60eafbf009a | 91 | uint8_t data[2]; |
gmehmet | 1:f60eafbf009a | 92 | |
gmehmet | 1:f60eafbf009a | 93 | data[0] = ((config.bwp << GYRO_BWP_POS) | (config.odr << GYRO_ODR_POS)); |
gmehmet | 1:f60eafbf009a | 94 | data[1] = config.range; |
gmehmet | 1:f60eafbf009a | 95 | |
gmehmet | 1:f60eafbf009a | 96 | return writeBlock(GYR_CONF, GYR_RANGE, data); |
gmehmet | 1:f60eafbf009a | 97 | } |
gmehmet | 1:f60eafbf009a | 98 | |
gmehmet | 1:f60eafbf009a | 99 | |
gmehmet | 1:f60eafbf009a | 100 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 101 | int32_t BMI160::getSensorConfig(AccConfig &config) |
gmehmet | 1:f60eafbf009a | 102 | { |
gmehmet | 1:f60eafbf009a | 103 | uint8_t data[2]; |
gmehmet | 1:f60eafbf009a | 104 | int32_t rtnVal = readBlock(ACC_CONF, ACC_RANGE, data); |
gmehmet | 1:f60eafbf009a | 105 | |
gmehmet | 1:f60eafbf009a | 106 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 107 | { |
gmehmet | 1:f60eafbf009a | 108 | config.range = static_cast<BMI160::AccRange>( |
gmehmet | 1:f60eafbf009a | 109 | (data[1] & ACC_RANGE_MASK)); |
gmehmet | 1:f60eafbf009a | 110 | config.us = static_cast<BMI160::AccUnderSampling>( |
gmehmet | 1:f60eafbf009a | 111 | ((data[0] & ACC_US_MASK) >> ACC_US_POS)); |
gmehmet | 1:f60eafbf009a | 112 | config.bwp = static_cast<BMI160::AccBandWidthParam>( |
gmehmet | 1:f60eafbf009a | 113 | ((data[0] & ACC_BWP_MASK) >> ACC_BWP_POS)); |
gmehmet | 1:f60eafbf009a | 114 | config.odr = static_cast<BMI160::AccOutputDataRate>( |
gmehmet | 1:f60eafbf009a | 115 | ((data[0] & ACC_ODR_MASK) >> ACC_ODR_POS)); |
gmehmet | 1:f60eafbf009a | 116 | } |
gmehmet | 1:f60eafbf009a | 117 | |
gmehmet | 1:f60eafbf009a | 118 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 119 | } |
gmehmet | 1:f60eafbf009a | 120 | |
gmehmet | 1:f60eafbf009a | 121 | |
gmehmet | 1:f60eafbf009a | 122 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 123 | int32_t BMI160::getSensorConfig(GyroConfig &config) |
gmehmet | 1:f60eafbf009a | 124 | { |
gmehmet | 1:f60eafbf009a | 125 | uint8_t data[2]; |
gmehmet | 1:f60eafbf009a | 126 | int32_t rtnVal = readBlock(GYR_CONF, GYR_RANGE, data); |
gmehmet | 1:f60eafbf009a | 127 | |
gmehmet | 1:f60eafbf009a | 128 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 129 | { |
gmehmet | 1:f60eafbf009a | 130 | config.range = static_cast<BMI160::GyroRange>( |
gmehmet | 1:f60eafbf009a | 131 | (data[1] & GYRO_RANGE_MASK)); |
gmehmet | 1:f60eafbf009a | 132 | config.bwp = static_cast<BMI160::GyroBandWidthParam>( |
gmehmet | 1:f60eafbf009a | 133 | ((data[0] & GYRO_BWP_MASK) >> GYRO_BWP_POS)); |
gmehmet | 1:f60eafbf009a | 134 | config.odr = static_cast<BMI160::GyroOutputDataRate>( |
gmehmet | 1:f60eafbf009a | 135 | ((data[0] & GYRO_ODR_MASK) >> GYRO_ODR_POS)); |
gmehmet | 1:f60eafbf009a | 136 | } |
gmehmet | 1:f60eafbf009a | 137 | |
gmehmet | 1:f60eafbf009a | 138 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 139 | } |
gmehmet | 1:f60eafbf009a | 140 | |
gmehmet | 1:f60eafbf009a | 141 | |
gmehmet | 1:f60eafbf009a | 142 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 143 | int32_t BMI160::getSensorAxis(SensorAxis axis, AxisData &data, AccRange range) |
gmehmet | 1:f60eafbf009a | 144 | { |
gmehmet | 1:f60eafbf009a | 145 | uint8_t localData[2]; |
gmehmet | 1:f60eafbf009a | 146 | int32_t rtnVal; |
gmehmet | 1:f60eafbf009a | 147 | |
gmehmet | 1:f60eafbf009a | 148 | switch(axis) |
gmehmet | 1:f60eafbf009a | 149 | { |
gmehmet | 1:f60eafbf009a | 150 | case X_AXIS: |
gmehmet | 1:f60eafbf009a | 151 | rtnVal = readBlock(DATA_14, DATA_15, localData); |
gmehmet | 1:f60eafbf009a | 152 | break; |
gmehmet | 1:f60eafbf009a | 153 | |
gmehmet | 1:f60eafbf009a | 154 | case Y_AXIS: |
gmehmet | 1:f60eafbf009a | 155 | rtnVal = readBlock(DATA_16, DATA_17, localData); |
gmehmet | 1:f60eafbf009a | 156 | break; |
gmehmet | 1:f60eafbf009a | 157 | |
gmehmet | 1:f60eafbf009a | 158 | case Z_AXIS: |
gmehmet | 1:f60eafbf009a | 159 | rtnVal = readBlock(DATA_18, DATA_19, localData); |
gmehmet | 1:f60eafbf009a | 160 | break; |
gmehmet | 1:f60eafbf009a | 161 | |
gmehmet | 1:f60eafbf009a | 162 | default: |
gmehmet | 1:f60eafbf009a | 163 | rtnVal = -1; |
gmehmet | 1:f60eafbf009a | 164 | break; |
gmehmet | 1:f60eafbf009a | 165 | } |
gmehmet | 1:f60eafbf009a | 166 | |
gmehmet | 1:f60eafbf009a | 167 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 168 | { |
gmehmet | 1:f60eafbf009a | 169 | data.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 170 | switch(range) |
gmehmet | 1:f60eafbf009a | 171 | { |
gmehmet | 1:f60eafbf009a | 172 | case SENS_2G: |
gmehmet | 1:f60eafbf009a | 173 | data.scaled = (data.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 174 | break; |
gmehmet | 1:f60eafbf009a | 175 | |
gmehmet | 1:f60eafbf009a | 176 | case SENS_4G: |
gmehmet | 1:f60eafbf009a | 177 | data.scaled = (data.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 178 | break; |
gmehmet | 1:f60eafbf009a | 179 | |
gmehmet | 1:f60eafbf009a | 180 | case SENS_8G: |
gmehmet | 1:f60eafbf009a | 181 | data.scaled = (data.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 182 | break; |
gmehmet | 1:f60eafbf009a | 183 | |
gmehmet | 1:f60eafbf009a | 184 | case SENS_16G: |
gmehmet | 1:f60eafbf009a | 185 | data.scaled = (data.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 186 | break; |
gmehmet | 1:f60eafbf009a | 187 | } |
gmehmet | 1:f60eafbf009a | 188 | } |
gmehmet | 1:f60eafbf009a | 189 | |
gmehmet | 1:f60eafbf009a | 190 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 191 | } |
gmehmet | 1:f60eafbf009a | 192 | |
gmehmet | 1:f60eafbf009a | 193 | |
gmehmet | 1:f60eafbf009a | 194 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 195 | int32_t BMI160::getSensorAxis(SensorAxis axis, AxisData &data, GyroRange range) |
gmehmet | 1:f60eafbf009a | 196 | { |
gmehmet | 1:f60eafbf009a | 197 | uint8_t localData[2]; |
gmehmet | 1:f60eafbf009a | 198 | int32_t rtnVal; |
gmehmet | 1:f60eafbf009a | 199 | |
gmehmet | 1:f60eafbf009a | 200 | switch(axis) |
gmehmet | 1:f60eafbf009a | 201 | { |
gmehmet | 1:f60eafbf009a | 202 | case X_AXIS: |
gmehmet | 1:f60eafbf009a | 203 | rtnVal = readBlock(DATA_8, DATA_9, localData); |
gmehmet | 1:f60eafbf009a | 204 | break; |
gmehmet | 1:f60eafbf009a | 205 | |
gmehmet | 1:f60eafbf009a | 206 | case Y_AXIS: |
gmehmet | 1:f60eafbf009a | 207 | rtnVal = readBlock(DATA_10, DATA_11, localData); |
gmehmet | 1:f60eafbf009a | 208 | break; |
gmehmet | 1:f60eafbf009a | 209 | |
gmehmet | 1:f60eafbf009a | 210 | case Z_AXIS: |
gmehmet | 1:f60eafbf009a | 211 | rtnVal = readBlock(DATA_12, DATA_13, localData); |
gmehmet | 1:f60eafbf009a | 212 | break; |
gmehmet | 1:f60eafbf009a | 213 | |
gmehmet | 1:f60eafbf009a | 214 | default: |
gmehmet | 1:f60eafbf009a | 215 | rtnVal = -1; |
gmehmet | 1:f60eafbf009a | 216 | break; |
gmehmet | 1:f60eafbf009a | 217 | } |
gmehmet | 1:f60eafbf009a | 218 | |
gmehmet | 1:f60eafbf009a | 219 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 220 | { |
gmehmet | 1:f60eafbf009a | 221 | data.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 222 | switch(range) |
gmehmet | 1:f60eafbf009a | 223 | { |
gmehmet | 1:f60eafbf009a | 224 | case DPS_2000: |
gmehmet | 1:f60eafbf009a | 225 | data.scaled = (data.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 226 | break; |
gmehmet | 1:f60eafbf009a | 227 | |
gmehmet | 1:f60eafbf009a | 228 | case DPS_1000: |
gmehmet | 1:f60eafbf009a | 229 | data.scaled = (data.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 230 | break; |
gmehmet | 1:f60eafbf009a | 231 | |
gmehmet | 1:f60eafbf009a | 232 | case DPS_500: |
gmehmet | 1:f60eafbf009a | 233 | data.scaled = (data.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 234 | break; |
gmehmet | 1:f60eafbf009a | 235 | |
gmehmet | 1:f60eafbf009a | 236 | case DPS_250: |
gmehmet | 1:f60eafbf009a | 237 | data.scaled = (data.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 238 | break; |
gmehmet | 1:f60eafbf009a | 239 | |
gmehmet | 1:f60eafbf009a | 240 | case DPS_125: |
gmehmet | 1:f60eafbf009a | 241 | data.scaled = (data.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 242 | break; |
gmehmet | 1:f60eafbf009a | 243 | } |
gmehmet | 1:f60eafbf009a | 244 | } |
gmehmet | 1:f60eafbf009a | 245 | |
gmehmet | 1:f60eafbf009a | 246 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 247 | } |
gmehmet | 1:f60eafbf009a | 248 | |
gmehmet | 1:f60eafbf009a | 249 | |
gmehmet | 1:f60eafbf009a | 250 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 251 | int32_t BMI160::getSensorXYZ(SensorData &data, AccRange range) |
gmehmet | 1:f60eafbf009a | 252 | { |
gmehmet | 1:f60eafbf009a | 253 | uint8_t localData[6]; |
gmehmet | 1:f60eafbf009a | 254 | int32_t rtnVal; |
gmehmet | 1:f60eafbf009a | 255 | |
gmehmet | 1:f60eafbf009a | 256 | if (m_use_irq == true && bmi160_irq_asserted == false) |
gmehmet | 1:f60eafbf009a | 257 | return -1; |
gmehmet | 1:f60eafbf009a | 258 | |
gmehmet | 1:f60eafbf009a | 259 | rtnVal = readBlock(DATA_14, DATA_19, localData); |
gmehmet | 1:f60eafbf009a | 260 | bmi160_irq_asserted = false; |
gmehmet | 1:f60eafbf009a | 261 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 262 | { |
gmehmet | 1:f60eafbf009a | 263 | data.xAxis.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 264 | data.yAxis.raw = ((localData[3] << 8) | localData[2]); |
gmehmet | 1:f60eafbf009a | 265 | data.zAxis.raw = ((localData[5] << 8) | localData[4]); |
gmehmet | 1:f60eafbf009a | 266 | |
gmehmet | 1:f60eafbf009a | 267 | switch(range) |
gmehmet | 1:f60eafbf009a | 268 | { |
gmehmet | 1:f60eafbf009a | 269 | case SENS_2G: |
gmehmet | 1:f60eafbf009a | 270 | data.xAxis.scaled = (data.xAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 271 | data.yAxis.scaled = (data.yAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 272 | data.zAxis.scaled = (data.zAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 273 | break; |
gmehmet | 1:f60eafbf009a | 274 | |
gmehmet | 1:f60eafbf009a | 275 | case SENS_4G: |
gmehmet | 1:f60eafbf009a | 276 | data.xAxis.scaled = (data.xAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 277 | data.yAxis.scaled = (data.yAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 278 | data.zAxis.scaled = (data.zAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 279 | break; |
gmehmet | 1:f60eafbf009a | 280 | |
gmehmet | 1:f60eafbf009a | 281 | case SENS_8G: |
gmehmet | 1:f60eafbf009a | 282 | data.xAxis.scaled = (data.xAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 283 | data.yAxis.scaled = (data.yAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 284 | data.zAxis.scaled = (data.zAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 285 | break; |
gmehmet | 1:f60eafbf009a | 286 | |
gmehmet | 1:f60eafbf009a | 287 | case SENS_16G: |
gmehmet | 1:f60eafbf009a | 288 | data.xAxis.scaled = (data.xAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 289 | data.yAxis.scaled = (data.yAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 290 | data.zAxis.scaled = (data.zAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 291 | break; |
gmehmet | 1:f60eafbf009a | 292 | } |
gmehmet | 1:f60eafbf009a | 293 | } |
gmehmet | 1:f60eafbf009a | 294 | |
gmehmet | 1:f60eafbf009a | 295 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 296 | } |
gmehmet | 1:f60eafbf009a | 297 | |
gmehmet | 1:f60eafbf009a | 298 | |
gmehmet | 1:f60eafbf009a | 299 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 300 | int32_t BMI160::getSensorXYZ(SensorData &data, GyroRange range) |
gmehmet | 1:f60eafbf009a | 301 | { |
gmehmet | 1:f60eafbf009a | 302 | uint8_t localData[6]; |
gmehmet | 1:f60eafbf009a | 303 | int32_t rtnVal = readBlock(DATA_8, DATA_13, localData); |
gmehmet | 1:f60eafbf009a | 304 | |
gmehmet | 1:f60eafbf009a | 305 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 306 | { |
gmehmet | 1:f60eafbf009a | 307 | data.xAxis.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 308 | data.yAxis.raw = ((localData[3] << 8) | localData[2]); |
gmehmet | 1:f60eafbf009a | 309 | data.zAxis.raw = ((localData[5] << 8) | localData[4]); |
gmehmet | 1:f60eafbf009a | 310 | |
gmehmet | 1:f60eafbf009a | 311 | switch(range) |
gmehmet | 1:f60eafbf009a | 312 | { |
gmehmet | 1:f60eafbf009a | 313 | case DPS_2000: |
gmehmet | 1:f60eafbf009a | 314 | data.xAxis.scaled = (data.xAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 315 | data.yAxis.scaled = (data.yAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 316 | data.zAxis.scaled = (data.zAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 317 | break; |
gmehmet | 1:f60eafbf009a | 318 | |
gmehmet | 1:f60eafbf009a | 319 | case DPS_1000: |
gmehmet | 1:f60eafbf009a | 320 | data.xAxis.scaled = (data.xAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 321 | data.yAxis.scaled = (data.yAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 322 | data.zAxis.scaled = (data.zAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 323 | break; |
gmehmet | 1:f60eafbf009a | 324 | |
gmehmet | 1:f60eafbf009a | 325 | case DPS_500: |
gmehmet | 1:f60eafbf009a | 326 | data.xAxis.scaled = (data.xAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 327 | data.yAxis.scaled = (data.yAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 328 | data.zAxis.scaled = (data.zAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 329 | break; |
gmehmet | 1:f60eafbf009a | 330 | |
gmehmet | 1:f60eafbf009a | 331 | case DPS_250: |
gmehmet | 1:f60eafbf009a | 332 | data.xAxis.scaled = (data.xAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 333 | data.yAxis.scaled = (data.yAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 334 | data.zAxis.scaled = (data.zAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 335 | break; |
gmehmet | 1:f60eafbf009a | 336 | |
gmehmet | 1:f60eafbf009a | 337 | case DPS_125: |
gmehmet | 1:f60eafbf009a | 338 | data.xAxis.scaled = (data.xAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 339 | data.yAxis.scaled = (data.yAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 340 | data.zAxis.scaled = (data.zAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 341 | break; |
gmehmet | 1:f60eafbf009a | 342 | } |
gmehmet | 1:f60eafbf009a | 343 | } |
gmehmet | 1:f60eafbf009a | 344 | |
gmehmet | 1:f60eafbf009a | 345 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 346 | } |
gmehmet | 1:f60eafbf009a | 347 | |
gmehmet | 1:f60eafbf009a | 348 | |
gmehmet | 1:f60eafbf009a | 349 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 350 | int32_t BMI160::getSensorXYZandSensorTime(SensorData &data, |
gmehmet | 1:f60eafbf009a | 351 | SensorTime &sensorTime, |
gmehmet | 1:f60eafbf009a | 352 | AccRange range) |
gmehmet | 1:f60eafbf009a | 353 | { |
gmehmet | 1:f60eafbf009a | 354 | uint8_t localData[9]; |
gmehmet | 1:f60eafbf009a | 355 | int32_t rtnVal = readBlock(DATA_14, SENSORTIME_2, localData); |
gmehmet | 1:f60eafbf009a | 356 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 357 | { |
gmehmet | 1:f60eafbf009a | 358 | data.xAxis.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 359 | data.yAxis.raw = ((localData[3] << 8) | localData[2]); |
gmehmet | 1:f60eafbf009a | 360 | data.zAxis.raw = ((localData[5] << 8) | localData[4]); |
gmehmet | 1:f60eafbf009a | 361 | |
gmehmet | 1:f60eafbf009a | 362 | switch(range) |
gmehmet | 1:f60eafbf009a | 363 | { |
gmehmet | 1:f60eafbf009a | 364 | case SENS_2G: |
gmehmet | 1:f60eafbf009a | 365 | data.xAxis.scaled = (data.xAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 366 | data.yAxis.scaled = (data.yAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 367 | data.zAxis.scaled = (data.zAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 368 | break; |
gmehmet | 1:f60eafbf009a | 369 | |
gmehmet | 1:f60eafbf009a | 370 | case SENS_4G: |
gmehmet | 1:f60eafbf009a | 371 | data.xAxis.scaled = (data.xAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 372 | data.yAxis.scaled = (data.yAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 373 | data.zAxis.scaled = (data.zAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 374 | break; |
gmehmet | 1:f60eafbf009a | 375 | |
gmehmet | 1:f60eafbf009a | 376 | case SENS_8G: |
gmehmet | 1:f60eafbf009a | 377 | data.xAxis.scaled = (data.xAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 378 | data.yAxis.scaled = (data.yAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 379 | data.zAxis.scaled = (data.zAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 380 | break; |
gmehmet | 1:f60eafbf009a | 381 | |
gmehmet | 1:f60eafbf009a | 382 | case SENS_16G: |
gmehmet | 1:f60eafbf009a | 383 | data.xAxis.scaled = (data.xAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 384 | data.yAxis.scaled = (data.yAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 385 | data.zAxis.scaled = (data.zAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 386 | break; |
gmehmet | 1:f60eafbf009a | 387 | } |
gmehmet | 1:f60eafbf009a | 388 | |
gmehmet | 1:f60eafbf009a | 389 | sensorTime.raw = ((localData[8] << 16) | (localData[7] << 8) | |
gmehmet | 1:f60eafbf009a | 390 | localData[6]); |
gmehmet | 1:f60eafbf009a | 391 | sensorTime.seconds = (sensorTime.raw * SENSOR_TIME_LSB); |
gmehmet | 1:f60eafbf009a | 392 | } |
gmehmet | 1:f60eafbf009a | 393 | |
gmehmet | 1:f60eafbf009a | 394 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 395 | } |
gmehmet | 1:f60eafbf009a | 396 | |
gmehmet | 1:f60eafbf009a | 397 | |
gmehmet | 1:f60eafbf009a | 398 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 399 | int32_t BMI160::getSensorXYZandSensorTime(SensorData &data, |
gmehmet | 1:f60eafbf009a | 400 | SensorTime &sensorTime, |
gmehmet | 1:f60eafbf009a | 401 | GyroRange range) |
gmehmet | 1:f60eafbf009a | 402 | { |
gmehmet | 1:f60eafbf009a | 403 | uint8_t localData[16]; |
gmehmet | 1:f60eafbf009a | 404 | int32_t rtnVal = readBlock(DATA_8, SENSORTIME_2, localData); |
gmehmet | 1:f60eafbf009a | 405 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 406 | { |
gmehmet | 1:f60eafbf009a | 407 | data.xAxis.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 408 | data.yAxis.raw = ((localData[3] << 8) | localData[2]); |
gmehmet | 1:f60eafbf009a | 409 | data.zAxis.raw = ((localData[5] << 8) | localData[4]); |
gmehmet | 1:f60eafbf009a | 410 | |
gmehmet | 1:f60eafbf009a | 411 | switch(range) |
gmehmet | 1:f60eafbf009a | 412 | { |
gmehmet | 1:f60eafbf009a | 413 | case DPS_2000: |
gmehmet | 1:f60eafbf009a | 414 | data.xAxis.scaled = (data.xAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 415 | data.yAxis.scaled = (data.yAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 416 | data.zAxis.scaled = (data.zAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 417 | break; |
gmehmet | 1:f60eafbf009a | 418 | |
gmehmet | 1:f60eafbf009a | 419 | case DPS_1000: |
gmehmet | 1:f60eafbf009a | 420 | data.xAxis.scaled = (data.xAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 421 | data.yAxis.scaled = (data.yAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 422 | data.zAxis.scaled = (data.zAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 423 | break; |
gmehmet | 1:f60eafbf009a | 424 | |
gmehmet | 1:f60eafbf009a | 425 | case DPS_500: |
gmehmet | 1:f60eafbf009a | 426 | data.xAxis.scaled = (data.xAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 427 | data.yAxis.scaled = (data.yAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 428 | data.zAxis.scaled = (data.zAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 429 | break; |
gmehmet | 1:f60eafbf009a | 430 | |
gmehmet | 1:f60eafbf009a | 431 | case DPS_250: |
gmehmet | 1:f60eafbf009a | 432 | data.xAxis.scaled = (data.xAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 433 | data.yAxis.scaled = (data.yAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 434 | data.zAxis.scaled = (data.zAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 435 | break; |
gmehmet | 1:f60eafbf009a | 436 | |
gmehmet | 1:f60eafbf009a | 437 | case DPS_125: |
gmehmet | 1:f60eafbf009a | 438 | data.xAxis.scaled = (data.xAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 439 | data.yAxis.scaled = (data.yAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 440 | data.zAxis.scaled = (data.zAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 441 | break; |
gmehmet | 1:f60eafbf009a | 442 | } |
gmehmet | 1:f60eafbf009a | 443 | |
gmehmet | 1:f60eafbf009a | 444 | sensorTime.raw = ((localData[14] << 16) | (localData[13] << 8) | |
gmehmet | 1:f60eafbf009a | 445 | localData[12]); |
gmehmet | 1:f60eafbf009a | 446 | sensorTime.seconds = (sensorTime.raw * SENSOR_TIME_LSB); |
gmehmet | 1:f60eafbf009a | 447 | } |
gmehmet | 1:f60eafbf009a | 448 | |
gmehmet | 1:f60eafbf009a | 449 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 450 | } |
gmehmet | 1:f60eafbf009a | 451 | |
gmehmet | 1:f60eafbf009a | 452 | |
gmehmet | 1:f60eafbf009a | 453 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 454 | int32_t BMI160::getGyroAccXYZandSensorTime(SensorData &accData, |
gmehmet | 1:f60eafbf009a | 455 | SensorData &gyroData, |
gmehmet | 1:f60eafbf009a | 456 | SensorTime &sensorTime, |
gmehmet | 1:f60eafbf009a | 457 | AccRange accRange, |
gmehmet | 1:f60eafbf009a | 458 | GyroRange gyroRange) |
gmehmet | 1:f60eafbf009a | 459 | { |
gmehmet | 1:f60eafbf009a | 460 | uint8_t localData[16]; |
gmehmet | 1:f60eafbf009a | 461 | int32_t rtnVal = readBlock(DATA_8, SENSORTIME_2, localData); |
gmehmet | 1:f60eafbf009a | 462 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 463 | { |
gmehmet | 1:f60eafbf009a | 464 | gyroData.xAxis.raw = ((localData[1] << 8) | localData[0]); |
gmehmet | 1:f60eafbf009a | 465 | gyroData.yAxis.raw = ((localData[3] << 8) | localData[2]); |
gmehmet | 1:f60eafbf009a | 466 | gyroData.zAxis.raw = ((localData[5] << 8) | localData[4]); |
gmehmet | 1:f60eafbf009a | 467 | |
gmehmet | 1:f60eafbf009a | 468 | accData.xAxis.raw = ((localData[7] << 8) | localData[6]); |
gmehmet | 1:f60eafbf009a | 469 | accData.yAxis.raw = ((localData[9] << 8) | localData[8]); |
gmehmet | 1:f60eafbf009a | 470 | accData.zAxis.raw = ((localData[11] << 8) | localData[10]); |
gmehmet | 1:f60eafbf009a | 471 | |
gmehmet | 1:f60eafbf009a | 472 | switch(gyroRange) |
gmehmet | 1:f60eafbf009a | 473 | { |
gmehmet | 1:f60eafbf009a | 474 | case DPS_2000: |
gmehmet | 1:f60eafbf009a | 475 | gyroData.xAxis.scaled = (gyroData.xAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 476 | gyroData.yAxis.scaled = (gyroData.yAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 477 | gyroData.zAxis.scaled = (gyroData.zAxis.raw/SENS_2000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 478 | break; |
gmehmet | 1:f60eafbf009a | 479 | |
gmehmet | 1:f60eafbf009a | 480 | case DPS_1000: |
gmehmet | 1:f60eafbf009a | 481 | gyroData.xAxis.scaled = (gyroData.xAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 482 | gyroData.yAxis.scaled = (gyroData.yAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 483 | gyroData.zAxis.scaled = (gyroData.zAxis.raw/SENS_1000_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 484 | break; |
gmehmet | 1:f60eafbf009a | 485 | |
gmehmet | 1:f60eafbf009a | 486 | case DPS_500: |
gmehmet | 1:f60eafbf009a | 487 | gyroData.xAxis.scaled = (gyroData.xAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 488 | gyroData.yAxis.scaled = (gyroData.yAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 489 | gyroData.zAxis.scaled = (gyroData.zAxis.raw/SENS_500_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 490 | break; |
gmehmet | 1:f60eafbf009a | 491 | |
gmehmet | 1:f60eafbf009a | 492 | case DPS_250: |
gmehmet | 1:f60eafbf009a | 493 | gyroData.xAxis.scaled = (gyroData.xAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 494 | gyroData.yAxis.scaled = (gyroData.yAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 495 | gyroData.zAxis.scaled = (gyroData.zAxis.raw/SENS_250_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 496 | break; |
gmehmet | 1:f60eafbf009a | 497 | |
gmehmet | 1:f60eafbf009a | 498 | case DPS_125: |
gmehmet | 1:f60eafbf009a | 499 | gyroData.xAxis.scaled = (gyroData.xAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 500 | gyroData.yAxis.scaled = (gyroData.yAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 501 | gyroData.zAxis.scaled = (gyroData.zAxis.raw/SENS_125_DPS_LSB_PER_DPS); |
gmehmet | 1:f60eafbf009a | 502 | break; |
gmehmet | 1:f60eafbf009a | 503 | } |
gmehmet | 1:f60eafbf009a | 504 | |
gmehmet | 1:f60eafbf009a | 505 | switch(accRange) |
gmehmet | 1:f60eafbf009a | 506 | { |
gmehmet | 1:f60eafbf009a | 507 | case SENS_2G: |
gmehmet | 1:f60eafbf009a | 508 | accData.xAxis.scaled = (accData.xAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 509 | accData.yAxis.scaled = (accData.yAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 510 | accData.zAxis.scaled = (accData.zAxis.raw/SENS_2G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 511 | break; |
gmehmet | 1:f60eafbf009a | 512 | |
gmehmet | 1:f60eafbf009a | 513 | case SENS_4G: |
gmehmet | 1:f60eafbf009a | 514 | accData.xAxis.scaled = (accData.xAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 515 | accData.yAxis.scaled = (accData.yAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 516 | accData.zAxis.scaled = (accData.zAxis.raw/SENS_4G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 517 | break; |
gmehmet | 1:f60eafbf009a | 518 | |
gmehmet | 1:f60eafbf009a | 519 | case SENS_8G: |
gmehmet | 1:f60eafbf009a | 520 | accData.xAxis.scaled = (accData.xAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 521 | accData.yAxis.scaled = (accData.yAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 522 | accData.zAxis.scaled = (accData.zAxis.raw/SENS_8G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 523 | break; |
gmehmet | 1:f60eafbf009a | 524 | |
gmehmet | 1:f60eafbf009a | 525 | case SENS_16G: |
gmehmet | 1:f60eafbf009a | 526 | accData.xAxis.scaled = (accData.xAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 527 | accData.yAxis.scaled = (accData.yAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 528 | accData.zAxis.scaled = (accData.zAxis.raw/SENS_16G_LSB_PER_G); |
gmehmet | 1:f60eafbf009a | 529 | break; |
gmehmet | 1:f60eafbf009a | 530 | } |
gmehmet | 1:f60eafbf009a | 531 | |
gmehmet | 1:f60eafbf009a | 532 | sensorTime.raw = ((localData[14] << 16) | (localData[13] << 8) | |
gmehmet | 1:f60eafbf009a | 533 | localData[12]); |
gmehmet | 1:f60eafbf009a | 534 | sensorTime.seconds = (sensorTime.raw * SENSOR_TIME_LSB); |
gmehmet | 1:f60eafbf009a | 535 | } |
gmehmet | 1:f60eafbf009a | 536 | |
gmehmet | 1:f60eafbf009a | 537 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 538 | } |
gmehmet | 1:f60eafbf009a | 539 | |
gmehmet | 1:f60eafbf009a | 540 | int32_t BMI160::setSampleRate(int sample_rate) |
gmehmet | 1:f60eafbf009a | 541 | { |
gmehmet | 1:f60eafbf009a | 542 | int sr_reg_val = -1; |
gmehmet | 1:f60eafbf009a | 543 | int i; |
gmehmet | 1:f60eafbf009a | 544 | const uint16_t odr_table[][2] = { |
gmehmet | 1:f60eafbf009a | 545 | {25, GYRO_ODR_6}, ///<25Hz |
gmehmet | 1:f60eafbf009a | 546 | {50, GYRO_ODR_7}, ///<50Hz |
gmehmet | 1:f60eafbf009a | 547 | {100, GYRO_ODR_8}, ///<100Hz |
gmehmet | 1:f60eafbf009a | 548 | {200, GYRO_ODR_9}, ///<200Hz |
gmehmet | 1:f60eafbf009a | 549 | {400, GYRO_ODR_10}, ///<400Hz |
gmehmet | 1:f60eafbf009a | 550 | {800, GYRO_ODR_11}, ///<800Hz |
gmehmet | 1:f60eafbf009a | 551 | {1600, GYRO_ODR_12}, ///<1600Hz |
gmehmet | 1:f60eafbf009a | 552 | {3200, GYRO_ODR_13}, ///<3200Hz |
gmehmet | 1:f60eafbf009a | 553 | }; |
gmehmet | 1:f60eafbf009a | 554 | |
gmehmet | 1:f60eafbf009a | 555 | int num_sr = sizeof(odr_table)/sizeof(odr_table[0]); |
gmehmet | 1:f60eafbf009a | 556 | for (i = 0; i < num_sr; i++) { |
gmehmet | 1:f60eafbf009a | 557 | if (sample_rate == odr_table[i][0]) { |
gmehmet | 1:f60eafbf009a | 558 | sr_reg_val = odr_table[i][1]; |
gmehmet | 1:f60eafbf009a | 559 | break; |
gmehmet | 1:f60eafbf009a | 560 | } |
gmehmet | 1:f60eafbf009a | 561 | } |
gmehmet | 1:f60eafbf009a | 562 | |
gmehmet | 1:f60eafbf009a | 563 | if (sr_reg_val == -1) |
gmehmet | 1:f60eafbf009a | 564 | return -2; |
gmehmet | 1:f60eafbf009a | 565 | |
gmehmet | 1:f60eafbf009a | 566 | AccConfig accConfigRead; |
gmehmet | 1:f60eafbf009a | 567 | if (getSensorConfig(accConfigRead) == BMI160::RTN_NO_ERROR) { |
gmehmet | 1:f60eafbf009a | 568 | accConfigRead.odr = (AccOutputDataRate)sr_reg_val; |
gmehmet | 1:f60eafbf009a | 569 | return setSensorConfig(accConfigRead) == BMI160::RTN_NO_ERROR ? 0 : -1; |
gmehmet | 1:f60eafbf009a | 570 | } else |
gmehmet | 1:f60eafbf009a | 571 | return -1; |
gmehmet | 1:f60eafbf009a | 572 | } |
gmehmet | 1:f60eafbf009a | 573 | |
gmehmet | 1:f60eafbf009a | 574 | |
gmehmet | 1:f60eafbf009a | 575 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 576 | int32_t BMI160::getSensorTime(SensorTime &sensorTime) |
gmehmet | 1:f60eafbf009a | 577 | { |
gmehmet | 1:f60eafbf009a | 578 | uint8_t localData[3]; |
gmehmet | 1:f60eafbf009a | 579 | int32_t rtnVal = readBlock(SENSORTIME_0, SENSORTIME_2, localData); |
gmehmet | 1:f60eafbf009a | 580 | |
gmehmet | 1:f60eafbf009a | 581 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 582 | { |
gmehmet | 1:f60eafbf009a | 583 | sensorTime.raw = ((localData[2] << 16) | (localData[1] << 8) | |
gmehmet | 1:f60eafbf009a | 584 | localData[0]); |
gmehmet | 1:f60eafbf009a | 585 | sensorTime.seconds = (sensorTime.raw * SENSOR_TIME_LSB); |
gmehmet | 1:f60eafbf009a | 586 | } |
gmehmet | 1:f60eafbf009a | 587 | |
gmehmet | 1:f60eafbf009a | 588 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 589 | } |
gmehmet | 1:f60eafbf009a | 590 | |
gmehmet | 1:f60eafbf009a | 591 | |
gmehmet | 1:f60eafbf009a | 592 | //***************************************************************************** |
gmehmet | 1:f60eafbf009a | 593 | int32_t BMI160::getTemperature(float *temp) |
gmehmet | 1:f60eafbf009a | 594 | { |
gmehmet | 1:f60eafbf009a | 595 | uint8_t data[2]; |
gmehmet | 1:f60eafbf009a | 596 | uint16_t rawTemp; |
gmehmet | 1:f60eafbf009a | 597 | |
gmehmet | 1:f60eafbf009a | 598 | int32_t rtnVal = readBlock(TEMPERATURE_0, TEMPERATURE_1, data); |
gmehmet | 1:f60eafbf009a | 599 | if(rtnVal == RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 600 | { |
gmehmet | 1:f60eafbf009a | 601 | rawTemp = ((data[1] << 8) | data[0]); |
gmehmet | 1:f60eafbf009a | 602 | if(rawTemp & 0x8000) |
gmehmet | 1:f60eafbf009a | 603 | { |
gmehmet | 1:f60eafbf009a | 604 | *temp = (23.0F - ((0x10000 - rawTemp)/512.0F)); |
gmehmet | 1:f60eafbf009a | 605 | } |
gmehmet | 1:f60eafbf009a | 606 | else |
gmehmet | 1:f60eafbf009a | 607 | { |
gmehmet | 1:f60eafbf009a | 608 | *temp = ((rawTemp/512.0F) + 23.0F); |
gmehmet | 1:f60eafbf009a | 609 | } |
gmehmet | 1:f60eafbf009a | 610 | } |
gmehmet | 1:f60eafbf009a | 611 | |
gmehmet | 1:f60eafbf009a | 612 | return rtnVal; |
gmehmet | 1:f60eafbf009a | 613 | } |
gmehmet | 1:f60eafbf009a | 614 | |
gmehmet | 1:f60eafbf009a | 615 | //*********************************************************************************** |
gmehmet | 1:f60eafbf009a | 616 | int32_t BMI160::BMI160_DefaultInitalize(){ |
gmehmet | 1:f60eafbf009a | 617 | |
gmehmet | 1:f60eafbf009a | 618 | //soft reset the accelerometer |
gmehmet | 1:f60eafbf009a | 619 | writeRegister(CMD ,SOFT_RESET); |
gmehmet | 1:f60eafbf009a | 620 | wait(0.1); |
gmehmet | 1:f60eafbf009a | 621 | |
gmehmet | 1:f60eafbf009a | 622 | //Power up sensors in normal mode |
gmehmet | 1:f60eafbf009a | 623 | if(setSensorPowerMode(BMI160::GYRO, BMI160::SUSPEND) != BMI160::RTN_NO_ERROR){ |
gmehmet | 1:f60eafbf009a | 624 | printf("Failed to set gyroscope power mode\n"); |
gmehmet | 1:f60eafbf009a | 625 | } |
gmehmet | 1:f60eafbf009a | 626 | |
gmehmet | 1:f60eafbf009a | 627 | wait(0.1); |
gmehmet | 1:f60eafbf009a | 628 | |
gmehmet | 1:f60eafbf009a | 629 | if(setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR){ |
gmehmet | 1:f60eafbf009a | 630 | printf("Failed to set accelerometer power mode\n"); |
gmehmet | 1:f60eafbf009a | 631 | } |
gmehmet | 1:f60eafbf009a | 632 | wait(0.1); |
gmehmet | 1:f60eafbf009a | 633 | |
gmehmet | 1:f60eafbf009a | 634 | BMI160::AccConfig accConfig; |
gmehmet | 1:f60eafbf009a | 635 | BMI160::AccConfig accConfigRead; |
gmehmet | 1:f60eafbf009a | 636 | accConfig.range = BMI160::SENS_2G; |
gmehmet | 1:f60eafbf009a | 637 | accConfig.us = BMI160::ACC_US_OFF; |
gmehmet | 1:f60eafbf009a | 638 | accConfig.bwp = BMI160::ACC_BWP_2; |
gmehmet | 1:f60eafbf009a | 639 | accConfig.odr = BMI160::ACC_ODR_6; |
gmehmet | 1:f60eafbf009a | 640 | if(setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 641 | { |
gmehmet | 1:f60eafbf009a | 642 | if(getSensorConfig(accConfigRead) == BMI160::RTN_NO_ERROR) |
gmehmet | 1:f60eafbf009a | 643 | { |
gmehmet | 1:f60eafbf009a | 644 | if((accConfig.range != accConfigRead.range) || |
gmehmet | 1:f60eafbf009a | 645 | (accConfig.us != accConfigRead.us) || |
gmehmet | 1:f60eafbf009a | 646 | (accConfig.bwp != accConfigRead.bwp) || |
gmehmet | 1:f60eafbf009a | 647 | (accConfig.odr != accConfigRead.odr)) |
gmehmet | 1:f60eafbf009a | 648 | { |
gmehmet | 1:f60eafbf009a | 649 | printf("ACC read data desn't equal set data\n\n"); |
gmehmet | 1:f60eafbf009a | 650 | printf("ACC Set Range = %d\n", accConfig.range); |
gmehmet | 1:f60eafbf009a | 651 | printf("ACC Set UnderSampling = %d\n", accConfig.us); |
gmehmet | 1:f60eafbf009a | 652 | printf("ACC Set BandWidthParam = %d\n", accConfig.bwp); |
gmehmet | 1:f60eafbf009a | 653 | printf("ACC Set OutputDataRate = %d\n\n", accConfig.odr); |
gmehmet | 1:f60eafbf009a | 654 | printf("ACC Read Range = %d\n", accConfigRead.range); |
gmehmet | 1:f60eafbf009a | 655 | printf("ACC Read UnderSampling = %d\n", accConfigRead.us); |
gmehmet | 1:f60eafbf009a | 656 | printf("ACC Read BandWidthParam = %d\n", accConfigRead.bwp); |
gmehmet | 1:f60eafbf009a | 657 | printf("ACC Read OutputDataRate = %d\n\n", accConfigRead.odr); |
gmehmet | 1:f60eafbf009a | 658 | } |
gmehmet | 1:f60eafbf009a | 659 | |
gmehmet | 1:f60eafbf009a | 660 | } |
gmehmet | 1:f60eafbf009a | 661 | else |
gmehmet | 1:f60eafbf009a | 662 | { |
gmehmet | 1:f60eafbf009a | 663 | printf("Failed to read back accelerometer configuration\n"); |
gmehmet | 1:f60eafbf009a | 664 | } |
gmehmet | 1:f60eafbf009a | 665 | } |
gmehmet | 1:f60eafbf009a | 666 | else |
gmehmet | 1:f60eafbf009a | 667 | { |
gmehmet | 1:f60eafbf009a | 668 | printf("Failed to set accelerometer configuration\n"); |
gmehmet | 1:f60eafbf009a | 669 | } |
gmehmet | 1:f60eafbf009a | 670 | return 0; |
gmehmet | 1:f60eafbf009a | 671 | } |
gmehmet | 1:f60eafbf009a | 672 | |
gmehmet | 1:f60eafbf009a | 673 | //*********************************************************************************** |
gmehmet | 1:f60eafbf009a | 674 | int32_t BMI160::enable_data_ready_interrupt() { |
gmehmet | 1:f60eafbf009a | 675 | uint8_t data = 0; |
gmehmet | 1:f60eafbf009a | 676 | uint8_t temp = 0; |
gmehmet | 1:f60eafbf009a | 677 | int32_t result; |
gmehmet | 1:f60eafbf009a | 678 | |
gmehmet | 1:f60eafbf009a | 679 | result = readRegister(INT_EN_1, &data); |
gmehmet | 1:f60eafbf009a | 680 | temp = data & ~0x10; |
gmehmet | 1:f60eafbf009a | 681 | data = temp | ((1 << 4) & 0x10); |
gmehmet | 1:f60eafbf009a | 682 | /* Writing data to INT ENABLE 1 Address */ |
gmehmet | 1:f60eafbf009a | 683 | result |= writeRegister(INT_EN_1, data); |
gmehmet | 1:f60eafbf009a | 684 | |
gmehmet | 1:f60eafbf009a | 685 | // configure in_out ctrl |
gmehmet | 1:f60eafbf009a | 686 | //bmi160_get_regs(BMI160_INT_OUT_CTRL_ADDR, &data, 1, dev); |
gmehmet | 1:f60eafbf009a | 687 | result |= readRegister(INT_OUT_CTRL, &data); |
gmehmet | 1:f60eafbf009a | 688 | data = 0x09; |
gmehmet | 1:f60eafbf009a | 689 | result |= writeRegister(INT_OUT_CTRL,data); |
gmehmet | 1:f60eafbf009a | 690 | |
gmehmet | 1:f60eafbf009a | 691 | //config int latch |
gmehmet | 1:f60eafbf009a | 692 | //bmi160_get_regs(BMI160_INT_LATCH_ADDR, &data, 1, dev); |
gmehmet | 1:f60eafbf009a | 693 | result |= readRegister(INT_LATCH, &data); |
gmehmet | 1:f60eafbf009a | 694 | data = 0x0F; |
gmehmet | 1:f60eafbf009a | 695 | result |= writeRegister(INT_LATCH, data); |
gmehmet | 1:f60eafbf009a | 696 | |
gmehmet | 1:f60eafbf009a | 697 | //bmi160_get_regs(BMI160_INT_MAP_1_ADDR, &data, 1, dev); |
gmehmet | 1:f60eafbf009a | 698 | result |= readRegister(INT_MAP_1, &data); |
gmehmet | 1:f60eafbf009a | 699 | data = 0x80; |
gmehmet | 1:f60eafbf009a | 700 | result |= writeRegister(INT_MAP_1, data); |
gmehmet | 1:f60eafbf009a | 701 | |
gmehmet | 1:f60eafbf009a | 702 | if(result != 0){ |
gmehmet | 1:f60eafbf009a | 703 | printf("BMI160::%s failed.\r\n", __func__); |
gmehmet | 1:f60eafbf009a | 704 | return -1; |
gmehmet | 1:f60eafbf009a | 705 | } |
gmehmet | 1:f60eafbf009a | 706 | |
gmehmet | 1:f60eafbf009a | 707 | m_bmi160_irq->disable_irq(); |
gmehmet | 1:f60eafbf009a | 708 | m_bmi160_irq->mode(PullUp); |
gmehmet | 1:f60eafbf009a | 709 | m_bmi160_irq->fall(this, &BMI160::irq_handler); |
gmehmet | 1:f60eafbf009a | 710 | m_bmi160_irq->enable_irq(); |
gmehmet | 1:f60eafbf009a | 711 | return 0; |
gmehmet | 1:f60eafbf009a | 712 | } |
gmehmet | 1:f60eafbf009a | 713 | |
gmehmet | 1:f60eafbf009a | 714 | void BMI160::irq_handler() { |
gmehmet | 1:f60eafbf009a | 715 | bmi160_irq_asserted = true; |
gmehmet | 1:f60eafbf009a | 716 | } |
gmehmet | 1:f60eafbf009a | 717 | |
gmehmet | 1:f60eafbf009a | 718 | int32_t BMI160::reset() { |
gmehmet | 1:f60eafbf009a | 719 | if (m_use_irq) |
gmehmet | 1:f60eafbf009a | 720 | m_bmi160_irq->disable_irq(); |
gmehmet | 1:f60eafbf009a | 721 | bmi160_irq_asserted = false; |
gmehmet | 1:f60eafbf009a | 722 | writeRegister(CMD, SOFT_RESET); |
gmehmet | 1:f60eafbf009a | 723 | return 0; |
gmehmet | 1:f60eafbf009a | 724 | } |