BMI160 Initial

Dependents:   MAX32630HSP3_IMU_HelloWorld MAX32630HSP3_IMU_HelloWorld MAX32630HSP3_Pitch_Charles Maxim_Squeeks

Files at this revision

API Documentation at this revision

Comitter:
j3
Date:
Thu Dec 08 00:32:41 2016 +0000
Parent:
1:a4c911640569
Child:
3:e1770675eca4
Commit message:
Made BMI160 abstract base class and created derived classes BMI160_I2C and BMI160_SPI to support both interfaces.

Changed in this revision

bmi160.cpp Show diff for this revision Revisions of this file
bmi160.h Show annotated file Show diff for this revision Revisions of this file
bmi160_i2c.cpp Show annotated file Show diff for this revision Revisions of this file
bmi160_spi.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/bmi160.cpp	Wed Dec 07 20:17:43 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,101 +0,0 @@
-/**********************************************************************
-* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-**********************************************************************/
-
-
-#include "bmi160.h"
-
-
-//*****************************************************************************
-BMI160::BMI160(I2C &i2cBus, uint8_t i2cAdrs)
-:m_i2cBus(i2cBus), m_Wadrs(i2cAdrs << 1), m_Radrs((i2cAdrs << 1) | 1)
-{
-}
-
-
-//*****************************************************************************
-BMI160::~BMI160()
-{
-}
-
-
-//*****************************************************************************   
-int32_t BMI160::readRegister(Registers reg, uint8_t *data)
-{
-    int32_t rtnVal = -1;
-    char packet[] = {static_cast<char>(reg)};
-    
-    if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
-    {
-        rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), 1);
-    }
-    
-    return rtnVal;
-}
-
-
-//*****************************************************************************
-int32_t BMI160::writeRegister(Registers reg, uint8_t data)
-{
-    char packet[] = {static_cast<char>(reg), static_cast<char>(data)};
-    
-    return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
-}
-
-
-//*****************************************************************************
-int32_t BMI160::readBlock(Registers startReg, Registers stopReg, uint8_t *data)
-{
-    int32_t rtnVal = -1;
-    int32_t numBytes = ((stopReg - startReg) + 1);
-    char packet[] = {static_cast<char>(startReg)};
-    
-    if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
-    {
-        rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), numBytes);
-    }
-    
-    return rtnVal;
-}
-
-
-//*****************************************************************************
-int32_t BMI160::writeBlock(Registers startReg, Registers stopReg, uint8_t *data)
-{
-    int32_t numBytes = ((stopReg - startReg) + 1);
-    char packet[numBytes + 1];
-    
-    packet[0] = static_cast<char>(startReg);
-    
-    memcpy(packet + 1, data, numBytes);
-    
-    return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
-}
--- a/bmi160.h	Wed Dec 07 20:17:43 2016 +0000
+++ b/bmi160.h	Thu Dec 08 00:32:41 2016 +0000
@@ -45,16 +45,15 @@
 current consumption is typically 950 μA, enabling always-on applications in
 battery driven devices. It is available in a compact 14-pin 2.5 x 3.0 x 0.8 mm³
 LGA package."
+
+This class is an abstract base class and can not be instaniated, use BMI160_I2C 
+or BMI160_SPI.
 */
 class BMI160
 {
 public:
 
-    ///BMI160 default I2C address
-    static const uint8_t I2C_ADRS_SDO_LO = 0x68;
-    ///BMI160 optional I2C address
-    static const uint8_t I2C_ADRS_SDO_HI = 0x69;
-    
+    ///Return value on success.
     static const uint8_t NO_ERROR = 0;
     
     ///BMI160 registers
@@ -154,18 +153,6 @@
         CMD = 0x7E
     };
     
-    ///@brief BMI160 Constructor.\n
-    ///
-    ///On Entry:
-    ///@param[in] i2cBus - reference to I2C bus for this device
-    ///@param[in] i2cAdrs - 7-bit I2C address
-    ///
-    ///On Exit:
-    ///@param[out] none
-    ///
-    ///@returns none
-    BMI160(I2C &i2cBus, uint8_t i2cAdrs);
-    
     
     ///@brief BMI160 Destructor.\n
     ///
@@ -176,7 +163,7 @@
     ///@param[out] none
     ///
     ///@returns none
-    ~BMI160();
+    virtual ~BMI160(){ }
     
     
     ///@brief Reads a single register.\n
@@ -188,7 +175,7 @@
     ///@param[out] data - holds contents of read register on success
     ///
     ///@returns 0 on success, non 0 on failure
-    int32_t readRegister(Registers reg, uint8_t *data);
+    virtual int32_t readRegister(Registers reg, uint8_t *data) = 0;
     
     
     ///@brief Writes a single register.\n
@@ -200,7 +187,7 @@
     ///@param[out] none
     ///
     ///@returns 0 on success, non 0 on failure
-    int32_t writeRegister(Registers reg, uint8_t data);
+    virtual int32_t writeRegister(Registers reg, const uint8_t data) = 0;
     
     
     ///@brief Reads a block of registers.\n
@@ -217,7 +204,7 @@
     ///@param[out] data - holds contents of read registers on success
     ///
     ///@returns 0 on success, non 0 on failure
-    int32_t readBlock(Registers startReg, Registers stopReg, uint8_t *data);
+    virtual int32_t readBlock(Registers startReg, Registers stopReg, uint8_t *data) = 0;
     
     
     ///@brief Writes a block of registers.\n
@@ -234,23 +221,90 @@
     ///@param[out] none
     ///
     ///@returns 0 on success, non 0 on failure
-    int32_t writeBlock(Registers startReg, Registers stopReg, uint8_t *data);
+    virtual int32_t writeBlock(Registers startReg, Registers stopReg, const uint8_t *data) = 0;
     
+private:
+
+};
+
+
+/**
+@brief BMI160_I2C - supports BMI160 object with I2C interface
+*/
+class BMI160_I2C: public BMI160
+{
+public:
+
+    ///BMI160 default I2C address.
+    static const uint8_t I2C_ADRS_SDO_LO = 0x68;
+    ///BMI160 optional I2C address.
+    static const uint8_t I2C_ADRS_SDO_HI = 0x69;
     
-    ///@brief fx documentation template.\n
+
+    ///@brief BMI160_I2C Constructor.\n
     ///
     ///On Entry:
-    ///@param[in] none 
+    ///@param[in] i2cBus - reference to I2C bus for this device
+    ///@param[in] i2cAdrs - 7-bit I2C address
     ///
     ///On Exit:
     ///@param[out] none
     ///
     ///@returns none
-
+    BMI160_I2C(I2C &i2cBus, uint8_t i2cAdrs);
+    
+    
+    virtual int32_t readRegister(Registers reg, uint8_t *data);
+    virtual int32_t writeRegister(Registers reg, const uint8_t data);
+    virtual int32_t readBlock(Registers startReg, Registers stopReg, uint8_t *data);
+    virtual int32_t writeBlock(Registers startReg, Registers stopReg, const uint8_t *data);
+    
 private:
 
     I2C m_i2cBus;
     uint8_t m_Wadrs, m_Radrs;
 };
 
+
+/**
+@brief BMI160_SPI - supports BMI160 object with SPI interface
+*/
+class BMI160_SPI: public BMI160
+{
+public:
+
+    ///@brief BMI160_SPI Constructor.\n
+    ///
+    ///On Entry:
+    ///@param[in] spiBus - reference to SPI bus for this device
+    ///@param[in] cs - reference to DigitalOut used for chip select
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    BMI160_SPI(SPI &spiBus, DigitalOut &cs);
+    
+    virtual int32_t readRegister(Registers reg, uint8_t *data);
+    virtual int32_t writeRegister(Registers reg, const uint8_t data);
+    virtual int32_t readBlock(Registers startReg, Registers stopReg, uint8_t *data);
+    virtual int32_t writeBlock(Registers startReg, Registers stopReg, const uint8_t *data);
+    
+private:
+
+    SPI m_spiBus;
+    DigitalOut m_cs;
+};
+
 #endif /* BMI160_H */
+
+
+///@brief fx documentation template.\n
+///
+///On Entry:
+///@param[in] none 
+///
+///On Exit:
+///@param[out] none
+///
+///@returns none
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bmi160_i2c.cpp	Thu Dec 08 00:32:41 2016 +0000
@@ -0,0 +1,95 @@
+/**********************************************************************
+* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+**********************************************************************/
+
+
+#include "bmi160.h"
+
+
+//*****************************************************************************
+BMI160_I2C::BMI160_I2C(I2C &i2cBus, uint8_t i2cAdrs)
+:m_i2cBus(i2cBus), m_Wadrs(i2cAdrs << 1), m_Radrs((i2cAdrs << 1) | 1)
+{
+}
+
+
+//*****************************************************************************   
+int32_t BMI160_I2C::readRegister(Registers reg, uint8_t *data)
+{
+    int32_t rtnVal = -1;
+    char packet[] = {static_cast<char>(reg)};
+    
+    if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
+    {
+        rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), 1);
+    }
+    
+    return rtnVal;
+}
+
+
+//*****************************************************************************
+int32_t BMI160_I2C::writeRegister(Registers reg, const uint8_t data)
+{
+    char packet[] = {static_cast<char>(reg), static_cast<char>(data)};
+    
+    return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
+}
+
+
+//*****************************************************************************
+int32_t BMI160_I2C::readBlock(Registers startReg, Registers stopReg, uint8_t *data)
+{
+    int32_t rtnVal = -1;
+    int32_t numBytes = ((stopReg - startReg) + 1);
+    char packet[] = {static_cast<char>(startReg)};
+    
+    if(m_i2cBus.write(m_Wadrs, packet, 1) == 0)
+    {
+        rtnVal = m_i2cBus.read(m_Radrs, reinterpret_cast<char *>(data), numBytes);
+    }
+    
+    return rtnVal;
+}
+
+
+//*****************************************************************************
+int32_t BMI160_I2C::writeBlock(Registers startReg, Registers stopReg, const uint8_t *data)
+{
+    int32_t numBytes = ((stopReg - startReg) + 1);
+    char packet[numBytes + 1];
+    
+    packet[0] = static_cast<char>(startReg);
+    
+    memcpy(packet + 1, data, numBytes);
+    
+    return m_i2cBus.write(m_Wadrs, packet, sizeof(packet));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bmi160_spi.cpp	Thu Dec 08 00:32:41 2016 +0000
@@ -0,0 +1,77 @@
+/**********************************************************************
+* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+**********************************************************************/
+
+
+#include "bmi160.h"
+
+
+//*****************************************************************************
+BMI160_SPI::BMI160_SPI(SPI &spiBus, DigitalOut &cs)
+:m_spiBus(spiBus), m_cs(cs)
+{
+}
+
+
+//*****************************************************************************   
+int32_t BMI160_SPI::readRegister(Registers reg, uint8_t *data)
+{
+    int32_t rtnVal = -1;
+    
+    return rtnVal;
+}
+
+
+//*****************************************************************************
+int32_t BMI160_SPI::writeRegister(Registers reg, const uint8_t data)
+{
+    int32_t rtnVal = -1;
+    
+    return rtnVal;
+}
+
+
+//*****************************************************************************
+int32_t BMI160_SPI::readBlock(Registers startReg, Registers stopReg, uint8_t *data)
+{
+    int32_t rtnVal = -1;
+    
+    return rtnVal;
+}
+
+
+//*****************************************************************************
+int32_t BMI160_SPI::writeBlock(Registers startReg, Registers stopReg, const uint8_t *data)
+{
+    int32_t rtnVal = -1;
+    
+    return rtnVal;
+}