First Commit as a new library

Dependents:   Host_Software_MAX32664GWEB_HR_wrist Host_Software_MAX32664GWEC_SpO2_HR Host_Software_MAX32664GWEB_HR_EXTENDED Host_Software_MAX32664GWEC_SpO2_HR-_EXTE ... more

Committer:
seyhmuscacina
Date:
Wed Dec 19 14:54:05 2018 +0300
Revision:
21:f7216b5dc6c0
Parent:
19:8e66f58bef44
adds interrupt support to the BMI160 library

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 //*****************************************************************************
seyhmuscacina 21:f7216b5dc6c0 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 {
seyhmuscacina 21:f7216b5dc6c0 41
j3 2:598e601e5846 42 }
j3 2:598e601e5846 43
seyhmuscacina 21:f7216b5dc6c0 44 BMI160_I2C::BMI160_I2C(I2C *i2cBus, uint8_t i2cAdrs, InterruptIn *int_pin)
seyhmuscacina 21:f7216b5dc6c0 45 :m_i2cBus(i2cBus), m_Wadrs(i2cAdrs << 1), m_Radrs((i2cAdrs << 1) | 1), BMI160(int_pin)
seyhmuscacina 21:f7216b5dc6c0 46 {
j3 2:598e601e5846 47
seyhmuscacina 21:f7216b5dc6c0 48 }
seyhmuscacina 21:f7216b5dc6c0 49
seyhmuscacina 21:f7216b5dc6c0 50 //*****************************************************************************
j3 2:598e601e5846 51 int32_t BMI160_I2C::readRegister(Registers reg, uint8_t *data)
j3 2:598e601e5846 52 {
j3 2:598e601e5846 53 int32_t rtnVal = -1;
j3 2:598e601e5846 54 char packet[] = {static_cast<char>(reg)};
seyhmuscacina 21:f7216b5dc6c0 55
seyhmuscacina 21:f7216b5dc6c0 56 if(m_i2cBus->write(m_Wadrs, packet, 1) == 0)
j3 2:598e601e5846 57 {
seyhmuscacina 21:f7216b5dc6c0 58 rtnVal = m_i2cBus->read(m_Radrs, reinterpret_cast<char *>(data), 1);
j3 2:598e601e5846 59 }
seyhmuscacina 21:f7216b5dc6c0 60
j3 2:598e601e5846 61 return rtnVal;
j3 2:598e601e5846 62 }
j3 2:598e601e5846 63
j3 2:598e601e5846 64
j3 2:598e601e5846 65 //*****************************************************************************
j3 2:598e601e5846 66 int32_t BMI160_I2C::writeRegister(Registers reg, const uint8_t data)
j3 2:598e601e5846 67 {
j3 2:598e601e5846 68 char packet[] = {static_cast<char>(reg), static_cast<char>(data)};
seyhmuscacina 21:f7216b5dc6c0 69
seyhmuscacina 21:f7216b5dc6c0 70 return m_i2cBus->write(m_Wadrs, packet, sizeof(packet));
j3 2:598e601e5846 71 }
j3 2:598e601e5846 72
j3 2:598e601e5846 73
j3 2:598e601e5846 74 //*****************************************************************************
seyhmuscacina 21:f7216b5dc6c0 75 int32_t BMI160_I2C::readBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 76 uint8_t *data)
j3 2:598e601e5846 77 {
j3 2:598e601e5846 78 int32_t rtnVal = -1;
j3 2:598e601e5846 79 int32_t numBytes = ((stopReg - startReg) + 1);
j3 2:598e601e5846 80 char packet[] = {static_cast<char>(startReg)};
seyhmuscacina 21:f7216b5dc6c0 81
seyhmuscacina 21:f7216b5dc6c0 82 if(m_i2cBus->write(m_Wadrs, packet, 1) == 0)
j3 2:598e601e5846 83 {
seyhmuscacina 21:f7216b5dc6c0 84 rtnVal = m_i2cBus->read(m_Radrs, reinterpret_cast<char *>(data), numBytes);
j3 2:598e601e5846 85 }
seyhmuscacina 21:f7216b5dc6c0 86
j3 2:598e601e5846 87 return rtnVal;
j3 2:598e601e5846 88 }
j3 2:598e601e5846 89
j3 2:598e601e5846 90
j3 2:598e601e5846 91 //*****************************************************************************
seyhmuscacina 21:f7216b5dc6c0 92 int32_t BMI160_I2C::writeBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 93 const uint8_t *data)
j3 2:598e601e5846 94 {
j3 2:598e601e5846 95 int32_t numBytes = ((stopReg - startReg) + 1);
seyhmuscacina 21:f7216b5dc6c0 96 char packet[numBytes + 1];
seyhmuscacina 21:f7216b5dc6c0 97
j3 2:598e601e5846 98 packet[0] = static_cast<char>(startReg);
seyhmuscacina 21:f7216b5dc6c0 99
j3 2:598e601e5846 100 memcpy(packet + 1, data, numBytes);
seyhmuscacina 21:f7216b5dc6c0 101
seyhmuscacina 21:f7216b5dc6c0 102 return m_i2cBus->write(m_Wadrs, packet, sizeof(packet));
j3 2:598e601e5846 103 }