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

Committer:
Pythia
Date:
Sat Aug 01 05:22:46 2015 +0000
Revision:
1:816ba6bfbb20
Parent:
0:8aa01dbab64e
Child:
2:606d0965f546
initial (empty) version

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 struct FXAS21002CQ_Config_struct FXAS21002CQ_Config =
Pythia 1:816ba6bfbb20 25 {
Pythia 1:816ba6bfbb20 26 {CIRCULAR_BUFFER, 0}, // F_SETUP;
Pythia 1:816ba6bfbb20 27 {HIGH_BW, SPI4, LOW_HP, DISABLED_HP, RANGE_1000}, // CTRL_REG0;
Pythia 1:816ba6bfbb20 28 {DISABLED_ELE, DISABLED_ZTEVE, DISABLED_YTEVE, DISABLED_XTEVE}, // RT_CFG;
Pythia 1:816ba6bfbb20 29 {DECREMENT_CNT, 0}, // RT_THS;
Pythia 1:816ba6bfbb20 30 {0}, // RT_COUNT;
Pythia 1:816ba6bfbb20 31 {NOT_RST, NOT_SELF_TEST, ODR_50, ACTIVE_GYRO, READY_GYRO}, // CTRL_REG1;
Pythia 1:816ba6bfbb20 32 {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 1:816ba6bfbb20 33 {WRAP_TO_ZERO, INT2_IS_INTERUPT, NOT_DOUBLE_RANGE} // CTRL_REG3;
Pythia 1:816ba6bfbb20 34 };
Pythia 1:816ba6bfbb20 35
Pythia 0:8aa01dbab64e 36 static int16_t dummy_int16_t = 0;
Pythia 0:8aa01dbab64e 37 static float dummy_float = 0.0f;
Pythia 0:8aa01dbab64e 38
Pythia 0:8aa01dbab64e 39
Pythia 0:8aa01dbab64e 40 FXAS21002CQ::FXAS21002CQ(I2C &i2c, uint8_t addr)
Pythia 0:8aa01dbab64e 41 {
Pythia 0:8aa01dbab64e 42 _i2c = &i2c;
Pythia 0:8aa01dbab64e 43 _addr = addr;
Pythia 0:8aa01dbab64e 44 /*
Pythia 0:8aa01dbab64e 45 // activate the peripheral
Pythia 0:8aa01dbab64e 46 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
Pythia 0:8aa01dbab64e 47 _i2c->frequency(400000);
Pythia 0:8aa01dbab64e 48 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 49 data[0] = FXOS8700Q_M_CTRL_REG1;
Pythia 0:8aa01dbab64e 50 data[1] = 0x1F;
Pythia 0:8aa01dbab64e 51 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 52 data[0] = FXOS8700Q_M_CTRL_REG2;
Pythia 0:8aa01dbab64e 53 data[1] = 0x20;
Pythia 0:8aa01dbab64e 54 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 55 data[0] = FXOS8700Q_XYZ_DATA_CFG;
Pythia 0:8aa01dbab64e 56 data[1] = 0x00;
Pythia 0:8aa01dbab64e 57 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 58 data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 59 data[1] = 0x1C;
Pythia 0:8aa01dbab64e 60 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 61 */
Pythia 0:8aa01dbab64e 62 }
Pythia 0:8aa01dbab64e 63
Pythia 0:8aa01dbab64e 64 FXAS21002CQ::~FXAS21002CQ()
Pythia 0:8aa01dbab64e 65 {
Pythia 0:8aa01dbab64e 66 _i2c = 0;
Pythia 0:8aa01dbab64e 67 _addr = 0;
Pythia 0:8aa01dbab64e 68 }
Pythia 0:8aa01dbab64e 69
Pythia 0:8aa01dbab64e 70 void FXAS21002CQ::readRegs(uint8_t addr, uint8_t *data, uint32_t len)
Pythia 0:8aa01dbab64e 71 {
Pythia 0:8aa01dbab64e 72 uint8_t t[1] = {addr};
Pythia 0:8aa01dbab64e 73 _i2c->write(_addr, (char *)t, sizeof(t), true);
Pythia 0:8aa01dbab64e 74 _i2c->read(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 75 }
Pythia 0:8aa01dbab64e 76
Pythia 0:8aa01dbab64e 77 uint8_t FXAS21002CQ::whoAmI()
Pythia 0:8aa01dbab64e 78 {
Pythia 0:8aa01dbab64e 79 uint8_t who_am_i = 0;
Pythia 0:8aa01dbab64e 80 readRegs(FXAS21002CQ_WHO_AM_I, &who_am_i, sizeof(who_am_i));
Pythia 0:8aa01dbab64e 81 return who_am_i;
Pythia 0:8aa01dbab64e 82 }
Pythia 0:8aa01dbab64e 83
Pythia 0:8aa01dbab64e 84 void FXAS21002CQ::writeRegs(uint8_t * data, uint32_t len) const
Pythia 0:8aa01dbab64e 85 {
Pythia 0:8aa01dbab64e 86 _i2c->write(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 87 }
Pythia 0:8aa01dbab64e 88
Pythia 0:8aa01dbab64e 89 int16_t FXAS21002CQ::getSensorAxis(uint8_t addr) const
Pythia 0:8aa01dbab64e 90 {
Pythia 0:8aa01dbab64e 91 uint8_t res[2];
Pythia 0:8aa01dbab64e 92 // readRegs(addr, res, sizeof(res));
Pythia 0:8aa01dbab64e 93 return static_cast<int16_t>((res[0] << 8) | res[1]);
Pythia 0:8aa01dbab64e 94 }
Pythia 0:8aa01dbab64e 95
Pythia 0:8aa01dbab64e 96 void FXAS21002CQ::enable(void) const
Pythia 0:8aa01dbab64e 97 {
Pythia 0:8aa01dbab64e 98 uint8_t data[2];
Pythia 0:8aa01dbab64e 99 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 100 // data[1] |= 0x01;
Pythia 0:8aa01dbab64e 101 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 102 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 103 }
Pythia 0:8aa01dbab64e 104
Pythia 0:8aa01dbab64e 105 void FXAS21002CQ::disable(void) const
Pythia 0:8aa01dbab64e 106 {
Pythia 0:8aa01dbab64e 107 uint8_t data[2];
Pythia 0:8aa01dbab64e 108 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 109 // data[1] &= 0xFE;
Pythia 0:8aa01dbab64e 110 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 111 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 112 }
Pythia 0:8aa01dbab64e 113
Pythia 0:8aa01dbab64e 114 uint32_t FXAS21002CQ::dataReady(void) const
Pythia 0:8aa01dbab64e 115 {
Pythia 0:8aa01dbab64e 116 // uint8_t stat = 0;
Pythia 0:8aa01dbab64e 117 // readRegs(FXOS8700Q_STATUS, &stat, 1);
Pythia 0:8aa01dbab64e 118 return (uint32_t)1;
Pythia 0:8aa01dbab64e 119 }
Pythia 0:8aa01dbab64e 120
Pythia 0:8aa01dbab64e 121 uint32_t FXAS21002CQ::sampleRate(uint32_t frequency) const
Pythia 0:8aa01dbab64e 122 {
Pythia 0:8aa01dbab64e 123 return(50); // for now sample rate is fixed at 50Hz
Pythia 0:8aa01dbab64e 124 }
Pythia 0:8aa01dbab64e 125
Pythia 0:8aa01dbab64e 126 int16_t FXAS21002CQ::getX(int16_t &x = dummy_int16_t) const
Pythia 0:8aa01dbab64e 127 {
Pythia 0:8aa01dbab64e 128 // x = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 129 // normalize_14bits(x);
Pythia 0:8aa01dbab64e 130 return x;
Pythia 0:8aa01dbab64e 131 }
Pythia 0:8aa01dbab64e 132
Pythia 0:8aa01dbab64e 133 int16_t FXAS21002CQ::getY(int16_t &y = dummy_int16_t) const
Pythia 0:8aa01dbab64e 134 {
Pythia 0:8aa01dbab64e 135 // y = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 136 // normalize_14bits(y);
Pythia 0:8aa01dbab64e 137 return y;
Pythia 0:8aa01dbab64e 138 }
Pythia 0:8aa01dbab64e 139
Pythia 0:8aa01dbab64e 140 int16_t FXAS21002CQ::getZ(int16_t &z = dummy_int16_t) const
Pythia 0:8aa01dbab64e 141 {
Pythia 0:8aa01dbab64e 142 // z = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 143 // normalize_14bits(z);
Pythia 0:8aa01dbab64e 144 return z;
Pythia 0:8aa01dbab64e 145 }
Pythia 0:8aa01dbab64e 146
Pythia 0:8aa01dbab64e 147 float FXAS21002CQ::getX(float &x = dummy_float) const
Pythia 0:8aa01dbab64e 148 {
Pythia 0:8aa01dbab64e 149 // int16_t val = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 150 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 151 // x = val / 4096.0f;
Pythia 0:8aa01dbab64e 152 return x;
Pythia 0:8aa01dbab64e 153 }
Pythia 0:8aa01dbab64e 154
Pythia 0:8aa01dbab64e 155 float FXAS21002CQ::getY(float &y = dummy_float) const
Pythia 0:8aa01dbab64e 156 {
Pythia 0:8aa01dbab64e 157 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 158 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 159 // y = val / 4096.0f;
Pythia 0:8aa01dbab64e 160 return y;
Pythia 0:8aa01dbab64e 161 }
Pythia 0:8aa01dbab64e 162
Pythia 0:8aa01dbab64e 163 float FXAS21002CQ::getZ(float &z = dummy_float) const
Pythia 0:8aa01dbab64e 164 {
Pythia 0:8aa01dbab64e 165 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 166 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 167 // z = val / 4096.0f;
Pythia 0:8aa01dbab64e 168 return z;
Pythia 0:8aa01dbab64e 169 }
Pythia 0:8aa01dbab64e 170
Pythia 0:8aa01dbab64e 171 void FXAS21002CQ::getAxis(motion_data_counts_t &xyz) const
Pythia 0:8aa01dbab64e 172 {
Pythia 0:8aa01dbab64e 173 uint8_t res[6];
Pythia 0:8aa01dbab64e 174 // readRegs(FXOS8700Q_OUT_X_MSB, res, sizeof(res));
Pythia 0:8aa01dbab64e 175 // xyz.x = static_cast<int16_t>((res[0] << 8) | res[1]) >> 2;
Pythia 0:8aa01dbab64e 176 // xyz.y = static_cast<int16_t>((res[2] << 8) | res[3]) >> 2;
Pythia 0:8aa01dbab64e 177 // xyz.z = static_cast<int16_t>((res[4] << 8) | res[5]) >> 2;
Pythia 0:8aa01dbab64e 178 // normalize_14bits(xyz.x);
Pythia 0:8aa01dbab64e 179 // normalize_14bits(xyz.y);
Pythia 0:8aa01dbab64e 180 // normalize_14bits(xyz.z);
Pythia 0:8aa01dbab64e 181 }
Pythia 0:8aa01dbab64e 182
Pythia 0:8aa01dbab64e 183 void FXAS21002CQ::getAxis(motion_data_units_t &xyz) const
Pythia 0:8aa01dbab64e 184 {
Pythia 0:8aa01dbab64e 185 // motion_data_counts_t _xyz;
Pythia 0:8aa01dbab64e 186 // FXOS8700QAccelerometer::getAxis(_xyz);
Pythia 0:8aa01dbab64e 187 // xyz.x = _xyz.x / 4096.0f;
Pythia 0:8aa01dbab64e 188 // xyz.y = _xyz.y / 4096.0f;
Pythia 0:8aa01dbab64e 189 // xyz.z = _xyz.z / 4096.0f;
Pythia 0:8aa01dbab64e 190 }
Pythia 0:8aa01dbab64e 191
Pythia 0:8aa01dbab64e 192 int8_t FXAS21002CQ::getTemperature(void)
Pythia 0:8aa01dbab64e 193 {
Pythia 0:8aa01dbab64e 194 uint8_t res;
Pythia 0:8aa01dbab64e 195 readRegs(FXAS21002CQ_TEMP, &res, sizeof(res));
Pythia 0:8aa01dbab64e 196 return (int8_t)res;
Pythia 0:8aa01dbab64e 197 }