9 #define D_SA0_HIGH_ADDRESS 0b0011101
10 #define D_SA0_LOW_ADDRESS 0b0011110
11 #define DLHC_DLM_DLH_MAG_ADDRESS 0b0011110
12 #define DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS 0b0011001
13 #define DLM_DLH_ACC_SA0_LOW_ADDRESS 0b0011000
15 #define TEST_REG_ERROR -1
18 #define DLM_WHO_ID 0x3C
33 _device = device_auto;
42 bool LSM303::timeoutOccurred()
44 bool tmp = did_timeout;
49 void LSM303::setTimeout(
unsigned int timeout)
54 unsigned int LSM303::getTimeout()
59 bool LSM303::init(deviceType device, sa0State sa0)
62 if (device == device_auto || sa0 == sa0_auto)
65 if (device == device_auto || device == device_D)
68 if (sa0 != sa0_low && testReg(D_SA0_HIGH_ADDRESS, WHO_AM_I) == D_WHO_ID)
75 else if (sa0 != sa0_high && testReg(D_SA0_LOW_ADDRESS, WHO_AM_I) == D_WHO_ID)
84 if (device == device_auto || device == device_DLHC || device == device_DLM || device == device_DLH)
87 if (sa0 != sa0_low && testReg(DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS, CTRL_REG1_A) != TEST_REG_ERROR)
91 if (device == device_auto)
100 device = (testReg(DLHC_DLM_DLH_MAG_ADDRESS, WHO_AM_I_M) == DLM_WHO_ID) ? device_DLHC : device_DLH;
104 else if (sa0 != sa0_high && testReg(DLM_DLH_ACC_SA0_LOW_ADDRESS, CTRL_REG1_A) != TEST_REG_ERROR)
108 if (device == device_auto)
111 device = (testReg(DLHC_DLM_DLH_MAG_ADDRESS, WHO_AM_I_M) == DLM_WHO_ID) ? device_DLM : device_DLH;
117 if (device == device_auto || sa0 == sa0_auto)
129 acc_address = mag_address = (sa0 == sa0_high) ? D_SA0_HIGH_ADDRESS : D_SA0_LOW_ADDRESS;
130 translated_regs[-OUT_X_L_M] = D_OUT_X_L_M;
131 translated_regs[-OUT_X_H_M] = D_OUT_X_H_M;
132 translated_regs[-OUT_Y_L_M] = D_OUT_Y_L_M;
133 translated_regs[-OUT_Y_H_M] = D_OUT_Y_H_M;
134 translated_regs[-OUT_Z_L_M] = D_OUT_Z_L_M;
135 translated_regs[-OUT_Z_H_M] = D_OUT_Z_H_M;
139 acc_address = DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS;
140 mag_address = DLHC_DLM_DLH_MAG_ADDRESS;
141 translated_regs[-OUT_X_H_M] = DLHC_OUT_X_H_M;
142 translated_regs[-OUT_X_L_M] = DLHC_OUT_X_L_M;
143 translated_regs[-OUT_Y_H_M] = DLHC_OUT_Y_H_M;
144 translated_regs[-OUT_Y_L_M] = DLHC_OUT_Y_L_M;
145 translated_regs[-OUT_Z_H_M] = DLHC_OUT_Z_H_M;
146 translated_regs[-OUT_Z_L_M] = DLHC_OUT_Z_L_M;
150 acc_address = (sa0 == sa0_high) ? DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS : DLM_DLH_ACC_SA0_LOW_ADDRESS;
151 mag_address = DLHC_DLM_DLH_MAG_ADDRESS;
152 translated_regs[-OUT_X_H_M] = DLM_OUT_X_H_M;
153 translated_regs[-OUT_X_L_M] = DLM_OUT_X_L_M;
154 translated_regs[-OUT_Y_H_M] = DLM_OUT_Y_H_M;
155 translated_regs[-OUT_Y_L_M] = DLM_OUT_Y_L_M;
156 translated_regs[-OUT_Z_H_M] = DLM_OUT_Z_H_M;
157 translated_regs[-OUT_Z_L_M] = DLM_OUT_Z_L_M;
161 acc_address = (sa0 == sa0_high) ? DLHC_DLM_DLH_ACC_SA0_HIGH_ADDRESS : DLM_DLH_ACC_SA0_LOW_ADDRESS;
162 mag_address = DLHC_DLM_DLH_MAG_ADDRESS;
163 translated_regs[-OUT_X_H_M] = DLH_OUT_X_H_M;
164 translated_regs[-OUT_X_L_M] = DLH_OUT_X_L_M;
165 translated_regs[-OUT_Y_H_M] = DLH_OUT_Y_H_M;
166 translated_regs[-OUT_Y_L_M] = DLH_OUT_Y_L_M;
167 translated_regs[-OUT_Z_H_M] = DLH_OUT_Z_H_M;
168 translated_regs[-OUT_Z_L_M] = DLH_OUT_Z_L_M;
188 void LSM303::enableDefault(
void)
191 if (_device == device_D)
197 writeReg(CTRL2, 0x00);
201 writeReg(CTRL1, 0x57);
207 writeReg(CTRL5, 0x64);
211 writeReg(CTRL6, 0x20);
215 writeReg(CTRL7, 0x00);
221 if (_device == device_DLHC)
225 writeAccReg(CTRL_REG4_A, 0x08);
229 writeAccReg(CTRL_REG1_A, 0x47);
235 writeAccReg(CTRL_REG4_A, 0x00);
239 writeAccReg(CTRL_REG1_A, 0x27);
246 writeMagReg(CRA_REG_M, 0x0C);
250 writeMagReg(CRB_REG_M, 0x20);
254 writeMagReg(MR_REG_M, 0x00);
259 void LSM303::writeAccReg(
byte reg,
byte value)
261 Wire.beginTransmission(acc_address);
264 last_status = Wire.endTransmission();
268 byte LSM303::readAccReg(
byte reg)
272 Wire.beginTransmission(acc_address);
274 last_status = Wire.endTransmission();
275 Wire.requestFrom(acc_address, (
byte)1);
277 Wire.endTransmission();
283 void LSM303::writeMagReg(
byte reg,
byte value)
285 Wire.beginTransmission(mag_address);
288 last_status = Wire.endTransmission();
292 byte LSM303::readMagReg(
int reg)
299 reg = translated_regs[-reg];
302 Wire.beginTransmission(mag_address);
304 last_status = Wire.endTransmission();
305 Wire.requestFrom(mag_address, (
byte)1);
307 Wire.endTransmission();
312 void LSM303::writeReg(
byte reg,
byte value)
315 if (_device == device_D || reg < CTRL_REG1_A)
317 writeMagReg(reg, value);
321 writeAccReg(reg, value);
327 byte LSM303::readReg(
int reg)
331 if (_device == device_D || reg < CTRL_REG1_A)
333 return readMagReg(reg);
337 return readAccReg(reg);
342 void LSM303::readAcc(
void)
344 Wire.beginTransmission(acc_address);
347 Wire.write(OUT_X_L_A | (1 << 7));
348 last_status = Wire.endTransmission();
349 Wire.requestFrom(acc_address, (
byte)6);
351 unsigned int millis_start = millis();
352 while (Wire.available() < 6) {
353 if (io_timeout > 0 && ((
unsigned int)millis() - millis_start) > io_timeout)
360 byte xla = Wire.read();
361 byte xha = Wire.read();
362 byte yla = Wire.read();
363 byte yha = Wire.read();
364 byte zla = Wire.read();
365 byte zha = Wire.read();
370 a.x = (int16_t)(xha << 8 | xla);
371 a.y = (int16_t)(yha << 8 | yla);
372 a.z = (int16_t)(zha << 8 | zla);
376 void LSM303::readMag(
void)
378 Wire.beginTransmission(mag_address);
381 Wire.write((_device == device_D) ? translated_regs[-OUT_X_L_M] | (1 << 7) : translated_regs[-OUT_X_H_M]);
382 last_status = Wire.endTransmission();
383 Wire.requestFrom(mag_address, (
byte)6);
385 unsigned int millis_start = millis();
386 while (Wire.available() < 6) {
387 if (io_timeout > 0 && ((
unsigned int)millis() - millis_start) > io_timeout)
394 byte xlm, xhm, ylm, yhm, zlm, zhm;
396 if (_device == device_D)
412 if (_device == device_DLH)
431 m.x = (int16_t)(xhm << 8 | xlm);
432 m.y = (int16_t)(yhm << 8 | ylm);
433 m.z = (int16_t)(zhm << 8 | zlm);
437 void LSM303::read(
void)
452 float LSM303::heading(
void)
454 if (_device == device_D)
456 return heading((vector<int>){1, 0, 0});
460 return heading((vector<int>){0, -1, 0});
464 void LSM303::vector_normalize(vector<float> *a)
466 float mag = sqrt(vector_dot(a, a));
474 int LSM303::testReg(
byte address, regAddr reg)
476 Wire.beginTransmission(address);
477 Wire.write((
byte)reg);
478 if (Wire.endTransmission() != 0)
480 return TEST_REG_ERROR;
483 Wire.requestFrom(address, (
byte)1);
484 if (Wire.available())
490 return TEST_REG_ERROR;