[MOD] Optimize SPI register writing
This commit is contained in:
parent
fe52311371
commit
29ede2c354
1 changed files with 8 additions and 0 deletions
|
@ -61,8 +61,16 @@ int16_t Module::SPIsetRegValue(uint32_t reg, uint8_t value, uint8_t msb, uint8_t
|
||||||
return(RADIOLIB_ERR_INVALID_BIT_RANGE);
|
return(RADIOLIB_ERR_INVALID_BIT_RANGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read the current value
|
||||||
uint8_t currentValue = SPIreadRegister(reg);
|
uint8_t currentValue = SPIreadRegister(reg);
|
||||||
uint8_t mask = ~((0b11111111 << (msb + 1)) | (0b11111111 >> (8 - lsb)));
|
uint8_t mask = ~((0b11111111 << (msb + 1)) | (0b11111111 >> (8 - lsb)));
|
||||||
|
|
||||||
|
// check if we actually need to update the register
|
||||||
|
if((currentValue & mask) == (value & mask)) {
|
||||||
|
return(RADIOLIB_ERR_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// update the register
|
||||||
uint8_t newValue = (currentValue & ~mask) | (value & mask);
|
uint8_t newValue = (currentValue & ~mask) | (value & mask);
|
||||||
SPIwriteRegister(reg, newValue);
|
SPIwriteRegister(reg, newValue);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue