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

Dependents:   rohm-SensorShield-example mbed_blinky

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KX224.cpp Source File

KX224.cpp

00001 /*****************************************************************************
00002   KX224_I2C.cpp
00003 
00004  Copyright (c) 2017 ROHM Co.,Ltd.
00005 
00006  Permission is hereby granted, free of charge, to any person obtaining a copy
00007  of this software and associated documentation files (the "Software"), to deal
00008  in the Software without restriction, including without limitation the rights
00009  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  copies of the Software, and to permit persons to whom the Software is
00011  furnished to do so, subject to the following conditions:
00012 
00013  The above copyright notice and this permission notice shall be included in
00014  all copies or substantial portions of the Software.
00015 
00016  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  THE SOFTWARE.
00023 *
00024 *  KX224-1053 3 axis accelerometer library
00025 *
00026 *  @modified by Ren Boting
00027 *  @version 1.0
00028 *  @date    18-February-2019
00029 *
00030 *  Library for "KX224-1053 3 axis accelerometer library"
00031 *    https://www.rohm.co.jp/sensor-shield-support/accelerometer
00032 *
00033 */
00034 #include "KX224.h"
00035 
00036 KX224::KX224(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr), _g_sens(4096)
00037 {
00038 }
00039 
00040 KX224::~KX224()
00041 {
00042 }
00043 
00044 uint8_t KX224::initialize(void)
00045 {
00046   char reg ;
00047   uint16_t gsel;
00048 
00049   read(KX224_WHO_AM_I, &reg, sizeof(reg));
00050   DEBUG_PRINT("\r\nKX224_WHO_AMI Register Value = 0x%x\r\n", reg);
00051   
00052   if (reg != KX224_WAI_VAL) {
00053       DEBUG_PRINT("\nCan't find KX224\r\n");
00054       return 1;
00055   }
00056 
00057   reg = KX224_CNTL1_VAL;
00058   write(KX224_CNTL1, &reg);
00059 
00060   reg = KX224_ODCNTL_VAL;
00061   write(KX224_ODCNTL, &reg);
00062 
00063   read(KX224_CNTL1, &reg, sizeof(reg));
00064   gsel = reg & KX224_CNTL1_GSELMASK;
00065 
00066   reg |= KX224_CNTL1_PC1;
00067   write(KX224_CNTL1, &reg);
00068   
00069   switch(gsel) {
00070     case KX224_CNTL1_GSEL_8G :
00071       // (Equivalent counts) / (Range) = (32768 / 8)
00072       _g_sens = 4096;
00073     break;
00074 
00075     case KX224_CNTL1_GSEL_16G :
00076       // (Equivalent counts) / (Range) = (32768 / 16)
00077       _g_sens = 2048;
00078     break;
00079 
00080     case KX224_CNTL1_GSEL_32G :
00081       // (Equivalent counts) / (Range) = (32768 / 32)
00082       _g_sens = 1024;
00083     break;
00084 
00085     default:
00086         DEBUG_PRINT("\r!!! rgsel value (0x%x) is invalid\r", gsel);
00087         return 2;
00088   }
00089   return 0;
00090 }
00091 
00092 void KX224::get_val(float *data)
00093 {
00094   char val[6];
00095   uint16_t acc[3];
00096   // get raw value
00097   read(KX224_XOUT_L, &val[0], 6);
00098   acc[0] = ((int16_t)val[1] << 8) | (int16_t)(val[0]);
00099   acc[1] = ((int16_t)val[3] << 8) | (int16_t)(val[2]);
00100   acc[2] = ((int16_t)val[5] << 8) | (int16_t)(val[4]);
00101 
00102   // Convert LSB to g
00103   data[0] = (float)acc[0] / _g_sens;
00104   data[1] = (float)acc[1] / _g_sens;
00105   data[2] = (float)acc[2] / _g_sens;
00106 }
00107 
00108 void KX224::write(uint8_t memory_address, char *data)
00109 {
00110   char tmp[3];
00111   tmp[0]=memory_address;
00112   tmp[1]=*data;
00113   m_i2c.write(m_addr, &tmp[0], 2);
00114 }
00115 
00116 void KX224::read(uint8_t memory_address, char *data, int size)
00117 {
00118   char t[1] = {memory_address};
00119   m_i2c.write(m_addr, t, 1, true);
00120   m_i2c.read(m_addr, data, size);
00121 }