Yunting Zou 201199716
Dependencies: mbed MotionSensor
FXOS8700Q/FXOS8700Q.h@3:4bd22344278d, 2020-05-14 (annotated)
- 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?
User | Revision | Line number | New 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 | #ifndef FXOS8700Q_H |
zhouyun123 | 0:047e14f53977 | 18 | #define FXOS8700Q_H |
zhouyun123 | 0:047e14f53977 | 19 | |
zhouyun123 | 0:047e14f53977 | 20 | #include "mbed.h" |
zhouyun123 | 0:047e14f53977 | 21 | #include "MotionSensor.h" |
zhouyun123 | 0:047e14f53977 | 22 | |
zhouyun123 | 0:047e14f53977 | 23 | // FXOS8700CQ I2C address |
zhouyun123 | 0:047e14f53977 | 24 | #define FXOS8700CQ_SLAVE_ADDR0 (0x1E<<1) // with pins SA0=0, SA1=0 |
zhouyun123 | 0:047e14f53977 | 25 | #define FXOS8700CQ_SLAVE_ADDR1 (0x1D<<1) // with pins SA0=1, SA1=0 |
zhouyun123 | 0:047e14f53977 | 26 | #define FXOS8700CQ_SLAVE_ADDR2 (0x1C<<1) // with pins SA0=0, SA1=1 |
zhouyun123 | 0:047e14f53977 | 27 | #define FXOS8700CQ_SLAVE_ADDR3 (0x1F<<1) // with pins SA0=1, SA1=1 |
zhouyun123 | 0:047e14f53977 | 28 | // FXOS8700CQ internal register addresses |
zhouyun123 | 0:047e14f53977 | 29 | #define FXOS8700Q_STATUS 0x00 |
zhouyun123 | 0:047e14f53977 | 30 | #define FXOS8700Q_OUT_X_MSB 0x01 |
zhouyun123 | 0:047e14f53977 | 31 | #define FXOS8700Q_OUT_Y_MSB 0x03 |
zhouyun123 | 0:047e14f53977 | 32 | #define FXOS8700Q_OUT_Z_MSB 0x05 |
zhouyun123 | 0:047e14f53977 | 33 | #define FXOS8700Q_M_OUT_X_MSB 0x33 |
zhouyun123 | 0:047e14f53977 | 34 | #define FXOS8700Q_M_OUT_Y_MSB 0x35 |
zhouyun123 | 0:047e14f53977 | 35 | #define FXOS8700Q_M_OUT_Z_MSB 0x37 |
zhouyun123 | 0:047e14f53977 | 36 | #define FXOS8700Q_WHOAMI 0x0D |
zhouyun123 | 0:047e14f53977 | 37 | #define FXOS8700Q_XYZ_DATA_CFG 0x0E |
zhouyun123 | 0:047e14f53977 | 38 | #define FXOS8700Q_CTRL_REG1 0x2A |
zhouyun123 | 0:047e14f53977 | 39 | #define FXOS8700Q_M_CTRL_REG1 0x5B |
zhouyun123 | 0:047e14f53977 | 40 | #define FXOS8700Q_M_CTRL_REG2 0x5C |
zhouyun123 | 0:047e14f53977 | 41 | #define FXOS8700Q_WHOAMI_VAL 0xC7 |
zhouyun123 | 0:047e14f53977 | 42 | |
zhouyun123 | 0:047e14f53977 | 43 | |
zhouyun123 | 0:047e14f53977 | 44 | /** FXOS8700Q accelerometer example |
zhouyun123 | 0:047e14f53977 | 45 | @code |
zhouyun123 | 0:047e14f53977 | 46 | #include "mbed.h" |
zhouyun123 | 0:047e14f53977 | 47 | #include "FXOS8700Q.h" |
zhouyun123 | 0:047e14f53977 | 48 | I2C i2c(PTE25, PTE24); |
zhouyun123 | 0:047e14f53977 | 49 | FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
zhouyun123 | 0:047e14f53977 | 50 | FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1); |
zhouyun123 | 0:047e14f53977 | 51 | int main(void) |
zhouyun123 | 0:047e14f53977 | 52 | { |
zhouyun123 | 0:047e14f53977 | 53 | motion_data_units_t acc_data, mag_data; |
zhouyun123 | 0:047e14f53977 | 54 | motion_data_counts_t acc_raw, mag_raw; |
zhouyun123 | 0:047e14f53977 | 55 | float faX, faY, faZ, fmX, fmY, fmZ, tmp_float; |
zhouyun123 | 0:047e14f53977 | 56 | int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int; |
zhouyun123 | 0:047e14f53977 | 57 | acc.enable(); |
zhouyun123 | 0:047e14f53977 | 58 | mag.enable(); |
zhouyun123 | 0:047e14f53977 | 59 | while (true) { |
zhouyun123 | 0:047e14f53977 | 60 | // counts based results |
zhouyun123 | 0:047e14f53977 | 61 | acc.getAxis(acc_raw); |
zhouyun123 | 0:047e14f53977 | 62 | mag.getAxis(mag_raw); |
zhouyun123 | 0:047e14f53977 | 63 | acc.getX(raX); |
zhouyun123 | 0:047e14f53977 | 64 | acc.getY(raY); |
zhouyun123 | 0:047e14f53977 | 65 | acc.getZ(raZ); |
zhouyun123 | 0:047e14f53977 | 66 | mag.getX(rmX); |
zhouyun123 | 0:047e14f53977 | 67 | mag.getY(rmY); |
zhouyun123 | 0:047e14f53977 | 68 | mag.getZ(rmZ); |
zhouyun123 | 0:047e14f53977 | 69 | // unit based results |
zhouyun123 | 0:047e14f53977 | 70 | acc.getAxis(acc_data); |
zhouyun123 | 0:047e14f53977 | 71 | mag.getAxis(mag_data); |
zhouyun123 | 0:047e14f53977 | 72 | acc.getX(faX); |
zhouyun123 | 0:047e14f53977 | 73 | acc.getY(faY); |
zhouyun123 | 0:047e14f53977 | 74 | acc.getZ(faZ); |
zhouyun123 | 0:047e14f53977 | 75 | mag.getX(fmX); |
zhouyun123 | 0:047e14f53977 | 76 | mag.getY(fmY); |
zhouyun123 | 0:047e14f53977 | 77 | mag.getZ(fmZ); |
zhouyun123 | 0:047e14f53977 | 78 | wait(0.1f); |
zhouyun123 | 0:047e14f53977 | 79 | } |
zhouyun123 | 0:047e14f53977 | 80 | } |
zhouyun123 | 0:047e14f53977 | 81 | @endcode |
zhouyun123 | 0:047e14f53977 | 82 | */ |
zhouyun123 | 0:047e14f53977 | 83 | |
zhouyun123 | 0:047e14f53977 | 84 | /** FXOS8700Q driver class |
zhouyun123 | 0:047e14f53977 | 85 | */ |
zhouyun123 | 0:047e14f53977 | 86 | class FXOS8700Q : public MotionSensor |
zhouyun123 | 0:047e14f53977 | 87 | { |
zhouyun123 | 0:047e14f53977 | 88 | public: |
zhouyun123 | 0:047e14f53977 | 89 | |
zhouyun123 | 0:047e14f53977 | 90 | /** Read a device register |
zhouyun123 | 0:047e14f53977 | 91 | @param addr The address to read from |
zhouyun123 | 0:047e14f53977 | 92 | @param data The data to read from it |
zhouyun123 | 0:047e14f53977 | 93 | @param len The amount of data to read from it |
zhouyun123 | 0:047e14f53977 | 94 | @return 0 if successful, negative number otherwise |
zhouyun123 | 0:047e14f53977 | 95 | */ |
zhouyun123 | 0:047e14f53977 | 96 | void readRegs(uint8_t addr, uint8_t *data, uint32_t len) const; |
zhouyun123 | 0:047e14f53977 | 97 | |
zhouyun123 | 0:047e14f53977 | 98 | /** Read the ID from a whoAmI register |
zhouyun123 | 0:047e14f53977 | 99 | @return The device whoAmI register contents |
zhouyun123 | 0:047e14f53977 | 100 | */ |
zhouyun123 | 0:047e14f53977 | 101 | uint8_t whoAmI(void) const; |
zhouyun123 | 0:047e14f53977 | 102 | |
zhouyun123 | 0:047e14f53977 | 103 | virtual void enable(void) const; |
zhouyun123 | 0:047e14f53977 | 104 | virtual void disable(void) const; |
zhouyun123 | 0:047e14f53977 | 105 | virtual uint32_t sampleRate(uint32_t frequency) const; |
zhouyun123 | 0:047e14f53977 | 106 | virtual uint32_t dataReady(void) const; |
zhouyun123 | 0:047e14f53977 | 107 | |
zhouyun123 | 0:047e14f53977 | 108 | protected: |
zhouyun123 | 0:047e14f53977 | 109 | I2C *_i2c; |
zhouyun123 | 0:047e14f53977 | 110 | uint8_t _addr; |
zhouyun123 | 0:047e14f53977 | 111 | |
zhouyun123 | 0:047e14f53977 | 112 | /** FXOS8700Q constructor |
zhouyun123 | 0:047e14f53977 | 113 | @param i2c a configured i2c object |
zhouyun123 | 0:047e14f53977 | 114 | @param addr addr of the I2C peripheral as wired |
zhouyun123 | 0:047e14f53977 | 115 | */ |
zhouyun123 | 0:047e14f53977 | 116 | FXOS8700Q(I2C &i2c, uint8_t addr); |
zhouyun123 | 0:047e14f53977 | 117 | |
zhouyun123 | 0:047e14f53977 | 118 | /** FXOS8700Q deconstructor |
zhouyun123 | 0:047e14f53977 | 119 | */ |
zhouyun123 | 0:047e14f53977 | 120 | ~FXOS8700Q(); |
zhouyun123 | 0:047e14f53977 | 121 | |
zhouyun123 | 0:047e14f53977 | 122 | void writeRegs(uint8_t *data, uint32_t len) const; |
zhouyun123 | 0:047e14f53977 | 123 | int16_t getSensorAxis(uint8_t addr) const; |
zhouyun123 | 0:047e14f53977 | 124 | }; |
zhouyun123 | 0:047e14f53977 | 125 | |
zhouyun123 | 0:047e14f53977 | 126 | /** FXOS8700QAccelerometer interface |
zhouyun123 | 0:047e14f53977 | 127 | */ |
zhouyun123 | 0:047e14f53977 | 128 | class FXOS8700QAccelerometer : public FXOS8700Q |
zhouyun123 | 0:047e14f53977 | 129 | { |
zhouyun123 | 0:047e14f53977 | 130 | public: |
zhouyun123 | 0:047e14f53977 | 131 | |
zhouyun123 | 0:047e14f53977 | 132 | FXOS8700QAccelerometer(I2C &i2c, uint8_t addr) : FXOS8700Q(i2c, addr) {} |
zhouyun123 | 0:047e14f53977 | 133 | |
zhouyun123 | 0:047e14f53977 | 134 | virtual int16_t getX(int16_t &x) const; |
zhouyun123 | 0:047e14f53977 | 135 | virtual int16_t getY(int16_t &y) const; |
zhouyun123 | 0:047e14f53977 | 136 | virtual int16_t getZ(int16_t &z) const; |
zhouyun123 | 0:047e14f53977 | 137 | virtual float getX(float &x) const; |
zhouyun123 | 0:047e14f53977 | 138 | virtual float getY(float &y) const; |
zhouyun123 | 0:047e14f53977 | 139 | virtual float getZ(float &z) const; |
zhouyun123 | 0:047e14f53977 | 140 | virtual void getAxis(motion_data_counts_t &xyz) const; |
zhouyun123 | 0:047e14f53977 | 141 | virtual void getAxis(motion_data_units_t &xyz) const; |
zhouyun123 | 0:047e14f53977 | 142 | |
zhouyun123 | 0:047e14f53977 | 143 | }; |
zhouyun123 | 0:047e14f53977 | 144 | |
zhouyun123 | 0:047e14f53977 | 145 | /** FXOS8700QMagnetometer interface |
zhouyun123 | 0:047e14f53977 | 146 | */ |
zhouyun123 | 0:047e14f53977 | 147 | class FXOS8700QMagnetometer : public FXOS8700Q |
zhouyun123 | 0:047e14f53977 | 148 | { |
zhouyun123 | 0:047e14f53977 | 149 | public: |
zhouyun123 | 0:047e14f53977 | 150 | |
zhouyun123 | 0:047e14f53977 | 151 | FXOS8700QMagnetometer(I2C &i2c, uint8_t addr) : FXOS8700Q(i2c, addr) {} |
zhouyun123 | 0:047e14f53977 | 152 | |
zhouyun123 | 0:047e14f53977 | 153 | virtual int16_t getX(int16_t &x) const; |
zhouyun123 | 0:047e14f53977 | 154 | virtual int16_t getY(int16_t &y) const; |
zhouyun123 | 0:047e14f53977 | 155 | virtual int16_t getZ(int16_t &z) const; |
zhouyun123 | 0:047e14f53977 | 156 | virtual float getX(float &x) const; |
zhouyun123 | 0:047e14f53977 | 157 | virtual float getY(float &y) const; |
zhouyun123 | 0:047e14f53977 | 158 | virtual float getZ(float &z) const; |
zhouyun123 | 0:047e14f53977 | 159 | virtual void getAxis(motion_data_counts_t &xyz) const; |
zhouyun123 | 0:047e14f53977 | 160 | virtual void getAxis(motion_data_units_t &xyz) const; |
zhouyun123 | 0:047e14f53977 | 161 | |
zhouyun123 | 0:047e14f53977 | 162 | }; |
zhouyun123 | 0:047e14f53977 | 163 | |
zhouyun123 | 0:047e14f53977 | 164 | #endif |