From b700b3cde6102cff68f25555bffe5f112e2111e4 Mon Sep 17 00:00:00 2001 From: randellhodges Date: Sat, 25 Apr 2020 11:35:35 -0500 Subject: [PATCH] Fix M261, i2c EEPROM, i2c Encoder for LPC (#17678) Co-authored-by: Scott Lahteine --- Marlin/src/HAL/shared/eeprom_i2c.cpp | 12 ++++++------ Marlin/src/core/macros.h | 6 +----- Marlin/src/feature/encoder_i2c.cpp | 4 ++-- Marlin/src/feature/twibus.cpp | 4 ++-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Marlin/src/HAL/shared/eeprom_i2c.cpp b/Marlin/src/HAL/shared/eeprom_i2c.cpp index 3eb72194ac..8ce3b88c48 100644 --- a/Marlin/src/HAL/shared/eeprom_i2c.cpp +++ b/Marlin/src/HAL/shared/eeprom_i2c.cpp @@ -39,7 +39,7 @@ // Private Variables // ------------------------ -static uint8_t eeprom_device_address = 0x50; +static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(0x50); // ------------------------ // Public functions @@ -54,7 +54,7 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) { eeprom_init(); - Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address)); + Wire.beginTransmission(eeprom_device_address); Wire.write((int)(eeprom_address >> 8)); // MSB Wire.write((int)(eeprom_address & 0xFF)); // LSB Wire.write(value); @@ -70,7 +70,7 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) { void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) { eeprom_init(); - Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address)); + Wire.beginTransmission(eeprom_device_address); Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB Wire.endTransmission(); @@ -82,7 +82,7 @@ void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) { flag |= Wire.read() ^ ptr[c]; if (flag) { - Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address)); + Wire.beginTransmission(eeprom_device_address); Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB Wire.write((uint8_t*)pos, n); @@ -99,7 +99,7 @@ uint8_t eeprom_read_byte(uint8_t *pos) { eeprom_init(); - Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address)); + Wire.beginTransmission(eeprom_device_address); Wire.write((int)(eeprom_address >> 8)); // MSB Wire.write((int)(eeprom_address & 0xFF)); // LSB Wire.endTransmission(); @@ -111,7 +111,7 @@ uint8_t eeprom_read_byte(uint8_t *pos) { void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) { eeprom_init(); - Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address)); + Wire.beginTransmission(eeprom_device_address); Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB Wire.endTransmission(); diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index 899baf7359..765f157e2f 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -292,11 +292,7 @@ #define FMOD(x, y) fmodf(x, y) #define HYPOT(x,y) SQRT(HYPOT2(x,y)) -#ifdef TARGET_LPC1768 - #define I2C_ADDRESS(A) ((A) << 1) -#else - #define I2C_ADDRESS(A) A -#endif +#define I2C_ADDRESS(A) (TERN(TARGET_LPC1768, (A) << 1, A)) // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments #define _NUM_ARGS(_,Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A,OUT,...) OUT diff --git a/Marlin/src/feature/encoder_i2c.cpp b/Marlin/src/feature/encoder_i2c.cpp index dfc29a70d3..dfac6ccdf5 100644 --- a/Marlin/src/feature/encoder_i2c.cpp +++ b/Marlin/src/feature/encoder_i2c.cpp @@ -305,7 +305,7 @@ int32_t I2CPositionEncoder::get_raw_count() { encoderCount.val = 0x00; - if (Wire.requestFrom((int)i2cAddress, 3) != 3) { + if (Wire.requestFrom(I2C_ADDRESS(i2cAddress), 3) != 3) { //houston, we have a problem... H = I2CPE_MAG_SIG_NF; return 0; @@ -744,7 +744,7 @@ void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) { Wire.endTransmission(); // Read value - if (Wire.requestFrom((int)address, 32)) { + if (Wire.requestFrom(I2C_ADDRESS(address), 32)) { char c; while (Wire.available() > 0 && (c = (char)Wire.read()) > 0) SERIAL_ECHO(c); diff --git a/Marlin/src/feature/twibus.cpp b/Marlin/src/feature/twibus.cpp index 9dbb1deb4f..4ed9faaa8a 100644 --- a/Marlin/src/feature/twibus.cpp +++ b/Marlin/src/feature/twibus.cpp @@ -104,8 +104,8 @@ bool TWIBus::request(const uint8_t bytes) { debug(PSTR("request"), bytes); // requestFrom() is a blocking function - if (Wire.requestFrom(addr, bytes) == 0) { - debug("request fail", addr); + if (Wire.requestFrom(I2C_ADDRESS(addr), bytes) == 0) { + debug("request fail", I2C_ADDRESS(addr)); return false; }