Program for testing development of BMI160 Library

Dependencies:   BMI160 max32630fthr mbed

Committer:
j3
Date:
Wed Dec 21 01:03:52 2016 +0000
Revision:
5:4deb38e46a10
Parent:
4:3d7fae7f7b75
Child:
6:1d23dc51ee3f
Added checks for setting power modes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:0db9a7ed2e63 1 /**********************************************************************
j3 0:0db9a7ed2e63 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:0db9a7ed2e63 3 *
j3 0:0db9a7ed2e63 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:0db9a7ed2e63 5 * copy of this software and associated documentation files (the "Software"),
j3 0:0db9a7ed2e63 6 * to deal in the Software without restriction, including without limitation
j3 0:0db9a7ed2e63 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:0db9a7ed2e63 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:0db9a7ed2e63 9 * Software is furnished to do so, subject to the following conditions:
j3 0:0db9a7ed2e63 10 *
j3 0:0db9a7ed2e63 11 * The above copyright notice and this permission notice shall be included
j3 0:0db9a7ed2e63 12 * in all copies or substantial portions of the Software.
j3 0:0db9a7ed2e63 13 *
j3 0:0db9a7ed2e63 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:0db9a7ed2e63 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:0db9a7ed2e63 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:0db9a7ed2e63 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:0db9a7ed2e63 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:0db9a7ed2e63 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:0db9a7ed2e63 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:0db9a7ed2e63 21 *
j3 0:0db9a7ed2e63 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:0db9a7ed2e63 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:0db9a7ed2e63 24 * Products, Inc. Branding Policy.
j3 0:0db9a7ed2e63 25 *
j3 0:0db9a7ed2e63 26 * The mere transfer of this software does not imply any licenses
j3 0:0db9a7ed2e63 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:0db9a7ed2e63 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:0db9a7ed2e63 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:0db9a7ed2e63 30 * ownership rights.
j3 0:0db9a7ed2e63 31 **********************************************************************/
j3 0:0db9a7ed2e63 32
j3 0:0db9a7ed2e63 33
j3 0:0db9a7ed2e63 34 #include "mbed.h"
j3 0:0db9a7ed2e63 35 #include "SDFileSystem.h"
j3 0:0db9a7ed2e63 36 #include "max32630fthr.h"
j3 0:0db9a7ed2e63 37 #include "bmi160.h"
j3 0:0db9a7ed2e63 38
j3 1:a3fa54415b4e 39
j3 0:0db9a7ed2e63 40 void dumpImuRegisters(BMI160 &imu);
j3 0:0db9a7ed2e63 41 void printRegister(BMI160 &imu, BMI160::Registers reg);
j3 1:a3fa54415b4e 42 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg);
j3 1:a3fa54415b4e 43
j3 0:0db9a7ed2e63 44
j3 0:0db9a7ed2e63 45 int main()
j3 0:0db9a7ed2e63 46 {
j3 1:a3fa54415b4e 47 MAX32630FTHR pegasus;
j3 1:a3fa54415b4e 48 pegasus.init(MAX32630FTHR::VIO_3V3);
j3 0:0db9a7ed2e63 49
j3 0:0db9a7ed2e63 50 DigitalOut rLED(LED1, LED_OFF);
j3 0:0db9a7ed2e63 51 DigitalOut gLED(LED2, LED_OFF);
j3 4:3d7fae7f7b75 52 DigitalOut bLED(LED3, LED_OFF);
j3 0:0db9a7ed2e63 53
j3 1:a3fa54415b4e 54 I2C i2cBus(P5_7, P6_0);
j3 3:250503cb7cb3 55 i2cBus.frequency(400000);
j3 1:a3fa54415b4e 56 BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
j3 0:0db9a7ed2e63 57
j3 5:4deb38e46a10 58 printf("\033[H"); //home
j3 5:4deb38e46a10 59 printf("\033[0J"); //erase from cursor to end of screen
j3 1:a3fa54415b4e 60
j3 4:3d7fae7f7b75 61 uint32_t failures = 0;
j3 4:3d7fae7f7b75 62
j3 5:4deb38e46a10 63 if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
j3 5:4deb38e46a10 64 {
j3 5:4deb38e46a10 65 printf("Failed to set gyroscope power mode\n");
j3 5:4deb38e46a10 66 failures++;
j3 5:4deb38e46a10 67 }
j3 5:4deb38e46a10 68 wait_ms(100);
j3 5:4deb38e46a10 69
j3 5:4deb38e46a10 70 if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
j3 5:4deb38e46a10 71 {
j3 5:4deb38e46a10 72 printf("Failed to set accelerometer power mode\n");
j3 5:4deb38e46a10 73 failures++;
j3 5:4deb38e46a10 74 }
j3 5:4deb38e46a10 75 wait_ms(100);
j3 5:4deb38e46a10 76
j3 4:3d7fae7f7b75 77
j3 4:3d7fae7f7b75 78 BMI160::AccConfig accConfig;
j3 4:3d7fae7f7b75 79 //example of using getSensorConfig
j3 4:3d7fae7f7b75 80 if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
j3 4:3d7fae7f7b75 81 {
j3 4:3d7fae7f7b75 82 printf("ACC Range = %d\n", accConfig.range);
j3 4:3d7fae7f7b75 83 printf("ACC UnderSampling = %d\n", accConfig.us);
j3 4:3d7fae7f7b75 84 printf("ACC BandWidthParam = %d\n", accConfig.bwp);
j3 4:3d7fae7f7b75 85 printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
j3 4:3d7fae7f7b75 86 }
j3 4:3d7fae7f7b75 87 else
j3 4:3d7fae7f7b75 88 {
j3 4:3d7fae7f7b75 89 printf("Failed to get accelerometer configuration\n");
j3 4:3d7fae7f7b75 90 failures++;
j3 4:3d7fae7f7b75 91 }
j3 1:a3fa54415b4e 92
j3 4:3d7fae7f7b75 93 //example of setting user defined configuration
j3 4:3d7fae7f7b75 94 accConfig.range = BMI160::SENS_4G;
j3 4:3d7fae7f7b75 95 accConfig.us = BMI160::ACC_US_OFF;
j3 4:3d7fae7f7b75 96 accConfig.bwp = BMI160::ACC_BWP_2;
j3 4:3d7fae7f7b75 97 accConfig.odr = BMI160::ACC_ODR_8;
j3 4:3d7fae7f7b75 98 if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
j3 4:3d7fae7f7b75 99 {
j3 4:3d7fae7f7b75 100 printf("ACC Range = %d\n", accConfig.range);
j3 4:3d7fae7f7b75 101 printf("ACC UnderSampling = %d\n", accConfig.us);
j3 4:3d7fae7f7b75 102 printf("ACC BandWidthParam = %d\n", accConfig.bwp);
j3 4:3d7fae7f7b75 103 printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
j3 4:3d7fae7f7b75 104 }
j3 4:3d7fae7f7b75 105 else
j3 4:3d7fae7f7b75 106 {
j3 4:3d7fae7f7b75 107 printf("Failed to set accelerometer configuration\n");
j3 4:3d7fae7f7b75 108 failures++;
j3 4:3d7fae7f7b75 109 }
j3 4:3d7fae7f7b75 110
j3 4:3d7fae7f7b75 111 BMI160::GyroConfig gyroConfig;
j3 4:3d7fae7f7b75 112 if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
j3 0:0db9a7ed2e63 113 {
j3 4:3d7fae7f7b75 114 printf("GYRO Range = %d\n", gyroConfig.range);
j3 4:3d7fae7f7b75 115 printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
j3 4:3d7fae7f7b75 116 printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
j3 4:3d7fae7f7b75 117 }
j3 4:3d7fae7f7b75 118 else
j3 4:3d7fae7f7b75 119 {
j3 4:3d7fae7f7b75 120 printf("Failed to get gyroscope configuration\n");
j3 4:3d7fae7f7b75 121 failures++;
j3 4:3d7fae7f7b75 122 }
j3 4:3d7fae7f7b75 123
j3 4:3d7fae7f7b75 124 wait(5.0);
j3 4:3d7fae7f7b75 125 printf("\033[H"); //home
j3 4:3d7fae7f7b75 126 printf("\033[0J"); //erase from cursor to end of screen
j3 4:3d7fae7f7b75 127
j3 4:3d7fae7f7b75 128 if(failures == 0)
j3 4:3d7fae7f7b75 129 {
j3 4:3d7fae7f7b75 130 float imuTemperature;
j3 4:3d7fae7f7b75 131 BMI160::SensorData accData;
j3 4:3d7fae7f7b75 132 BMI160::SensorData gyroData;
j3 4:3d7fae7f7b75 133 BMI160::SensorTime sensorTime;
j3 3:250503cb7cb3 134
j3 4:3d7fae7f7b75 135 while(1)
j3 4:3d7fae7f7b75 136 {
j3 4:3d7fae7f7b75 137 imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
j3 4:3d7fae7f7b75 138 imu.getTemperature(&imuTemperature);
j3 4:3d7fae7f7b75 139
j3 4:3d7fae7f7b75 140 printf("ACC xAxis = %s%4.3f\n", "\033[K", accData.xAxis.scaled);
j3 4:3d7fae7f7b75 141 printf("ACC yAxis = %s%4.3f\n", "\033[K", accData.yAxis.scaled);
j3 4:3d7fae7f7b75 142 printf("ACC zAxis = %s%4.3f\n\n", "\033[K", accData.zAxis.scaled);
j3 4:3d7fae7f7b75 143
j3 4:3d7fae7f7b75 144 printf("GYRO xAxis = %s%5.1f\n", "\033[K", gyroData.xAxis.scaled);
j3 4:3d7fae7f7b75 145 printf("GYRO yAxis = %s%5.1f\n", "\033[K", gyroData.yAxis.scaled);
j3 4:3d7fae7f7b75 146 printf("GYRO zAxis = %s%5.1f\n\n", "\033[K", gyroData.zAxis.scaled);
j3 4:3d7fae7f7b75 147
j3 4:3d7fae7f7b75 148 printf("Sensor Time = %s%f\n", "\033[K", sensorTime.seconds);
j3 4:3d7fae7f7b75 149 printf("Sensor Temperature = %s%5.3f\n", "\033[K", imuTemperature);
j3 4:3d7fae7f7b75 150
j3 4:3d7fae7f7b75 151 printf("\033[H"); //home
j3 4:3d7fae7f7b75 152 gLED = !gLED;
j3 4:3d7fae7f7b75 153 }
j3 4:3d7fae7f7b75 154 }
j3 4:3d7fae7f7b75 155 else
j3 4:3d7fae7f7b75 156 {
j3 4:3d7fae7f7b75 157 while(1)
j3 4:3d7fae7f7b75 158 {
j3 4:3d7fae7f7b75 159 rLED = !rLED;
j3 4:3d7fae7f7b75 160 wait(0.25);
j3 4:3d7fae7f7b75 161 }
j3 0:0db9a7ed2e63 162 }
j3 0:0db9a7ed2e63 163 }
j3 0:0db9a7ed2e63 164
j3 0:0db9a7ed2e63 165
j3 0:0db9a7ed2e63 166 //*****************************************************************************
j3 0:0db9a7ed2e63 167 void dumpImuRegisters(BMI160 &imu)
j3 0:0db9a7ed2e63 168 {
j3 0:0db9a7ed2e63 169 printRegister(imu, BMI160::CHIP_ID);
j3 1:a3fa54415b4e 170 printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA);
j3 1:a3fa54415b4e 171 printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1);
j3 1:a3fa54415b4e 172 printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST);
j3 1:a3fa54415b4e 173 printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1);
j3 0:0db9a7ed2e63 174 printRegister(imu, BMI160::CMD);
j3 0:0db9a7ed2e63 175 printf("\n");
j3 0:0db9a7ed2e63 176 }
j3 0:0db9a7ed2e63 177
j3 0:0db9a7ed2e63 178
j3 0:0db9a7ed2e63 179 //*****************************************************************************
j3 0:0db9a7ed2e63 180 void printRegister(BMI160 &imu, BMI160::Registers reg)
j3 0:0db9a7ed2e63 181 {
j3 0:0db9a7ed2e63 182 uint8_t data;
j3 1:a3fa54415b4e 183 if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR)
j3 0:0db9a7ed2e63 184 {
j3 0:0db9a7ed2e63 185 printf("IMU Register 0x%02x = 0x%02x\n", reg, data);
j3 0:0db9a7ed2e63 186 }
j3 0:0db9a7ed2e63 187 else
j3 0:0db9a7ed2e63 188 {
j3 0:0db9a7ed2e63 189 printf("Failed to read register\n");
j3 0:0db9a7ed2e63 190 }
j3 0:0db9a7ed2e63 191 }
j3 0:0db9a7ed2e63 192
j3 0:0db9a7ed2e63 193
j3 0:0db9a7ed2e63 194 //*****************************************************************************
j3 1:a3fa54415b4e 195 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg)
j3 0:0db9a7ed2e63 196 {
j3 0:0db9a7ed2e63 197 uint8_t numBytes = ((stopReg - startReg) + 1);
j3 1:a3fa54415b4e 198 uint8_t buff[numBytes];
j3 0:0db9a7ed2e63 199 uint8_t offset = static_cast<uint8_t>(startReg);
j3 0:0db9a7ed2e63 200
j3 1:a3fa54415b4e 201 if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR)
j3 0:0db9a7ed2e63 202 {
j3 0:0db9a7ed2e63 203 for(uint8_t idx = offset; idx < (numBytes + offset); idx++)
j3 0:0db9a7ed2e63 204 {
j3 0:0db9a7ed2e63 205 printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]);
j3 0:0db9a7ed2e63 206 }
j3 0:0db9a7ed2e63 207 }
j3 0:0db9a7ed2e63 208 else
j3 0:0db9a7ed2e63 209 {
j3 0:0db9a7ed2e63 210 printf("Failed to read block\n");
j3 0:0db9a7ed2e63 211 }
j3 0:0db9a7ed2e63 212 }