A mbed-os v5 driver for KX224-1053 (3 axis accelerometer, made by Rohm).

Dependents:   rohm-SensorShield-example mbed_blinky

Files at this revision

API Documentation at this revision

Comitter:
Ren Boting
Date:
Mon Feb 18 13:03:38 2019 +0900
Child:
1:b226b04d2c21
Commit message:
Init commit for mbed os v5 driver, Rohm KX224-1053 sensor.

Changed in this revision

KX224.cpp Show annotated file Show diff for this revision Revisions of this file
KX224.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KX224.cpp	Mon Feb 18 13:03:38 2019 +0900
@@ -0,0 +1,122 @@
+/*****************************************************************************
+  KX224_I2C.cpp
+
+ Copyright (c) 2017 ROHM Co.,Ltd.
+
+ 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 THE
+ AUTHORS OR COPYRIGHT HOLDERS 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.
+*
+*  KX224-1053 3 axis accelerometer library
+*
+*  @modified by Ren Boting
+*  @version 1.0
+*  @date    18-February-2019
+*
+*  Library for "KX224-1053 3 axis accelerometer library"
+*    https://www.rohm.co.jp/sensor-shield-support/accelerometer
+*
+*/
+#include "KX224.h"
+
+KX224::KX224(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr), _g_sens(4096)
+{
+}
+
+KX224::~KX224()
+{
+}
+
+uint8_t KX224::initialize(void)
+{
+  char reg ;
+  uint16_t gsel;
+
+  read(KX224_WHO_AM_I, &reg, sizeof(reg));
+  DEBUG_PRINT("\r\nKX224_WHO_AMI Register Value = 0x%x\r\n", reg);
+  
+  if (reg != KX224_WAI_VAL) {
+	  DEBUG_PRINT("\nCan't find KX224\r\n");
+	  return 1;
+  }
+
+  reg = KX224_CNTL1_VAL;
+  write(KX224_CNTL1, &reg);
+
+  reg = KX224_ODCNTL_VAL;
+  write(KX224_ODCNTL, &reg);
+
+  read(KX224_CNTL1, &reg, sizeof(reg));
+  gsel = reg & KX224_CNTL1_GSELMASK;
+
+  reg |= KX224_CNTL1_PC1;
+  write(KX224_CNTL1, &reg);
+  
+  switch(gsel) {
+    case KX224_CNTL1_GSEL_8G :
+      // (Equivalent counts) / (Range) = (32768 / 8)
+      _g_sens = 4096;
+    break;
+
+    case KX224_CNTL1_GSEL_16G :
+      // (Equivalent counts) / (Range) = (32768 / 16)
+      _g_sens = 2048;
+    break;
+
+    case KX224_CNTL1_GSEL_32G :
+      // (Equivalent counts) / (Range) = (32768 / 32)
+      _g_sens = 1024;
+    break;
+
+    default:
+    	DEBUG_PRINT("\r!!! rgsel value (0x%x) is invalid\r", gsel);
+    	return 2;
+    	break;
+  }
+  return 0;
+}
+
+void KX224::get_val(float *data)
+{
+  char val[6];
+  uint16_t acc[3];
+  // get raw value
+  read(KX224_XOUT_L, &val[0], 6);
+  acc[0] = ((int16_t)val[1] << 8) | (int16_t)(val[0]);
+  acc[1] = ((int16_t)val[3] << 8) | (int16_t)(val[2]);
+  acc[2] = ((int16_t)val[5] << 8) | (int16_t)(val[4]);
+
+  // Convert LSB to g
+  data[0] = (float)acc[0] / _g_sens;
+  data[1] = (float)acc[1] / _g_sens;
+  data[2] = (float)acc[2] / _g_sens;
+}
+
+void KX224::write(uint8_t memory_address, char *data)
+{
+  char tmp[3];
+  tmp[0]=memory_address;
+  tmp[1]=*data;
+  m_i2c.write(m_addr, &tmp[0], 2);
+}
+
+void KX224::read(uint8_t memory_address, char *data, int size)
+{
+  char t[1] = {memory_address};
+  m_i2c.write(m_addr, t, 1, true);
+  m_i2c.read(m_addr, data, size);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KX224.h	Mon Feb 18 13:03:38 2019 +0900
@@ -0,0 +1,101 @@
+/*****************************************************************************
+  KX224_I2C.h
+
+ Copyright (c) 2017 ROHM Co.,Ltd.
+
+ 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 THE
+ AUTHORS OR COPYRIGHT HOLDERS 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.
+*
+*  KX224-1053 3 axis accelerometer library
+*
+*  @modified by Ren Boting
+*  @version 1.0
+*  @date    18-February-2019
+*
+*  Library for "KX224-1053 3 axis accelerometer library"
+*    https://www.rohm.co.jp/sensor-shield-support/accelerometer
+*
+*/
+#ifndef _KX224_H_
+#define _KX224_H_
+
+#include "mbed.h"
+
+#define KX224_DEVICE_ADDRESS_1E   (0x1E << 1)    // 7bit Addrss
+#define KX224_DEVICE_ADDRESS_1F   (0x1F << 1)    // 7bit Address
+#define KX224_WAI_VAL             (0x2B)
+
+#define KX224_XOUT_L              (0x06)
+#define KX224_WHO_AM_I            (0x0F)
+#define KX224_CNTL1               (0x18)
+#define KX224_ODCNTL              (0x1B)
+
+#define KX224_CNTL1_TPE           (1 << 0)
+#define KX224_CNTL1_WUFE          (1 << 1)
+#define KX224_CNTL1_TDTE          (1 << 2)
+#define KX224_CNTL1_GSELMASK      (0x18)
+#define KX224_CNTL1_GSEL_8G       (0x00)
+#define KX224_CNTL1_GSEL_16G      (0x08)
+#define KX224_CNTL1_GSEL_32G      (0x10)
+#define KX224_CNTL1_DRDYE         (1 << 5)
+#define KX224_CNTL1_RES           (1 << 6)
+#define KX224_CNTL1_PC1           (1 << 7)
+
+#define KX224_ODCNTL_OSA_50HZ     (2)
+#define KX224_ODCNTL_LPRO         (1 << 6)
+#define KX224_IIR_BYPASS          (1 << 7)
+
+#define KX224_CNTL1_VAL           (KX224_CNTL1_RES | KX224_CNTL1_GSEL_8G)
+#define KX224_ODCNTL_VAL          (KX224_ODCNTL_OSA_50HZ)
+
+#define _DEBUG
+
+#ifdef _DEBUG
+#undef DEBUG_PRINT
+extern Serial pc;
+#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+class KX224
+{
+public:
+    /**
+    * KX224 constructor
+    *
+    * @param sda SDA pin
+    * @param sdl SCL pin
+    * @param addr slave address of the I2C peripheral (default: 0x1E)
+    */
+	KX224(PinName sda, PinName scl, int slave_addr = KX224_DEVICE_ADDRESS_1E);
+    /**
+    * KX224 destructor
+    */
+    ~KX224();
+    uint8_t initialize(void);
+    void get_val(float *data);
+private:
+    I2C m_i2c;
+    int m_addr;
+    uint16_t _g_sens;
+    void write(uint8_t memory_address, char *data);
+    void read(uint8_t memory_address, char *data, int size);
+};
+
+#endif // _KX224_H_