Fix SPI register masking in busWriteBuf#11242
Fix SPI register masking in busWriteBuf#11242sensei-hacker wants to merge 1 commit intoiNavFlight:maintenance-9.xfrom
Conversation
Correct the SPI register address masking in busWriteBuf() to clear the MSB instead of setting it. For SPI protocol, the MSB indicates read (1) or write (0) operations. Write operations should use (reg & 0x7F) to clear the MSB, not (reg | 0x80) which sets it. This matches the correct implementation in busWrite() at line 318 which uses (reg & 0x7F) for write operations. Note: This bug affects theoretical future SPI devices using buffer writes. Current devices using busWriteBuf() are all I2C-based (VL53L0X, VL53L1X, MLX90393, TERARANGER_EVO, US42 rangefinders) and are unaffected. Fixes iNavFlight#10674
PR Compliance Guide 🔍All compliance sections have been disabled in the configurations. |
Branch Targeting SuggestionYou've targeted the
If This is an automated suggestion to help route contributions to the appropriate branch. |
PR Code Suggestions ✨No code suggestions found for the PR. |
User description
Summary
Corrects SPI register address masking in busWriteBuf() to clear the MSB instead of setting it.
Problem
For SPI protocol, the MSB indicates read (1) or write (0) operations. The busWriteBuf() function incorrectly used
reg | 0x80which sets the MSB, when it should usereg & 0x7Fto clear it for write operations.This is inconsistent with the correct implementation in busWrite() at line 318.
Solution
Change line 286 from:
To:
Impact
Current hardware: None - all devices using busWriteBuf() are I2C-based (VL53L0X, VL53L1X, MLX90393, TERARANGER_EVO, US42 rangefinders).
Future hardware: Prevents silent failures if SPI devices use multi-byte buffer writes.
This is a defensive fix for code correctness.
Fixes #10674
PR Type
Bug fix
Description
Corrects SPI register masking in busWriteBuf() to clear MSB for writes
Changes register operation from OR (0x80) to AND (0x7F) masking
Aligns implementation with correct busWrite() function behavior
Prevents future silent failures in SPI multi-byte buffer writes
Diagram Walkthrough
File Walkthrough
bus.c
Fix SPI write register masking operationsrc/main/drivers/bus.c
reg | 0x80toreg & 0x7FinbusWriteBuf()
I2C-based