Maxim Integrated / Mbed 2 deprecated MAX32630HSP3_IMU_HelloWorld

Dependencies:   BMI160 max32630hsp2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 **********************************************************************/
00032 
00033 
00034 #include "mbed.h"
00035 #include "bmi160.h"
00036 #include "max32630hsp.h"
00037 
00038 MAX32630HSP icarus(MAX32630HSP::VIO_3V3);
00039 
00040 DigitalOut rLED(LED1, LED_OFF);
00041 DigitalOut gLED(LED2, LED_OFF);
00042 DigitalOut bLED(LED3, LED_OFF);
00043 
00044 Serial pc(USBTX,USBRX);
00045 
00046 I2C i2cBus(I2C2_SDA, I2C2_SCL);
00047 
00048 BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
00049 
00050 void dumpImuRegisters(BMI160 &imu);
00051 void printRegister(BMI160 &imu, BMI160::Registers reg);
00052 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg);
00053 
00054 int main()
00055 {
00056     i2cBus.frequency(400000);
00057 
00058     pc.printf("\033[H");  //home
00059     pc.printf("\033[0J");  //erase from cursor to end of screen
00060 
00061     uint32_t failures = 0;
00062 
00063     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
00064     {
00065         pc.printf("Failed to set gyroscope power mode\r\n");
00066         failures++;
00067     }
00068     wait_ms(100);
00069 
00070     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
00071     {
00072         pc.printf("Failed to set accelerometer power mode\r\n");
00073         failures++;
00074     }
00075     wait_ms(100);
00076 
00077 
00078     BMI160::AccConfig accConfig;
00079     //example of using getSensorConfig
00080     if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
00081     {
00082         pc.printf("ACC Range = %d\r\n", accConfig.range);
00083         pc.printf("ACC UnderSampling = %d\r\n", accConfig.us);
00084         pc.printf("ACC BandWidthParam = %d\r\n", accConfig.bwp);
00085         pc.printf("ACC OutputDataRate = %d\r\n\r\n", accConfig.odr);
00086     }
00087     else
00088     {
00089         pc.printf("Failed to get accelerometer configuration\r\n");
00090         failures++;
00091     }
00092 
00093     //example of setting user defined configuration
00094     accConfig.range = BMI160::SENS_4G;
00095     accConfig.us = BMI160::ACC_US_OFF;
00096     accConfig.bwp = BMI160::ACC_BWP_2;
00097     accConfig.odr = BMI160::ACC_ODR_8;
00098     if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
00099     {
00100         pc.printf("ACC Range = %d\r\n", accConfig.range);
00101         pc.printf("ACC UnderSampling = %d\r\n", accConfig.us);
00102         pc.printf("ACC BandWidthParam = %d\r\n", accConfig.bwp);
00103         pc.printf("ACC OutputDataRate = %d\r\n\r\n", accConfig.odr);
00104     }
00105     else
00106     {
00107         pc.printf("Failed to set accelerometer configuration\r\n");
00108         failures++;
00109     }
00110 
00111     BMI160::GyroConfig gyroConfig;
00112     if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
00113     {
00114         pc.printf("GYRO Range = %d\r\n", gyroConfig.range);
00115         pc.printf("GYRO BandWidthParam = %d\r\n", gyroConfig.bwp);
00116         pc.printf("GYRO OutputDataRate = %d\r\n\r\n", gyroConfig.odr);
00117     }
00118     else
00119     {
00120         pc.printf("Failed to get gyroscope configuration\r\n");
00121         failures++;
00122     }
00123 
00124     wait(1.0);
00125     pc.printf("\033[H");  //home
00126     pc.printf("\033[0J");  //erase from cursor to end of screen
00127 
00128     if(failures == 0)
00129     {
00130         float imuTemperature;
00131         BMI160::SensorData accData;
00132         BMI160::SensorData gyroData;
00133         BMI160::SensorTime sensorTime;
00134 
00135         while(1)
00136         {
00137             imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
00138             imu.getTemperature(&imuTemperature);
00139 
00140             pc.printf("ACC xAxis = %s%4.3f\r\n", "\033[K", accData.xAxis.scaled);
00141             pc.printf("ACC yAxis = %s%4.3f\r\n", "\033[K", accData.yAxis.scaled);
00142             pc.printf("ACC zAxis = %s%4.3f\r\n\r\n", "\033[K", accData.zAxis.scaled);
00143 
00144             pc.printf("GYRO xAxis = %s%5.1f\r\n", "\033[K", gyroData.xAxis.scaled);
00145             pc.printf("GYRO yAxis = %s%5.1f\r\n", "\033[K", gyroData.yAxis.scaled);
00146             pc.printf("GYRO zAxis = %s%5.1f\r\n\r\n", "\033[K", gyroData.zAxis.scaled);
00147 
00148             pc.printf("Sensor Time = %s%f\r\n", "\033[K", sensorTime.seconds);
00149             pc.printf("Sensor Temperature = %s%5.3f\r\n", "\033[K", imuTemperature);
00150 
00151             pc.printf("\033[H");  //home
00152             gLED = !gLED;
00153             wait(0.5);
00154         }
00155     }
00156     else
00157     {
00158         while(1)
00159         {
00160             rLED = !rLED;
00161             wait(0.25);
00162         }
00163     }
00164 }
00165 
00166 
00167 //*****************************************************************************
00168 void dumpImuRegisters(BMI160 &imu)
00169 {
00170     printRegister(imu, BMI160::CHIP_ID);
00171     printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA);
00172     printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1);
00173     printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST);
00174     printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1);
00175     printRegister(imu, BMI160::CMD);
00176     pc.printf("\r\n");
00177 }
00178 
00179 
00180 //*****************************************************************************
00181 void printRegister(BMI160 &imu, BMI160::Registers reg)
00182 {
00183     uint8_t data;
00184     if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR)
00185     {
00186         pc.printf("IMU Register 0x%02x = 0x%02x\r\n", reg, data);
00187     }
00188     else
00189     {
00190         pc.printf("Failed to read register\r\n");
00191     }
00192 }
00193 
00194 
00195 //*****************************************************************************
00196 void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg)
00197 {
00198     uint8_t numBytes = ((stopReg - startReg) + 1);
00199     uint8_t buff[32];
00200     uint8_t offset = static_cast<uint8_t>(startReg);
00201 
00202     if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR)
00203     {
00204         for(uint8_t idx = offset; idx < (numBytes + offset); idx++)
00205         {
00206             pc.printf("IMU Register 0x%02x = 0x%02x\r\n", idx, buff[idx - offset]);
00207         }
00208     }
00209     else
00210     {
00211         pc.printf("Failed to read block\r\n");
00212     }
00213 }