From 770d2cdc77ec1ab3cebb4c428773d223c93b44f6 Mon Sep 17 00:00:00 2001 From: Orel Date: Mon, 20 Jan 2020 15:22:36 +0100 Subject: [PATCH 1/4] Add a new identifier to set the I2C master id --- Marlin/Configuration_adv.h | 1 + Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h | 4 +--- Marlin/src/feature/dac/dac_mcp4728.h | 2 ++ Marlin/src/inc/Conditionals_adv.h | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 315c0714b4f6..5236a63c6e06 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2481,6 +2481,7 @@ //#define EXPERIMENTAL_I2CBUS #define I2C_SLAVE_ADDRESS 0 // Set a value from 8 to 127 to act as a slave +//#define I2C_MASTER_ID 1 // Set the I2C master id to be used. For now, only used for LPC176x boards. // @section extras diff --git a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h index c2d6c2b81d01..1dbd34d78e3d 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h +++ b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h @@ -25,9 +25,7 @@ * HAL_LPC1768/include/i2c_util.h */ -#ifndef USEDI2CDEV_M - #define USEDI2CDEV_M 1 // By default use I2C1 controller -#endif +#define USEDI2CDEV_M I2C_MASTER_ID #if USEDI2CDEV_M == 0 #define I2CDEV_M LPC_I2C0 diff --git a/Marlin/src/feature/dac/dac_mcp4728.h b/Marlin/src/feature/dac/dac_mcp4728.h index f25310f7cae7..867ccb5600fe 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.h +++ b/Marlin/src/feature/dac/dac_mcp4728.h @@ -27,6 +27,8 @@ #include "../../core/types.h" +#define USEDI2CDEV_M I2C_MASTER_ID + #include /** diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index 4e0fe843907b..f98ea0b2ccce 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -140,3 +140,9 @@ #if ENABLED(JOYSTICK) #define POLL_JOG #endif + +// Set a default i2c master id if not defined +// It might be placed in HAL/HAL_LPC1768/inc/Conditionals_adv.h as it is only used for LPC176x boards for now +#ifndef I2C_MASTER_ID + #define I2C_MASTER_ID 1 +#endif From bf5c75f0a512311ac5ceda6bb61fc94bd14d0806 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 20 Jan 2020 19:49:02 -0600 Subject: [PATCH 2/4] Update Configuration_adv.h --- Marlin/Configuration_adv.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 5236a63c6e06..648e3761b253 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2449,6 +2449,14 @@ #endif // HAS_L64XX +// @section i2cbus + +// +// I2C Master ID for LPC176x LCD and Digital Current control +// Does not apply to other peripherals based on the Wire library. +// +//#define I2C_MASTER_ID 1 // Set a value from 0 to 2 + /** * TWI/I2C BUS * @@ -2477,11 +2485,10 @@ * echo:i2c-reply: from:99 bytes:5 data:hello */ -// @section i2cbus - //#define EXPERIMENTAL_I2CBUS -#define I2C_SLAVE_ADDRESS 0 // Set a value from 8 to 127 to act as a slave -//#define I2C_MASTER_ID 1 // Set the I2C master id to be used. For now, only used for LPC176x boards. +#if ENABLED(EXPERIMENTAL_I2CBUS) + #define I2C_SLAVE_ADDRESS 0 // Set a value from 8 to 127 to act as a slave +#endif // @section extras From 0861ca2a36b606be003ad0df1cfddf83049f8f7e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 20 Jan 2020 20:00:21 -0600 Subject: [PATCH 3/4] Define I2C_MASTER_ID only where needed --- Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c | 7 +++---- Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h | 12 ++++++++---- Marlin/src/feature/dac/dac_mcp4728.h | 2 -- Marlin/src/inc/Conditionals_adv.h | 6 ------ 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c index a451f4c43f75..f3dc6d230818 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c +++ b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c @@ -26,7 +26,6 @@ #ifdef TARGET_LPC1768 -#include "../../../inc/MarlinConfigPre.h" #include "i2c_util.h" #define U8G_I2C_OPT_FAST 16 // from u8g.h @@ -43,13 +42,13 @@ void configure_i2c(const uint8_t clock_option) { PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; - #if USEDI2CDEV_M == 0 + #if I2C_MASTER_ID == 0 PinCfg.Funcnum = 1; PinCfg.Pinnum = 27; // SDA0 / D57 AUX-1 ... SCL0 / D58 AUX-1 - #elif USEDI2CDEV_M == 1 + #elif I2C_MASTER_ID == 1 PinCfg.Funcnum = 3; PinCfg.Pinnum = 0; // SDA1 / D20 SCA ... SCL1 / D21 SCL - #elif USEDI2CDEV_M == 2 + #elif I2C_MASTER_ID == 2 PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; // SDA2 / D38 X_ENABLE_PIN ... SCL2 / D55 X_DIR_PIN #endif diff --git a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h index 1dbd34d78e3d..671a1e3746f3 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h +++ b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h @@ -25,13 +25,17 @@ * HAL_LPC1768/include/i2c_util.h */ -#define USEDI2CDEV_M I2C_MASTER_ID +#include "../../../inc/MarlinConfigPre.h" -#if USEDI2CDEV_M == 0 +#ifndef I2C_MASTER_ID + #define I2C_MASTER_ID 1 +#endif + +#if I2C_MASTER_ID == 0 #define I2CDEV_M LPC_I2C0 -#elif USEDI2CDEV_M == 1 +#elif I2C_MASTER_ID == 1 #define I2CDEV_M LPC_I2C1 -#elif USEDI2CDEV_M == 2 +#elif I2C_MASTER_ID == 2 #define I2CDEV_M LPC_I2C2 #else #error "Master I2C device not defined!" diff --git a/Marlin/src/feature/dac/dac_mcp4728.h b/Marlin/src/feature/dac/dac_mcp4728.h index 867ccb5600fe..f25310f7cae7 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.h +++ b/Marlin/src/feature/dac/dac_mcp4728.h @@ -27,8 +27,6 @@ #include "../../core/types.h" -#define USEDI2CDEV_M I2C_MASTER_ID - #include /** diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index f98ea0b2ccce..4e0fe843907b 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -140,9 +140,3 @@ #if ENABLED(JOYSTICK) #define POLL_JOG #endif - -// Set a default i2c master id if not defined -// It might be placed in HAL/HAL_LPC1768/inc/Conditionals_adv.h as it is only used for LPC176x boards for now -#ifndef I2C_MASTER_ID - #define I2C_MASTER_ID 1 -#endif From aeb0661d004bfec48b90f12d6046578814e10e63 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 20 Jan 2020 20:05:27 -0600 Subject: [PATCH 4/4] Fix include location --- Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp index 8afb60752e1a..360cce7f662f 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp @@ -25,15 +25,15 @@ #ifdef TARGET_LPC1768 +#include "../include/i2c_util.h" +#include "../../../core/millis_t.h" + extern int millis(); #ifdef __cplusplus extern "C" { #endif -#include "../include/i2c_util.h" -#include "../../../core/millis_t.h" - ////////////////////////////////////////////////////////////////////////////////////// // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to