Library for Bosch Sensortech BMI160 IMU

Dependents:   MAX32630FTHR_BALANCE_BOT MPSMAX_copy MAX32630FTHR_BALANCE_BOT SELF_BALANCING_BOT

Committer:
j3
Date:
Fri Dec 09 00:29:29 2016 +0000
Revision:
3:e1770675eca4
Parent:
2:598e601e5846
Child:
9:ca6b5fecdd63
Added register enumeration documentation

Who changed what in which revision?

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