diff --git a/Marlin/src/HAL/shared/eeprom_if_i2c.cpp b/Marlin/src/HAL/shared/eeprom_if_i2c.cpp index 5118d20c76f55..9f475977c6fb2 100644 --- a/Marlin/src/HAL/shared/eeprom_if_i2c.cpp +++ b/Marlin/src/HAL/shared/eeprom_if_i2c.cpp @@ -61,23 +61,24 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE // Public functions // ------------------------ +#define UNDER_2K (MARLIN_EEPROM_SIZE <= 2048) + +// Combine Address high bits into the device address on <=16Kbit (2K) and >512Kbit (64K) EEPROMs. +// Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas EEPROM model numbers refer to bits. +// e.g., The "16" in BL24C16 indicates a 16Kbit (2KB) size. static uint8_t _eeprom_calc_device_address(uint8_t * const pos) { const unsigned eeprom_address = (unsigned)pos; - // Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas most EEPROMs advertise bits - if (MARLIN_EEPROM_SIZE <= 2048 || MARLIN_EEPROM_SIZE > 65536) { - // Address high bits are stuffed into device address on <=16Kbit and >512Kbit EEPROMs - return uint8_t(eeprom_device_address | ((eeprom_address >> (MARLIN_EEPROM_SIZE <= 2048 ? 8 : 16)) & 0x07)); - } + if (UNDER_2K || MARLIN_EEPROM_SIZE > 65536) + return uint8_t(eeprom_device_address | ((eeprom_address >> (UNDER_2K ? 8 : 16)) & 0x07)); return eeprom_device_address; } static void _eeprom_begin(uint8_t * const pos) { const unsigned eeprom_address = (unsigned)pos; Wire.beginTransmission(_eeprom_calc_device_address(pos)); - if (MARLIN_EEPROM_SIZE > 2048) { - Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed - } - Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low + if (!UNDER_2K) + Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed + Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low } void eeprom_write_byte(uint8_t *pos, uint8_t value) {