Path: blob/master/libraries/AP_Compass/AP_Compass_AK09916.h
9670 views
/*1* This file is free software: you can redistribute it and/or modify it2* under the terms of the GNU General Public License as published by the3* Free Software Foundation, either version 3 of the License, or4* (at your option) any later version.5*6* This file is distributed in the hope that it will be useful, but7* WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.9* See the GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License along12* with this program. If not, see <http://www.gnu.org/licenses/>.13*/14#pragma once1516#include "AP_Compass_config.h"1718#if AP_COMPASS_AK09916_ENABLED1920#include <AP_Common/AP_Common.h>21#include <AP_HAL/AP_HAL.h>22#include <AP_HAL/Device.h>23#include <AP_Math/AP_Math.h>2425#include "AP_Compass.h"26#include "AP_Compass_Backend.h"2728#ifndef HAL_COMPASS_AK09916_I2C_ADDR29# define HAL_COMPASS_AK09916_I2C_ADDR 0x0C30#endif313233#ifndef HAL_COMPASS_ICM20948_I2C_ADDR34# define HAL_COMPASS_ICM20948_I2C_ADDR 0x6935#endif3637#ifndef HAL_COMPASS_ICM20948_I2C_ADDR238# define HAL_COMPASS_ICM20948_I2C_ADDR2 0x6839#endif4041class AuxiliaryBus;42class AuxiliaryBusSlave;43class AP_InertialSensor;44class AP_AK09916_BusDriver;4546class AP_Compass_AK09916 : public AP_Compass_Backend47{48public:49/* Probe for AK09916 standalone on I2C bus */50static AP_Compass_Backend *probe(AP_HAL::OwnPtr<AP_HAL::Device> dev,51bool force_external,52enum Rotation rotation);5354#if AP_COMPASS_ICM20948_ENABLED55/* Probe for AK09916 on auxiliary bus of ICM20948, connected through I2C */56static AP_Compass_Backend *probe_ICM20948(AP_HAL::OwnPtr<AP_HAL::Device> dev,57AP_HAL::OwnPtr<AP_HAL::Device> dev_icm,58bool force_external,59enum Rotation rotation);6061/* Probe for AK09916 on auxiliary bus of ICM20948, connected through SPI by default */62static AP_Compass_Backend *probe_ICM20948(uint8_t mpu9250_instance, enum Rotation rotation);63static AP_Compass_Backend *probe_ICM20948_SPI(uint8_t mpu9250_instance,64enum Rotation rotation);6566/* Probe for AK09916 on auxiliary bus of ICM20948, connected through I2C */67static AP_Compass_Backend *probe_ICM20948_I2C(uint8_t mpu9250_instance,68enum Rotation rotation);69#endif7071static constexpr const char *name = "AK09916";7273virtual ~AP_Compass_AK09916();7475void read() override;7677/* Must be public so the BusDriver can access its definition */78struct PACKED sample_regs {79uint8_t st1;80int16_t val[3];81uint8_t tmps;82uint8_t st2;83};8485private:86AP_Compass_AK09916(AP_AK09916_BusDriver *bus, bool force_external,87enum Rotation rotation);8889bool init();90void _make_factory_sensitivity_adjustment(Vector3f &field) const;91void _make_adc_sensitivity_adjustment(Vector3f &field) const;9293bool _reset();94bool _setup_mode();95bool _check_id();96bool _calibrate();9798void _update();99100AP_AK09916_BusDriver *_bus;101102bool _force_external;103bool _initialized;104enum Rotation _rotation;105enum AP_Compass_Backend::DevTypes _devtype;106uint8_t no_data;107};108109110class AP_AK09916_BusDriver111{112public:113virtual ~AP_AK09916_BusDriver() { }114115virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) = 0;116virtual bool register_read(uint8_t reg, uint8_t *val) = 0;117virtual bool register_write(uint8_t reg, uint8_t val, bool checked=false) = 0;118119virtual AP_HAL::Semaphore *get_semaphore() = 0;120121virtual bool configure() { return true; }122virtual bool start_measurements() { return true; }123virtual AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t, AP_HAL::Device::PeriodicCb) = 0;124125// set device type within a device class126virtual void set_device_type(uint8_t devtype) = 0;127128// return 24 bit bus identifier129virtual uint32_t get_bus_id(void) const = 0;130131/**132setup for register value checking. Frequency is how often to133check registers. If set to 10 then every 10th call to134check_next_register will check a register135*/136virtual void setup_checked_registers(uint8_t num_regs) {}137virtual void check_next_register(void) {}138};139140class AP_AK09916_BusDriver_HALDevice: public AP_AK09916_BusDriver141{142public:143AP_AK09916_BusDriver_HALDevice(AP_HAL::OwnPtr<AP_HAL::Device> dev);144145virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;146virtual bool register_read(uint8_t reg, uint8_t *val) override;147virtual bool register_write(uint8_t reg, uint8_t val, bool checked) override;148149virtual AP_HAL::Semaphore *get_semaphore() override;150AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;151152// set device type within a device class153void set_device_type(uint8_t devtype) override {154_dev->set_device_type(devtype);155}156157// return 24 bit bus identifier158uint32_t get_bus_id(void) const override {159return _dev->get_bus_id();160}161162/**163setup for register value checking. Frequency is how often to164check registers. If set to 10 then every 10th call to165check_next_register will check a register166*/167void setup_checked_registers(uint8_t num_regs) override {168_dev->setup_checked_registers(num_regs);169}170void check_next_register(void) override {171_dev->check_next_register();172}173174private:175AP_HAL::OwnPtr<AP_HAL::Device> _dev;176};177178class AP_AK09916_BusDriver_Auxiliary : public AP_AK09916_BusDriver179{180public:181AP_AK09916_BusDriver_Auxiliary(AP_InertialSensor &ins, uint8_t backend_id,182uint8_t backend_instance, uint8_t addr);183~AP_AK09916_BusDriver_Auxiliary();184185bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;186bool register_read(uint8_t reg, uint8_t *val) override;187bool register_write(uint8_t reg, uint8_t val, bool checked) override;188189AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;190191AP_HAL::Semaphore *get_semaphore() override;192193bool configure() override;194bool start_measurements() override;195196// set device type within a device class197void set_device_type(uint8_t devtype) override;198199// return 24 bit bus identifier200uint32_t get_bus_id(void) const override;201202private:203AuxiliaryBus *_bus;204AuxiliaryBusSlave *_slave;205bool _started;206};207208#endif // AP_COMPASS_AK09916_ENABLED209210211