A basic library for the FXOS8700Q combination accelerometer / magnetometer

Dependencies:   MotionSensor

Dependents:   K64F_eCompass_LCD Hello_FXOS8700Q rtos_compass K64F_eCompass ... more

This library supports the 6 axis combination Accelerometer / Magnetometer. Functions are provided to retrieve data in raw 16 bit signed integers or unit converted G's and micro-teslas

Committer:
JimCarver
Date:
Fri Apr 25 16:45:51 2014 +0000
Revision:
7:c53dda05b8cf
Parent:
6:cdc362f08339
Child:
8:52116f8fb3da
Version 1.0 using MotionSensor virtual class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 3:eb1271ef90bc 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
JimCarver 3:eb1271ef90bc 2 *
JimCarver 3:eb1271ef90bc 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
JimCarver 3:eb1271ef90bc 4 * and associated documentation files (the "Software"), to deal in the Software without
JimCarver 3:eb1271ef90bc 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
JimCarver 3:eb1271ef90bc 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
JimCarver 3:eb1271ef90bc 7 * Software is furnished to do so, subject to the following conditions:
JimCarver 3:eb1271ef90bc 8 *
JimCarver 3:eb1271ef90bc 9 * The above copyright notice and this permission notice shall be included in all copies or
JimCarver 3:eb1271ef90bc 10 * substantial portions of the Software.
JimCarver 3:eb1271ef90bc 11 *
JimCarver 3:eb1271ef90bc 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
JimCarver 3:eb1271ef90bc 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
JimCarver 3:eb1271ef90bc 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
JimCarver 3:eb1271ef90bc 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
JimCarver 3:eb1271ef90bc 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
JimCarver 3:eb1271ef90bc 17 */
JimCarver 3:eb1271ef90bc 18
JimCarver 0:2562215f5bc0 19 #include "FXOS8700Q.h"
JimCarver 1:8b53edef272f 20 #define UINT14_MAX 16383
JimCarver 1:8b53edef272f 21
JimCarver 1:8b53edef272f 22
JimCarver 5:c4176a12f7d5 23 FXOS8700Q_acc::FXOS8700Q_acc(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
JimCarver 1:8b53edef272f 24 // activate the peripheral
JimCarver 1:8b53edef272f 25 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
JimCarver 4:be6abf9f2d59 26 m_i2c.frequency(400000);
JimCarver 1:8b53edef272f 27 writeRegs(data, 2);
JimCarver 1:8b53edef272f 28 data[0] = FXOS8700Q_M_CTRL_REG1;
JimCarver 1:8b53edef272f 29 data[1] = 0x1F;
JimCarver 1:8b53edef272f 30 writeRegs(data, 2);
JimCarver 1:8b53edef272f 31 data[0] = FXOS8700Q_M_CTRL_REG2;
JimCarver 1:8b53edef272f 32 data[1] = 0x20;
JimCarver 1:8b53edef272f 33 writeRegs(data, 2);
JimCarver 1:8b53edef272f 34 data[0] = FXOS8700Q_XYZ_DATA_CFG;
JimCarver 1:8b53edef272f 35 data[1] = 0x00;
JimCarver 1:8b53edef272f 36 writeRegs(data, 2);
JimCarver 1:8b53edef272f 37 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 6:cdc362f08339 38 data[1] = 0x18;//0x1D;
JimCarver 6:cdc362f08339 39 writeRegs(data, 2);
JimCarver 6:cdc362f08339 40 }
JimCarver 6:cdc362f08339 41
JimCarver 6:cdc362f08339 42 FXOS8700Q_acc::~FXOS8700Q_acc() { }
JimCarver 6:cdc362f08339 43
JimCarver 6:cdc362f08339 44 void FXOS8700Q_acc::enable(void) {
JimCarver 6:cdc362f08339 45 uint8_t data[2];
JimCarver 6:cdc362f08339 46 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
JimCarver 6:cdc362f08339 47 data[1] |= 0x01;
JimCarver 6:cdc362f08339 48 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 6:cdc362f08339 49 writeRegs(data, 2);
JimCarver 6:cdc362f08339 50 }
JimCarver 6:cdc362f08339 51
JimCarver 6:cdc362f08339 52 void FXOS8700Q_acc::disable(void) {
JimCarver 6:cdc362f08339 53 uint8_t data[2];
JimCarver 6:cdc362f08339 54 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
JimCarver 6:cdc362f08339 55 data[1] &= 0xFE;
JimCarver 6:cdc362f08339 56 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 1:8b53edef272f 57 writeRegs(data, 2);
JimCarver 1:8b53edef272f 58 }
JimCarver 0:2562215f5bc0 59
JimCarver 0:2562215f5bc0 60
JimCarver 1:8b53edef272f 61
JimCarver 6:cdc362f08339 62 uint32_t FXOS8700Q_acc::whoAmI() {
JimCarver 1:8b53edef272f 63 uint8_t who_am_i = 0;
JimCarver 1:8b53edef272f 64 readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1);
JimCarver 6:cdc362f08339 65 return (uint32_t) who_am_i;
JimCarver 6:cdc362f08339 66 }
JimCarver 6:cdc362f08339 67
JimCarver 6:cdc362f08339 68 uint32_t FXOS8700Q_acc::dataReady(void) {
JimCarver 6:cdc362f08339 69 uint8_t stat = 0;
JimCarver 6:cdc362f08339 70 readRegs(FXOS8700Q_STATUS, &stat, 1);
JimCarver 6:cdc362f08339 71 return (uint32_t) stat;
JimCarver 6:cdc362f08339 72 }
JimCarver 6:cdc362f08339 73
JimCarver 6:cdc362f08339 74 uint32_t FXOS8700Q_acc::sampleRate(uint32_t f) {
JimCarver 6:cdc362f08339 75 return(50); // for now sample rate is fixed at 50Hz
JimCarver 1:8b53edef272f 76 }
JimCarver 1:8b53edef272f 77
JimCarver 6:cdc362f08339 78 void FXOS8700Q_acc::getX(float * x) {
JimCarver 6:cdc362f08339 79 *x = (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0f);
JimCarver 6:cdc362f08339 80 }
JimCarver 6:cdc362f08339 81
JimCarver 6:cdc362f08339 82 void FXOS8700Q_acc::getY(float * y) {
JimCarver 6:cdc362f08339 83 *y = (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0f);
JimCarver 0:2562215f5bc0 84 }
JimCarver 1:8b53edef272f 85
JimCarver 6:cdc362f08339 86 void FXOS8700Q_acc::getZ(float * z) {
JimCarver 6:cdc362f08339 87 *z = (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0f);
JimCarver 1:8b53edef272f 88 }
JimCarver 1:8b53edef272f 89
JimCarver 6:cdc362f08339 90 void FXOS8700Q_acc::getX(int16_t * d) {
JimCarver 6:cdc362f08339 91 *d = getAccAxis(FXOS8700Q_OUT_X_MSB);
JimCarver 6:cdc362f08339 92 }
JimCarver 6:cdc362f08339 93
JimCarver 6:cdc362f08339 94 void FXOS8700Q_acc::getY(int16_t * d) {
JimCarver 6:cdc362f08339 95 *d = getAccAxis(FXOS8700Q_OUT_Y_MSB);
JimCarver 6:cdc362f08339 96 }
JimCarver 6:cdc362f08339 97
JimCarver 6:cdc362f08339 98 void FXOS8700Q_acc::getZ(int16_t * d) {
JimCarver 6:cdc362f08339 99 *d = getAccAxis(FXOS8700Q_OUT_Z_MSB);
JimCarver 1:8b53edef272f 100 }
JimCarver 1:8b53edef272f 101
JimCarver 1:8b53edef272f 102
JimCarver 6:cdc362f08339 103 void FXOS8700Q_acc::getAxis(MotionSensorDataUnits &data) {
JimCarver 6:cdc362f08339 104 int16_t acc, t[3];
JimCarver 6:cdc362f08339 105 uint8_t res[6];
JimCarver 6:cdc362f08339 106 readRegs(FXOS8700Q_OUT_X_MSB, res, 6);
JimCarver 6:cdc362f08339 107
JimCarver 6:cdc362f08339 108 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 6:cdc362f08339 109 if (acc > UINT14_MAX/2)
JimCarver 6:cdc362f08339 110 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 111 t[0] = acc;
JimCarver 6:cdc362f08339 112 acc = (res[2] << 6) | (res[3] >> 2);
JimCarver 6:cdc362f08339 113 if (acc > UINT14_MAX/2)
JimCarver 6:cdc362f08339 114 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 115 t[1] = acc;
JimCarver 6:cdc362f08339 116 acc = (res[4] << 6) | (res[5] >> 2);
JimCarver 6:cdc362f08339 117 if (acc > UINT14_MAX/2)
JimCarver 6:cdc362f08339 118 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 119 t[2] = acc;
JimCarver 6:cdc362f08339 120 data.x = ((float) t[0]) / 4096.0f;
JimCarver 6:cdc362f08339 121 data.y = ((float) t[1]) / 4096.0f;
JimCarver 6:cdc362f08339 122 data.z = ((float) t[2]) / 4096.0f;
JimCarver 0:2562215f5bc0 123 }
JimCarver 0:2562215f5bc0 124
JimCarver 6:cdc362f08339 125
JimCarver 6:cdc362f08339 126 void FXOS8700Q_acc::getAxis(MotionSensorDataCounts &data) {
JimCarver 1:8b53edef272f 127 int16_t acc;
JimCarver 1:8b53edef272f 128 uint8_t res[6];
JimCarver 1:8b53edef272f 129 readRegs(FXOS8700Q_OUT_X_MSB, res, 6);
JimCarver 0:2562215f5bc0 130
JimCarver 1:8b53edef272f 131 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 1:8b53edef272f 132 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 133 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 134 data.x = acc;
JimCarver 1:8b53edef272f 135 acc = (res[2] << 6) | (res[3] >> 2);
JimCarver 1:8b53edef272f 136 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 137 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 138 data.y = acc;
JimCarver 1:8b53edef272f 139 acc = (res[4] << 6) | (res[5] >> 2);
JimCarver 1:8b53edef272f 140 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 141 acc -= UINT14_MAX;
JimCarver 6:cdc362f08339 142 data.z = acc;
JimCarver 0:2562215f5bc0 143 }
JimCarver 0:2562215f5bc0 144
JimCarver 6:cdc362f08339 145 void FXOS8700Q_acc::readRegs(int addr, uint8_t * data, int len) {
JimCarver 6:cdc362f08339 146 char t[1] = {addr};
JimCarver 6:cdc362f08339 147 m_i2c.write(m_addr, t, 1, true);
JimCarver 6:cdc362f08339 148 m_i2c.read(m_addr, (char *)data, len);
JimCarver 1:8b53edef272f 149 }
JimCarver 1:8b53edef272f 150
JimCarver 6:cdc362f08339 151 void FXOS8700Q_acc::writeRegs(uint8_t * data, int len) {
JimCarver 6:cdc362f08339 152 m_i2c.write(m_addr, (char *)data, len);
JimCarver 6:cdc362f08339 153 }
JimCarver 6:cdc362f08339 154
JimCarver 6:cdc362f08339 155
JimCarver 5:c4176a12f7d5 156 int16_t FXOS8700Q_acc::getAccAxis(uint8_t addr) {
JimCarver 1:8b53edef272f 157 int16_t acc;
JimCarver 1:8b53edef272f 158 uint8_t res[2];
JimCarver 1:8b53edef272f 159 readRegs(addr, res, 2);
JimCarver 0:2562215f5bc0 160
JimCarver 1:8b53edef272f 161 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 1:8b53edef272f 162 if (acc > UINT14_MAX/2)
JimCarver 1:8b53edef272f 163 acc -= UINT14_MAX;
JimCarver 1:8b53edef272f 164
JimCarver 1:8b53edef272f 165 return acc;
JimCarver 1:8b53edef272f 166 }
JimCarver 0:2562215f5bc0 167
JimCarver 6:cdc362f08339 168
JimCarver 6:cdc362f08339 169
JimCarver 6:cdc362f08339 170 FXOS8700Q_mag::FXOS8700Q_mag(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
JimCarver 6:cdc362f08339 171 // activate the peripheral
JimCarver 6:cdc362f08339 172 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
JimCarver 6:cdc362f08339 173 m_i2c.frequency(400000);
JimCarver 6:cdc362f08339 174 writeRegs(data, 2);
JimCarver 6:cdc362f08339 175 data[0] = FXOS8700Q_M_CTRL_REG1;
JimCarver 6:cdc362f08339 176 data[1] = 0x1F;
JimCarver 6:cdc362f08339 177 writeRegs(data, 2);
JimCarver 6:cdc362f08339 178 data[0] = FXOS8700Q_M_CTRL_REG2;
JimCarver 6:cdc362f08339 179 data[1] = 0x20;
JimCarver 6:cdc362f08339 180 writeRegs(data, 2);
JimCarver 6:cdc362f08339 181 data[0] = FXOS8700Q_XYZ_DATA_CFG;
JimCarver 6:cdc362f08339 182 data[1] = 0x00;
JimCarver 6:cdc362f08339 183 writeRegs(data, 2);
JimCarver 6:cdc362f08339 184 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 6:cdc362f08339 185 data[1] = 0x18;//0x1D;
JimCarver 6:cdc362f08339 186 writeRegs(data, 2);
JimCarver 6:cdc362f08339 187 }
JimCarver 6:cdc362f08339 188
JimCarver 6:cdc362f08339 189 FXOS8700Q_mag::~FXOS8700Q_mag() { }
JimCarver 6:cdc362f08339 190
JimCarver 6:cdc362f08339 191 void FXOS8700Q_mag::enable(void) {
JimCarver 6:cdc362f08339 192 uint8_t data[2];
JimCarver 6:cdc362f08339 193 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
JimCarver 6:cdc362f08339 194 data[1] |= 0x01;
JimCarver 6:cdc362f08339 195 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 6:cdc362f08339 196 writeRegs(data, 2);
JimCarver 6:cdc362f08339 197 }
JimCarver 6:cdc362f08339 198
JimCarver 6:cdc362f08339 199 void FXOS8700Q_mag::disable(void) {
JimCarver 6:cdc362f08339 200 uint8_t data[2];
JimCarver 6:cdc362f08339 201 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
JimCarver 6:cdc362f08339 202 data[1] &= 0xFE;
JimCarver 6:cdc362f08339 203 data[0] = FXOS8700Q_CTRL_REG1;
JimCarver 6:cdc362f08339 204 writeRegs(data, 2);
JimCarver 6:cdc362f08339 205 }
JimCarver 6:cdc362f08339 206
JimCarver 6:cdc362f08339 207
JimCarver 6:cdc362f08339 208
JimCarver 6:cdc362f08339 209 uint32_t FXOS8700Q_mag::whoAmI() {
JimCarver 6:cdc362f08339 210 uint8_t who_am_i = 0;
JimCarver 6:cdc362f08339 211 readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1);
JimCarver 6:cdc362f08339 212 return (uint32_t) who_am_i;
JimCarver 6:cdc362f08339 213 }
JimCarver 6:cdc362f08339 214
JimCarver 6:cdc362f08339 215 uint32_t FXOS8700Q_mag::dataReady(void) {
JimCarver 6:cdc362f08339 216 uint8_t stat = 0;
JimCarver 6:cdc362f08339 217 readRegs(FXOS8700Q_STATUS, &stat, 1);
JimCarver 6:cdc362f08339 218 return (uint32_t) stat;
JimCarver 6:cdc362f08339 219 }
JimCarver 6:cdc362f08339 220
JimCarver 6:cdc362f08339 221 uint32_t FXOS8700Q_mag::sampleRate(uint32_t f) {
JimCarver 6:cdc362f08339 222 return(50); // for now sample rate is fixed at 50Hz
JimCarver 6:cdc362f08339 223 }
JimCarver 6:cdc362f08339 224
JimCarver 6:cdc362f08339 225 void FXOS8700Q_mag::getX(float * x) {
JimCarver 7:c53dda05b8cf 226 *x = (float(getAccAxis(FXOS8700Q_M_OUT_X_MSB)) * 0.1f);
JimCarver 6:cdc362f08339 227 }
JimCarver 6:cdc362f08339 228
JimCarver 6:cdc362f08339 229 void FXOS8700Q_mag::getY(float * y) {
JimCarver 7:c53dda05b8cf 230 *y = (float(getAccAxis(FXOS8700Q_M_OUT_Y_MSB)) * 0.1f);
JimCarver 6:cdc362f08339 231 }
JimCarver 6:cdc362f08339 232
JimCarver 6:cdc362f08339 233 void FXOS8700Q_mag::getZ(float * z) {
JimCarver 7:c53dda05b8cf 234 *z = (float(getAccAxis(FXOS8700Q_M_OUT_Z_MSB)) * 0.1f);
JimCarver 6:cdc362f08339 235 }
JimCarver 6:cdc362f08339 236
JimCarver 6:cdc362f08339 237 void FXOS8700Q_mag::getX(int16_t * d) {
JimCarver 6:cdc362f08339 238 *d = getAccAxis(FXOS8700Q_M_OUT_X_MSB);
JimCarver 6:cdc362f08339 239 }
JimCarver 6:cdc362f08339 240
JimCarver 6:cdc362f08339 241 void FXOS8700Q_mag::getY(int16_t * d) {
JimCarver 6:cdc362f08339 242 *d = getAccAxis(FXOS8700Q_M_OUT_Y_MSB);
JimCarver 6:cdc362f08339 243 }
JimCarver 6:cdc362f08339 244
JimCarver 6:cdc362f08339 245 void FXOS8700Q_mag::getZ(int16_t * d) {
JimCarver 6:cdc362f08339 246 *d = getAccAxis(FXOS8700Q_M_OUT_Z_MSB);
JimCarver 6:cdc362f08339 247 }
JimCarver 6:cdc362f08339 248
JimCarver 6:cdc362f08339 249
JimCarver 6:cdc362f08339 250 void FXOS8700Q_mag::getAxis(MotionSensorDataUnits &data) {
JimCarver 6:cdc362f08339 251 int16_t t[3];
JimCarver 6:cdc362f08339 252 uint8_t res[6];
JimCarver 6:cdc362f08339 253 readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6);
JimCarver 6:cdc362f08339 254
JimCarver 6:cdc362f08339 255 t[0] = (res[0] << 8) | res[1];
JimCarver 6:cdc362f08339 256 t[1] = (res[2] << 8) | res[3];
JimCarver 6:cdc362f08339 257 t[2] = (res[4] << 8) | res[5];
JimCarver 6:cdc362f08339 258
JimCarver 6:cdc362f08339 259 data.x = ((float) t[0]) * 0.1f;
JimCarver 6:cdc362f08339 260 data.y = ((float) t[1]) * 0.1f;
JimCarver 6:cdc362f08339 261 data.z = ((float) t[2]) * 0.1f;
JimCarver 6:cdc362f08339 262 }
JimCarver 6:cdc362f08339 263
JimCarver 6:cdc362f08339 264
JimCarver 6:cdc362f08339 265 void FXOS8700Q_mag::getAxis(MotionSensorDataCounts &data) {
JimCarver 6:cdc362f08339 266 int16_t acc;
JimCarver 6:cdc362f08339 267 uint8_t res[6];
JimCarver 7:c53dda05b8cf 268 readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6);
JimCarver 6:cdc362f08339 269
JimCarver 6:cdc362f08339 270 data.x = (res[0] << 8) | res[1];
JimCarver 6:cdc362f08339 271 data.y = (res[2] << 8) | res[3];
JimCarver 6:cdc362f08339 272 data.z = (res[4] << 8) | res[5];
JimCarver 6:cdc362f08339 273 }
JimCarver 6:cdc362f08339 274
JimCarver 6:cdc362f08339 275 void FXOS8700Q_mag::readRegs(int addr, uint8_t * data, int len) {
JimCarver 1:8b53edef272f 276 char t[1] = {addr};
JimCarver 1:8b53edef272f 277 m_i2c.write(m_addr, t, 1, true);
JimCarver 1:8b53edef272f 278 m_i2c.read(m_addr, (char *)data, len);
JimCarver 0:2562215f5bc0 279 }
JimCarver 1:8b53edef272f 280
JimCarver 6:cdc362f08339 281 void FXOS8700Q_mag::writeRegs(uint8_t * data, int len) {
JimCarver 1:8b53edef272f 282 m_i2c.write(m_addr, (char *)data, len);
JimCarver 1:8b53edef272f 283 }
JimCarver 1:8b53edef272f 284
JimCarver 6:cdc362f08339 285
JimCarver 6:cdc362f08339 286 int16_t FXOS8700Q_mag::getAccAxis(uint8_t addr) {
JimCarver 6:cdc362f08339 287 int16_t acc;
JimCarver 6:cdc362f08339 288 uint8_t res[2];
JimCarver 6:cdc362f08339 289 readRegs(addr, res, 2);
JimCarver 6:cdc362f08339 290
JimCarver 6:cdc362f08339 291 acc = (res[0] << 8) | res[1];
JimCarver 6:cdc362f08339 292
JimCarver 6:cdc362f08339 293 return acc;
JimCarver 6:cdc362f08339 294 }