LSM6DS3.cpp 29 KB

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