Yunting Zou 201199716

Dependencies:   mbed MotionSensor

Committer:
zhouyun123
Date:
Thu May 14 17:18:58 2020 +0000
Revision:
3:4bd22344278d
Parent:
0:047e14f53977
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zhouyun123 0:047e14f53977 1 /* FXOS8700Q sensor driver
zhouyun123 0:047e14f53977 2 * Copyright (c) 2014-2015 ARM Limited
zhouyun123 0:047e14f53977 3 *
zhouyun123 0:047e14f53977 4 * Licensed under the Apache License, Version 2.0 (the "License");
zhouyun123 0:047e14f53977 5 * you may not use this file except in compliance with the License.
zhouyun123 0:047e14f53977 6 * You may obtain a copy of the License at
zhouyun123 0:047e14f53977 7 *
zhouyun123 0:047e14f53977 8 * http://www.apache.org/licenses/LICENSE-2.0
zhouyun123 0:047e14f53977 9 *
zhouyun123 0:047e14f53977 10 * Unless required by applicable law or agreed to in writing, software
zhouyun123 0:047e14f53977 11 * distributed under the License is distributed on an "AS IS" BASIS,
zhouyun123 0:047e14f53977 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
zhouyun123 0:047e14f53977 13 * See the License for the specific language governing permissions and
zhouyun123 0:047e14f53977 14 * limitations under the License.
zhouyun123 0:047e14f53977 15 */
zhouyun123 0:047e14f53977 16
zhouyun123 0:047e14f53977 17 #include "FXOS8700Q.h"
zhouyun123 0:047e14f53977 18
zhouyun123 0:047e14f53977 19 const uint16_t uint14_max = 0x3FFF;
zhouyun123 0:047e14f53977 20 void static inline normalize_14bits(int16_t &x)
zhouyun123 0:047e14f53977 21 {
zhouyun123 0:047e14f53977 22 x = ((x) > (uint14_max/2)) ? (x - uint14_max) : (x);
zhouyun123 0:047e14f53977 23 }
zhouyun123 0:047e14f53977 24
zhouyun123 0:047e14f53977 25 static int16_t dummy_int16_t = 0;
zhouyun123 0:047e14f53977 26 static float dummy_float = 0.0f;
zhouyun123 0:047e14f53977 27
zhouyun123 0:047e14f53977 28 FXOS8700Q::FXOS8700Q(I2C &i2c, uint8_t addr)
zhouyun123 0:047e14f53977 29 {
zhouyun123 0:047e14f53977 30 _i2c = &i2c;
zhouyun123 0:047e14f53977 31 _addr = addr;
zhouyun123 0:047e14f53977 32 // activate the peripheral
zhouyun123 0:047e14f53977 33 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
zhouyun123 0:047e14f53977 34 _i2c->frequency(400000);
zhouyun123 0:047e14f53977 35 writeRegs(data, 2);
zhouyun123 0:047e14f53977 36 data[0] = FXOS8700Q_M_CTRL_REG1;
zhouyun123 0:047e14f53977 37 data[1] = 0x1F;
zhouyun123 0:047e14f53977 38 writeRegs(data, 2);
zhouyun123 0:047e14f53977 39 data[0] = FXOS8700Q_M_CTRL_REG2;
zhouyun123 0:047e14f53977 40 data[1] = 0x20;
zhouyun123 0:047e14f53977 41 writeRegs(data, 2);
zhouyun123 0:047e14f53977 42 data[0] = FXOS8700Q_XYZ_DATA_CFG;
zhouyun123 0:047e14f53977 43 data[1] = 0x00;
zhouyun123 0:047e14f53977 44 writeRegs(data, 2);
zhouyun123 0:047e14f53977 45 data[0] = FXOS8700Q_CTRL_REG1;
zhouyun123 0:047e14f53977 46 data[1] = 0x1C;
zhouyun123 0:047e14f53977 47 writeRegs(data, 2);
zhouyun123 0:047e14f53977 48 }
zhouyun123 0:047e14f53977 49
zhouyun123 0:047e14f53977 50 FXOS8700Q::~FXOS8700Q()
zhouyun123 0:047e14f53977 51 {
zhouyun123 0:047e14f53977 52 _i2c = 0;
zhouyun123 0:047e14f53977 53 _addr = 0;
zhouyun123 0:047e14f53977 54 }
zhouyun123 0:047e14f53977 55
zhouyun123 0:047e14f53977 56 void FXOS8700Q::readRegs(uint8_t addr, uint8_t *data, uint32_t len) const
zhouyun123 0:047e14f53977 57 {
zhouyun123 0:047e14f53977 58 uint8_t t[1] = {addr};
zhouyun123 0:047e14f53977 59 _i2c->write(_addr, (char *)t, sizeof(t), true);
zhouyun123 0:047e14f53977 60 _i2c->read(_addr, (char *)data, len);
zhouyun123 0:047e14f53977 61 }
zhouyun123 0:047e14f53977 62
zhouyun123 0:047e14f53977 63 uint8_t FXOS8700Q::whoAmI() const
zhouyun123 0:047e14f53977 64 {
zhouyun123 0:047e14f53977 65 uint8_t who_am_i = 0;
zhouyun123 0:047e14f53977 66 readRegs(FXOS8700Q_WHOAMI, &who_am_i, sizeof(who_am_i));
zhouyun123 0:047e14f53977 67 return who_am_i;
zhouyun123 0:047e14f53977 68 }
zhouyun123 0:047e14f53977 69
zhouyun123 0:047e14f53977 70 void FXOS8700Q::writeRegs(uint8_t * data, uint32_t len) const
zhouyun123 0:047e14f53977 71 {
zhouyun123 0:047e14f53977 72 _i2c->write(_addr, (char *)data, len);
zhouyun123 0:047e14f53977 73 }
zhouyun123 0:047e14f53977 74
zhouyun123 0:047e14f53977 75
zhouyun123 0:047e14f53977 76 int16_t FXOS8700Q::getSensorAxis(uint8_t addr) const
zhouyun123 0:047e14f53977 77 {
zhouyun123 0:047e14f53977 78 uint8_t res[2];
zhouyun123 0:047e14f53977 79 readRegs(addr, res, sizeof(res));
zhouyun123 0:047e14f53977 80 return static_cast<int16_t>((res[0] << 8) | res[1]);
zhouyun123 0:047e14f53977 81 }
zhouyun123 0:047e14f53977 82
zhouyun123 0:047e14f53977 83 void FXOS8700Q::enable(void) const
zhouyun123 0:047e14f53977 84 {
zhouyun123 0:047e14f53977 85 uint8_t data[2];
zhouyun123 0:047e14f53977 86 readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
zhouyun123 0:047e14f53977 87 data[1] |= 0x01;
zhouyun123 0:047e14f53977 88 data[0] = FXOS8700Q_CTRL_REG1;
zhouyun123 0:047e14f53977 89 writeRegs(data, sizeof(data));
zhouyun123 0:047e14f53977 90 }
zhouyun123 0:047e14f53977 91
zhouyun123 0:047e14f53977 92 void FXOS8700Q::disable(void) const
zhouyun123 0:047e14f53977 93 {
zhouyun123 0:047e14f53977 94 uint8_t data[2];
zhouyun123 0:047e14f53977 95 readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
zhouyun123 0:047e14f53977 96 data[1] &= 0xFE;
zhouyun123 0:047e14f53977 97 data[0] = FXOS8700Q_CTRL_REG1;
zhouyun123 0:047e14f53977 98 writeRegs(data, sizeof(data));
zhouyun123 0:047e14f53977 99 }
zhouyun123 0:047e14f53977 100
zhouyun123 0:047e14f53977 101 uint32_t FXOS8700Q::dataReady(void) const
zhouyun123 0:047e14f53977 102 {
zhouyun123 0:047e14f53977 103 uint8_t stat = 0;
zhouyun123 0:047e14f53977 104 readRegs(FXOS8700Q_STATUS, &stat, 1);
zhouyun123 0:047e14f53977 105 return (uint32_t)stat;
zhouyun123 0:047e14f53977 106 }
zhouyun123 0:047e14f53977 107
zhouyun123 0:047e14f53977 108 uint32_t FXOS8700Q::sampleRate(uint32_t frequency) const
zhouyun123 0:047e14f53977 109 {
zhouyun123 0:047e14f53977 110 return(50); // for now sample rate is fixed at 50Hz
zhouyun123 0:047e14f53977 111 }
zhouyun123 0:047e14f53977 112
zhouyun123 0:047e14f53977 113 int16_t FXOS8700QAccelerometer::getX(int16_t &x = dummy_int16_t) const
zhouyun123 0:047e14f53977 114 {
zhouyun123 0:047e14f53977 115 x = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
zhouyun123 0:047e14f53977 116 normalize_14bits(x);
zhouyun123 0:047e14f53977 117 return x;
zhouyun123 0:047e14f53977 118 }
zhouyun123 0:047e14f53977 119
zhouyun123 0:047e14f53977 120 int16_t FXOS8700QAccelerometer::getY(int16_t &y = dummy_int16_t) const
zhouyun123 0:047e14f53977 121 {
zhouyun123 0:047e14f53977 122 y = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
zhouyun123 0:047e14f53977 123 normalize_14bits(y);
zhouyun123 0:047e14f53977 124 return y;
zhouyun123 0:047e14f53977 125 }
zhouyun123 0:047e14f53977 126
zhouyun123 0:047e14f53977 127 int16_t FXOS8700QAccelerometer::getZ(int16_t &z = dummy_int16_t) const
zhouyun123 0:047e14f53977 128 {
zhouyun123 0:047e14f53977 129 z = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
zhouyun123 0:047e14f53977 130 normalize_14bits(z);
zhouyun123 0:047e14f53977 131 return z;
zhouyun123 0:047e14f53977 132 }
zhouyun123 0:047e14f53977 133
zhouyun123 0:047e14f53977 134 float FXOS8700QAccelerometer::getX(float &x = dummy_float) const
zhouyun123 0:047e14f53977 135 {
zhouyun123 0:047e14f53977 136 int16_t val = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
zhouyun123 0:047e14f53977 137 normalize_14bits(val);
zhouyun123 0:047e14f53977 138 x = val / 4096.0f;
zhouyun123 0:047e14f53977 139 return x;
zhouyun123 0:047e14f53977 140 }
zhouyun123 0:047e14f53977 141
zhouyun123 0:047e14f53977 142 float FXOS8700QAccelerometer::getY(float &y = dummy_float) const
zhouyun123 0:047e14f53977 143 {
zhouyun123 0:047e14f53977 144 int16_t val = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
zhouyun123 0:047e14f53977 145 normalize_14bits(val);
zhouyun123 0:047e14f53977 146 y = val / 4096.0f;
zhouyun123 0:047e14f53977 147 return y;
zhouyun123 0:047e14f53977 148 }
zhouyun123 0:047e14f53977 149
zhouyun123 0:047e14f53977 150 float FXOS8700QAccelerometer::getZ(float &z = dummy_float) const
zhouyun123 0:047e14f53977 151 {
zhouyun123 0:047e14f53977 152 int16_t val = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
zhouyun123 0:047e14f53977 153 normalize_14bits(val);
zhouyun123 0:047e14f53977 154 z = val / 4096.0f;
zhouyun123 0:047e14f53977 155 return z;
zhouyun123 0:047e14f53977 156 }
zhouyun123 0:047e14f53977 157
zhouyun123 0:047e14f53977 158 void FXOS8700QAccelerometer::getAxis(motion_data_counts_t &xyz) const
zhouyun123 0:047e14f53977 159 {
zhouyun123 0:047e14f53977 160 uint8_t res[6];
zhouyun123 0:047e14f53977 161 readRegs(FXOS8700Q_OUT_X_MSB, res, sizeof(res));
zhouyun123 0:047e14f53977 162 xyz.x = static_cast<int16_t>((res[0] << 8) | res[1]) >> 2;
zhouyun123 0:047e14f53977 163 xyz.y = static_cast<int16_t>((res[2] << 8) | res[3]) >> 2;
zhouyun123 0:047e14f53977 164 xyz.z = static_cast<int16_t>((res[4] << 8) | res[5]) >> 2;
zhouyun123 0:047e14f53977 165 normalize_14bits(xyz.x);
zhouyun123 0:047e14f53977 166 normalize_14bits(xyz.y);
zhouyun123 0:047e14f53977 167 normalize_14bits(xyz.z);
zhouyun123 0:047e14f53977 168 }
zhouyun123 0:047e14f53977 169
zhouyun123 0:047e14f53977 170 void FXOS8700QAccelerometer::getAxis(motion_data_units_t &xyz) const
zhouyun123 0:047e14f53977 171 {
zhouyun123 0:047e14f53977 172 motion_data_counts_t _xyz;
zhouyun123 0:047e14f53977 173 FXOS8700QAccelerometer::getAxis(_xyz);
zhouyun123 0:047e14f53977 174 xyz.x = _xyz.x / 4096.0f;
zhouyun123 0:047e14f53977 175 xyz.y = _xyz.y / 4096.0f;
zhouyun123 0:047e14f53977 176 xyz.z = _xyz.z / 4096.0f;
zhouyun123 0:047e14f53977 177 }
zhouyun123 0:047e14f53977 178
zhouyun123 0:047e14f53977 179 int16_t FXOS8700QMagnetometer::getX(int16_t &x = dummy_int16_t) const
zhouyun123 0:047e14f53977 180 {
zhouyun123 0:047e14f53977 181 x = getSensorAxis(FXOS8700Q_M_OUT_X_MSB);
zhouyun123 0:047e14f53977 182 return x;
zhouyun123 0:047e14f53977 183 }
zhouyun123 0:047e14f53977 184
zhouyun123 0:047e14f53977 185 int16_t FXOS8700QMagnetometer::getY(int16_t &y = dummy_int16_t) const
zhouyun123 0:047e14f53977 186 {
zhouyun123 0:047e14f53977 187 y = getSensorAxis(FXOS8700Q_M_OUT_Y_MSB);
zhouyun123 0:047e14f53977 188 return y;
zhouyun123 0:047e14f53977 189 }
zhouyun123 0:047e14f53977 190
zhouyun123 0:047e14f53977 191 int16_t FXOS8700QMagnetometer::getZ(int16_t &z = dummy_int16_t) const
zhouyun123 0:047e14f53977 192 {
zhouyun123 0:047e14f53977 193 z = getSensorAxis(FXOS8700Q_M_OUT_Z_MSB);
zhouyun123 0:047e14f53977 194 return z;
zhouyun123 0:047e14f53977 195 }
zhouyun123 0:047e14f53977 196
zhouyun123 0:047e14f53977 197 float FXOS8700QMagnetometer::getX(float &x = dummy_float) const
zhouyun123 0:047e14f53977 198 {
zhouyun123 0:047e14f53977 199 x = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_X_MSB)) * 0.1f;
zhouyun123 0:047e14f53977 200 return x;
zhouyun123 0:047e14f53977 201 }
zhouyun123 0:047e14f53977 202
zhouyun123 0:047e14f53977 203 float FXOS8700QMagnetometer::getY(float &y = dummy_float) const
zhouyun123 0:047e14f53977 204 {
zhouyun123 0:047e14f53977 205 y = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_Y_MSB)) * 0.1f;
zhouyun123 0:047e14f53977 206 return y;
zhouyun123 0:047e14f53977 207 }
zhouyun123 0:047e14f53977 208
zhouyun123 0:047e14f53977 209 float FXOS8700QMagnetometer::getZ(float &z = dummy_float) const
zhouyun123 0:047e14f53977 210 {
zhouyun123 0:047e14f53977 211 z = static_cast<float>(getSensorAxis(FXOS8700Q_M_OUT_Z_MSB)) * 0.1f;
zhouyun123 0:047e14f53977 212 return z;
zhouyun123 0:047e14f53977 213 }
zhouyun123 0:047e14f53977 214
zhouyun123 0:047e14f53977 215 void FXOS8700QMagnetometer::getAxis(motion_data_counts_t &xyz) const
zhouyun123 0:047e14f53977 216 {
zhouyun123 0:047e14f53977 217 uint8_t res[6];
zhouyun123 0:047e14f53977 218 readRegs(FXOS8700Q_M_OUT_X_MSB, res, sizeof(res));
zhouyun123 0:047e14f53977 219 xyz.x = (res[0] << 8) | res[1];
zhouyun123 0:047e14f53977 220 xyz.y = (res[2] << 8) | res[3];
zhouyun123 0:047e14f53977 221 xyz.z = (res[4] << 8) | res[5];
zhouyun123 0:047e14f53977 222 }
zhouyun123 0:047e14f53977 223
zhouyun123 0:047e14f53977 224 void FXOS8700QMagnetometer::getAxis(motion_data_units_t &xyz) const
zhouyun123 0:047e14f53977 225 {
zhouyun123 0:047e14f53977 226 motion_data_counts_t _xyz;
zhouyun123 0:047e14f53977 227 FXOS8700QMagnetometer::getAxis(_xyz);
zhouyun123 0:047e14f53977 228 xyz.x = static_cast<float>(_xyz.x * 0.1f);
zhouyun123 0:047e14f53977 229 xyz.y = static_cast<float>(_xyz.y * 0.1f);
zhouyun123 0:047e14f53977 230 xyz.z = static_cast<float>(_xyz.z * 0.1f);
zhouyun123 0:047e14f53977 231 }