AP_Compass_AK09916.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <AP_Common/AP_Common.h>
  17. #include <AP_HAL/AP_HAL.h>
  18. #include <AP_HAL/I2CDevice.h>
  19. #include <AP_Math/AP_Math.h>
  20. #include "AP_Compass.h"
  21. #include "AP_Compass_Backend.h"
  22. #ifndef HAL_COMPASS_AK09916_I2C_ADDR
  23. # define HAL_COMPASS_AK09916_I2C_ADDR 0x0C
  24. #endif
  25. #ifndef HAL_COMPASS_ICM20948_I2C_ADDR
  26. # define HAL_COMPASS_ICM20948_I2C_ADDR 0x69
  27. #endif
  28. class AuxiliaryBus;
  29. class AuxiliaryBusSlave;
  30. class AP_InertialSensor;
  31. class AP_AK09916_BusDriver;
  32. class AP_Compass_AK09916 : public AP_Compass_Backend
  33. {
  34. public:
  35. /* Probe for AK09916 standalone on I2C bus */
  36. static AP_Compass_Backend *probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
  37. bool force_external,
  38. enum Rotation rotation);
  39. /* Probe for AK09916 on auxiliary bus of ICM20948, connected through I2C */
  40. static AP_Compass_Backend *probe_ICM20948(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
  41. AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev_icm,
  42. bool force_external,
  43. enum Rotation rotation);
  44. /* Probe for AK09916 on auxiliary bus of ICM20948, connected through SPI */
  45. static AP_Compass_Backend *probe_ICM20948(uint8_t mpu9250_instance,
  46. enum Rotation rotation);
  47. static constexpr const char *name = "AK09916";
  48. virtual ~AP_Compass_AK09916();
  49. void read() override;
  50. private:
  51. AP_Compass_AK09916(AP_AK09916_BusDriver *bus, bool force_external,
  52. enum Rotation rotation);
  53. bool init();
  54. void _make_factory_sensitivity_adjustment(Vector3f &field) const;
  55. void _make_adc_sensitivity_adjustment(Vector3f &field) const;
  56. bool _reset();
  57. bool _setup_mode();
  58. bool _check_id();
  59. bool _calibrate();
  60. void _update();
  61. AP_AK09916_BusDriver *_bus;
  62. float _magnetometer_ASA[3] {0, 0, 0};
  63. bool _force_external;
  64. uint8_t _compass_instance;
  65. bool _initialized;
  66. enum Rotation _rotation;
  67. };
  68. class AP_AK09916_BusDriver
  69. {
  70. public:
  71. virtual ~AP_AK09916_BusDriver() { }
  72. virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) = 0;
  73. virtual bool register_read(uint8_t reg, uint8_t *val) = 0;
  74. virtual bool register_write(uint8_t reg, uint8_t val) = 0;
  75. virtual AP_HAL::Semaphore *get_semaphore() = 0;
  76. virtual bool configure() { return true; }
  77. virtual bool start_measurements() { return true; }
  78. virtual AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t, AP_HAL::Device::PeriodicCb) = 0;
  79. // set device type within a device class
  80. virtual void set_device_type(uint8_t devtype) = 0;
  81. // return 24 bit bus identifier
  82. virtual uint32_t get_bus_id(void) const = 0;
  83. };
  84. class AP_AK09916_BusDriver_HALDevice: public AP_AK09916_BusDriver
  85. {
  86. public:
  87. AP_AK09916_BusDriver_HALDevice(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev);
  88. virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
  89. virtual bool register_read(uint8_t reg, uint8_t *val) override;
  90. virtual bool register_write(uint8_t reg, uint8_t val) override;
  91. virtual AP_HAL::Semaphore *get_semaphore() override;
  92. AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
  93. // set device type within a device class
  94. void set_device_type(uint8_t devtype) override {
  95. _dev->set_device_type(devtype);
  96. }
  97. // return 24 bit bus identifier
  98. uint32_t get_bus_id(void) const override {
  99. return _dev->get_bus_id();
  100. }
  101. private:
  102. AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
  103. };
  104. class AP_AK09916_BusDriver_Auxiliary : public AP_AK09916_BusDriver
  105. {
  106. public:
  107. AP_AK09916_BusDriver_Auxiliary(AP_InertialSensor &ins, uint8_t backend_id,
  108. uint8_t backend_instance, uint8_t addr);
  109. ~AP_AK09916_BusDriver_Auxiliary();
  110. bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
  111. bool register_read(uint8_t reg, uint8_t *val) override;
  112. bool register_write(uint8_t reg, uint8_t val) override;
  113. AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
  114. AP_HAL::Semaphore *get_semaphore() override;
  115. bool configure() override;
  116. bool start_measurements() override;
  117. // set device type within a device class
  118. void set_device_type(uint8_t devtype) override;
  119. // return 24 bit bus identifier
  120. uint32_t get_bus_id(void) const override;
  121. private:
  122. AuxiliaryBus *_bus;
  123. AuxiliaryBusSlave *_slave;
  124. bool _started;
  125. };