Skip to content

Commit 561a1ee

Browse files
committed
tweak
1 parent 5c861c4 commit 561a1ee

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Marlin/src/HAL/shared/eeprom_if_i2c.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,24 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
6161
// Public functions
6262
// ------------------------
6363

64+
#define SMALL_EEPROM (MARLIN_EEPROM_SIZE <= 2048)
65+
66+
// Combine Address high bits into the device address on <=16Kbit (2K) and >512Kbit (64K) EEPROMs.
67+
// Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas EEPROM model numbers refer to bits.
68+
// e.g., The "16" in BL24C16 indicates a 16Kbit (2KB) size.
6469
static uint8_t _eeprom_calc_device_address(uint8_t * const pos) {
6570
const unsigned eeprom_address = (unsigned)pos;
66-
// Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas most EEPROMs advertise bits
67-
if (MARLIN_EEPROM_SIZE <= 2048 || MARLIN_EEPROM_SIZE > 65536) {
68-
// Address high bits are stuffed into device address on <=16Kbit and >512Kbit EEPROMs
69-
return uint8_t(eeprom_device_address | ((eeprom_address >> (MARLIN_EEPROM_SIZE <= 2048 ? 8 : 16)) & 0x07));
70-
}
71-
return eeprom_device_address;
71+
return (SMALL_EEPROM || MARLIN_EEPROM_SIZE > 65536)
72+
? uint8_t(eeprom_device_address | ((eeprom_address >> (SMALL_EEPROM ? 8 : 16)) & 0x07))
73+
: eeprom_device_address;
7274
}
7375

7476
static void _eeprom_begin(uint8_t * const pos) {
7577
const unsigned eeprom_address = (unsigned)pos;
7678
Wire.beginTransmission(_eeprom_calc_device_address(pos));
77-
if (MARLIN_EEPROM_SIZE > 2048) {
78-
Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed
79-
}
80-
Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low
79+
if (!SMALL_EEPROM)
80+
Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed
81+
Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low
8182
}
8283

8384
void eeprom_write_byte(uint8_t *pos, uint8_t value) {

0 commit comments

Comments
 (0)