Library to access multiple giro - with ability to setup them when starting

Committer:
Pythia
Date:
Mon Aug 24 14:29:35 2015 +0000
Revision:
2:606d0965f546
Parent:
1:816ba6bfbb20
Initial version of the software

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pythia 0:8aa01dbab64e 1 /* FXAS21002CQ sensor driver
Pythia 0:8aa01dbab64e 2 * Copyright (c) 2015 WD
Pythia 0:8aa01dbab64e 3 *
Pythia 0:8aa01dbab64e 4 *
Pythia 0:8aa01dbab64e 5 * Unless required by applicable law or agreed to in writing, software
Pythia 0:8aa01dbab64e 6 * distributed under the License is distributed on an "AS IS" BASIS,
Pythia 0:8aa01dbab64e 7 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pythia 0:8aa01dbab64e 8 * See the License for the specific language governing permissions and
Pythia 0:8aa01dbab64e 9 * limitations under the License.
Pythia 0:8aa01dbab64e 10 */
Pythia 0:8aa01dbab64e 11
Pythia 0:8aa01dbab64e 12 #include "FXAS21002CQ.h"
Pythia 0:8aa01dbab64e 13
Pythia 1:816ba6bfbb20 14
Pythia 1:816ba6bfbb20 15 struct FXAS21002CQ_F_SETUP_struct F_SETUP = {CIRCULAR_BUFFER, 0};
Pythia 1:816ba6bfbb20 16 struct FXAS21002CQ_CTRL_REG0_struct CTRL_REG0;
Pythia 1:816ba6bfbb20 17 struct FXAS21002CQ_RT_CFG_struct RT_CFG;
Pythia 1:816ba6bfbb20 18 struct FXAS21002CQ_RT_THS_struct RT_THS;
Pythia 1:816ba6bfbb20 19 struct FXAS21002CQ_RT_COUNT_struct RT_COUNT;
Pythia 1:816ba6bfbb20 20 struct FXAS21002CQ_CTRL_REG1_struct CTRL_REG1;
Pythia 1:816ba6bfbb20 21 struct FXAS21002CQ_CTRL_REG2_struct CTRL_REG2;
Pythia 1:816ba6bfbb20 22 struct FXAS21002CQ_CTRL_REG3_struct CTRL_REG3;
Pythia 1:816ba6bfbb20 23
Pythia 1:816ba6bfbb20 24
Pythia 0:8aa01dbab64e 25 static int16_t dummy_int16_t = 0;
Pythia 0:8aa01dbab64e 26 static float dummy_float = 0.0f;
Pythia 0:8aa01dbab64e 27
Pythia 2:606d0965f546 28 FXAS21002CQ::FXAS21002CQ(I2C &i2c, const uint8_t addr, const struct FXAS21002CQ_Config_struct &FXAS21002CQ_Config)
Pythia 0:8aa01dbab64e 29 {
Pythia 0:8aa01dbab64e 30 _i2c = &i2c;
Pythia 0:8aa01dbab64e 31 _addr = addr;
Pythia 0:8aa01dbab64e 32 /*
Pythia 0:8aa01dbab64e 33 // activate the peripheral
Pythia 0:8aa01dbab64e 34 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
Pythia 0:8aa01dbab64e 35 _i2c->frequency(400000);
Pythia 0:8aa01dbab64e 36 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 37 data[0] = FXOS8700Q_M_CTRL_REG1;
Pythia 0:8aa01dbab64e 38 data[1] = 0x1F;
Pythia 0:8aa01dbab64e 39 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 40 data[0] = FXOS8700Q_M_CTRL_REG2;
Pythia 0:8aa01dbab64e 41 data[1] = 0x20;
Pythia 0:8aa01dbab64e 42 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 43 data[0] = FXOS8700Q_XYZ_DATA_CFG;
Pythia 0:8aa01dbab64e 44 data[1] = 0x00;
Pythia 0:8aa01dbab64e 45 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 46 data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 47 data[1] = 0x1C;
Pythia 0:8aa01dbab64e 48 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 49 */
Pythia 0:8aa01dbab64e 50 }
Pythia 0:8aa01dbab64e 51
Pythia 2:606d0965f546 52 FXAS21002CQ::FXAS21002CQ(I2C &i2c, const uint8_t addr)
Pythia 2:606d0965f546 53 {
Pythia 2:606d0965f546 54 const struct FXAS21002CQ_Config_struct FXAS21002CQ_Config =
Pythia 2:606d0965f546 55 {
Pythia 2:606d0965f546 56 {CIRCULAR_BUFFER, 0}, // F_SETUP;
Pythia 2:606d0965f546 57 {HIGH_BW, SPI4, LOW_HP, DISABLED_HP, RANGE_1000}, // CTRL_REG0;
Pythia 2:606d0965f546 58 {DISABLED_ELE, DISABLED_ZTEVE, DISABLED_YTEVE, DISABLED_XTEVE}, // RT_CFG;
Pythia 2:606d0965f546 59 {DECREMENT_CNT, 0}, // RT_THS;
Pythia 2:606d0965f546 60 {0}, // RT_COUNT;
Pythia 2:606d0965f546 61 {NOT_RST, NOT_SELF_TEST, ODR_50, ACTIVE_GYRO, READY_GYRO}, // CTRL_REG1;
Pythia 2:606d0965f546 62 {FIFO_TO_INT2, FIFO_INT_DISABLE, RATE_TO_INT2, RATE_INT_DISABLE, DRDY_TO_INT2, DRDY_INT_DISABLE, INT_ACT_HIGH, INT_PUSH_PULL}, // CTRL_REG2;
Pythia 2:606d0965f546 63 {WRAP_TO_ZERO, INT2_IS_INTERUPT, NOT_DOUBLE_RANGE} // CTRL_REG3;
Pythia 2:606d0965f546 64 };
Pythia 2:606d0965f546 65
Pythia 2:606d0965f546 66 FXAS21002CQ::FXAS21002CQ(i2c, addr, FXAS21002CQ_Config);
Pythia 2:606d0965f546 67 }
Pythia 2:606d0965f546 68
Pythia 0:8aa01dbab64e 69 FXAS21002CQ::~FXAS21002CQ()
Pythia 0:8aa01dbab64e 70 {
Pythia 0:8aa01dbab64e 71 _i2c = 0;
Pythia 0:8aa01dbab64e 72 _addr = 0;
Pythia 0:8aa01dbab64e 73 }
Pythia 0:8aa01dbab64e 74
Pythia 0:8aa01dbab64e 75 void FXAS21002CQ::readRegs(uint8_t addr, uint8_t *data, uint32_t len)
Pythia 0:8aa01dbab64e 76 {
Pythia 0:8aa01dbab64e 77 uint8_t t[1] = {addr};
Pythia 0:8aa01dbab64e 78 _i2c->write(_addr, (char *)t, sizeof(t), true);
Pythia 0:8aa01dbab64e 79 _i2c->read(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 80 }
Pythia 0:8aa01dbab64e 81
Pythia 0:8aa01dbab64e 82 uint8_t FXAS21002CQ::whoAmI()
Pythia 0:8aa01dbab64e 83 {
Pythia 0:8aa01dbab64e 84 uint8_t who_am_i = 0;
Pythia 0:8aa01dbab64e 85 readRegs(FXAS21002CQ_WHO_AM_I, &who_am_i, sizeof(who_am_i));
Pythia 0:8aa01dbab64e 86 return who_am_i;
Pythia 0:8aa01dbab64e 87 }
Pythia 0:8aa01dbab64e 88
Pythia 0:8aa01dbab64e 89 void FXAS21002CQ::writeRegs(uint8_t * data, uint32_t len) const
Pythia 0:8aa01dbab64e 90 {
Pythia 0:8aa01dbab64e 91 _i2c->write(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 92 }
Pythia 0:8aa01dbab64e 93
Pythia 0:8aa01dbab64e 94 int16_t FXAS21002CQ::getSensorAxis(uint8_t addr) const
Pythia 0:8aa01dbab64e 95 {
Pythia 0:8aa01dbab64e 96 uint8_t res[2];
Pythia 0:8aa01dbab64e 97 // readRegs(addr, res, sizeof(res));
Pythia 0:8aa01dbab64e 98 return static_cast<int16_t>((res[0] << 8) | res[1]);
Pythia 0:8aa01dbab64e 99 }
Pythia 0:8aa01dbab64e 100
Pythia 0:8aa01dbab64e 101 void FXAS21002CQ::enable(void) const
Pythia 0:8aa01dbab64e 102 {
Pythia 0:8aa01dbab64e 103 uint8_t data[2];
Pythia 0:8aa01dbab64e 104 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 105 // data[1] |= 0x01;
Pythia 0:8aa01dbab64e 106 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 107 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 108 }
Pythia 0:8aa01dbab64e 109
Pythia 0:8aa01dbab64e 110 void FXAS21002CQ::disable(void) const
Pythia 0:8aa01dbab64e 111 {
Pythia 0:8aa01dbab64e 112 uint8_t data[2];
Pythia 0:8aa01dbab64e 113 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 114 // data[1] &= 0xFE;
Pythia 0:8aa01dbab64e 115 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 116 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 117 }
Pythia 0:8aa01dbab64e 118
Pythia 0:8aa01dbab64e 119 uint32_t FXAS21002CQ::dataReady(void) const
Pythia 0:8aa01dbab64e 120 {
Pythia 0:8aa01dbab64e 121 // uint8_t stat = 0;
Pythia 0:8aa01dbab64e 122 // readRegs(FXOS8700Q_STATUS, &stat, 1);
Pythia 0:8aa01dbab64e 123 return (uint32_t)1;
Pythia 0:8aa01dbab64e 124 }
Pythia 0:8aa01dbab64e 125
Pythia 0:8aa01dbab64e 126 uint32_t FXAS21002CQ::sampleRate(uint32_t frequency) const
Pythia 0:8aa01dbab64e 127 {
Pythia 0:8aa01dbab64e 128 return(50); // for now sample rate is fixed at 50Hz
Pythia 0:8aa01dbab64e 129 }
Pythia 0:8aa01dbab64e 130
Pythia 0:8aa01dbab64e 131 int16_t FXAS21002CQ::getX(int16_t &x = dummy_int16_t) const
Pythia 0:8aa01dbab64e 132 {
Pythia 0:8aa01dbab64e 133 // x = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 134 // normalize_14bits(x);
Pythia 0:8aa01dbab64e 135 return x;
Pythia 0:8aa01dbab64e 136 }
Pythia 0:8aa01dbab64e 137
Pythia 0:8aa01dbab64e 138 int16_t FXAS21002CQ::getY(int16_t &y = dummy_int16_t) const
Pythia 0:8aa01dbab64e 139 {
Pythia 0:8aa01dbab64e 140 // y = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 141 // normalize_14bits(y);
Pythia 0:8aa01dbab64e 142 return y;
Pythia 0:8aa01dbab64e 143 }
Pythia 0:8aa01dbab64e 144
Pythia 0:8aa01dbab64e 145 int16_t FXAS21002CQ::getZ(int16_t &z = dummy_int16_t) const
Pythia 0:8aa01dbab64e 146 {
Pythia 0:8aa01dbab64e 147 // z = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 148 // normalize_14bits(z);
Pythia 0:8aa01dbab64e 149 return z;
Pythia 0:8aa01dbab64e 150 }
Pythia 0:8aa01dbab64e 151
Pythia 0:8aa01dbab64e 152 float FXAS21002CQ::getX(float &x = dummy_float) const
Pythia 0:8aa01dbab64e 153 {
Pythia 0:8aa01dbab64e 154 // int16_t val = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 155 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 156 // x = val / 4096.0f;
Pythia 0:8aa01dbab64e 157 return x;
Pythia 0:8aa01dbab64e 158 }
Pythia 0:8aa01dbab64e 159
Pythia 0:8aa01dbab64e 160 float FXAS21002CQ::getY(float &y = dummy_float) const
Pythia 0:8aa01dbab64e 161 {
Pythia 0:8aa01dbab64e 162 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 163 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 164 // y = val / 4096.0f;
Pythia 0:8aa01dbab64e 165 return y;
Pythia 0:8aa01dbab64e 166 }
Pythia 0:8aa01dbab64e 167
Pythia 0:8aa01dbab64e 168 float FXAS21002CQ::getZ(float &z = dummy_float) const
Pythia 0:8aa01dbab64e 169 {
Pythia 0:8aa01dbab64e 170 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 171 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 172 // z = val / 4096.0f;
Pythia 0:8aa01dbab64e 173 return z;
Pythia 0:8aa01dbab64e 174 }
Pythia 0:8aa01dbab64e 175
Pythia 0:8aa01dbab64e 176 void FXAS21002CQ::getAxis(motion_data_counts_t &xyz) const
Pythia 0:8aa01dbab64e 177 {
Pythia 0:8aa01dbab64e 178 uint8_t res[6];
Pythia 0:8aa01dbab64e 179 // readRegs(FXOS8700Q_OUT_X_MSB, res, sizeof(res));
Pythia 0:8aa01dbab64e 180 // xyz.x = static_cast<int16_t>((res[0] << 8) | res[1]) >> 2;
Pythia 0:8aa01dbab64e 181 // xyz.y = static_cast<int16_t>((res[2] << 8) | res[3]) >> 2;
Pythia 0:8aa01dbab64e 182 // xyz.z = static_cast<int16_t>((res[4] << 8) | res[5]) >> 2;
Pythia 0:8aa01dbab64e 183 // normalize_14bits(xyz.x);
Pythia 0:8aa01dbab64e 184 // normalize_14bits(xyz.y);
Pythia 0:8aa01dbab64e 185 // normalize_14bits(xyz.z);
Pythia 0:8aa01dbab64e 186 }
Pythia 0:8aa01dbab64e 187
Pythia 0:8aa01dbab64e 188 void FXAS21002CQ::getAxis(motion_data_units_t &xyz) const
Pythia 0:8aa01dbab64e 189 {
Pythia 0:8aa01dbab64e 190 // motion_data_counts_t _xyz;
Pythia 0:8aa01dbab64e 191 // FXOS8700QAccelerometer::getAxis(_xyz);
Pythia 0:8aa01dbab64e 192 // xyz.x = _xyz.x / 4096.0f;
Pythia 0:8aa01dbab64e 193 // xyz.y = _xyz.y / 4096.0f;
Pythia 0:8aa01dbab64e 194 // xyz.z = _xyz.z / 4096.0f;
Pythia 0:8aa01dbab64e 195 }
Pythia 0:8aa01dbab64e 196
Pythia 0:8aa01dbab64e 197 int8_t FXAS21002CQ::getTemperature(void)
Pythia 0:8aa01dbab64e 198 {
Pythia 0:8aa01dbab64e 199 uint8_t res;
Pythia 0:8aa01dbab64e 200 readRegs(FXAS21002CQ_TEMP, &res, sizeof(res));
Pythia 0:8aa01dbab64e 201 return (int8_t)res;
Pythia 0:8aa01dbab64e 202 }
Pythia 2:606d0965f546 203