Repository navigation
The driver had several problems in the arithmetic around the sensor and in the way the work is serialized. All of them are fixed, and the reset command is available again.
Fixed
The humidity was converted to zero at 100 %
(uint16_t)(humidity / 100.0 * 65536) gave 0 at 100 %RH, so the sensor received the driest possible air when the air was the most humid. The multiplier is 65535 in table 10 of the SGP40 v1.2 and SGP41 v1.0 datasheets (#4).
The values outside the documented range (0..100 % and -45..130 °C) were silently replaced with the closest limit, so a measurement computed from an impossible input looked valid. They are reported as ESP_ERR_INVALID_ARG now (#4).
The commands were built with unaligned 16-bit accesses
The 3-byte Sensirion frames put every other word on an odd address. *(uint16_t *)p broke the alignment requirement and the strict aliasing rules: the Xtensa cores tolerate the access, the RISC-V ones raise an exception (#5).
The gas index was updated outside the device mutex
.eil.yml promises thread_safe: yes, but sgp4x_measure_raw() released the mutex before GasIndexAlgorithm_process() touched the filter state in the descriptor, so two tasks corrupted it (#6).
ESP_FAIL was used for the unsupported operations
A conditioning on the SGP40 and an unknown type in the descriptor reported ESP_FAIL, the same code as a genuinely failed operation. They report ESP_ERR_NOT_SUPPORTED now (#7).
sgp4x_init_desc() accepted an unknown device type
SGP4x_TYPE_SGP40 is 0, so a zero-initialized descriptor silently became an SGP40 (#8).
New
sgp4x_soft_reset()is back (#9). It was lost in 2.0.0, although both datasheets document0x0006as the way back to the idle mode after an interrupted conditioning or a heater off. The command is sent to the device address, so the other devices on the bus are not reset.
Documentation
The header lists the real return codes of every function — ESP_ERR_INVALID_ARG, ESP_ERR_NOT_SUPPORTED, ESP_ERR_TIMEOUT, ESP_ERR_INVALID_CRC and ESP_FAIL for a failed self-test — and the two constraints: the conditioning must not last longer than 10 seconds, and sgp4x_measure_index() keeps the algorithm state in the descriptor, so the sampling interval has to be constant (#10).
Verification
- Both examples build on ESP-IDF 6.1 without warnings,
astyle --projectreports no changes. - The byte order and the CRC were checked against table 10 of the datasheets:
0x8000 → 80 00 A2,0x6666 → 66 66 93,0x0000 → 00 00 81,0xFFFF → FF FF AC. - The unaligned access was reproduced on the host under
-fsanitize=undefinedbefore the fix and is gone after it. 100.0%RH gives0xFFFF,-45 °Cgives0x0000,130 °Cgives0xFFFF.
Full changelog: 2.0.0...2.1.0