diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 043e486d5d..122a7a458d 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -4718,6 +4718,11 @@ // //#define PINS_DEBUGGING +// +// M265 - I2C Scanner +// +//#define I2C_SCANNER + // Enable Tests that will run at startup and produce a report //#define MARLIN_TEST_BUILD diff --git a/Marlin/src/gcode/feature/i2c/M265.cpp b/Marlin/src/gcode/feature/i2c/M265.cpp new file mode 100644 index 0000000000..193ca1c7bd --- /dev/null +++ b/Marlin/src/gcode/feature/i2c/M265.cpp @@ -0,0 +1,69 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2025 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfig.h" + +#if ENABLED(I2C_SCANNER) + +#include "../../../libs/hex_print.h" +#include "../../gcode.h" +#include // Include Wire library for I2C communication + +/** + * M265: I2C Scanner - Scan for I2C devices on DOGLCD I2C pins + * + * Usage: M265 + * + * Scans I2C addresses 0x08 to 0x77 and reports any responding devices. + */ +void GcodeSuite::M265() { + Wire.begin(); + int device_count = 0; + + SERIAL_ECHOLNPGM("Scanning I2C (0x08-0x77)..."); + for (uint8_t address = 0x08; address <= 0x77; address++) { + Wire.beginTransmission(address); + const uint8_t error = Wire.endTransmission(); + + if (error == 0) { + // Device found + device_count++; + SERIAL_ECHOLNPGM("I2C device found at address 0x", hex_byte(address)); + } + else if (error == 4) + SERIAL_ECHOLNPGM("Unknown error at address 0x", hex_byte(address)); + + safe_delay(5); // Small delay between scans + } + + SERIAL_ECHOPGM("I2C scan complete. "); + if (device_count == 0) + SERIAL_ECHOLNPGM("No I2C devices found"); + else { + SERIAL_ECHOLN("Found ", device_count, " device"); + if (device_count > 1) SERIAL_CHAR('s'); + SERIAL_EOL(); + } + +} + +#endif // I2C_SCANNER diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 9088407f81..d4f74336de 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -826,6 +826,10 @@ void GcodeSuite::process_parsed_command(bool no_ok/*=false*/) { case 261: M261(); break; // M261: Request data from an i2c slave #endif + #if ENABLED(I2C_SCANNER) + case 265: M265(); break; // M265: I2C Scanner + #endif + #if ENABLED(PREVENT_COLD_EXTRUSION) case 302: M302(); break; // M302: Allow cold extrudes (set the minimum extrude temperature) #endif diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 319262c31e..87d626effc 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -211,6 +211,7 @@ * M256 - Set LCD brightness: 'M256 B' (0-255). (Requires an LCD with brightness control) * M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS) * M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS) + * M265 - i2c Scanner - Scan for I2C devices. (Requires I2C_SCANNER) * M280 - Set servo position absolute: 'M280 P S'. (Requires servos) * M281 - Set servo min|max position: 'M281 P L U'. (Requires EDITABLE_SERVO_ANGLES) * M282 - Detach servo: 'M282 P'. (Requires SERVO_DETACH_GCODE) @@ -965,6 +966,10 @@ private: static void M261(); #endif + #if ENABLED(I2C_SCANNER) + static void M265(); + #endif + #if HAS_SERVOS static void M280(); #if ENABLED(EDITABLE_SERVO_ANGLES) diff --git a/Marlin/src/inc/Conditionals-5-post.h b/Marlin/src/inc/Conditionals-5-post.h index e0c38ad7b6..3d248f7b29 100644 --- a/Marlin/src/inc/Conditionals-5-post.h +++ b/Marlin/src/inc/Conditionals-5-post.h @@ -3651,7 +3651,7 @@ #endif // Flag whether hex_print.cpp is needed -#if ANY(AUTO_BED_LEVELING_UBL, M100_FREE_MEMORY_WATCHER, DEBUG_GCODE_PARSER, TMC_DEBUG, MARLIN_DEV_MODE, DEBUG_CARDREADER, M20_TIMESTAMP_SUPPORT, HAS_STM32_UID) +#if ANY(AUTO_BED_LEVELING_UBL, M100_FREE_MEMORY_WATCHER, DEBUG_GCODE_PARSER, TMC_DEBUG, MARLIN_DEV_MODE, DEBUG_CARDREADER, M20_TIMESTAMP_SUPPORT, HAS_STM32_UID, I2C_SCANNER) #define NEED_HEX_PRINT 1 #endif diff --git a/buildroot/tests/DUE b/buildroot/tests/DUE index 33f6bd511a..448da6f9d4 100755 --- a/buildroot/tests/DUE +++ b/buildroot/tests/DUE @@ -39,7 +39,7 @@ exec_test $1 $2 "RAMPS4DUE_EFB with ABL (Bilinear), ExtUI, S-Curve, many options restore_configs opt_set MOTHERBOARD BOARD_RADDS Z_DRIVER_TYPE A4988 Z2_DRIVER_TYPE A4988 Z3_DRIVER_TYPE A4988 \ X_MAX_PIN -1 Y_MAX_PIN -1 -opt_enable ENDSTOPPULLUPS BLTOUCH AUTO_BED_LEVELING_BILINEAR \ +opt_enable ENDSTOPPULLUPS BLTOUCH AUTO_BED_LEVELING_BILINEAR I2C_SCANNER \ Z_STEPPER_AUTO_ALIGN Z_STEPPER_ALIGN_STEPPER_XY Z_SAFE_HOMING exec_test $1 $2 "RADDS with ABL (Bilinear), Triple Z Axis, Z_STEPPER_AUTO_ALIGN, E_DUAL_STEPPER_DRIVERS" "$3" diff --git a/ini/features.ini b/ini/features.ini index 960116e53b..3799c2864c 100644 --- a/ini/features.ini +++ b/ini/features.ini @@ -268,7 +268,8 @@ HAS_FILAMENT_SENSOR = build_src_filter=+ HAS_CUTTER = build_src_filter=+ + HAS_DRIVER_SAFE_POWER_PROTECT = build_src_filter=+ -EXPERIMENTAL_I2CBUS = build_src_filter=+ + +EXPERIMENTAL_I2CBUS = build_src_filter=+ + +I2C_SCANNER = build_src_filter=+ G26_MESH_VALIDATION = build_src_filter=+ ASSISTED_TRAMMING = build_src_filter=+ + HAS_MESH = build_src_filter=+