mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-08-02 04:58:15 +08:00
fix(drivers/SPL06): fix SPI read off-by-one dropping data bytes (#27074)
Fix: transfer `len + 1` bytes and copy received data from offset 1, skipping the dummy byte. Copy into caller's buffer only on success.
This commit is contained in:
@@ -98,7 +98,17 @@ SPL06_SPI::read(uint8_t addr, uint8_t *buf, uint8_t len)
|
||||
{
|
||||
uint8_t tx_buf[len + 1] = {(uint8_t)(addr | DIR_READ)}; // GCC support VLA, let's use it
|
||||
|
||||
return transfer(tx_buf, buf, len);
|
||||
int res = transfer(tx_buf, tx_buf, (len + 1));
|
||||
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
buf[i] = tx_buf[1 + i];
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif // CONFIG_SPI
|
||||
|
||||
Reference in New Issue
Block a user