/* Host-side tests exercise the real vendor drivers and shared CRC helpers. */ #include #include #include #include #include #include "sen5x_i2c.h" #include "sen66_i2c.h" static uint8_t expected_address; static uint16_t command; static uint16_t words[9]; static bool bad_crc, fail_read, fail_write; static uint8_t crc(const uint8_t *data) { uint8_t value = 0xff; for (unsigned n = 0; n < 2; n++) { value ^= data[n]; for (unsigned bit = 0; bit < 8; bit++) value = (value & 0x80) ? (uint8_t)((value << 1) ^ 0x31) : (uint8_t)(value << 1); } return value; } int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t *data, uint16_t size) { assert(address == expected_address && size >= 2); command = (uint16_t)((data[0] << 8) | data[1]); return fail_write ? -1 : 0; } int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t *data, uint16_t size) { assert(address == expected_address && size % 3 == 0 && size <= 27); if (fail_read) return -1; for (unsigned i = 0; i < size / 3; i++) { data[3*i] = (uint8_t)(words[i] >> 8); data[3*i+1] = (uint8_t)words[i]; data[3*i+2] = crc(&data[3*i]); } if (bad_crc) data[2] ^= 1; return 0; } void sensirion_i2c_hal_sleep_usec(uint32_t usec) { (void)usec; } int main(void) { bool ready = false; uint8_t padding; uint16_t pm1, pm2, pm4, pm10, co2; int16_t rh, temp, voc, nox; expected_address = 0x6b; sen66_init(expected_address); words[0] = 1; assert(sen66_get_data_ready(&padding, &ready) == 0 && ready && padding == 0); assert(command == 0x0202); uint16_t measured[] = {10, 25, 30, 40, 4567, 5000, 1200, 0x7fff, 1456}; memcpy(words, measured, sizeof(measured)); #define READ66() sen66_read_measured_values_as_integers(&pm1, &pm2, &pm4, &pm10, &rh, &temp, &voc, &nox, &co2) assert(READ66() == 0 && command == 0x0300); assert(pm2 == 25 && rh == 4567 && temp == 5000 && voc == 1200 && nox == 0x7fff && co2 == 1456); words[5] = (uint16_t)-1000; words[8] = 0xffff; assert(READ66() == 0 && temp == -1000 && co2 == 0xffff); bad_crc = true; assert(READ66() != 0); bad_crc = false; fail_read = true; assert(READ66() != 0); fail_read = false; fail_write = true; assert(READ66() != 0); fail_write = false; expected_address = 0x69; words[0] = 1; assert(sen5x_read_data_ready(&ready) == 0 && ready && command == 0x0202); memcpy(words, measured, sizeof(measured)); #define READ54() sen5x_read_measured_values(&pm1, &pm2, &pm4, &pm10, &rh, &temp, &voc, &nox) assert(READ54() == 0 && command == 0x03c4); assert(pm2 == 25 && rh == 4567 && temp == 5000 && voc == 1200 && nox == 0x7fff); bad_crc = true; assert(READ54() != 0); bad_crc = false; fail_write = true; assert(READ54() != 0); puts("PASS: SEN54/SEN66 address and command selection, measured payloads, signed values, sentinels, CRC and transport errors"); }