diff --git a/libraries/AP_HAL/Device.cpp b/libraries/AP_HAL/Device.cpp index ad4a3ed4e2..740562eab6 100644 --- a/libraries/AP_HAL/Device.cpp +++ b/libraries/AP_HAL/Device.cpp @@ -103,12 +103,13 @@ bool AP_HAL::Device::check_next_register(void) if (_bank_select) { if (!_bank_select(reg.bank)) { - // Cannot set bank + // Cannot set bank #if 0 - printf("Device 0x%x set bank 0x%02x\n", - (unsigned)get_bus_id(), - (unsigned)reg.bank); + printf("Device 0x%x set bank 0x%02x\n", + (unsigned)get_bus_id(), + (unsigned)reg.bank); #endif + _checked.last_reg_fail = reg; return false; } } @@ -123,8 +124,22 @@ bool AP_HAL::Device::check_next_register(void) (unsigned)reg.regnum, (unsigned)v, (unsigned)reg.value); #endif write_register(reg.regnum, reg.value); + _checked.last_reg_fail = reg; + _checked.last_reg_fail.value = v; return false; } _checked.next = (_checked.next+1) % _checked.n_set; return true; } + +/* + check one register value, returning information on the failure + */ +bool AP_HAL::Device::check_next_register(struct checkreg &fail) +{ + if (check_next_register()) { + return true; + } + fail = _checked.last_reg_fail; + return false; +} diff --git a/libraries/AP_HAL/Device.h b/libraries/AP_HAL/Device.h index 5402bd3d5c..ac67f70c00 100644 --- a/libraries/AP_HAL/Device.h +++ b/libraries/AP_HAL/Device.h @@ -201,6 +201,20 @@ public: */ bool check_next_register(void); + // checked registers + struct checkreg { + uint8_t bank; + uint8_t regnum; + uint8_t value; + }; + + /** + * check next register value for correctness, with return of + * failure value. Return false if value is incorrect or register + * checking has not been setup + */ + bool check_next_register(struct checkreg &fail); + /** * Wrapper function over #transfer() to read a sequence of bytes from * device. No value is written, differently from the #read_registers() @@ -390,18 +404,13 @@ protected: private: BankSelectCb _bank_select; - // checked registers - struct checkreg { - uint8_t bank; - uint8_t regnum; - uint8_t value; - }; struct { uint8_t n_allocated; uint8_t n_set; uint8_t next; uint8_t frequency; uint8_t counter; + struct checkreg last_reg_fail; struct checkreg *regs; } _checked; };