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

Dependents:   rohm-SensorShield-example

Committer:
MACRUM
Date:
Wed Feb 27 04:43:05 2019 +0000
Revision:
1:b226b04d2c21
Parent:
0:c447f35ff54a
Disable debug print by default

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ren Boting 0:c447f35ff54a 1 /*****************************************************************************
Ren Boting 0:c447f35ff54a 2 KX224_I2C.cpp
Ren Boting 0:c447f35ff54a 3
Ren Boting 0:c447f35ff54a 4 Copyright (c) 2017 ROHM Co.,Ltd.
Ren Boting 0:c447f35ff54a 5
Ren Boting 0:c447f35ff54a 6 Permission is hereby granted, free of charge, to any person obtaining a copy
Ren Boting 0:c447f35ff54a 7 of this software and associated documentation files (the "Software"), to deal
Ren Boting 0:c447f35ff54a 8 in the Software without restriction, including without limitation the rights
Ren Boting 0:c447f35ff54a 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Ren Boting 0:c447f35ff54a 10 copies of the Software, and to permit persons to whom the Software is
Ren Boting 0:c447f35ff54a 11 furnished to do so, subject to the following conditions:
Ren Boting 0:c447f35ff54a 12
Ren Boting 0:c447f35ff54a 13 The above copyright notice and this permission notice shall be included in
Ren Boting 0:c447f35ff54a 14 all copies or substantial portions of the Software.
Ren Boting 0:c447f35ff54a 15
Ren Boting 0:c447f35ff54a 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ren Boting 0:c447f35ff54a 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Ren Boting 0:c447f35ff54a 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Ren Boting 0:c447f35ff54a 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Ren Boting 0:c447f35ff54a 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Ren Boting 0:c447f35ff54a 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Ren Boting 0:c447f35ff54a 22 THE SOFTWARE.
Ren Boting 0:c447f35ff54a 23 *
Ren Boting 0:c447f35ff54a 24 * KX224-1053 3 axis accelerometer library
Ren Boting 0:c447f35ff54a 25 *
Ren Boting 0:c447f35ff54a 26 * @modified by Ren Boting
Ren Boting 0:c447f35ff54a 27 * @version 1.0
Ren Boting 0:c447f35ff54a 28 * @date 18-February-2019
Ren Boting 0:c447f35ff54a 29 *
Ren Boting 0:c447f35ff54a 30 * Library for "KX224-1053 3 axis accelerometer library"
Ren Boting 0:c447f35ff54a 31 * https://www.rohm.co.jp/sensor-shield-support/accelerometer
Ren Boting 0:c447f35ff54a 32 *
Ren Boting 0:c447f35ff54a 33 */
Ren Boting 0:c447f35ff54a 34 #include "KX224.h"
Ren Boting 0:c447f35ff54a 35
Ren Boting 0:c447f35ff54a 36 KX224::KX224(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr), _g_sens(4096)
Ren Boting 0:c447f35ff54a 37 {
Ren Boting 0:c447f35ff54a 38 }
Ren Boting 0:c447f35ff54a 39
Ren Boting 0:c447f35ff54a 40 KX224::~KX224()
Ren Boting 0:c447f35ff54a 41 {
Ren Boting 0:c447f35ff54a 42 }
Ren Boting 0:c447f35ff54a 43
Ren Boting 0:c447f35ff54a 44 uint8_t KX224::initialize(void)
Ren Boting 0:c447f35ff54a 45 {
Ren Boting 0:c447f35ff54a 46 char reg ;
Ren Boting 0:c447f35ff54a 47 uint16_t gsel;
Ren Boting 0:c447f35ff54a 48
Ren Boting 0:c447f35ff54a 49 read(KX224_WHO_AM_I, &reg, sizeof(reg));
Ren Boting 0:c447f35ff54a 50 DEBUG_PRINT("\r\nKX224_WHO_AMI Register Value = 0x%x\r\n", reg);
Ren Boting 0:c447f35ff54a 51
Ren Boting 0:c447f35ff54a 52 if (reg != KX224_WAI_VAL) {
Ren Boting 0:c447f35ff54a 53 DEBUG_PRINT("\nCan't find KX224\r\n");
Ren Boting 0:c447f35ff54a 54 return 1;
Ren Boting 0:c447f35ff54a 55 }
Ren Boting 0:c447f35ff54a 56
Ren Boting 0:c447f35ff54a 57 reg = KX224_CNTL1_VAL;
Ren Boting 0:c447f35ff54a 58 write(KX224_CNTL1, &reg);
Ren Boting 0:c447f35ff54a 59
Ren Boting 0:c447f35ff54a 60 reg = KX224_ODCNTL_VAL;
Ren Boting 0:c447f35ff54a 61 write(KX224_ODCNTL, &reg);
Ren Boting 0:c447f35ff54a 62
Ren Boting 0:c447f35ff54a 63 read(KX224_CNTL1, &reg, sizeof(reg));
Ren Boting 0:c447f35ff54a 64 gsel = reg & KX224_CNTL1_GSELMASK;
Ren Boting 0:c447f35ff54a 65
Ren Boting 0:c447f35ff54a 66 reg |= KX224_CNTL1_PC1;
Ren Boting 0:c447f35ff54a 67 write(KX224_CNTL1, &reg);
Ren Boting 0:c447f35ff54a 68
Ren Boting 0:c447f35ff54a 69 switch(gsel) {
Ren Boting 0:c447f35ff54a 70 case KX224_CNTL1_GSEL_8G :
Ren Boting 0:c447f35ff54a 71 // (Equivalent counts) / (Range) = (32768 / 8)
Ren Boting 0:c447f35ff54a 72 _g_sens = 4096;
Ren Boting 0:c447f35ff54a 73 break;
Ren Boting 0:c447f35ff54a 74
Ren Boting 0:c447f35ff54a 75 case KX224_CNTL1_GSEL_16G :
Ren Boting 0:c447f35ff54a 76 // (Equivalent counts) / (Range) = (32768 / 16)
Ren Boting 0:c447f35ff54a 77 _g_sens = 2048;
Ren Boting 0:c447f35ff54a 78 break;
Ren Boting 0:c447f35ff54a 79
Ren Boting 0:c447f35ff54a 80 case KX224_CNTL1_GSEL_32G :
Ren Boting 0:c447f35ff54a 81 // (Equivalent counts) / (Range) = (32768 / 32)
Ren Boting 0:c447f35ff54a 82 _g_sens = 1024;
Ren Boting 0:c447f35ff54a 83 break;
Ren Boting 0:c447f35ff54a 84
Ren Boting 0:c447f35ff54a 85 default:
Ren Boting 0:c447f35ff54a 86 DEBUG_PRINT("\r!!! rgsel value (0x%x) is invalid\r", gsel);
Ren Boting 0:c447f35ff54a 87 return 2;
Ren Boting 0:c447f35ff54a 88 }
Ren Boting 0:c447f35ff54a 89 return 0;
Ren Boting 0:c447f35ff54a 90 }
Ren Boting 0:c447f35ff54a 91
Ren Boting 0:c447f35ff54a 92 void KX224::get_val(float *data)
Ren Boting 0:c447f35ff54a 93 {
Ren Boting 0:c447f35ff54a 94 char val[6];
Ren Boting 0:c447f35ff54a 95 uint16_t acc[3];
Ren Boting 0:c447f35ff54a 96 // get raw value
Ren Boting 0:c447f35ff54a 97 read(KX224_XOUT_L, &val[0], 6);
Ren Boting 0:c447f35ff54a 98 acc[0] = ((int16_t)val[1] << 8) | (int16_t)(val[0]);
Ren Boting 0:c447f35ff54a 99 acc[1] = ((int16_t)val[3] << 8) | (int16_t)(val[2]);
Ren Boting 0:c447f35ff54a 100 acc[2] = ((int16_t)val[5] << 8) | (int16_t)(val[4]);
Ren Boting 0:c447f35ff54a 101
Ren Boting 0:c447f35ff54a 102 // Convert LSB to g
Ren Boting 0:c447f35ff54a 103 data[0] = (float)acc[0] / _g_sens;
Ren Boting 0:c447f35ff54a 104 data[1] = (float)acc[1] / _g_sens;
Ren Boting 0:c447f35ff54a 105 data[2] = (float)acc[2] / _g_sens;
Ren Boting 0:c447f35ff54a 106 }
Ren Boting 0:c447f35ff54a 107
Ren Boting 0:c447f35ff54a 108 void KX224::write(uint8_t memory_address, char *data)
Ren Boting 0:c447f35ff54a 109 {
Ren Boting 0:c447f35ff54a 110 char tmp[3];
Ren Boting 0:c447f35ff54a 111 tmp[0]=memory_address;
Ren Boting 0:c447f35ff54a 112 tmp[1]=*data;
Ren Boting 0:c447f35ff54a 113 m_i2c.write(m_addr, &tmp[0], 2);
Ren Boting 0:c447f35ff54a 114 }
Ren Boting 0:c447f35ff54a 115
Ren Boting 0:c447f35ff54a 116 void KX224::read(uint8_t memory_address, char *data, int size)
Ren Boting 0:c447f35ff54a 117 {
Ren Boting 0:c447f35ff54a 118 char t[1] = {memory_address};
Ren Boting 0:c447f35ff54a 119 m_i2c.write(m_addr, t, 1, true);
Ren Boting 0:c447f35ff54a 120 m_i2c.read(m_addr, data, size);
Ren Boting 0:c447f35ff54a 121 }