/**************************************************************************** * * Copyright (c) 2020 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name PX4 nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ #include "IST8310.hpp" using namespace time_literals; static constexpr int16_t combine(uint8_t msb, uint8_t lsb) { return (msb << 8u) | lsb; } IST8310::IST8310(const I2CSPIDriverConfig &config) : I2C(config), I2CSPIDriver(config), _px4_mag(get_device_id(), config.rotation) { } IST8310::~IST8310() { perf_free(_reset_perf); perf_free(_bad_register_perf); perf_free(_bad_transfer_perf); } int IST8310::init() { int ret = I2C::init(); if (ret != PX4_OK) { DEVICE_DEBUG("I2C::init failed (%i)", ret); return ret; } return Reset() ? 0 : -1; } bool IST8310::Reset() { _state = STATE::RESET; ScheduleClear(); ScheduleNow(); return true; } void IST8310::print_status() { I2CSPIDriverBase::print_status(); perf_print_counter(_reset_perf); perf_print_counter(_bad_register_perf); perf_print_counter(_bad_transfer_perf); } int IST8310::probe() { // Apparently, the IST8310's WHOAMI register is writeable. Presumably, // this can get corrupted by bus noise. It is only reset if powered off // for 30s or by a reset. // // Therefore, we do a reset before doing the WHOAMI. RegisterWrite(Register::CNTL2, CNTL2_BIT::SRST); px4_usleep(10000); const uint8_t WAI = RegisterRead(Register::WAI); if (WAI != Device_ID) { DEVICE_DEBUG("unexpected WAI 0x%02x", WAI); return PX4_ERROR; } return PX4_OK; } void IST8310::RunImpl() { const hrt_abstime now = hrt_absolute_time(); switch (_state) { case STATE::RESET: // CNTL2: Software Reset RegisterWrite(Register::CNTL2, CNTL2_BIT::SRST); _reset_timestamp = now; _failure_count = 0; _state = STATE::WAIT_FOR_RESET; perf_count(_reset_perf); ScheduleDelayed(50_ms); // Power On Reset: max 50ms break; case STATE::WAIT_FOR_RESET: // SRST: This bit is automatically reset to zero after POR routine if ((RegisterRead(Register::WAI) == Device_ID) && ((RegisterRead(Register::CNTL2) & CNTL2_BIT::SRST) == 0)) { // if reset succeeded then configure _state = STATE::CONFIGURE; ScheduleDelayed(10_ms); } else { // RESET not complete if (hrt_elapsed_time(&_reset_timestamp) > 1000_ms) { PX4_DEBUG("Reset failed, retrying"); _state = STATE::RESET; ScheduleDelayed(100_ms); } else { PX4_DEBUG("Reset not complete, check again in 10 ms"); ScheduleDelayed(10_ms); } } break; case STATE::CONFIGURE: if (Configure()) { // if configure succeeded then start measurement cycle _state = STATE::MEASURE; ScheduleDelayed(20_ms); } else { // CONFIGURE not complete if (hrt_elapsed_time(&_reset_timestamp) > 1000_ms) { PX4_DEBUG("Configure failed, resetting"); _state = STATE::RESET; } else { PX4_DEBUG("Configure failed, retrying"); } ScheduleDelayed(100_ms); } break; case STATE::MEASURE: RegisterWrite(Register::CNTL1, CNTL1_BIT::MODE_SINGLE_MEASUREMENT); _state = STATE::READ; ScheduleDelayed(20_ms); // Wait at least 6ms. (minimum waiting time for 16 times internal average setup) break; case STATE::READ: { struct TransferBuffer { uint8_t STAT1; uint8_t DATAXL; uint8_t DATAXH; uint8_t DATAYL; uint8_t DATAYH; uint8_t DATAZL; uint8_t DATAZH; } buffer{}; bool success = false; uint8_t cmd = static_cast(Register::STAT1); if (transfer(&cmd, 1, (uint8_t *)&buffer, sizeof(buffer)) == PX4_OK) { if (buffer.STAT1 & STAT1_BIT::DRDY) { int16_t x = combine(buffer.DATAXH, buffer.DATAXL); int16_t y = combine(buffer.DATAYH, buffer.DATAYL); int16_t z = combine(buffer.DATAZH, buffer.DATAZL); // sensor's frame is +x forward, +y right, +z up z = (z == INT16_MIN) ? INT16_MAX : -z; // flip z _px4_mag.set_error_count(perf_event_count(_bad_register_perf) + perf_event_count(_bad_transfer_perf)); _px4_mag.update(now, x, y, z); success = true; if (_failure_count > 0) { _failure_count--; } } } else { perf_count(_bad_transfer_perf); } if (!success) { _failure_count++; // full reset if things are failing consistently if (_failure_count > 10) { Reset(); return; } } if (!success || hrt_elapsed_time(&_last_config_check_timestamp) > 100_ms) { // check configuration registers periodically or immediately following any failure if (RegisterCheck(_register_cfg[_checked_register])) { _last_config_check_timestamp = now; _checked_register = (_checked_register + 1) % size_register_cfg; } else { // register check failed, force reset perf_count(_bad_register_perf); Reset(); return; } } // initiate next measurement RegisterWrite(Register::CNTL1, CNTL1_BIT::MODE_SINGLE_MEASUREMENT); ScheduleDelayed(20_ms); // Wait at least 6ms. (minimum waiting time for 16 times internal average setup) } break; } } bool IST8310::Configure() { // first set and clear all configured register bits for (const auto ®_cfg : _register_cfg) { RegisterSetAndClearBits(reg_cfg.reg, reg_cfg.set_bits, reg_cfg.clear_bits); } // now check that all are configured bool success = true; for (const auto ®_cfg : _register_cfg) { if (!RegisterCheck(reg_cfg)) { success = false; } } _px4_mag.set_scale(1.f / 1320.f); // 1320 LSB/Gauss return success; } bool IST8310::RegisterCheck(const register_config_t ®_cfg) { bool success = true; const uint8_t reg_value = RegisterRead(reg_cfg.reg); if (reg_cfg.set_bits && ((reg_value & reg_cfg.set_bits) != reg_cfg.set_bits)) { PX4_DEBUG("0x%02hhX: 0x%02hhX (0x%02hhX not set)", (uint8_t)reg_cfg.reg, reg_value, reg_cfg.set_bits); success = false; } if (reg_cfg.clear_bits && ((reg_value & reg_cfg.clear_bits) != 0)) { PX4_DEBUG("0x%02hhX: 0x%02hhX (0x%02hhX not cleared)", (uint8_t)reg_cfg.reg, reg_value, reg_cfg.clear_bits); success = false; } return success; } uint8_t IST8310::RegisterRead(Register reg) { const uint8_t cmd = static_cast(reg); uint8_t buffer{}; transfer(&cmd, 1, &buffer, 1); return buffer; } void IST8310::RegisterWrite(Register reg, uint8_t value) { uint8_t buffer[2] { (uint8_t)reg, value }; transfer(buffer, sizeof(buffer), nullptr, 0); } void IST8310::RegisterSetAndClearBits(Register reg, uint8_t setbits, uint8_t clearbits) { const uint8_t orig_val = RegisterRead(reg); uint8_t val = (orig_val & ~clearbits) | setbits; if (orig_val != val) { RegisterWrite(reg, val); } }