Scott Lahteine 3 лет назад
Родитель
Сommit
cc3e878f90
Аккаунт пользователя с таким Email не найден
1 измененных файлов: 18 добавлений и 7 удалений
  1. 18
    7
      Marlin/src/HAL/shared/eeprom_if_i2c.cpp

+ 18
- 7
Marlin/src/HAL/shared/eeprom_if_i2c.cpp Просмотреть файл

@@ -51,6 +51,18 @@ void eeprom_init() {
51 51
 
52 52
 static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRESS);
53 53
 
54
+void _beginTransmission(const uint16_t memoryAddress) {
55
+  if (MARLIN_EEPROM_SIZE > 0x4000) {  // Use two-byte addressing for EEPROMs >16kb
56
+    Wire.beginTransmission(eeprom_device_address);
57
+    Wire.write(memoryAddress >> 8);   // Address High Byte
58
+  }
59
+  else {
60
+    const uint8_t addr = eeprom_device_address | byte((memoryAddress >> 8) & 0x07);
61
+    Wire.beginTransmission(addr);
62
+  }
63
+  Wire.write(memoryAddress & 0xFF);   // Address Low Byte (or only byte for chips <= 16Kb like 24C02/04/08/16)
64
+}
65
+
54 66
 // ------------------------
55 67
 // Public functions
56 68
 // ------------------------
@@ -58,9 +70,7 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
58 70
 void eeprom_write_byte(uint8_t *pos, unsigned char value) {
59 71
   const unsigned eeprom_address = (unsigned)pos;
60 72
 
61
-  Wire.beginTransmission(eeprom_device_address);
62
-  Wire.write(int(eeprom_address >> 8));   // MSB
63
-  Wire.write(int(eeprom_address & 0xFF)); // LSB
73
+  _beginTransmission(eeprom_address);
64 74
   Wire.write(value);
65 75
   Wire.endTransmission();
66 76
 
@@ -72,11 +82,12 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) {
72 82
 uint8_t eeprom_read_byte(uint8_t *pos) {
73 83
   const unsigned eeprom_address = (unsigned)pos;
74 84
 
75
-  Wire.beginTransmission(eeprom_device_address);
76
-  Wire.write(int(eeprom_address >> 8));   // MSB
77
-  Wire.write(int(eeprom_address & 0xFF)); // LSB
85
+  _beginTransmission(eeprom_address);
78 86
   Wire.endTransmission();
79
-  Wire.requestFrom(eeprom_device_address, (byte)1);
87
+
88
+  // For EEPROMs <=16Kb the lower address bits are used for 2Kb page address
89
+  const int addr = eeprom_device_address | (MARLIN_EEPROM_SIZE <= 0x4000 ? byte((eeprom_address >> 8) & 0x07) : byte(0));
90
+  Wire.requestFrom(addr, byte(1));
80 91
   return Wire.available() ? Wire.read() : 0xFF;
81 92
 }
82 93
 

Загрузка…
Отмена
Сохранить