Library for Bosch Sensortec BMI160 IMU

Dependents:   Rocket MAX32630FTHR_JOYSTICK MAX32630FTHR_IMU_Hello_World Pike_the_Flipper_Main_Branch ... more

Fork of BMI160 by Justin Jordan

Committer:
j3
Date:
Wed Dec 07 19:45:06 2016 +0000
Revision:
0:bb5b832891fb
Init Commit, basic skeleton of class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:bb5b832891fb 1 /**********************************************************************
j3 0:bb5b832891fb 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:bb5b832891fb 3 *
j3 0:bb5b832891fb 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:bb5b832891fb 5 * copy of this software and associated documentation files (the "Software"),
j3 0:bb5b832891fb 6 * to deal in the Software without restriction, including without limitation
j3 0:bb5b832891fb 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:bb5b832891fb 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:bb5b832891fb 9 * Software is furnished to do so, subject to the following conditions:
j3 0:bb5b832891fb 10 *
j3 0:bb5b832891fb 11 * The above copyright notice and this permission notice shall be included
j3 0:bb5b832891fb 12 * in all copies or substantial portions of the Software.
j3 0:bb5b832891fb 13 *
j3 0:bb5b832891fb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:bb5b832891fb 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:bb5b832891fb 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:bb5b832891fb 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:bb5b832891fb 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:bb5b832891fb 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:bb5b832891fb 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:bb5b832891fb 21 *
j3 0:bb5b832891fb 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:bb5b832891fb 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:bb5b832891fb 24 * Products, Inc. Branding Policy.
j3 0:bb5b832891fb 25 *
j3 0:bb5b832891fb 26 * The mere transfer of this software does not imply any licenses
j3 0:bb5b832891fb 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:bb5b832891fb 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:bb5b832891fb 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:bb5b832891fb 30 * ownership rights.
j3 0:bb5b832891fb 31 **********************************************************************/
j3 0:bb5b832891fb 32
j3 0:bb5b832891fb 33
j3 0:bb5b832891fb 34 #include "bmi160.h"
j3 0:bb5b832891fb 35
j3 0:bb5b832891fb 36
j3 0:bb5b832891fb 37 //*****************************************************************************
j3 0:bb5b832891fb 38 BMI160::BMI160(I2C &i2cBus, uint8_t i2cAdrs)
j3 0:bb5b832891fb 39 :m_i2cBus(i2cBus), m_Wadrs(i2cAdrs << 1), m_Radrs((i2cAdrs << 1) | 1)
j3 0:bb5b832891fb 40 {
j3 0:bb5b832891fb 41 }
j3 0:bb5b832891fb 42
j3 0:bb5b832891fb 43
j3 0:bb5b832891fb 44 //*****************************************************************************
j3 0:bb5b832891fb 45 BMI160::~BMI160()
j3 0:bb5b832891fb 46 {
j3 0:bb5b832891fb 47 }
j3 0:bb5b832891fb 48
j3 0:bb5b832891fb 49
j3 0:bb5b832891fb 50 //*****************************************************************************
j3 0:bb5b832891fb 51 int32_t BMI160::readRegister(Registers reg, uint8_t *data)
j3 0:bb5b832891fb 52 {
j3 0:bb5b832891fb 53 int32_t rtnVal = -1;
j3 0:bb5b832891fb 54 char packet[] = {static_cast<char>(reg)};
j3 0:bb5b832891fb 55
j3 0:bb5b832891fb 56 if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
j3 0:bb5b832891fb 57 {
j3 0:bb5b832891fb 58 rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), 1);
j3 0:bb5b832891fb 59 }
j3 0:bb5b832891fb 60
j3 0:bb5b832891fb 61 return rtnVal;
j3 0:bb5b832891fb 62 }
j3 0:bb5b832891fb 63
j3 0:bb5b832891fb 64
j3 0:bb5b832891fb 65 //*****************************************************************************
j3 0:bb5b832891fb 66 int32_t BMI160::writeRegister(Registers reg, uint8_t data)
j3 0:bb5b832891fb 67 {
j3 0:bb5b832891fb 68 char packet[] = {static_cast<char>(reg), static_cast<char>(data)};
j3 0:bb5b832891fb 69
j3 0:bb5b832891fb 70 return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
j3 0:bb5b832891fb 71 }
j3 0:bb5b832891fb 72
j3 0:bb5b832891fb 73
j3 0:bb5b832891fb 74 //*****************************************************************************
j3 0:bb5b832891fb 75 int32_t BMI160::readBlock(Registers startReg, Registers stopReg, uint8_t *data)
j3 0:bb5b832891fb 76 {
j3 0:bb5b832891fb 77 int32_t rtnVal = -1;
j3 0:bb5b832891fb 78 int32_t numBytes = ((stopReg - startReg) + 1);
j3 0:bb5b832891fb 79 char packet[] = {static_cast<char>(startReg)};
j3 0:bb5b832891fb 80
j3 0:bb5b832891fb 81 if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
j3 0:bb5b832891fb 82 {
j3 0:bb5b832891fb 83 rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), numBytes);
j3 0:bb5b832891fb 84 }
j3 0:bb5b832891fb 85
j3 0:bb5b832891fb 86 return rtnVal;
j3 0:bb5b832891fb 87 }
j3 0:bb5b832891fb 88
j3 0:bb5b832891fb 89
j3 0:bb5b832891fb 90 //*****************************************************************************
j3 0:bb5b832891fb 91 int32_t BMI160::writeBlock(Registers startReg, Registers stopReg, uint8_t *data)
j3 0:bb5b832891fb 92 {
j3 0:bb5b832891fb 93 int32_t numBytes = ((stopReg - startReg) + 1);
j3 0:bb5b832891fb 94 char packet[numBytes + 1];
j3 0:bb5b832891fb 95
j3 0:bb5b832891fb 96 packet[0] = static_cast<char>(startReg);
j3 0:bb5b832891fb 97
j3 0:bb5b832891fb 98 memcpy(packet + 1, data, numBytes);
j3 0:bb5b832891fb 99
j3 0:bb5b832891fb 100 return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
j3 0:bb5b832891fb 101 }