[MOD] Optimize SPI register writing

This commit is contained in:
jgromes 2024-11-10 17:43:28 +01:00
parent fe52311371
commit 29ede2c354

View file

@ -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);
}
// read the current value
uint8_t currentValue = SPIreadRegister(reg);
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);
SPIwriteRegister(reg, newValue);