Browse Source

🐛 Fix small/huge I2C EEPROM address (#22081)

Kyle Repinski 3 years ago
parent
commit
7c5e3b9071
No account linked to committer's email address
1 changed files with 17 additions and 4 deletions
  1. 17
    4
      Marlin/src/HAL/shared/eeprom_if_i2c.cpp

+ 17
- 4
Marlin/src/HAL/shared/eeprom_if_i2c.cpp View File

@@ -61,11 +61,24 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
61 61
 // Public functions
62 62
 // ------------------------
63 63
 
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.
69
+static uint8_t _eeprom_calc_device_address(uint8_t * const pos) {
70
+  const unsigned eeprom_address = (unsigned)pos;
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;
74
+}
75
+
64 76
 static void _eeprom_begin(uint8_t * const pos) {
65 77
   const unsigned eeprom_address = (unsigned)pos;
66
-  Wire.beginTransmission(eeprom_device_address);
67
-  Wire.write(int(eeprom_address >> 8));   // Address High
68
-  Wire.write(int(eeprom_address & 0xFF)); // Address Low
78
+  Wire.beginTransmission(_eeprom_calc_device_address(pos));
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
69 82
 }
70 83
 
71 84
 void eeprom_write_byte(uint8_t *pos, uint8_t value) {
@@ -81,7 +94,7 @@ void eeprom_write_byte(uint8_t *pos, uint8_t value) {
81 94
 uint8_t eeprom_read_byte(uint8_t *pos) {
82 95
   _eeprom_begin(pos);
83 96
   Wire.endTransmission();
84
-  Wire.requestFrom(eeprom_device_address, (byte)1);
97
+  Wire.requestFrom(_eeprom_calc_device_address(pos), (byte)1);
85 98
   return Wire.available() ? Wire.read() : 0xFF;
86 99
 }
87 100
 

Loading…
Cancel
Save