Hello World edited
Dependencies: mbed BMI160 max32630fthr
main.cpp@12:1a0142dd9994, 2021-12-29 (annotated)
- Committer:
- walterluu
- Date:
- Wed Dec 29 06:58:49 2021 +0000
- Revision:
- 12:1a0142dd9994
- Parent:
- 10:9882df01637d
Hello World edited
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 | 10:9882df01637d | 35 | //#include "max32630fthr.h" |
j3 | 0:0db9a7ed2e63 | 36 | #include "bmi160.h" |
j3 | 0:0db9a7ed2e63 | 37 | |
j3 | 1:a3fa54415b4e | 38 | |
j3 | 0:0db9a7ed2e63 | 39 | void dumpImuRegisters(BMI160 &imu); |
j3 | 0:0db9a7ed2e63 | 40 | void printRegister(BMI160 &imu, BMI160::Registers reg); |
j3 | 1:a3fa54415b4e | 41 | void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg); |
j3 | 1:a3fa54415b4e | 42 | |
j3 | 0:0db9a7ed2e63 | 43 | |
j3 | 0:0db9a7ed2e63 | 44 | int main() |
j3 | 0:0db9a7ed2e63 | 45 | { |
j3 | 10:9882df01637d | 46 | //MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
j3 | 0:0db9a7ed2e63 | 47 | |
j3 | 0:0db9a7ed2e63 | 48 | DigitalOut rLED(LED1, LED_OFF); |
j3 | 0:0db9a7ed2e63 | 49 | DigitalOut gLED(LED2, LED_OFF); |
j3 | 4:3d7fae7f7b75 | 50 | DigitalOut bLED(LED3, LED_OFF); |
j3 | 0:0db9a7ed2e63 | 51 | |
j3 | 1:a3fa54415b4e | 52 | I2C i2cBus(P5_7, P6_0); |
j3 | 3:250503cb7cb3 | 53 | i2cBus.frequency(400000); |
j3 | 1:a3fa54415b4e | 54 | BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO); |
j3 | 0:0db9a7ed2e63 | 55 | |
walterluu | 12:1a0142dd9994 | 56 | // printf("\033[H"); //home |
walterluu | 12:1a0142dd9994 | 57 | // printf("\033[0J"); //erase from cursor to end of screen |
walterluu | 12:1a0142dd9994 | 58 | |
j3 | 1:a3fa54415b4e | 59 | |
j3 | 4:3d7fae7f7b75 | 60 | uint32_t failures = 0; |
j3 | 4:3d7fae7f7b75 | 61 | |
j3 | 5:4deb38e46a10 | 62 | if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) |
j3 | 5:4deb38e46a10 | 63 | { |
j3 | 5:4deb38e46a10 | 64 | printf("Failed to set gyroscope power mode\n"); |
j3 | 5:4deb38e46a10 | 65 | failures++; |
j3 | 5:4deb38e46a10 | 66 | } |
j3 | 5:4deb38e46a10 | 67 | wait_ms(100); |
j3 | 5:4deb38e46a10 | 68 | |
j3 | 5:4deb38e46a10 | 69 | if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) |
j3 | 5:4deb38e46a10 | 70 | { |
j3 | 5:4deb38e46a10 | 71 | printf("Failed to set accelerometer power mode\n"); |
j3 | 5:4deb38e46a10 | 72 | failures++; |
j3 | 5:4deb38e46a10 | 73 | } |
j3 | 5:4deb38e46a10 | 74 | wait_ms(100); |
j3 | 5:4deb38e46a10 | 75 | |
j3 | 4:3d7fae7f7b75 | 76 | |
j3 | 4:3d7fae7f7b75 | 77 | BMI160::AccConfig accConfig; |
j3 | 4:3d7fae7f7b75 | 78 | //example of using getSensorConfig |
j3 | 4:3d7fae7f7b75 | 79 | if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) |
j3 | 4:3d7fae7f7b75 | 80 | { |
j3 | 4:3d7fae7f7b75 | 81 | printf("ACC Range = %d\n", accConfig.range); |
j3 | 4:3d7fae7f7b75 | 82 | printf("ACC UnderSampling = %d\n", accConfig.us); |
j3 | 4:3d7fae7f7b75 | 83 | printf("ACC BandWidthParam = %d\n", accConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 84 | printf("ACC OutputDataRate = %d\n\n", accConfig.odr); |
j3 | 4:3d7fae7f7b75 | 85 | } |
j3 | 4:3d7fae7f7b75 | 86 | else |
j3 | 4:3d7fae7f7b75 | 87 | { |
j3 | 4:3d7fae7f7b75 | 88 | printf("Failed to get accelerometer configuration\n"); |
j3 | 4:3d7fae7f7b75 | 89 | failures++; |
j3 | 4:3d7fae7f7b75 | 90 | } |
j3 | 1:a3fa54415b4e | 91 | |
j3 | 4:3d7fae7f7b75 | 92 | //example of setting user defined configuration |
j3 | 4:3d7fae7f7b75 | 93 | accConfig.range = BMI160::SENS_4G; |
j3 | 4:3d7fae7f7b75 | 94 | accConfig.us = BMI160::ACC_US_OFF; |
j3 | 4:3d7fae7f7b75 | 95 | accConfig.bwp = BMI160::ACC_BWP_2; |
j3 | 4:3d7fae7f7b75 | 96 | accConfig.odr = BMI160::ACC_ODR_8; |
j3 | 4:3d7fae7f7b75 | 97 | if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) |
j3 | 4:3d7fae7f7b75 | 98 | { |
j3 | 4:3d7fae7f7b75 | 99 | printf("ACC Range = %d\n", accConfig.range); |
j3 | 4:3d7fae7f7b75 | 100 | printf("ACC UnderSampling = %d\n", accConfig.us); |
j3 | 4:3d7fae7f7b75 | 101 | printf("ACC BandWidthParam = %d\n", accConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 102 | printf("ACC OutputDataRate = %d\n\n", accConfig.odr); |
j3 | 4:3d7fae7f7b75 | 103 | } |
j3 | 4:3d7fae7f7b75 | 104 | else |
j3 | 4:3d7fae7f7b75 | 105 | { |
j3 | 4:3d7fae7f7b75 | 106 | printf("Failed to set accelerometer configuration\n"); |
j3 | 4:3d7fae7f7b75 | 107 | failures++; |
j3 | 4:3d7fae7f7b75 | 108 | } |
j3 | 4:3d7fae7f7b75 | 109 | |
j3 | 4:3d7fae7f7b75 | 110 | BMI160::GyroConfig gyroConfig; |
j3 | 4:3d7fae7f7b75 | 111 | if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 112 | { |
j3 | 4:3d7fae7f7b75 | 113 | printf("GYRO Range = %d\n", gyroConfig.range); |
j3 | 4:3d7fae7f7b75 | 114 | printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp); |
j3 | 4:3d7fae7f7b75 | 115 | printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr); |
j3 | 4:3d7fae7f7b75 | 116 | } |
j3 | 4:3d7fae7f7b75 | 117 | else |
j3 | 4:3d7fae7f7b75 | 118 | { |
j3 | 4:3d7fae7f7b75 | 119 | printf("Failed to get gyroscope configuration\n"); |
j3 | 4:3d7fae7f7b75 | 120 | failures++; |
j3 | 4:3d7fae7f7b75 | 121 | } |
j3 | 4:3d7fae7f7b75 | 122 | |
j3 | 6:1d23dc51ee3f | 123 | wait(1.0); |
walterluu | 12:1a0142dd9994 | 124 | // printf("\033[H"); //home |
walterluu | 12:1a0142dd9994 | 125 | // printf("\033[0J"); //erase from cursor to end of screen |
j3 | 4:3d7fae7f7b75 | 126 | |
j3 | 3:250503cb7cb3 | 127 | |
walterluu | 12:1a0142dd9994 | 128 | bLED = !bLED; |
walterluu | 12:1a0142dd9994 | 129 | wait(5); |
walterluu | 12:1a0142dd9994 | 130 | printf("Hello World!!\n"); |
walterluu | 12:1a0142dd9994 | 131 | wait(3); |
walterluu | 12:1a0142dd9994 | 132 | printf("Testing"); |
walterluu | 12:1a0142dd9994 | 133 | wait(5); |
walterluu | 12:1a0142dd9994 | 134 | bLED = !bLED; |
walterluu | 12:1a0142dd9994 | 135 | |
walterluu | 12:1a0142dd9994 | 136 | // while (1) { |
walterluu | 12:1a0142dd9994 | 137 | // for (int i = 0; i < 5; i++) { |
walterluu | 12:1a0142dd9994 | 138 | // printf("Hello World!!\n"); |
walterluu | 12:1a0142dd9994 | 139 | // printf("Testing"); |
walterluu | 12:1a0142dd9994 | 140 | // // rLED = !rLED; |
walterluu | 12:1a0142dd9994 | 141 | // gLED = !gLED; |
walterluu | 12:1a0142dd9994 | 142 | // } |
walterluu | 12:1a0142dd9994 | 143 | // } |
walterluu | 12:1a0142dd9994 | 144 | |
walterluu | 12:1a0142dd9994 | 145 | |
walterluu | 12:1a0142dd9994 | 146 | // if(failures == 0) |
walterluu | 12:1a0142dd9994 | 147 | // { |
walterluu | 12:1a0142dd9994 | 148 | // float imuTemperature; |
walterluu | 12:1a0142dd9994 | 149 | // BMI160::SensorData accData; |
walterluu | 12:1a0142dd9994 | 150 | // BMI160::SensorData gyroData; |
walterluu | 12:1a0142dd9994 | 151 | // BMI160::SensorTime sensorTime; |
walterluu | 12:1a0142dd9994 | 152 | // |
walterluu | 12:1a0142dd9994 | 153 | // while(1) |
walterluu | 12:1a0142dd9994 | 154 | // { |
walterluu | 12:1a0142dd9994 | 155 | // imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range); |
walterluu | 12:1a0142dd9994 | 156 | // imu.getTemperature(&imuTemperature); |
walterluu | 12:1a0142dd9994 | 157 | // |
walterluu | 12:1a0142dd9994 | 158 | // printf("ACC xAxis = %s%4.3f\n", "\033[K", accData.xAxis.scaled); |
walterluu | 12:1a0142dd9994 | 159 | // printf("ACC yAxis = %s%4.3f\n", "\033[K", accData.yAxis.scaled); |
walterluu | 12:1a0142dd9994 | 160 | // printf("ACC zAxis = %s%4.3f\n\n", "\033[K", accData.zAxis.scaled); |
walterluu | 12:1a0142dd9994 | 161 | // |
walterluu | 12:1a0142dd9994 | 162 | // printf("GYRO xAxis = %s%5.1f\n", "\033[K", gyroData.xAxis.scaled); |
walterluu | 12:1a0142dd9994 | 163 | // printf("GYRO yAxis = %s%5.1f\n", "\033[K", gyroData.yAxis.scaled); |
walterluu | 12:1a0142dd9994 | 164 | // printf("GYRO zAxis = %s%5.1f\n\n", "\033[K", gyroData.zAxis.scaled); |
walterluu | 12:1a0142dd9994 | 165 | // |
walterluu | 12:1a0142dd9994 | 166 | // printf("Sensor Time = %s%f\n", "\033[K", sensorTime.seconds); |
walterluu | 12:1a0142dd9994 | 167 | // printf("Sensor Temperature = %s%5.3f\n", "\033[K", imuTemperature); |
walterluu | 12:1a0142dd9994 | 168 | // |
walterluu | 12:1a0142dd9994 | 169 | // printf("\033[H"); //home |
walterluu | 12:1a0142dd9994 | 170 | // gLED = !gLED; |
walterluu | 12:1a0142dd9994 | 171 | // } |
walterluu | 12:1a0142dd9994 | 172 | // } |
walterluu | 12:1a0142dd9994 | 173 | // else |
walterluu | 12:1a0142dd9994 | 174 | // { |
walterluu | 12:1a0142dd9994 | 175 | // while(1) |
walterluu | 12:1a0142dd9994 | 176 | // { |
walterluu | 12:1a0142dd9994 | 177 | // rLED = !rLED; |
walterluu | 12:1a0142dd9994 | 178 | // wait(0.25); |
walterluu | 12:1a0142dd9994 | 179 | // } |
walterluu | 12:1a0142dd9994 | 180 | // } |
j3 | 0:0db9a7ed2e63 | 181 | } |
j3 | 0:0db9a7ed2e63 | 182 | |
j3 | 0:0db9a7ed2e63 | 183 | |
j3 | 0:0db9a7ed2e63 | 184 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 185 | void dumpImuRegisters(BMI160 &imu) |
j3 | 0:0db9a7ed2e63 | 186 | { |
j3 | 0:0db9a7ed2e63 | 187 | printRegister(imu, BMI160::CHIP_ID); |
j3 | 1:a3fa54415b4e | 188 | printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA); |
j3 | 1:a3fa54415b4e | 189 | printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1); |
j3 | 1:a3fa54415b4e | 190 | printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST); |
j3 | 1:a3fa54415b4e | 191 | printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1); |
j3 | 0:0db9a7ed2e63 | 192 | printRegister(imu, BMI160::CMD); |
j3 | 0:0db9a7ed2e63 | 193 | printf("\n"); |
j3 | 0:0db9a7ed2e63 | 194 | } |
j3 | 0:0db9a7ed2e63 | 195 | |
j3 | 0:0db9a7ed2e63 | 196 | |
j3 | 0:0db9a7ed2e63 | 197 | //***************************************************************************** |
j3 | 0:0db9a7ed2e63 | 198 | void printRegister(BMI160 &imu, BMI160::Registers reg) |
j3 | 0:0db9a7ed2e63 | 199 | { |
j3 | 0:0db9a7ed2e63 | 200 | uint8_t data; |
j3 | 1:a3fa54415b4e | 201 | if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 202 | { |
j3 | 0:0db9a7ed2e63 | 203 | printf("IMU Register 0x%02x = 0x%02x\n", reg, data); |
j3 | 0:0db9a7ed2e63 | 204 | } |
j3 | 0:0db9a7ed2e63 | 205 | else |
j3 | 0:0db9a7ed2e63 | 206 | { |
j3 | 0:0db9a7ed2e63 | 207 | printf("Failed to read register\n"); |
j3 | 0:0db9a7ed2e63 | 208 | } |
j3 | 0:0db9a7ed2e63 | 209 | } |
j3 | 0:0db9a7ed2e63 | 210 | |
j3 | 0:0db9a7ed2e63 | 211 | |
j3 | 0:0db9a7ed2e63 | 212 | //***************************************************************************** |
j3 | 1:a3fa54415b4e | 213 | void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg) |
j3 | 0:0db9a7ed2e63 | 214 | { |
j3 | 0:0db9a7ed2e63 | 215 | uint8_t numBytes = ((stopReg - startReg) + 1); |
j3 | 1:a3fa54415b4e | 216 | uint8_t buff[numBytes]; |
j3 | 0:0db9a7ed2e63 | 217 | uint8_t offset = static_cast<uint8_t>(startReg); |
j3 | 0:0db9a7ed2e63 | 218 | |
j3 | 1:a3fa54415b4e | 219 | if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR) |
j3 | 0:0db9a7ed2e63 | 220 | { |
j3 | 0:0db9a7ed2e63 | 221 | for(uint8_t idx = offset; idx < (numBytes + offset); idx++) |
j3 | 0:0db9a7ed2e63 | 222 | { |
j3 | 0:0db9a7ed2e63 | 223 | printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]); |
j3 | 0:0db9a7ed2e63 | 224 | } |
j3 | 0:0db9a7ed2e63 | 225 | } |
j3 | 0:0db9a7ed2e63 | 226 | else |
j3 | 0:0db9a7ed2e63 | 227 | { |
j3 | 0:0db9a7ed2e63 | 228 | printf("Failed to read block\n"); |
j3 | 0:0db9a7ed2e63 | 229 | } |
j3 | 0:0db9a7ed2e63 | 230 | } |