![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Example
Dependencies: mbed BMI160 max32630fthr
main.cpp@3:250503cb7cb3, 2016-12-20 (annotated)
- Committer:
- j3
- Date:
- Tue Dec 20 01:30:12 2016 +0000
- Revision:
- 3:250503cb7cb3
- Parent:
- 2:0d7433075663
- Child:
- 4:3d7fae7f7b75
working
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:0db9a7ed2e63 | 52 | DigitalOut bLED(LED3, LED_ON); |
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 | 1:a3fa54415b4e | 58 | imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL); |
j3 | 1:a3fa54415b4e | 59 | wait_ms(100); |
j3 | 1:a3fa54415b4e | 60 | imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL); |
j3 | 2:0d7433075663 | 61 | wait_ms(100); |
j3 | 1:a3fa54415b4e | 62 | |
j3 | 3:250503cb7cb3 | 63 | float imuTemperature, sensorTime; |
j3 | 2:0d7433075663 | 64 | BMI160::SensorData accData; |
j3 | 2:0d7433075663 | 65 | //BMI160::SensorData gyroData; |
j3 | 1:a3fa54415b4e | 66 | |
j3 | 0:0db9a7ed2e63 | 67 | while(1) |
j3 | 0:0db9a7ed2e63 | 68 | { |
j3 | 3:250503cb7cb3 | 69 | imu.getAccXYZ(accData, BMI160::DEFAULT_ACC_CONFIG); |
j3 | 3:250503cb7cb3 | 70 | imu.getSensorTime(&sensorTime); |
j3 | 2:0d7433075663 | 71 | imu.getTemperature(&imuTemperature); |
j3 | 0:0db9a7ed2e63 | 72 | |
j3 | 3:250503cb7cb3 | 73 | |
j3 | 2:0d7433075663 | 74 | printf("ACC xAxis = %s%4.3f\n", "\033[K", accData.xAxis.scaled); |
j3 | 2:0d7433075663 | 75 | printf("ACC yAxis = %s%4.3f\n", "\033[K", accData.yAxis.scaled); |
j3 | 2:0d7433075663 | 76 | printf("ACC zAxis = %s%4.3f\n", "\033[K", accData.zAxis.scaled); |
j3 | 3:250503cb7cb3 | 77 | printf("Sensor Time = %s%f\n", "\033[K", sensorTime); |
j3 | 3:250503cb7cb3 | 78 | printf("Sensor Temperature = %s%5.3f\n", "\033[K", imuTemperature); |
j3 | 2:0d7433075663 | 79 | printf("\033[H"); //home |
j3 | 0:0db9a7ed2e63 | 80 | } |
j3 | 0:0db9a7ed2e63 | 81 | } |
j3 | 0:0db9a7ed2e63 | 82 | |
j3 | 0:0db9a7ed2e63 | 83 | |
j3 | 0:0db9a7ed2e63 | 84 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 85 | void dumpImuRegisters(BMI160 &imu) |
j3 | 0:0db9a7ed2e63 | 86 | { |
j3 | 0:0db9a7ed2e63 | 87 | printRegister(imu, BMI160::CHIP_ID); |
j3 | 1:a3fa54415b4e | 88 | printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA); |
j3 | 1:a3fa54415b4e | 89 | printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1); |
j3 | 1:a3fa54415b4e | 90 | printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST); |
j3 | 1:a3fa54415b4e | 91 | printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1); |
j3 | 0:0db9a7ed2e63 | 92 | printRegister(imu, BMI160::CMD); |
j3 | 0:0db9a7ed2e63 | 93 | printf("\n"); |
j3 | 0:0db9a7ed2e63 | 94 | } |
j3 | 0:0db9a7ed2e63 | 95 | |
j3 | 0:0db9a7ed2e63 | 96 | |
j3 | 0:0db9a7ed2e63 | 97 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 98 | void printRegister(BMI160 &imu, BMI160::Registers reg) |
j3 | 0:0db9a7ed2e63 | 99 | { |
j3 | 0:0db9a7ed2e63 | 100 | uint8_t data; |
j3 | 1:a3fa54415b4e | 101 | if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 102 | { |
j3 | 0:0db9a7ed2e63 | 103 | printf("IMU Register 0x%02x = 0x%02x\n", reg, data); |
j3 | 0:0db9a7ed2e63 | 104 | } |
j3 | 0:0db9a7ed2e63 | 105 | else |
j3 | 0:0db9a7ed2e63 | 106 | { |
j3 | 0:0db9a7ed2e63 | 107 | printf("Failed to read register\n"); |
j3 | 0:0db9a7ed2e63 | 108 | } |
j3 | 0:0db9a7ed2e63 | 109 | } |
j3 | 0:0db9a7ed2e63 | 110 | |
j3 | 0:0db9a7ed2e63 | 111 | |
j3 | 0:0db9a7ed2e63 | 112 | //***************************************************************************** |
j3 | 1:a3fa54415b4e | 113 | void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg) |
j3 | 0:0db9a7ed2e63 | 114 | { |
j3 | 0:0db9a7ed2e63 | 115 | uint8_t numBytes = ((stopReg - startReg) + 1); |
j3 | 1:a3fa54415b4e | 116 | uint8_t buff[numBytes]; |
j3 | 0:0db9a7ed2e63 | 117 | uint8_t offset = static_cast<uint8_t>(startReg); |
j3 | 0:0db9a7ed2e63 | 118 | |
j3 | 1:a3fa54415b4e | 119 | if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 120 | { |
j3 | 0:0db9a7ed2e63 | 121 | for(uint8_t idx = offset; idx < (numBytes + offset); idx++) |
j3 | 0:0db9a7ed2e63 | 122 | { |
j3 | 0:0db9a7ed2e63 | 123 | printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]); |
j3 | 0:0db9a7ed2e63 | 124 | } |
j3 | 0:0db9a7ed2e63 | 125 | } |
j3 | 0:0db9a7ed2e63 | 126 | else |
j3 | 0:0db9a7ed2e63 | 127 | { |
j3 | 0:0db9a7ed2e63 | 128 | printf("Failed to read block\n"); |
j3 | 0:0db9a7ed2e63 | 129 | } |
j3 | 0:0db9a7ed2e63 | 130 | } |