diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index dc9fbb2ba787..3e9957dc6772 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -815,6 +815,7 @@ void idle(bool no_stepper_sleep/*=false*/) { TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick()); TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick()); TERN_(AUTO_REPORT_POSITION, position_auto_reporter.tick()); + TERN_(BUFFER_MONITORING, queue.auto_report_buffer_statistics()); } #endif diff --git a/Marlin/src/gcode/queue.h b/Marlin/src/gcode/queue.h index ea99ce7a2d54..f4898556e244 100644 --- a/Marlin/src/gcode/queue.h +++ b/Marlin/src/gcode/queue.h @@ -192,6 +192,45 @@ class GCodeQueue { */ static void flush_and_request_resend(const serial_index_t serial_ind); + #if ENABLED(BUFFER_MONITORING) + /** + * Track buffer underruns + */ + static uint32_t command_buffer_underruns; + static bool command_buffer_empty; + static millis_t max_command_buffer_empty_duration; + static millis_t command_buffer_empty_at; + + static uint32_t planner_buffer_underruns; + static bool planner_buffer_empty; + static millis_t max_planner_buffer_empty_duration; + static millis_t planner_buffer_empty_at; + + /** + * Report buffer statistics to the host to be able to detect buffer underruns + * + * Returns "M576 " followed by: + * P Planner space remaining + * B Command buffer space remaining + * PU Number of planner buffer underruns since last report + * PD Max time in ms the planner buffer was empty since last report + * BU Number of command buffer underruns since last report + * BD Max time in ms the command buffer was empty since last report + */ + static void report_buffer_statistics(); + + static uint8_t auto_buffer_report_interval; + static millis_t next_buffer_report_ms; + static void auto_report_buffer_statistics(); + static inline void set_auto_report_interval(uint8_t v) { + NOMORE(v, 60); + auto_buffer_report_interval = v; + next_buffer_report_ms = millis() + 1000UL * v; + } + #endif + +private: + /** * (Re)Set the current line number for the last received command */