Skip to content

Commit ac5e58d

Browse files
committed
Ask for the version register more than once
1 parent 703108a commit ac5e58d

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/unit/unit_WS1850S.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace {
1717
// VERSION_REG value read from actual WS1850S hardware.
1818
// Not documented in the WS1850S datasheet (MFRC522 returns 0x91 or 0x92).
1919
constexpr uint8_t ws1850s_firmware_version{0x15};
20+
// How many times to ask for the version register before deciding the unit is not there
21+
constexpr uint_fast8_t DETECT_ATTEMPTS{3};
2022
} // namespace
2123

2224
using namespace m5::utility::mmh3;
@@ -35,12 +37,18 @@ const types::attr_t UnitWS1850S::attr{attribute::AccessI2C};
3537

3638
bool UnitWS1850S::begin()
3739
{
40+
// Reading the version register is this unit's first transaction, and a first read can come
41+
// back unanswered on a bus that has just been brought up, even with the unit in place.
42+
// UnitMFRC522::begin() opens with a write and does not need this
3843
uint8_t ver{};
39-
if (!readRegister8(VERSION_REG, ver, 0) || ver != ws1850s_firmware_version) {
40-
M5_LIB_LOGE("Cannot detect WS1850S %x", ver);
41-
return false;
44+
for (uint_fast8_t i = 0; i < DETECT_ATTEMPTS; ++i) {
45+
if (readRegister8(VERSION_REG, ver, 0) && ver == ws1850s_firmware_version) {
46+
return UnitMFRC522::begin();
47+
}
48+
m5::utility::delay(1);
4249
}
43-
return UnitMFRC522::begin();
50+
M5_LIB_LOGE("Cannot detect WS1850S %x", ver);
51+
return false;
4452
}
4553

4654
/*!

0 commit comments

Comments
 (0)