Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: BG96_K6xF_pelion-example-frdm BG96_K6xF_pelion-example-frdm_Temp
FXOS8700Q.cpp
00001 /* FXOS8700Q sensor driver 00002 * Copyright (c) 2014-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "FXOS8700Q.h" 00018 00019 const uint16_t uint14_max = 0x3FFF; 00020 void static inline normalize_14bits(int16_t &x) 00021 { 00022 x = ((x) > (uint14_max/2)) ? (x - uint14_max) : (x); 00023 } 00024 00025 static int16_t dummy_int16_t = 0; 00026 static float dummy_float = 0.0f; 00027 00028 FXOS8700Q::FXOS8700Q(I2C &i2c, uint8_t addr) 00029 { 00030 _i2c = &i2c; 00031 _addr = addr; 00032 // activate the peripheral 00033 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00}; 00034 _i2c->frequency(400000); 00035 writeRegs(data, 2); 00036 data[0] = FXOS8700Q_M_CTRL_REG1; 00037 data[1] = 0x1F; 00038 writeRegs(data, 2); 00039 data[0] = FXOS8700Q_M_CTRL_REG2; 00040 data[1] = 0x20; 00041 writeRegs(data, 2); 00042 data[0] = FXOS8700Q_XYZ_DATA_CFG; 00043 data[1] = 0x00; 00044 writeRegs(data, 2); 00045 data[0] = FXOS8700Q_CTRL_REG1; 00046 data[1] = 0x1C; 00047 writeRegs(data, 2); 00048 } 00049 00050 FXOS8700Q::~FXOS8700Q() 00051 { 00052 _i2c = 0; 00053 _addr = 0; 00054 } 00055 00056 void FXOS8700Q::readRegs(int addr, uint8_t *data, uint32_t len) const 00057 { 00058 char t[1] = {(char)addr}; 00059 _i2c->write(_addr, t, sizeof(t), true); 00060 _i2c->read(_addr, (char *)data, len); 00061 } 00062 00063 uint8_t FXOS8700Q::whoAmI() const 00064 { 00065 uint8_t who_am_i = 0; 00066 readRegs(FXOS8700Q_WHOAMI, &who_am_i, sizeof(who_am_i)); 00067 return who_am_i; 00068 } 00069 00070 void FXOS8700Q::writeRegs(uint8_t * data, uint32_t len) const 00071 { 00072 _i2c->write(_addr, (char *)data, len); 00073 } 00074 00075 int16_t FXOS8700Q::getSensorAxis(uint8_t addr) const 00076 { 00077 uint8_t res[2]; 00078 readRegs(addr, res, sizeof(res)); 00079 return static_cast<int16_t>((res[0] << 8) | res[1]); 00080 } 00081 00082 void FXOS8700Q::enable(void) const 00083 { 00084 uint8_t data[2]; 00085 readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1); 00086 data[1] |= 0x01; 00087 data[0] = FXOS8700Q_CTRL_REG1; 00088 writeRegs(data, sizeof(data)); 00089 } 00090 00091 void FXOS8700Q::disable(void) const 00092 { 00093 uint8_t data[2]; 00094 readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1); 00095 data[1] &= 0xFE; 00096 data[0] = FXOS8700Q_CTRL_REG1; 00097 writeRegs(data, sizeof(data)); 00098 } 00099 00100 uint32_t FXOS8700Q::dataReady(void) const 00101 { 00102 uint8_t stat = 0; 00103 readRegs(FXOS8700Q_STATUS, &stat, 1); 00104 return (uint32_t)stat; 00105 } 00106 00107 uint32_t FXOS8700Q::sampleRate(uint32_t frequency) const 00108 { 00109 return(50); // for now sample rate is fixed at 50Hz 00110 } 00111 00112 int16_t FXOS8700QAccelerometer::getX(int16_t &x = dummy_int16_t) const 00113 { 00114 x = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2; 00115 normalize_14bits(x); 00116 return x; 00117 } 00118 00119 int16_t FXOS8700QAccelerometer::getY(int16_t &y = dummy_int16_t) const 00120 { 00121 y = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2; 00122 normalize_14bits(y); 00123 return y; 00124 } 00125 00126 int16_t FXOS8700QAccelerometer::getZ(int16_t &z = dummy_int16_t) const 00127 { 00128 z = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2; 00129 normalize_14bits(z); 00130 return z; 00131 } 00132 00133 float FXOS8700QAccelerometer::getX(float &x = dummy_float) const 00134 { 00135 int16_t val = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2; 00136 normalize_14bits(val); 00137 x = val / 4096.0f; 00138 return x; 00139 } 00140 00141 float FXOS8700QAccelerometer::getY(float &y = dummy_float) const 00142 { 00143 int16_t val = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2; 00144 normalize_14bits(val); 00145 y = val / 4096.0f; 00146 return y; 00147 } 00148 00149 float FXOS8700QAccelerometer::getZ(float &z = dummy_float) const 00150 { 00151 int16_t val = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2; 00152 normalize_14bits(val); 00153 z = val / 4096.0f; 00154 return z; 00155 } 00156 00157 void FXOS8700QAccelerometer::getAxis(motion_data_counts_t &xyz) const 00158 { 00159 uint8_t res[6]; 00160 readRegs(FXOS8700Q_OUT_X_MSB, res, sizeof(res)); 00161 xyz.x = static_cast<int16_t>((res[0] << 8) | res[1]) >> 2; 00162 xyz.y = static_cast<int16_t>((res[2] << 8) | res[3]) >> 2; 00163 xyz.z = static_cast<int16_t>((res[4] << 8) | res[5]) >> 2; 00164 normalize_14bits(xyz.x); 00165 normalize_14bits(xyz.y); 00166 normalize_14bits(xyz.z); 00167 } 00168 00169 void FXOS8700QAccelerometer::getAxis(motion_data_units_t &xyz) const 00170 { 00171 motion_data_counts_t _xyz; 00172 FXOS8700QAccelerometer::getAxis(_xyz); 00173 xyz.x = _xyz.x / 4096.0f; 00174 xyz.y = _xyz.y / 4096.0f; 00175 xyz.z = _xyz.z / 4096.0f; 00176 } 00177 00178 int16_t FXOS8700QMagnetometer::getX(int16_t &x = dummy_int16_t) const 00179 { 00180 x = getSensorAxis(FXOS8700Q_M_OUT_X_MSB); 00181 return x; 00182 } 00183 00184 int16_t FXOS8700QMagnetometer::getY(int16_t &y = dummy_int16_t) const 00185 { 00186 y = getSensorAxis(FXOS8700Q_M_OUT_Y_MSB); 00187 return y; 00188 } 00189 00190 int16_t FXOS8700QMagnetometer::getZ(int16_t &z = dummy_int16_t) const 00191 { 00192 z = getSensorAxis(FXOS8700Q_M_OUT_Z_MSB); 00193 return z; 00194 } 00195 00196 float FXOS8700QMagnetometer::getX(float &x = dummy_float) const 00197 { 00198 x = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_X_MSB)) * 0.1f; 00199 return x; 00200 } 00201 00202 float FXOS8700QMagnetometer::getY(float &y = dummy_float) const 00203 { 00204 y = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_Y_MSB)) * 0.1f; 00205 return y; 00206 } 00207 00208 float FXOS8700QMagnetometer::getZ(float &z = dummy_float) const 00209 { 00210 z = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_Z_MSB)) * 0.1f; 00211 return z; 00212 } 00213 00214 void FXOS8700QMagnetometer::getAxis(motion_data_counts_t &xyz) const 00215 { 00216 uint8_t res[6]; 00217 readRegs(FXOS8700Q_M_OUT_X_MSB, res, sizeof(res)); 00218 xyz.x = (res[0] << 8) | res[1]; 00219 xyz.y = (res[2] << 8) | res[3]; 00220 xyz.z = (res[4] << 8) | res[5]; 00221 } 00222 00223 void FXOS8700QMagnetometer::getAxis(motion_data_units_t &xyz) const 00224 { 00225 motion_data_counts_t _xyz; 00226 FXOS8700QMagnetometer::getAxis(_xyz); 00227 xyz.x = static_cast<float>(_xyz.x * 0.1f); 00228 xyz.y = static_cast<float>(_xyz.y * 0.1f); 00229 xyz.z = static_cast<float>(_xyz.z * 0.1f); 00230 }
Generated on Sun Jul 31 2022 19:04:43 by
