LSM6DS3.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /******************************************************************************
  2. SparkFunLSM6DS3.cpp
  3. LSM6DS3 Arduino and Teensy Driver
  4. Marshall Taylor @ SparkFun Electronics
  5. May 20, 2015
  6. https://github.com/sparkfun/LSM6DS3_Breakout
  7. https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library
  8. Resources:
  9. Uses Wire.h for i2c operation
  10. Uses SPI.h for SPI operation
  11. Either can be omitted if not used
  12. Development environment specifics:
  13. Arduino IDE 1.6.4
  14. Teensy loader 1.23
  15. This code is released under the [MIT License](http://opensource.org/licenses/MIT).
  16. Please review the LICENSE.md file included with this example. If you have any questions
  17. or concerns with licensing, please contact techsupport@sparkfun.com.
  18. Distributed as-is; no warranty is given.
  19. ******************************************************************************/
  20. //See SparkFunLSM6DS3.h for additional topology notes.
  21. #include "LSM6DS3.h"
  22. #include "stdint.h"
  23. #include "Wire.h"
  24. #include "SPI.h"
  25. #if defined(ARDUINO_XIAO_MG24)
  26. #define Wire Wire1
  27. #define PIN_LSM6DS3TR_C_POWER PD5
  28. #endif
  29. //****************************************************************************//
  30. //
  31. // LSM6DS3Core functions.
  32. //
  33. // Construction arguments:
  34. // ( uint8_t busType, uint8_t inputArg ),
  35. //
  36. // where inputArg is address for I2C_MODE and chip select pin
  37. // number for SPI_MODE
  38. //
  39. // For SPI, construct LSM6DS3Core myIMU(SPI_MODE, 10);
  40. // For I2C, construct LSM6DS3Core myIMU(I2C_MODE, 0x6B);
  41. //
  42. // Default construction is I2C mode, address 0x6B.
  43. //
  44. //****************************************************************************//
  45. LSM6DS3Core::LSM6DS3Core(uint8_t busType, uint8_t inputArg) : commInterface(I2C_MODE), I2CAddress(0x6B),
  46. chipSelectPin(10) {
  47. commInterface = busType;
  48. if (commInterface == I2C_MODE) {
  49. I2CAddress = inputArg;
  50. }
  51. if (commInterface == SPI_MODE) {
  52. chipSelectPin = inputArg;
  53. }
  54. }
  55. status_t LSM6DS3Core::beginCore(void) {
  56. status_t returnError = IMU_SUCCESS;
  57. #ifdef PIN_LSM6DS3TR_C_POWER
  58. pinMode(PIN_LSM6DS3TR_C_POWER, OUTPUT);
  59. #if defined(NRF52840_XXAA)
  60. NRF_P1->PIN_CNF[8] = ((uint32_t)NRF_GPIO_PIN_DIR_OUTPUT << GPIO_PIN_CNF_DIR_Pos)
  61. | ((uint32_t)NRF_GPIO_PIN_INPUT_DISCONNECT << GPIO_PIN_CNF_INPUT_Pos)
  62. | ((uint32_t)NRF_GPIO_PIN_NOPULL << GPIO_PIN_CNF_PULL_Pos)
  63. | ((uint32_t)NRF_GPIO_PIN_H0H1 << GPIO_PIN_CNF_DRIVE_Pos)
  64. | ((uint32_t)NRF_GPIO_PIN_NOSENSE << GPIO_PIN_CNF_SENSE_Pos);
  65. #endif
  66. digitalWrite(PIN_LSM6DS3TR_C_POWER, HIGH);
  67. delay(10);
  68. #endif
  69. switch (commInterface) {
  70. case I2C_MODE:
  71. Wire.begin();
  72. break;
  73. case SPI_MODE:
  74. #if defined(NRF52840_XXAA)
  75. // start the SPI library:
  76. SPI.begin();
  77. // Maximum SPI frequency is 10MHz, could divide by 2 here:
  78. #if defined(ARDUINO_XIAO_RA4M1) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_RP2350)
  79. SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  80. #elif defined(ARDUINO_XIAO_MG24)
  81. // SPI.setClockDivider(SPI_CLOCK_DIV4);
  82. SPI.setClockDivider(4);
  83. #endif
  84. // Data is read and written MSb first.
  85. #ifdef ESP32
  86. SPI.setBitOrder(SPI_MSBFIRST);
  87. #elif defined(ARDUINO_XIAO_RA4M1)
  88. // noting
  89. #else
  90. SPI.setBitOrder(MSBFIRST);
  91. #endif
  92. // Data is captured on rising edge of clock (CPHA = 0)
  93. // Base value of the clock is HIGH (CPOL = 1)
  94. // MODE3 for 328p operation
  95. #ifdef __AVR__
  96. SPI.setDataMode(SPI_MODE3);
  97. #else
  98. #endif
  99. // MODE0 for Teensy 3.1 operation
  100. #ifdef __MK20DX256__
  101. SPI.setDataMode(SPI_MODE0);
  102. #else
  103. #endif
  104. // initalize the data ready and chip select pins:
  105. pinMode(chipSelectPin, OUTPUT);
  106. digitalWrite(chipSelectPin, HIGH);
  107. #endif
  108. break;
  109. default:
  110. break;
  111. }
  112. //Spin for a few ms
  113. volatile uint8_t temp = 0;
  114. for (uint16_t i = 0; i < 10000; i++) {
  115. temp++;
  116. }
  117. //Check the ID register to determine if the operation was a success.
  118. uint8_t readCheck;
  119. readRegister(&readCheck, LSM6DS3_ACC_GYRO_WHO_AM_I_REG);
  120. if (!(readCheck == LSM6DS3_ACC_GYRO_WHO_AM_I || readCheck == LSM6DS3_C_ACC_GYRO_WHO_AM_I)) {
  121. returnError = IMU_HW_ERROR;
  122. }
  123. return returnError;
  124. }
  125. //****************************************************************************//
  126. //
  127. // ReadRegisterRegion
  128. //
  129. // Parameters:
  130. // *outputPointer -- Pass &variable (base address of) to save read data to
  131. // offset -- register to read
  132. // length -- number of bytes to read
  133. //
  134. // Note: Does not know if the target memory space is an array or not, or
  135. // if there is the array is big enough. if the variable passed is only
  136. // two bytes long and 3 bytes are requested, this will over-write some
  137. // other memory!
  138. //
  139. //****************************************************************************//
  140. status_t LSM6DS3Core::readRegisterRegion(uint8_t* outputPointer, uint8_t offset, uint8_t length) {
  141. status_t returnError = IMU_SUCCESS;
  142. //define pointer that will point to the external space
  143. uint8_t i = 0;
  144. uint8_t c = 0;
  145. #if defined(NRF52840_XXAA)
  146. uint8_t tempFFCounter = 0;
  147. #endif
  148. switch (commInterface) {
  149. case I2C_MODE:
  150. Wire.beginTransmission(I2CAddress);
  151. Wire.write(offset);
  152. if (Wire.endTransmission() != 0) {
  153. returnError = IMU_HW_ERROR;
  154. } else { //OK, all worked, keep going
  155. // request 6 bytes from slave device
  156. Wire.requestFrom(I2CAddress, length);
  157. while ((Wire.available()) && (i < length)) { // slave may send less than requested
  158. c = Wire.read(); // receive a byte as character
  159. *outputPointer = c;
  160. outputPointer++;
  161. i++;
  162. }
  163. }
  164. break;
  165. case SPI_MODE:
  166. #if defined(NRF52840_XXAA)
  167. // take the chip select low to select the device:
  168. digitalWrite(chipSelectPin, LOW);
  169. // send the device the register you want to read:
  170. SPI.transfer(offset | 0x80); //Ored with "read request" bit
  171. while (i < length) { // slave may send less than requested
  172. c = SPI.transfer(0x00); // receive a byte as character
  173. if (c == 0xFF) {
  174. //May have problem
  175. tempFFCounter++;
  176. }
  177. *outputPointer = c;
  178. outputPointer++;
  179. i++;
  180. }
  181. if (tempFFCounter == i) {
  182. //Ok, we've recieved all ones, report
  183. returnError = IMU_ALL_ONES_WARNING;
  184. }
  185. // take the chip select high to de-select:
  186. digitalWrite(chipSelectPin, HIGH);
  187. #endif
  188. break;
  189. default:
  190. break;
  191. }
  192. return returnError;
  193. }
  194. //****************************************************************************//
  195. //
  196. // ReadRegister
  197. //
  198. // Parameters:
  199. // *outputPointer -- Pass &variable (address of) to save read data to
  200. // offset -- register to read
  201. //
  202. //****************************************************************************//
  203. status_t LSM6DS3Core::readRegister(uint8_t* outputPointer, uint8_t offset) {
  204. //Return value
  205. uint8_t result = 0;
  206. uint8_t numBytes = 1;
  207. status_t returnError = IMU_SUCCESS;
  208. switch (commInterface) {
  209. case I2C_MODE:
  210. Wire.beginTransmission(I2CAddress);
  211. Wire.write(offset);
  212. if (Wire.endTransmission() != 0) {
  213. returnError = IMU_HW_ERROR;
  214. }
  215. Wire.requestFrom(I2CAddress, numBytes);
  216. while (Wire.available()) { // slave may send less than requested
  217. result = Wire.read(); // receive a byte as a proper uint8_t
  218. }
  219. break;
  220. case SPI_MODE:
  221. #if defined(NRF52840_XXAA)
  222. // take the chip select low to select the device:
  223. digitalWrite(chipSelectPin, LOW);
  224. // send the device the register you want to read:
  225. SPI.transfer(offset | 0x80); //Ored with "read request" bit
  226. // send a value of 0 to read the first byte returned:
  227. result = SPI.transfer(0x00);
  228. // take the chip select high to de-select:
  229. digitalWrite(chipSelectPin, HIGH);
  230. if (result == 0xFF) {
  231. //we've recieved all ones, report
  232. returnError = IMU_ALL_ONES_WARNING;
  233. }
  234. #endif
  235. break;
  236. default:
  237. break;
  238. }
  239. *outputPointer = result;
  240. return returnError;
  241. }
  242. //****************************************************************************//
  243. //
  244. // readRegisterInt16
  245. //
  246. // Parameters:
  247. // *outputPointer -- Pass &variable (base address of) to save read data to
  248. // offset -- register to read
  249. //
  250. //****************************************************************************//
  251. status_t LSM6DS3Core::readRegisterInt16(int16_t* outputPointer, uint8_t offset) {
  252. uint8_t myBuffer[2] = {0, 0};
  253. status_t returnError = readRegisterRegion(myBuffer, offset, 2); //Does memory transfer
  254. int16_t output = (int16_t)myBuffer[0] | int16_t(myBuffer[1] << 8);
  255. *outputPointer = output;
  256. return returnError;
  257. }
  258. //****************************************************************************//
  259. //
  260. // writeRegister
  261. //
  262. // Parameters:
  263. // offset -- register to write
  264. // dataToWrite -- 8 bit data to write to register
  265. //
  266. //****************************************************************************//
  267. status_t LSM6DS3Core::writeRegister(uint8_t offset, uint8_t dataToWrite) {
  268. status_t returnError = IMU_SUCCESS;
  269. switch (commInterface) {
  270. case I2C_MODE:
  271. //Write the byte
  272. Wire.beginTransmission(I2CAddress);
  273. Wire.write(offset);
  274. Wire.write(dataToWrite);
  275. if (Wire.endTransmission() != 0) {
  276. returnError = IMU_HW_ERROR;
  277. }
  278. break;
  279. case SPI_MODE:
  280. #if defined(NRF52840_XXAA)
  281. // take the chip select low to select the device:
  282. digitalWrite(chipSelectPin, LOW);
  283. // send the device the register you want to read:
  284. SPI.transfer(offset);
  285. // send a value of 0 to read the first byte returned:
  286. SPI.transfer(dataToWrite);
  287. // decrement the number of bytes left to read:
  288. // take the chip select high to de-select:
  289. digitalWrite(chipSelectPin, HIGH);
  290. #endif
  291. break;
  292. //No way to check error on this write (Except to read back but that's not reliable)
  293. default:
  294. break;
  295. }
  296. return returnError;
  297. }
  298. status_t LSM6DS3Core::embeddedPage(void) {
  299. status_t returnError = writeRegister(LSM6DS3_ACC_GYRO_RAM_ACCESS, 0x80);
  300. return returnError;
  301. }
  302. status_t LSM6DS3Core::basePage(void) {
  303. status_t returnError = writeRegister(LSM6DS3_ACC_GYRO_RAM_ACCESS, 0x00);
  304. return returnError;
  305. }
  306. //****************************************************************************//
  307. //
  308. // Main user class -- wrapper for the core class + maths
  309. //
  310. // Construct with same rules as the core ( uint8_t busType, uint8_t inputArg )
  311. //
  312. //****************************************************************************//
  313. LSM6DS3::LSM6DS3(uint8_t busType, uint8_t inputArg) : LSM6DS3Core(busType, inputArg) {
  314. //Construct with these default settings
  315. settings.gyroEnabled = 1; //Can be 0 or 1
  316. settings.gyroRange = 2000; //Max deg/s. Can be: 125, 245, 500, 1000, 2000
  317. settings.gyroSampleRate = 416; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666
  318. settings.gyroBandWidth = 400; //Hz. Can be: 50, 100, 200, 400;
  319. settings.gyroFifoEnabled = 1; //Set to include gyro in FIFO
  320. settings.gyroFifoDecimation = 1; //set 1 for on /1
  321. settings.accelEnabled = 1;
  322. settings.accelODROff = 1;
  323. settings.accelRange = 16; //Max G force readable. Can be: 2, 4, 8, 16
  324. settings.accelSampleRate = 416; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
  325. settings.accelBandWidth = 100; //Hz. Can be: 50, 100, 200, 400;
  326. settings.accelFifoEnabled = 1; //Set to include accelerometer in the FIFO
  327. settings.accelFifoDecimation = 1; //set 1 for on /1
  328. settings.tempEnabled = 1;
  329. //Select interface mode
  330. settings.commMode = 1; //Can be modes 1, 2 or 3
  331. //FIFO control data
  332. settings.fifoThreshold = 3000; //Can be 0 to 4096 (16 bit bytes)
  333. settings.fifoSampleRate = 10; //default 10Hz
  334. settings.fifoModeWord = 0; //Default off
  335. allOnesCounter = 0;
  336. nonSuccessCounter = 0;
  337. }
  338. //****************************************************************************//
  339. //
  340. // Configuration section
  341. //
  342. // This uses the stored SensorSettings to start the IMU
  343. // Use statements such as "myIMU.settings.commInterface = SPI_MODE;" or
  344. // "myIMU.settings.accelEnabled = 1;" to configure before calling .begin();
  345. //
  346. //****************************************************************************//
  347. status_t LSM6DS3::begin() {
  348. //Check the settings structure values to determine how to setup the device
  349. uint8_t dataToWrite = 0; //Temporary variable
  350. //Begin the inherited core. This gets the physical wires connected
  351. status_t returnError = beginCore();
  352. //Setup the accelerometer******************************
  353. dataToWrite = 0; //Start Fresh!
  354. if (settings.accelEnabled == 1) {
  355. //Build config reg
  356. //First patch in filter bandwidth
  357. switch (settings.accelBandWidth) {
  358. case 50:
  359. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_50Hz;
  360. break;
  361. case 100:
  362. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
  363. break;
  364. case 200:
  365. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
  366. break;
  367. default: //set default case to max passthrough
  368. case 400:
  369. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_400Hz;
  370. break;
  371. }
  372. //Next, patch in full scale
  373. switch (settings.accelRange) {
  374. case 2:
  375. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_2g;
  376. break;
  377. case 4:
  378. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_4g;
  379. break;
  380. case 8:
  381. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
  382. break;
  383. default: //set default case to 16(max)
  384. case 16:
  385. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_16g;
  386. break;
  387. }
  388. //Lastly, patch in accelerometer ODR
  389. switch (settings.accelSampleRate) {
  390. case 13:
  391. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_13Hz;
  392. break;
  393. case 26:
  394. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_26Hz;
  395. break;
  396. case 52:
  397. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_52Hz;
  398. break;
  399. default: //Set default to 104
  400. case 104:
  401. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
  402. break;
  403. case 208:
  404. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_208Hz;
  405. break;
  406. case 416:
  407. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_416Hz;
  408. break;
  409. case 833:
  410. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_833Hz;
  411. break;
  412. case 1660:
  413. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_1660Hz;
  414. break;
  415. case 3330:
  416. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_3330Hz;
  417. break;
  418. case 6660:
  419. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_6660Hz;
  420. break;
  421. case 13330:
  422. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_13330Hz;
  423. break;
  424. }
  425. } else {
  426. //dataToWrite already = 0 (powerdown);
  427. }
  428. //Now, write the patched together data
  429. writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
  430. //Set the ODR bit
  431. readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);
  432. dataToWrite &= ~((uint8_t)LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED);
  433. if (settings.accelODROff == 1) {
  434. dataToWrite |= LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED;
  435. }
  436. writeRegister(LSM6DS3_ACC_GYRO_CTRL4_C, dataToWrite);
  437. //Setup the gyroscope**********************************************
  438. dataToWrite = 0; //Start Fresh!
  439. if (settings.gyroEnabled == 1) {
  440. //Build config reg
  441. //First, patch in full scale
  442. switch (settings.gyroRange) {
  443. case 125:
  444. dataToWrite |= LSM6DS3_ACC_GYRO_FS_125_ENABLED;
  445. break;
  446. case 245:
  447. dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_245dps;
  448. break;
  449. case 500:
  450. dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_500dps;
  451. break;
  452. case 1000:
  453. dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_1000dps;
  454. break;
  455. default: //Default to full 2000DPS range
  456. case 2000:
  457. dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_2000dps;
  458. break;
  459. }
  460. //Lastly, patch in gyro ODR
  461. switch (settings.gyroSampleRate) {
  462. case 13:
  463. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_13Hz;
  464. break;
  465. case 26:
  466. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_26Hz;
  467. break;
  468. case 52:
  469. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_52Hz;
  470. break;
  471. default: //Set default to 104
  472. case 104:
  473. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_104Hz;
  474. break;
  475. case 208:
  476. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_208Hz;
  477. break;
  478. case 416:
  479. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_416Hz;
  480. break;
  481. case 833:
  482. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_833Hz;
  483. break;
  484. case 1660:
  485. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_1660Hz;
  486. break;
  487. }
  488. } else {
  489. //dataToWrite already = 0 (powerdown);
  490. }
  491. //Write the byte
  492. writeRegister(LSM6DS3_ACC_GYRO_CTRL2_G, dataToWrite);
  493. //Return WHO AM I reg //Not no mo!
  494. uint8_t result;
  495. readRegister(&result, LSM6DS3_ACC_GYRO_WHO_AM_I_REG);
  496. //Setup the internal temperature sensor
  497. if (settings.tempEnabled == 1) {
  498. if (result == LSM6DS3_ACC_GYRO_WHO_AM_I) { //0x69 LSM6DS3
  499. settings.tempSensitivity = 16; // Sensitivity to scale 16
  500. } else if (result == LSM6DS3_C_ACC_GYRO_WHO_AM_I) { //0x6A LSM6dS3-C
  501. settings.tempSensitivity = 256; // Sensitivity to scale 256
  502. }
  503. }
  504. if (settings.timestampEnabled) {
  505. // Enable the timestamp counter (CTRL10_C寄存器)
  506. uint8_t ctrl10_c;
  507. readRegister(&ctrl10_c, LSM6DS3_ACC_GYRO_CTRL10_C);
  508. ctrl10_c |= LSM6DS3_ACC_GYRO_ZEN_G_ENABLED; // set TIMER_EN
  509. writeRegister(LSM6DS3_ACC_GYRO_CTRL10_C, ctrl10_c);
  510. }
  511. return returnError;
  512. }
  513. //****************************************************************************//
  514. //
  515. // Accelerometer section
  516. //
  517. //****************************************************************************//
  518. int16_t LSM6DS3::readRawAccelX(void) {
  519. int16_t output;
  520. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTX_L_XL);
  521. if (errorLevel != IMU_SUCCESS) {
  522. if (errorLevel == IMU_ALL_ONES_WARNING) {
  523. allOnesCounter++;
  524. } else {
  525. nonSuccessCounter++;
  526. }
  527. }
  528. return output;
  529. }
  530. float LSM6DS3::readFloatAccelX(void) {
  531. float output = calcAccel(readRawAccelX());
  532. return output;
  533. }
  534. int16_t LSM6DS3::readRawAccelY(void) {
  535. int16_t output;
  536. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTY_L_XL);
  537. if (errorLevel != IMU_SUCCESS) {
  538. if (errorLevel == IMU_ALL_ONES_WARNING) {
  539. allOnesCounter++;
  540. } else {
  541. nonSuccessCounter++;
  542. }
  543. }
  544. return output;
  545. }
  546. float LSM6DS3::readFloatAccelY(void) {
  547. float output = calcAccel(readRawAccelY());
  548. return output;
  549. }
  550. int16_t LSM6DS3::readRawAccelZ(void) {
  551. int16_t output;
  552. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTZ_L_XL);
  553. if (errorLevel != IMU_SUCCESS) {
  554. if (errorLevel == IMU_ALL_ONES_WARNING) {
  555. allOnesCounter++;
  556. } else {
  557. nonSuccessCounter++;
  558. }
  559. }
  560. return output;
  561. }
  562. float LSM6DS3::readFloatAccelZ(void) {
  563. float output = calcAccel(readRawAccelZ());
  564. return output;
  565. }
  566. float LSM6DS3::calcAccel(int16_t input) {
  567. float output = (float)input * 0.061 * (settings.accelRange >> 1) / 1000;
  568. return output;
  569. }
  570. //****************************************************************************//
  571. //
  572. // Gyroscope section
  573. //
  574. //****************************************************************************//
  575. int16_t LSM6DS3::readRawGyroX(void) {
  576. int16_t output;
  577. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTX_L_G);
  578. if (errorLevel != IMU_SUCCESS) {
  579. if (errorLevel == IMU_ALL_ONES_WARNING) {
  580. allOnesCounter++;
  581. } else {
  582. nonSuccessCounter++;
  583. }
  584. }
  585. return output;
  586. }
  587. float LSM6DS3::readFloatGyroX(void) {
  588. float output = calcGyro(readRawGyroX());
  589. return output;
  590. }
  591. int16_t LSM6DS3::readRawGyroY(void) {
  592. int16_t output;
  593. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTY_L_G);
  594. if (errorLevel != IMU_SUCCESS) {
  595. if (errorLevel == IMU_ALL_ONES_WARNING) {
  596. allOnesCounter++;
  597. } else {
  598. nonSuccessCounter++;
  599. }
  600. }
  601. return output;
  602. }
  603. float LSM6DS3::readFloatGyroY(void) {
  604. float output = calcGyro(readRawGyroY());
  605. return output;
  606. }
  607. int16_t LSM6DS3::readRawGyroZ(void) {
  608. int16_t output;
  609. status_t errorLevel = readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUTZ_L_G);
  610. if (errorLevel != IMU_SUCCESS) {
  611. if (errorLevel == IMU_ALL_ONES_WARNING) {
  612. allOnesCounter++;
  613. } else {
  614. nonSuccessCounter++;
  615. }
  616. }
  617. return output;
  618. }
  619. float LSM6DS3::readFloatGyroZ(void) {
  620. float output = calcGyro(readRawGyroZ());
  621. return output;
  622. }
  623. float LSM6DS3::calcGyro(int16_t input) {
  624. uint8_t gyroRangeDivisor = settings.gyroRange / 125;
  625. if (settings.gyroRange == 245) {
  626. gyroRangeDivisor = 2;
  627. }
  628. float output = (float)input * 4.375 * (gyroRangeDivisor) / 1000;
  629. return output;
  630. }
  631. //****************************************************************************//
  632. //
  633. // Temperature section
  634. //
  635. //****************************************************************************//
  636. int16_t LSM6DS3::readRawTemp(void) {
  637. int16_t output;
  638. readRegisterInt16(&output, LSM6DS3_ACC_GYRO_OUT_TEMP_L);
  639. return output;
  640. }
  641. float LSM6DS3::readTempC(void) {
  642. float output = (float)readRawTemp() / settings.tempSensitivity; //divide by tempSensitivity to scale
  643. output += 25; //Add 25 degrees to remove offset
  644. return output;
  645. }
  646. float LSM6DS3::readTempF(void) {
  647. float output = (float)readRawTemp() / settings.tempSensitivity; //divide by tempSensitivity to scale
  648. output += 25; //Add 25 degrees to remove offset
  649. output = (output * 9) / 5 + 32;
  650. return output;
  651. }
  652. //****************************************************************************//
  653. //
  654. // Timestamp section
  655. //
  656. //****************************************************************************//
  657. uint32_t LSM6DS3::fifoTimestamp() {
  658. uint8_t data[6];
  659. status_t error = readRegisterRegion(data, LSM6DS3_ACC_GYRO_FIFO_DATA_OUT_L, 3*sizeof(uint16_t));
  660. if (error == IMU_SUCCESS) {
  661. return (data[1] << 16) | (data[0] << 8) | data[3];
  662. }
  663. return 0;
  664. }
  665. //****************************************************************************//
  666. //
  667. // FIFO section
  668. //
  669. //****************************************************************************//
  670. void LSM6DS3::fifoBegin(void) {
  671. //CONFIGURE THE VARIOUS FIFO SETTINGS
  672. //
  673. //
  674. //This section first builds a bunch of config words, then goes through
  675. //and writes them all.
  676. //Split and mask the threshold
  677. uint8_t thresholdLByte = settings.fifoThreshold & 0x00FF;
  678. uint8_t thresholdHByte = (settings.fifoThreshold & 0x0F00) >> 8;
  679. //Pedo bits not configured (ctl2)
  680. //CONFIGURE FIFO_CTRL3
  681. uint8_t tempFIFO_CTRL3 = 0;
  682. if (settings.gyroFifoEnabled == 1) {
  683. //Set up gyro stuff
  684. //Build on FIFO_CTRL3
  685. //Set decimation
  686. tempFIFO_CTRL3 |= (settings.gyroFifoDecimation & 0x07) << 3;
  687. }
  688. if (settings.accelFifoEnabled == 1) {
  689. //Set up accelerometer stuff
  690. //Build on FIFO_CTRL3
  691. //Set decimation
  692. tempFIFO_CTRL3 |= (settings.accelFifoDecimation & 0x07);
  693. }
  694. if (settings.timestampFifoEnabled == 1) {
  695. // Write timestamp data to the FIFO.
  696. uint8_t fifo_ctrl2;
  697. readRegister(&fifo_ctrl2, LSM6DS3_ACC_GYRO_FIFO_CTRL2);
  698. fifo_ctrl2 |= LSM6DS3_ACC_GYRO_TIM_PEDO_FIFO_EN_ENABLED; // FIFO_CTRL2(07H)->TIMER_PEDO_FIFO_EN
  699. writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL2, fifo_ctrl2);
  700. // translates to "Set timestamp resolution
  701. uint8_t tap_cfg;
  702. readRegister(&tap_cfg, LSM6DS3_ACC_GYRO_WAKE_UP_DUR);
  703. tap_cfg &= ~(1 << 4);
  704. // timestampResolution
  705. // 0: 6.4ms
  706. // 1: 25us
  707. tap_cfg |= (settings.timestampResolution & 0x01) << 4;
  708. writeRegister(LSM6DS3_ACC_GYRO_WAKE_UP_DUR, tap_cfg);
  709. }
  710. // CONFIGURE FIFO_CTRL4
  711. // Select the decimation factor for the 4th FIFO dataset by configuring the DEC_DS4_FIFO[2:0] field in the FIFO_CTRL4 register.
  712. //If you don't need the third and fourth datasets, this value can be set to 0
  713. uint8_t tempFIFO_CTRL4 = 0x09;
  714. //CONFIGURE FIFO_CTRL5
  715. uint8_t tempFIFO_CTRL5 = 0;
  716. switch (settings.fifoSampleRate) {
  717. default: //set default case to 10Hz(slowest)
  718. case 10:
  719. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_10Hz;
  720. break;
  721. case 25:
  722. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_25Hz;
  723. break;
  724. case 50:
  725. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_50Hz;
  726. break;
  727. case 100:
  728. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_100Hz;
  729. break;
  730. case 200:
  731. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_200Hz;
  732. break;
  733. case 400:
  734. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_400Hz;
  735. break;
  736. case 800:
  737. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_800Hz;
  738. break;
  739. case 1600:
  740. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_1600Hz;
  741. break;
  742. case 3300:
  743. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_3300Hz;
  744. break;
  745. case 6600:
  746. tempFIFO_CTRL5 |= LSM6DS3_ACC_GYRO_ODR_FIFO_6600Hz;
  747. break;
  748. }
  749. //Hard code the fifo mode here:
  750. tempFIFO_CTRL5 |= settings.fifoModeWord = 6; //set mode:
  751. //Write the data
  752. writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL1, thresholdLByte);
  753. //writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL2, thresholdHByte);
  754. writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL3, tempFIFO_CTRL3);
  755. writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL4, tempFIFO_CTRL4);
  756. writeRegister(LSM6DS3_ACC_GYRO_FIFO_CTRL5, tempFIFO_CTRL5);
  757. }
  758. void LSM6DS3::fifoClear(void) {
  759. //Drain the fifo data and dump it
  760. while ((fifoGetStatus() & 0x1000) == 0) {
  761. fifoRead();
  762. }
  763. }
  764. int16_t LSM6DS3::fifoRead(void) {
  765. //Pull the last data from the fifo
  766. uint8_t tempReadByte = 0;
  767. uint16_t tempAccumulator = 0;
  768. readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_DATA_OUT_L);
  769. tempAccumulator = tempReadByte;
  770. readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_DATA_OUT_H);
  771. tempAccumulator |= ((uint16_t)tempReadByte << 8);
  772. return tempAccumulator;
  773. }
  774. uint16_t LSM6DS3::fifoGetStatus(void) {
  775. //Return some data on the state of the fifo
  776. uint8_t tempReadByte = 0;
  777. uint16_t tempAccumulator = 0;
  778. readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_STATUS1);
  779. tempAccumulator = tempReadByte;
  780. readRegister(&tempReadByte, LSM6DS3_ACC_GYRO_FIFO_STATUS2);
  781. tempAccumulator |= (tempReadByte << 8);
  782. return tempAccumulator;
  783. }
  784. void LSM6DS3::fifoEnd(void) {
  785. // turn off the fifo
  786. writeRegister(LSM6DS3_ACC_GYRO_FIFO_STATUS1, 0x00); //Disable
  787. }