Driver library for the Freescale FXOS8700Q sensor
Dependencies: MotionSensor
Dependents: frdm-copter shipiot_frdm_bipio AxisData WebSockets_K64 ... more
Fork of FXOS8700Q by
FXOS8700Q.h
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 #ifndef FXOS8700Q_H 00018 #define FXOS8700Q_H 00019 00020 #include "mbed.h" 00021 #include "MotionSensor.h" 00022 00023 // FXOS8700CQ I2C address 00024 #define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0 00025 #define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0 00026 #define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1 00027 #define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1 00028 // FXOS8700CQ internal register addresses 00029 #define FXOS8700Q_STATUS 0x00 00030 #define FXOS8700Q_OUT_X_MSB 0x01 00031 #define FXOS8700Q_OUT_Y_MSB 0x03 00032 #define FXOS8700Q_OUT_Z_MSB 0x05 00033 #define FXOS8700Q_M_OUT_X_MSB 0x33 00034 #define FXOS8700Q_M_OUT_Y_MSB 0x35 00035 #define FXOS8700Q_M_OUT_Z_MSB 0x37 00036 #define FXOS8700Q_WHOAMI 0x0D 00037 #define FXOS8700Q_XYZ_DATA_CFG 0x0E 00038 #define FXOS8700Q_CTRL_REG1 0x2A 00039 #define FXOS8700Q_M_CTRL_REG1 0x5B 00040 #define FXOS8700Q_M_CTRL_REG2 0x5C 00041 #define FXOS8700Q_WHOAMI_VAL 0xC7 00042 00043 00044 /** FXOS8700Q accelerometer example 00045 @code 00046 #include "mbed.h" 00047 #include "FXOS8700Q.h" 00048 I2C i2c(PTE25, PTE24); 00049 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors 00050 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1); 00051 int main(void) 00052 { 00053 motion_data_units_t acc_data, mag_data; 00054 motion_data_counts_t acc_raw, mag_raw; 00055 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float; 00056 int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int; 00057 acc.enable(); 00058 mag.enable(); 00059 while (true) { 00060 // counts based results 00061 acc.getAxis(acc_raw); 00062 mag.getAxis(mag_raw); 00063 acc.getX(raX); 00064 acc.getY(raY); 00065 acc.getZ(raZ); 00066 mag.getX(rmX); 00067 mag.getY(rmY); 00068 mag.getZ(rmZ); 00069 // unit based results 00070 acc.getAxis(acc_data); 00071 mag.getAxis(mag_data); 00072 acc.getX(faX); 00073 acc.getY(faY); 00074 acc.getZ(faZ); 00075 mag.getX(fmX); 00076 mag.getY(fmY); 00077 mag.getZ(fmZ); 00078 wait(0.1f); 00079 } 00080 } 00081 @endcode 00082 */ 00083 00084 /** FXOS8700Q driver class 00085 */ 00086 class FXOS8700Q : public MotionSensor 00087 { 00088 public: 00089 00090 /** Read a device register 00091 @param addr The address to read from 00092 @param data The data to read from it 00093 @param len The amount of data to read from it 00094 @return 0 if successful, negative number otherwise 00095 */ 00096 void readRegs(uint8_t addr, uint8_t *data, uint32_t len) const; 00097 00098 /** Read the ID from a whoAmI register 00099 @return The device whoAmI register contents 00100 */ 00101 uint8_t whoAmI(void) const; 00102 00103 virtual void enable(void) const; 00104 virtual void disable(void) const; 00105 virtual uint32_t sampleRate(uint32_t frequency) const; 00106 virtual uint32_t dataReady(void) const; 00107 00108 protected: 00109 I2C *_i2c; 00110 uint8_t _addr; 00111 00112 /** FXOS8700Q constructor 00113 @param i2c a configured i2c object 00114 @param addr addr of the I2C peripheral as wired 00115 */ 00116 FXOS8700Q(I2C &i2c, uint8_t addr); 00117 00118 /** FXOS8700Q deconstructor 00119 */ 00120 ~FXOS8700Q(); 00121 00122 void writeRegs(uint8_t *data, uint32_t len) const; 00123 int16_t getSensorAxis(uint8_t addr) const; 00124 }; 00125 00126 /** FXOS8700QAccelerometer interface 00127 */ 00128 class FXOS8700QAccelerometer : public FXOS8700Q 00129 { 00130 public: 00131 00132 FXOS8700QAccelerometer(I2C &i2c, uint8_t addr) : FXOS8700Q(i2c, addr) {} 00133 00134 virtual int16_t getX(int16_t &x) const; 00135 virtual int16_t getY(int16_t &y) const; 00136 virtual int16_t getZ(int16_t &z) const; 00137 virtual float getX(float &x) const; 00138 virtual float getY(float &y) const; 00139 virtual float getZ(float &z) const; 00140 virtual void getAxis(motion_data_counts_t &xyz) const; 00141 virtual void getAxis(motion_data_units_t &xyz) const; 00142 00143 }; 00144 00145 /** FXOS8700QMagnetometer interface 00146 */ 00147 class FXOS8700QMagnetometer : public FXOS8700Q 00148 { 00149 public: 00150 00151 FXOS8700QMagnetometer(I2C &i2c, uint8_t addr) : FXOS8700Q(i2c, addr) {} 00152 00153 virtual int16_t getX(int16_t &x) const; 00154 virtual int16_t getY(int16_t &y) const; 00155 virtual int16_t getZ(int16_t &z) const; 00156 virtual float getX(float &x) const; 00157 virtual float getY(float &y) const; 00158 virtual float getZ(float &z) const; 00159 virtual void getAxis(motion_data_counts_t &xyz) const; 00160 virtual void getAxis(motion_data_units_t &xyz) const; 00161 00162 }; 00163 00164 #endif
Generated on Tue Jul 12 2022 15:03:56 by 1.7.2