intiial commit

Dependencies:   MAX8614X USBDevice max32630hsp_test

Committer:
phonemacro
Date:
Fri Aug 10 23:23:21 2018 +0000
Revision:
0:9e5a4f845510
Child:
1:854f8a89a527
initial commit, working for device id

Who changed what in which revision?

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