Kionix KX123 accelerometer C++ driver. Can be used for some extend also with kx022, kx023, kx122, etc. when used features are present in sensor.

Dependents:   kionix-kx123-hello

Committer:
MikkoZ
Date:
Thu Sep 29 15:05:08 2016 +0000
Revision:
0:a3f43eb92f86
Child:
1:f328083fb80b
Kionix KX123 minimal C++ driver. Initial version. Uses RegisterWriter library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikkoZ 0:a3f43eb92f86 1 /* Copyright 2016 Rohm Semiconductor
MikkoZ 0:a3f43eb92f86 2
MikkoZ 0:a3f43eb92f86 3 Licensed under the Apache License, Version 2.0 (the "License");
MikkoZ 0:a3f43eb92f86 4 you may not use this file except in compliance with the License.
MikkoZ 0:a3f43eb92f86 5 You may obtain a copy of the License at
MikkoZ 0:a3f43eb92f86 6
MikkoZ 0:a3f43eb92f86 7 http://www.apache.org/licenses/LICENSE-2.0
MikkoZ 0:a3f43eb92f86 8
MikkoZ 0:a3f43eb92f86 9 Unless required by applicable law or agreed to in writing, software
MikkoZ 0:a3f43eb92f86 10 distributed under the License is distributed on an "AS IS" BASIS,
MikkoZ 0:a3f43eb92f86 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikkoZ 0:a3f43eb92f86 12 See the License for the specific language governing permissions and
MikkoZ 0:a3f43eb92f86 13 limitations under the License.
MikkoZ 0:a3f43eb92f86 14 */
MikkoZ 0:a3f43eb92f86 15 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
MikkoZ 0:a3f43eb92f86 16 #include "RegisterWriter/RegisterWriter/RegisterWriter.h"
MikkoZ 0:a3f43eb92f86 17
MikkoZ 0:a3f43eb92f86 18 #include "kx123_registers.h"
MikkoZ 0:a3f43eb92f86 19 #include "kx123.h"
MikkoZ 0:a3f43eb92f86 20
MikkoZ 0:a3f43eb92f86 21 /**
MikkoZ 0:a3f43eb92f86 22 * KX123 accelerometer driver
MikkoZ 0:a3f43eb92f86 23 */
MikkoZ 0:a3f43eb92f86 24 /**
MikkoZ 0:a3f43eb92f86 25 * Create a KX123 instance which is connected to pre-instantiated I2C object.
MikkoZ 0:a3f43eb92f86 26 *
MikkoZ 0:a3f43eb92f86 27 * @param sad slave address of sensor.
MikkoZ 0:a3f43eb92f86 28 * @param wai who_am_i value (i.e. sensor type/model)
MikkoZ 0:a3f43eb92f86 29 */
MikkoZ 0:a3f43eb92f86 30 KX123::KX123(RegisterWriter &i2c_obj, uint8_t newsad, uint8_t newwai) : i2c_rw(i2c_obj) {
MikkoZ 0:a3f43eb92f86 31 sad = newsad;
MikkoZ 0:a3f43eb92f86 32 wai = newwai;
MikkoZ 0:a3f43eb92f86 33 }
MikkoZ 0:a3f43eb92f86 34
MikkoZ 0:a3f43eb92f86 35 /**
MikkoZ 0:a3f43eb92f86 36 * KX123 destructor
MikkoZ 0:a3f43eb92f86 37 */
MikkoZ 0:a3f43eb92f86 38 KX123::~KX123(){
MikkoZ 0:a3f43eb92f86 39 }
MikkoZ 0:a3f43eb92f86 40
MikkoZ 0:a3f43eb92f86 41 /**
MikkoZ 0:a3f43eb92f86 42 * Check if sensor is connected, setup defaults to sensor and start measuring.
MikkoZ 0:a3f43eb92f86 43 * @return true on error, false on ok
MikkoZ 0:a3f43eb92f86 44 */
MikkoZ 0:a3f43eb92f86 45 bool KX123::set_defaults()
MikkoZ 0:a3f43eb92f86 46 {
MikkoZ 0:a3f43eb92f86 47 unsigned char buf;
MikkoZ 0:a3f43eb92f86 48
MikkoZ 0:a3f43eb92f86 49 DEBUG_print("\n\r");
MikkoZ 0:a3f43eb92f86 50 DEBUG_print("KX123 init started\n\r");
MikkoZ 0:a3f43eb92f86 51 i2c_rw.read_register(sad, KX122_WHO_AM_I, &buf, 1);
MikkoZ 0:a3f43eb92f86 52 if (buf == KX123_WHO_AM_I_WAI_ID) {
MikkoZ 0:a3f43eb92f86 53 DEBUG_print("KX123 found. (WAI %d) ", buf);
MikkoZ 0:a3f43eb92f86 54 } else {
MikkoZ 0:a3f43eb92f86 55 DEBUG_print("KX123 not found (WAI %d, not %d). ", buf, KX123_WHO_AM_I_WAI_ID);
MikkoZ 0:a3f43eb92f86 56 switch(buf){
MikkoZ 0:a3f43eb92f86 57 case KX012_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 58 DEBUG_print("Found KX012");
MikkoZ 0:a3f43eb92f86 59 break;
MikkoZ 0:a3f43eb92f86 60 case KX022_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 61 DEBUG_print("Found KX022");
MikkoZ 0:a3f43eb92f86 62 break;
MikkoZ 0:a3f43eb92f86 63 case KX023_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 64 DEBUG_print("Found KX023");
MikkoZ 0:a3f43eb92f86 65 break;
MikkoZ 0:a3f43eb92f86 66 case KX23H_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 67 DEBUG_print("Found KX23H");
MikkoZ 0:a3f43eb92f86 68 break;
MikkoZ 0:a3f43eb92f86 69 case KX112_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 70 DEBUG_print("Found KX112");
MikkoZ 0:a3f43eb92f86 71 break;
MikkoZ 0:a3f43eb92f86 72 case KX122_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 73 DEBUG_print("Found KX122");
MikkoZ 0:a3f43eb92f86 74 break;
MikkoZ 0:a3f43eb92f86 75 case KX124_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 76 DEBUG_print("Found KX124");
MikkoZ 0:a3f43eb92f86 77 break;
MikkoZ 0:a3f43eb92f86 78 case KX222_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 79 DEBUG_print("Found KX222");
MikkoZ 0:a3f43eb92f86 80 break;
MikkoZ 0:a3f43eb92f86 81 case KX224_WHO_AM_I_WAI_ID:
MikkoZ 0:a3f43eb92f86 82 DEBUG_print("Found KX224");
MikkoZ 0:a3f43eb92f86 83 break;
MikkoZ 0:a3f43eb92f86 84 default:
MikkoZ 0:a3f43eb92f86 85 DEBUG_print("Not even other sensor found from same family.\n\r");
MikkoZ 0:a3f43eb92f86 86 return true;
MikkoZ 0:a3f43eb92f86 87 }
MikkoZ 0:a3f43eb92f86 88 DEBUG_print(" though, trying to use that.\n\r");
MikkoZ 0:a3f43eb92f86 89 }
MikkoZ 0:a3f43eb92f86 90
MikkoZ 0:a3f43eb92f86 91 //First set CNTL1 PC1-bit to stand-by mode, after that setup can be made
MikkoZ 0:a3f43eb92f86 92 i2c_rw.write_register(sad, KX122_CNTL1, 0 );
MikkoZ 0:a3f43eb92f86 93
MikkoZ 0:a3f43eb92f86 94 //set_tilt_position_defaults();
MikkoZ 0:a3f43eb92f86 95
MikkoZ 0:a3f43eb92f86 96 //ODCNTL: Output Data Rate control (ODR)
MikkoZ 0:a3f43eb92f86 97 i2c_rw.write_register(sad, KX122_ODCNTL, KX122_ODCNTL_OSA_25600);
MikkoZ 0:a3f43eb92f86 98 //Setup G-range and 8/16-bit resolution + set CNTL1 PC1-bit to operating mode (also WUF_EN, TP_EN and DT_EN)
MikkoZ 0:a3f43eb92f86 99 i2c_rw.write_register(sad, KX122_CNTL1, ( KX122_CNTL1_PC1 | KX122_CNTL1_GSEL_8G | KX122_CNTL1_RES ) );
MikkoZ 0:a3f43eb92f86 100
MikkoZ 0:a3f43eb92f86 101 //resolution_divider = 32768/2; //KX122_CNTL1_GSEL_2G
MikkoZ 0:a3f43eb92f86 102 //resolution_divider = 32768/4; //KX122_CNTL1_GSEL_4G
MikkoZ 0:a3f43eb92f86 103 resolution_divider = 32768/8; //KX122_CNTL1_GSEL_8G
MikkoZ 0:a3f43eb92f86 104
MikkoZ 0:a3f43eb92f86 105 return false;
MikkoZ 0:a3f43eb92f86 106 }
MikkoZ 0:a3f43eb92f86 107
MikkoZ 0:a3f43eb92f86 108
MikkoZ 0:a3f43eb92f86 109 /**
MikkoZ 0:a3f43eb92f86 110 CNTL PC1-bit has to be set to stand-by before this command.
MikkoZ 0:a3f43eb92f86 111 return true on error
MikkoZ 0:a3f43eb92f86 112 **/
MikkoZ 0:a3f43eb92f86 113 void KX123::set_tilt_position_defaults(){
MikkoZ 0:a3f43eb92f86 114 //CNTL3: Tilt position control, directional tap control and motion wakeup control
MikkoZ 0:a3f43eb92f86 115 i2c_rw.write_register(sad, KX122_CNTL3, ( KX122_CNTL3_OTP_50 | KX122_CNTL3_OTDT_400 ) );
MikkoZ 0:a3f43eb92f86 116 //TILT_TIMER: Setup tilt position timer (~=filter)
MikkoZ 0:a3f43eb92f86 117 i2c_rw.write_register(sad, KX122_TILT_TIMER, 0x01);
MikkoZ 0:a3f43eb92f86 118 return;
MikkoZ 0:a3f43eb92f86 119 }
MikkoZ 0:a3f43eb92f86 120
MikkoZ 0:a3f43eb92f86 121
MikkoZ 0:a3f43eb92f86 122 /**
MikkoZ 0:a3f43eb92f86 123 return read_error, true on error, false on read ok.
MikkoZ 0:a3f43eb92f86 124 **/
MikkoZ 0:a3f43eb92f86 125 bool KX123::getresults_raw(int16_t* buf){
MikkoZ 0:a3f43eb92f86 126 #define RESULTS_LEN 6
MikkoZ 0:a3f43eb92f86 127 uint8_t tmp[RESULTS_LEN]; //XYZ (lhlhlh)
MikkoZ 0:a3f43eb92f86 128 uint8_t read_bytes;
MikkoZ 0:a3f43eb92f86 129
MikkoZ 0:a3f43eb92f86 130 read_bytes = i2c_rw.read_register(sad, KX122_XOUT_L, &tmp[0], sizeof(tmp));
MikkoZ 0:a3f43eb92f86 131 if (read_bytes != RESULTS_LEN){
MikkoZ 0:a3f43eb92f86 132 return true;
MikkoZ 0:a3f43eb92f86 133 }
MikkoZ 0:a3f43eb92f86 134 buf[0] = ( tmp[1] << 8 ) | tmp[0]; //X
MikkoZ 0:a3f43eb92f86 135 buf[1] = ( tmp[3] << 8 ) | tmp[2]; //Y
MikkoZ 0:a3f43eb92f86 136 buf[2] = ( tmp[5] << 8 ) | tmp[4]; //Z
MikkoZ 0:a3f43eb92f86 137 return false;
MikkoZ 0:a3f43eb92f86 138 }
MikkoZ 0:a3f43eb92f86 139
MikkoZ 0:a3f43eb92f86 140
MikkoZ 0:a3f43eb92f86 141 bool KX123::getresults_g(float* buf){
MikkoZ 0:a3f43eb92f86 142 int16_t raw[3];
MikkoZ 0:a3f43eb92f86 143 int read_error;
MikkoZ 0:a3f43eb92f86 144
MikkoZ 0:a3f43eb92f86 145 read_error = getresults_raw(&raw[0]);
MikkoZ 0:a3f43eb92f86 146 if (read_error){
MikkoZ 0:a3f43eb92f86 147 return read_error;
MikkoZ 0:a3f43eb92f86 148 }
MikkoZ 0:a3f43eb92f86 149
MikkoZ 0:a3f43eb92f86 150 //Scale raw values to G-scale
MikkoZ 0:a3f43eb92f86 151 buf[0] = ((float)raw[0]) / resolution_divider;
MikkoZ 0:a3f43eb92f86 152 buf[1] = ((float)raw[1]) / resolution_divider;
MikkoZ 0:a3f43eb92f86 153 buf[2] = ((float)raw[2]) / resolution_divider;
MikkoZ 0:a3f43eb92f86 154 return false;
MikkoZ 0:a3f43eb92f86 155 }
MikkoZ 0:a3f43eb92f86 156
MikkoZ 0:a3f43eb92f86 157