imu.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "imu.h"
  2. imu_t imu_types[] = {
  3. {
  4. BMI160_DEV_ADDR,
  5. bmi160_begin,
  6. bmi160_end,
  7. bmi160_read,
  8. "BMI160"
  9. },
  10. {
  11. LSM6DS3_ADDRESS,
  12. lsm6ds3trc_begin,
  13. lsm6ds3trc_end,
  14. lsm6ds3trc_read,
  15. "LSM6DS3"
  16. },
  17. {
  18. LSM6DSO_DEV_ADDRESS,
  19. lsm6dso_begin,
  20. lsm6dso_end,
  21. lsm6dso_read,
  22. "LSM6DSO"
  23. }
  24. };
  25. imu_t* imu_found;
  26. bool imu_begin() {
  27. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  28. if (imu_found == NULL) {
  29. imu_found = find_imu();
  30. FURI_LOG_E(IMU_TAG, "Found Device %s", imu_found->name);
  31. }
  32. bool ret = 0;
  33. if (imu_found != NULL) {
  34. ret = imu_found->begin();
  35. }
  36. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  37. return ret;
  38. }
  39. void imu_end() {
  40. if (imu_found == NULL) return;
  41. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  42. imu_found->end();
  43. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  44. }
  45. int imu_read(double* vec) {
  46. if (imu_found == NULL) return 0;
  47. furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
  48. int ret = imu_found->read(vec);
  49. furi_hal_i2c_release(&furi_hal_i2c_handle_external);
  50. return ret;
  51. }
  52. imu_t* get_imu(uint8_t address) {
  53. unsigned int i;
  54. for(i = 0; i < 3; i++) {
  55. if (imu_types[i].address == address) {
  56. return &imu_types[i];
  57. }
  58. }
  59. return NULL;
  60. }
  61. /**
  62. * Gives the first found i2c address, there should be only one device connected.
  63. **/
  64. unsigned int imu_scan_i2c() {
  65. unsigned int address;
  66. for(address = 1; address < 0xff; address++) {
  67. if(furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, address, 50)) {
  68. FURI_LOG_E(IMU_TAG, "found i2c device address 0x%X", address);
  69. return address;
  70. }
  71. }
  72. return 0;
  73. }
  74. imu_t* find_imu() {
  75. unsigned int address = imu_scan_i2c();
  76. return get_imu(address);
  77. }