Skip to content

Commit a1f363a

Browse files
committed
[IST8310] Reset all I2C addresses on startup
Reading the WAI register is unreliable as the chip sometimes returns wrong values or boots with the wrong I2C address. This can be fixed by sending the software reset command to all four possible I2C addresses.
1 parent c4ade17 commit a1f363a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/drivers/magnetometer/isentek/ist8310/IST8310.cpp

+25-22
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,31 @@ void IST8310::print_status()
8585

8686
int IST8310::probe()
8787
{
88-
uint8_t id = RegisterRead(Register::WAI);
89-
90-
if (id != Device_ID) {
91-
DEVICE_DEBUG("unexpected WAI 0x%02x", id);
92-
93-
// Apparently, the IST8310's WHOAMI register is writeable. Presumably,
94-
// this can get corrupted by bus noise. It is only reset if powered off
95-
// for 30s or by a reset.
96-
RegisterWrite(Register::CNTL2, CNTL2_BIT::SRST);
97-
98-
auto start_time = hrt_absolute_time();
99-
100-
while (hrt_elapsed_time(&start_time) < 50_ms) {
101-
px4_usleep(10'000);
102-
id = RegisterRead(Register::WAI);
88+
// Reading the WAI register is not always reliable, it can return 0xff or
89+
// other values if the sensor has been powered up in a certain way. In
90+
// addition, the I2C address is not always correct, sometimes it boots with
91+
// 0x0C rather than 0x0E.
92+
const auto start_time = hrt_absolute_time();
93+
94+
while (hrt_elapsed_time(&start_time) < 50_ms) {
95+
set_device_address(I2C_ADDRESS_DEFAULT);
96+
const int WAI = RegisterRead(Register::WAI);
97+
98+
if (WAI == Device_ID) {
99+
// Device has the right I2C address and register content
100+
return PX4_OK;
101+
}
103102

104-
if (id == Device_ID) {
105-
return PX4_OK;
106-
}
103+
// send reset command to all four possible addresses
104+
for (uint8_t addr = 0x0C; addr <= 0x0F; addr++) {
105+
set_device_address(addr);
106+
RegisterWrite(Register::CNTL2, CNTL2_BIT::SRST);
107107
}
108108

109-
return PX4_ERROR;
109+
px4_usleep(10'000);
110110
}
111111

112-
return PX4_OK;
112+
return PX4_ERROR;
113113
}
114114

115115
void IST8310::RunImpl()
@@ -290,11 +290,14 @@ bool IST8310::RegisterCheck(const register_config_t &reg_cfg)
290290
return success;
291291
}
292292

293-
uint8_t IST8310::RegisterRead(Register reg)
293+
int IST8310::RegisterRead(Register reg)
294294
{
295295
const uint8_t cmd = static_cast<uint8_t>(reg);
296296
uint8_t buffer{};
297-
transfer(&cmd, 1, &buffer, 1);
297+
const int ret = transfer(&cmd, 1, &buffer, 1);
298+
299+
if (ret != OK) { return -1; }
300+
298301
return buffer;
299302
}
300303

src/drivers/magnetometer/isentek/ist8310/IST8310.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class IST8310 : public device::I2C, public I2CSPIDriver<IST8310>
7979

8080
bool RegisterCheck(const register_config_t &reg_cfg);
8181

82-
uint8_t RegisterRead(Register reg);
82+
int RegisterRead(Register reg);
8383
void RegisterWrite(Register reg, uint8_t value);
8484
void RegisterSetAndClearBits(Register reg, uint8_t setbits, uint8_t clearbits);
8585

0 commit comments

Comments
 (0)