Skip to content

Commit

Permalink
Support for Debug Codes - Dnnn (MarlinFirmware#19225)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
  • Loading branch information
2 people authored and vgadreau committed Dec 9, 2020
1 parent 7ec1520 commit dd3fa1e
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 38 deletions.
2 changes: 2 additions & 0 deletions Marlin/src/HAL/AVR/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ void HAL_init();
inline void HAL_clear_reset_source() { MCUSR = 0; }
inline uint8_t HAL_get_reset_source() { return MCUSR; }

inline void HAL_reboot() {} // reboot the board or restart the bootloader

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
extern "C" {
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/DUE/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ void sei(); // Enable interrupts
void HAL_clear_reset_source(); // clear reset reason
uint8_t HAL_get_reset_source(); // get reset reason

inline void HAL_reboot() {} // reboot the board or restart the bootloader

//
// ADC
//
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/ESP32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void HAL_clear_reset_source();
// reset reason
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

void _delay_ms(int delay);

#pragma GCC diagnostic push
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/LINUX/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ uint16_t HAL_adc_get_result();
inline void HAL_clear_reset_source(void) {}
inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }

inline void HAL_reboot() {} // reboot the board or restart the bootloader

/* ---------------- Delay in cycles */
FORCE_INLINE static void DELAY_CYCLES(uint64_t x) {
Clock::delayCycles(x);
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/LPC1768/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255,
void HAL_clear_reset_source(void);
uint8_t HAL_get_reset_source(void);

inline void HAL_reboot() {} // reboot the board or restart the bootloader

// Add strcmp_P if missing
#ifndef strcmp_P
#define strcmp_P(a, b) strcmp((a), (b))
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/SAMD51/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ typedef int8_t pin_t;
void HAL_clear_reset_source(); // clear reset reason
uint8_t HAL_get_reset_source(); // get reset reason

inline void HAL_reboot() {} // reboot the board or restart the bootloader

//
// ADC
//
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/STM32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void HAL_clear_reset_source();
// Reset reason
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

void _delay_ms(const int delay);

extern "C" char* _sbrk(int incr);
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/STM32F1/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void HAL_clear_reset_source();
// Reset reason
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

void _delay_ms(const int delay);

#pragma GCC diagnostic push
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/STM32_F4_F7/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ void HAL_clear_reset_source();
// Reset reason
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

void _delay_ms(const int delay);

/*
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/TEENSY31_32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ void HAL_clear_reset_source();
// Get the reason for the reset
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }

#pragma GCC diagnostic push
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/TEENSY35_36/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void HAL_clear_reset_source();
// Reset reason
uint8_t HAL_get_reset_source();

inline void HAL_reboot() {} // reboot the board or restart the bootloader

FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }

#pragma GCC diagnostic push
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
#define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
#define NUMERIC(a) WITHIN(a, '0', '9')
#define DECIMAL(a) (NUMERIC(a) || a == '.')
#define HEXCHR(a) (NUMERIC(a) ? (a) - '0' : WITHIN(a, 'a', 'f') ? ((a) - 'a' + 10) : WITHIN(a, 'A', 'F') ? ((a) - 'A' + 10) : -1)
#define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
#define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
#define COUNT(a) (sizeof(a)/sizeof(*a))
Expand Down
11 changes: 3 additions & 8 deletions Marlin/src/feature/e_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ class EmergencyParser {

case EP_N:
switch (c) {
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9': case '-': case ' ': break;
case '0' ... '9':
case '-': case ' ': break;
case 'M': state = EP_M; break;
default: state = EP_IGNORE;
}
Expand Down Expand Up @@ -153,10 +151,7 @@ class EmergencyParser {
case EP_M876S:
switch (c) {
case ' ': break;
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9':
case '0' ... '9':
state = EP_M876SN;
M876_reason = (uint8_t)(c - '0');
break;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/control/T.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* Tx Same as T?, but nozzle doesn't have to be preheated. Tc requires a preheated nozzle to finish filament load.
* Tc Load to nozzle after filament was prepared by Tc and nozzle is already heated.
*/
void GcodeSuite::T(const uint8_t tool_index) {
void GcodeSuite::T(const int8_t tool_index) {

DEBUG_SECTION(log_T, "T", DEBUGGING(LEVELING));
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("...(", tool_index, ")");
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {

case 'T': T(parser.codenum); break; // Tn: Tool Change

#if ENABLED(MARLIN_DEV_MODE)
case 'D': D(parser.codenum); break; // Dn: Debug codes
#endif

default:
#if ENABLED(WIFI_CUSTOM_COMMAND)
if (wifi_custom_command(parser.command_ptr)) break;
Expand Down
5 changes: 4 additions & 1 deletion Marlin/src/gcode/gcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
* M995 - Touch screen calibration for TFT display
* M997 - Perform in-application firmware update
* M999 - Restart after being stopped by error
* D... - Custom Development G-code. Add hooks to 'gcode_D.cpp' for developers to test features. (Requires MARLIN_DEV_MODE)
*
* "T" Codes
*
Expand Down Expand Up @@ -408,6 +409,8 @@ class GcodeSuite {

private:

TERN_(MARLIN_DEV_MODE, static void D(const int16_t dcode));

static void G0_G1(TERN_(HAS_FAST_MOVES, const bool fast_move=false));

TERN_(ARC_SUPPORT, static void G2_G3(const bool clockwise));
Expand Down Expand Up @@ -882,7 +885,7 @@ class GcodeSuite {

TERN_(CONTROLLER_FAN_EDITABLE, static void M710());

static void T(const uint8_t tool_index);
static void T(const int8_t tool_index);

};

Expand Down
173 changes: 173 additions & 0 deletions Marlin/src/gcode/gcode_d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
*
*/
#include "../inc/MarlinConfigPre.h"

#if ENABLED(MARLIN_DEV_MODE)

#include "gcode.h"
#include "../module/settings.h"
#include "../libs/hex_print.h"
#include "../HAL/shared/eeprom_if.h"

/**
* Dn: G-code for development and testing
*
* See https://reprap.org/wiki/G-code#D:_Debug_codes
*
* Put whatever else you need here to test ongoing development.
*/
void GcodeSuite::D(const int16_t dcode) {
switch (dcode) {

case -1:
for (;;); // forever

case 0:
HAL_reboot();
break;

case 1: {
// Zero or pattern-fill the EEPROM data
#if ENABLED(EEPROM_SETTINGS)
persistentStore.access_start();
size_t total = persistentStore.capacity();
int pos = 0;
const uint8_t value = 0x0;
while(total--) {
persistentStore.write_data(pos, &value, 1);
}
persistentStore.access_finish();
#else
settings.reset();
settings.save();
#endif
HAL_reboot();
} break;

case 2: { // D2 Read / Write SRAM
#define SRAM_SIZE 8192
uint8_t *pointer = parser.hex_adr_val('A');
uint16_t len = parser.ushortval('C', 1);
uintptr_t addr = (uintptr_t)pointer;
NOMORE(addr, (size_t)(SRAM_SIZE - 1));
NOMORE(len, SRAM_SIZE - addr);
if (parser.seenval('X')) {
// Write the hex bytes after the X
uint16_t val = parser.hex_val('X');
while (len--) {
*pointer = val;
pointer++;
}
}
else {
while (len--) print_hex_byte(*(pointer++));
SERIAL_EOL();
}
} break;

case 3: { // D3 Read / Write EEPROM
uint8_t *pointer = parser.hex_adr_val('A');
uint16_t len = parser.ushortval('C', 1);
uintptr_t addr = (uintptr_t)pointer;
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE size_t(E2END + 1)
#endif
NOMORE(addr, (size_t)(MARLIN_EEPROM_SIZE - 1));
NOMORE(len, MARLIN_EEPROM_SIZE - addr);
if (parser.seenval('X')) {
uint16_t val = parser.hex_val('X');
#if ENABLED(EEPROM_SETTINGS)
persistentStore.access_start();
while(len--) {
int pos = 0;
persistentStore.write_data(pos, (uint8_t *)&val, sizeof(val));
}
SERIAL_EOL();
persistentStore.access_finish();
#else
SERIAL_ECHOLN("NO EEPROM");
#endif
}
else {
while (len--) {
// Read bytes from EEPROM
#if ENABLED(EEPROM_SETTINGS)
persistentStore.access_start();
uint8_t val;
while(len--) {
int pos = 0;
if (!persistentStore.read_data(pos, (uint8_t *)&val, sizeof(val))) {
print_hex_byte(val);
}
}
SERIAL_EOL();
persistentStore.access_finish();
#else
SERIAL_ECHOLN("NO EEPROM");
#endif
}
SERIAL_EOL();
}
} break;

case 4: { // D4 Read / Write PIN
// const uint8_t pin = parser.byteval('P');
// const bool is_out = parser.boolval('F'),
// val = parser.byteval('V', LOW);
if (parser.seenval('X')) {
// TODO: Write the hex bytes after the X
//while (len--) {
//}
}
else {
// while (len--) {
// TODO: Read bytes from EEPROM
// print_hex_byte(eeprom_read_byte(*(adr++));
// }
SERIAL_EOL();
}
} break;

case 5: { // D4 Read / Write onboard Flash
#define FLASH_SIZE 1024
uint8_t *pointer = parser.hex_adr_val('A');
uint16_t len = parser.ushortval('C', 1);
uintptr_t addr = (uintptr_t)pointer;
NOMORE(addr, (size_t)(FLASH_SIZE - 1));
NOMORE(len, FLASH_SIZE - addr);
if (parser.seenval('X')) {
// TODO: Write the hex bytes after the X
//while (len--) {
//}
}
else {
// while (len--) {
// TODO: Read bytes from EEPROM
// print_hex_byte(eeprom_read_byte(adr++));
// }
SERIAL_EOL();
}
} break;
}
}

#endif
Loading

0 comments on commit dd3fa1e

Please sign in to comment.