Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Compass/AP_Compass_IST8308.cpp
Views: 1798
/*1* Copyright (C) 2018 Lucas De Marchi. All rights reserved.2*3* This file is free software: you can redistribute it and/or modify it4* under the terms of the GNU General Public License as published by the5* Free Software Foundation, either version 2 of the License, or6* (at your option) any later version.7*8* This file is distributed in the hope that it will be useful, but9* WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.11* See the GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License along14* with this program. If not, see <http://www.gnu.org/licenses/>.15*/16#include "AP_Compass_IST8308.h"1718#if AP_COMPASS_IST8308_ENABLED1920#include <stdio.h>21#include <utility>2223#include <AP_HAL/AP_HAL.h>24#include <AP_HAL/utility/sparse-endian.h>25#include <AP_Math/AP_Math.h>2627#define WAI_REG 0x028#define DEVICE_ID 0x082930#define STAT1_REG 0x1031#define STAT1_VAL_DRDY 0x132#define STAT1_VAL_DOR 0x23334#define DATAX_L_REG 0x1135#define DATAX_H_REG 0x1236#define DATAY_L_REG 0x1337#define DATAY_H_REG 0x1438#define DATAZ_L_REG 0x1539#define DATAZ_H_REG 0x164041#define CNTL1_REG 0x304243#define CNTL2_REG 0x3144#define CNTL2_VAL_STANDBY_MODE 0x045#define CNTL2_VAL_SINGLE_MODE 0x146#define CNTL2_VAL_CONT_ODR10_MODE 0x247#define CNTL2_VAL_CONT_ODR20_MODE 0x448#define CNTL2_VAL_CONT_ODR50_MODE 0x649#define CNTL2_VAL_CONT_ODR100_MODE 0x850#define CNTL2_VAL_CONT_ODR200_MODE 0xA51#define CNTL2_VAL_CONT_ODR8_MODE 0xB52#define CNTL2_VAL_CONT_ODR1_MODE 0xC53#define CNTL2_VAL_CONT_ODR0P5_MODE 0xD54#define CNTL2_VAL_SINGLE_TEST_MODE 0x105556#define CNTL3_REG 0x3257#define CNTL3_VAL_SRST 158#define CNTL3_VAL_DRDY_POLARITY_HIGH (1 << 2)59#define CNTL3_VAL_DRDY_EN (1 << 3)6061#define CNTL4_REG 0x3462#define CNTL4_VAL_DYNAMIC_RANGE_500 063#define CNTL4_VAL_DYNAMIC_RANGE_200 0x16465#define OSRCNTL_REG 0x4166#define OSRCNTL_VAL_XZ_1 (0)67#define OSRCNTL_VAL_XZ_2 (1)68#define OSRCNTL_VAL_XZ_4 (2)69#define OSRCNTL_VAL_XZ_8 (3)70#define OSRCNTL_VAL_XZ_16 (4)71#define OSRCNTL_VAL_XZ_32 (4)72#define OSRCNTL_VAL_Y_1 (0 << 3)73#define OSRCNTL_VAL_Y_2 (1 << 3)74#define OSRCNTL_VAL_Y_4 (2 << 3)75#define OSRCNTL_VAL_Y_8 (3 << 3)76#define OSRCNTL_VAL_Y_16 (4 << 3)77#define OSRCNTL_VAL_Y_32 (5 << 3)7879#define SAMPLING_PERIOD_USEC (10 * AP_USEC_PER_MSEC)8081extern const AP_HAL::HAL &hal;8283AP_Compass_Backend *AP_Compass_IST8308::probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,84bool force_external,85enum Rotation rotation)86{87if (!dev) {88return nullptr;89}9091AP_Compass_IST8308 *sensor = NEW_NOTHROW AP_Compass_IST8308(std::move(dev), force_external, rotation);92if (!sensor || !sensor->init()) {93delete sensor;94return nullptr;95}9697return sensor;98}99100AP_Compass_IST8308::AP_Compass_IST8308(AP_HAL::OwnPtr<AP_HAL::Device> dev,101bool force_external,102enum Rotation rotation)103: _dev(std::move(dev))104, _rotation(rotation)105, _force_external(force_external)106{107}108109bool AP_Compass_IST8308::init()110{111uint8_t reset_count = 0;112113_dev->get_semaphore()->take_blocking();114115// high retries for init116_dev->set_retries(10);117118uint8_t whoami;119if (!_dev->read_registers(WAI_REG, &whoami, 1) ||120whoami != DEVICE_ID) {121// not an IST8308122goto fail;123}124125for (; reset_count < 5; reset_count++) {126if (!_dev->write_register(CNTL3_REG, CNTL3_VAL_SRST)) {127hal.scheduler->delay(10);128continue;129}130131hal.scheduler->delay(20);132133uint8_t cntl3 = 0xFF;134if (_dev->read_registers(CNTL3_REG, &cntl3, 1) &&135(cntl3 & 0x01) == 0) {136break;137}138}139140if (reset_count == 5) {141printf("IST8308: failed to reset device\n");142goto fail;143}144145// DRDY enabled146// Dynamic Range=±500 uT, Sensitivity=6.6 LSB/uT147// OSR 16 (max 100Hz)148// Start continuous mode at 100Hz149if (!_dev->write_register(CNTL3_REG, CNTL3_VAL_DRDY_EN) ||150!_dev->write_register(CNTL4_REG, CNTL4_VAL_DYNAMIC_RANGE_500) ||151!_dev->write_register(OSRCNTL_REG, OSRCNTL_VAL_Y_16 | OSRCNTL_VAL_XZ_16) ||152!_dev->write_register(CNTL2_REG, CNTL2_VAL_CONT_ODR100_MODE)) {153printf("IST8308: found device but could not set it up\n");154goto fail;155}156157// lower retries for run158_dev->set_retries(3);159160_dev->get_semaphore()->give();161162//register compass instance163_dev->set_device_type(DEVTYPE_IST8308);164if (!register_compass(_dev->get_bus_id(), _instance)) {165return false;166}167set_dev_id(_instance, _dev->get_bus_id());168169printf("%s found on bus %u id %u address 0x%02x\n", name,170_dev->bus_num(), unsigned(_dev->get_bus_id()), _dev->get_bus_address());171172set_rotation(_instance, _rotation);173174if (_force_external) {175set_external(_instance, true);176}177178_dev->register_periodic_callback(SAMPLING_PERIOD_USEC,179FUNCTOR_BIND_MEMBER(&AP_Compass_IST8308::timer, void));180181return true;182183fail:184_dev->get_semaphore()->give();185return false;186}187188void AP_Compass_IST8308::timer()189{190struct PACKED {191le16_t rx;192le16_t ry;193le16_t rz;194} buffer;195uint8_t stat;196197if (!_dev->read_registers(STAT1_REG, &stat, 1) ||198!(stat & STAT1_VAL_DRDY)) {199return;200}201202if (!_dev->read_registers(DATAX_L_REG, (uint8_t *) &buffer,203sizeof(buffer))) {204return;205}206207auto x = static_cast<int16_t>(le16toh(buffer.rx));208auto y = static_cast<int16_t>(le16toh(buffer.ry));209auto z = static_cast<int16_t>(le16toh(buffer.rz));210211// flip Z to conform to right-hand rule convention212z = -z;213214/* Resolution: 0.1515 µT/LSB - already convert to milligauss */215Vector3f field = Vector3f{x * 1.515f, y * 1.515f, z * 1.515f};216217accumulate_sample(field, _instance);218}219220void AP_Compass_IST8308::read()221{222drain_accumulated_samples(_instance);223}224225#endif // AP_COMPASS_IST8308_ENABLED226227228