perf(ds18b20-ds2480): передавай байты в Data Mode

This commit is contained in:
2026-09-15 22:45:07 +03:00
parent 3020de7e0c
commit def3eb08f3
7 changed files with 128 additions and 13 deletions

View File

@@ -12,6 +12,7 @@ typedef struct {
unsigned prepared, calibrated, pending, reply, pulse, hold, convert, copy;
unsigned fail_read, fail_write, fail_prepare, corrupt_reply, reset_reply;
unsigned fail_power, fail_stop, reject_config;
unsigned data_mode, escape;
} fake;
static void crc_scratch(fake *f) { f->scratch[8] = ds2480_crc8(f->scratch, 8); }
@@ -91,6 +92,7 @@ static int prepare(void *user)
fake *f = user;
++f->prepared;
f->pending = f->pulse = f->calibrated = 0;
f->data_mode = f->escape = 0;
return (int)f->fail_prepare;
}
@@ -101,6 +103,17 @@ static int transmit(void *user, const uint8_t *data, uint32_t size, uint32_t tim
CHECK(size == 1 && timeout == 20 && !f->pending);
if (f->fail_write) return -1;
if (!f->calibrated) { CHECK(command == 0xC1); f->calibrated = 1; return 0; }
if (f->data_mode) {
unsigned i, reply = 0;
if (command == 0xE3 && !f->escape) { f->escape=1; return 0; }
if (!f->escape || command == 0xE3) {
f->escape=0;
for (i=0; i<8; ++i) reply |= (unsigned)wire_bit(f,(uint8_t)((command>>i)&1)) << i;
f->reply=reply; f->pending=1; return 0;
}
f->escape=0; f->data_mode=0;
}
if (command == 0xE1) { f->data_mode=1; return 0; }
CHECK(!f->pulse || command == 0xF1);
if (command == 0x3F) f->reply = 0x3E;
else if (command == 0xC1) {
@@ -188,6 +201,8 @@ int main(void)
CHECK(raw == (i == 9 ? -168 : i == 10 ? -164 : i == 11 ? -162 : -161));
}
CHECK(f.copy == 4);
CHECK(ds18b20_ds2480_configure(&bus, f.rom[0], (int8_t)-29, 2, 12, 0) == DS2480_OK);
CHECK(f.scratch[2] == 0xE3); /* Escaped byte followed by an ordinary byte. */
f.reject_config = 1;
CHECK(ds18b20_ds2480_configure(&bus, f.rom[0], 1, 2, 9, 1) == DS2480_DATA && f.copy == 4);
f.scratch[8] ^= 1; raw = 999;