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.h
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 #ifndef _KX224_H_
Ren Boting 0:c447f35ff54a 35 #define _KX224_H_
Ren Boting 0:c447f35ff54a 36
Ren Boting 0:c447f35ff54a 37 #include "mbed.h"
Ren Boting 0:c447f35ff54a 38
Ren Boting 0:c447f35ff54a 39 #define KX224_DEVICE_ADDRESS_1E (0x1E << 1) // 7bit Addrss
Ren Boting 0:c447f35ff54a 40 #define KX224_DEVICE_ADDRESS_1F (0x1F << 1) // 7bit Address
Ren Boting 0:c447f35ff54a 41 #define KX224_WAI_VAL (0x2B)
Ren Boting 0:c447f35ff54a 42
Ren Boting 0:c447f35ff54a 43 #define KX224_XOUT_L (0x06)
Ren Boting 0:c447f35ff54a 44 #define KX224_WHO_AM_I (0x0F)
Ren Boting 0:c447f35ff54a 45 #define KX224_CNTL1 (0x18)
Ren Boting 0:c447f35ff54a 46 #define KX224_ODCNTL (0x1B)
Ren Boting 0:c447f35ff54a 47
Ren Boting 0:c447f35ff54a 48 #define KX224_CNTL1_TPE (1 << 0)
Ren Boting 0:c447f35ff54a 49 #define KX224_CNTL1_WUFE (1 << 1)
Ren Boting 0:c447f35ff54a 50 #define KX224_CNTL1_TDTE (1 << 2)
Ren Boting 0:c447f35ff54a 51 #define KX224_CNTL1_GSELMASK (0x18)
Ren Boting 0:c447f35ff54a 52 #define KX224_CNTL1_GSEL_8G (0x00)
Ren Boting 0:c447f35ff54a 53 #define KX224_CNTL1_GSEL_16G (0x08)
Ren Boting 0:c447f35ff54a 54 #define KX224_CNTL1_GSEL_32G (0x10)
Ren Boting 0:c447f35ff54a 55 #define KX224_CNTL1_DRDYE (1 << 5)
Ren Boting 0:c447f35ff54a 56 #define KX224_CNTL1_RES (1 << 6)
Ren Boting 0:c447f35ff54a 57 #define KX224_CNTL1_PC1 (1 << 7)
Ren Boting 0:c447f35ff54a 58
Ren Boting 0:c447f35ff54a 59 #define KX224_ODCNTL_OSA_50HZ (2)
Ren Boting 0:c447f35ff54a 60 #define KX224_ODCNTL_LPRO (1 << 6)
Ren Boting 0:c447f35ff54a 61 #define KX224_IIR_BYPASS (1 << 7)
Ren Boting 0:c447f35ff54a 62
Ren Boting 0:c447f35ff54a 63 #define KX224_CNTL1_VAL (KX224_CNTL1_RES | KX224_CNTL1_GSEL_8G)
Ren Boting 0:c447f35ff54a 64 #define KX224_ODCNTL_VAL (KX224_ODCNTL_OSA_50HZ)
Ren Boting 0:c447f35ff54a 65
Ren Boting 0:c447f35ff54a 66 #ifdef _DEBUG
Ren Boting 0:c447f35ff54a 67 #undef DEBUG_PRINT
MACRUM 1:b226b04d2c21 68 #define DEBUG_PRINT(...) printf(__VA_ARGS__)
Ren Boting 0:c447f35ff54a 69 #else
Ren Boting 0:c447f35ff54a 70 #define DEBUG_PRINT(...)
Ren Boting 0:c447f35ff54a 71 #endif
Ren Boting 0:c447f35ff54a 72
Ren Boting 0:c447f35ff54a 73 class KX224
Ren Boting 0:c447f35ff54a 74 {
Ren Boting 0:c447f35ff54a 75 public:
Ren Boting 0:c447f35ff54a 76 /**
Ren Boting 0:c447f35ff54a 77 * KX224 constructor
Ren Boting 0:c447f35ff54a 78 *
Ren Boting 0:c447f35ff54a 79 * @param sda SDA pin
Ren Boting 0:c447f35ff54a 80 * @param sdl SCL pin
Ren Boting 0:c447f35ff54a 81 * @param addr slave address of the I2C peripheral (default: 0x1E)
Ren Boting 0:c447f35ff54a 82 */
Ren Boting 0:c447f35ff54a 83 KX224(PinName sda, PinName scl, int slave_addr = KX224_DEVICE_ADDRESS_1E);
Ren Boting 0:c447f35ff54a 84 /**
Ren Boting 0:c447f35ff54a 85 * KX224 destructor
Ren Boting 0:c447f35ff54a 86 */
Ren Boting 0:c447f35ff54a 87 ~KX224();
Ren Boting 0:c447f35ff54a 88 uint8_t initialize(void);
Ren Boting 0:c447f35ff54a 89 void get_val(float *data);
Ren Boting 0:c447f35ff54a 90 private:
Ren Boting 0:c447f35ff54a 91 I2C m_i2c;
Ren Boting 0:c447f35ff54a 92 int m_addr;
Ren Boting 0:c447f35ff54a 93 uint16_t _g_sens;
Ren Boting 0:c447f35ff54a 94 void write(uint8_t memory_address, char *data);
Ren Boting 0:c447f35ff54a 95 void read(uint8_t memory_address, char *data, int size);
Ren Boting 0:c447f35ff54a 96 };
Ren Boting 0:c447f35ff54a 97
Ren Boting 0:c447f35ff54a 98 #endif // _KX224_H_