2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* M100 Free Memory Watcher
|
2016-12-15 16:21:32 +01:00
|
|
|
*
|
2016-03-25 07:19:46 +01:00
|
|
|
* This code watches the free memory block between the bottom of the heap and the top of the stack.
|
|
|
|
* This memory block is initialized and watched via the M100 command.
|
2016-12-15 16:21:32 +01:00
|
|
|
*
|
2016-03-25 07:19:46 +01:00
|
|
|
* M100 I Initializes the free memory block and prints vitals statistics about the area
|
|
|
|
* M100 F Identifies how much of the free memory block remains free and unused. It also
|
|
|
|
* detects and reports any corruption within the free memory block that may have
|
|
|
|
* happened due to errant firmware.
|
|
|
|
* M100 D Does a hex display of the free memory block along with a flag for any errant
|
|
|
|
* data that does not match the expected value.
|
|
|
|
* M100 C x Corrupts x locations within the free memory block. This is useful to check the
|
|
|
|
* correctness of the M100 F and M100 D commands.
|
2016-12-15 16:21:32 +01:00
|
|
|
*
|
2017-03-18 16:15:54 +01:00
|
|
|
* Initial version by Roxy-3D
|
2016-03-25 07:19:46 +01:00
|
|
|
*/
|
2015-10-03 08:08:58 +02:00
|
|
|
#define M100_FREE_MEMORY_DUMPER // Comment out to remove Dump sub-command
|
|
|
|
#define M100_FREE_MEMORY_CORRUPTOR // Comment out to remove Corrupt sub-command
|
2015-07-06 02:42:13 +02:00
|
|
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
2015-08-03 21:30:37 +02:00
|
|
|
#if ENABLED(M100_FREE_MEMORY_WATCHER)
|
2016-07-11 20:44:30 +02:00
|
|
|
extern char* __brkval;
|
2015-07-06 02:42:13 +02:00
|
|
|
extern size_t __heap_start, __heap_end, __flp;
|
2016-07-11 20:44:30 +02:00
|
|
|
extern char __bss_end;
|
2015-07-06 02:42:13 +02:00
|
|
|
|
|
|
|
//
|
2015-07-19 01:59:12 +02:00
|
|
|
// Utility functions used by M100 to get its work done.
|
2015-07-06 02:42:13 +02:00
|
|
|
//
|
|
|
|
|
2017-03-18 16:15:54 +01:00
|
|
|
#include "hex_print_routines.h"
|
|
|
|
|
2016-07-11 20:44:30 +02:00
|
|
|
char* top_of_stack();
|
|
|
|
int how_many_E5s_are_here(char*);
|
2017-04-10 22:20:20 +02:00
|
|
|
int free_memory_is_corrupted(); // int not bool!!!! it will tell us how many blocks of
|
|
|
|
// free memory it found.
|
2015-10-03 08:08:58 +02:00
|
|
|
void gcode_M100() {
|
2016-07-11 20:44:30 +02:00
|
|
|
static bool m100_not_initialized = true;
|
|
|
|
char* sp, *ptr;
|
2015-10-03 08:08:58 +02:00
|
|
|
int i, j, n;
|
|
|
|
//
|
|
|
|
// M100 D dumps the free memory block from __brkval to the stack pointer.
|
|
|
|
// malloc() eats memory from the start of the block and the stack grows
|
|
|
|
// up from the bottom of the block. Solid 0xE5's indicate nothing has
|
|
|
|
// used that memory yet. There should not be anything but 0xE5's within
|
|
|
|
// the block of 0xE5's. If there is, that would indicate memory corruption
|
|
|
|
// probably caused by bad pointers. Any unexpected values will be flagged in
|
|
|
|
// the right hand column to help spotting them.
|
|
|
|
//
|
2017-04-10 22:20:20 +02:00
|
|
|
SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval) );
|
|
|
|
SERIAL_ECHOPAIR("\n__bss_end : 0x", hex_word((uint16_t)&__bss_end));
|
|
|
|
//
|
|
|
|
// With out malloc() we need to be smart and use &__bss_end
|
|
|
|
//
|
|
|
|
ptr = __brkval ? __brkval : &__bss_end;
|
|
|
|
SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr));
|
|
|
|
|
|
|
|
sp = top_of_stack();
|
|
|
|
SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
|
|
|
|
|
2016-07-10 05:11:23 +02:00
|
|
|
#if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
|
|
|
|
if (code_seen('D')) {
|
|
|
|
//
|
|
|
|
// We want to start and end the dump on a nice 16 byte boundry even though
|
|
|
|
// the values we are using are not 16 byte aligned.
|
|
|
|
//
|
2017-04-10 22:20:20 +02:00
|
|
|
ptr = (char*) ((uint16_t) ptr & 0xfff0);
|
|
|
|
sp = (char*) ((uint16_t) sp | 0x000f);
|
|
|
|
|
2016-07-10 05:11:23 +02:00
|
|
|
n = sp - ptr;
|
|
|
|
//
|
|
|
|
// This is the main loop of the Dump command.
|
|
|
|
//
|
|
|
|
while (ptr < sp) {
|
2017-03-29 02:45:54 +02:00
|
|
|
print_hex_word((uint16_t)ptr); // Print the address
|
2016-07-10 05:11:23 +02:00
|
|
|
SERIAL_CHAR(':');
|
|
|
|
for (i = 0; i < 16; i++) { // and 16 data bytes
|
2017-04-10 22:20:20 +02:00
|
|
|
if (i==8)
|
|
|
|
SERIAL_CHAR('-');
|
2017-03-29 02:45:54 +02:00
|
|
|
print_hex_byte(*(ptr + i));
|
2016-06-28 01:29:35 +02:00
|
|
|
SERIAL_CHAR(' ');
|
2016-07-10 05:11:23 +02:00
|
|
|
}
|
|
|
|
SERIAL_CHAR('|'); // now show where non 0xE5's are
|
2017-03-29 02:45:54 +02:00
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
SERIAL_CHAR((*(ptr + i) == (char)0xe5) ? ' ' : '?');
|
2016-07-10 05:11:23 +02:00
|
|
|
SERIAL_EOL;
|
|
|
|
ptr += 16;
|
2017-04-10 22:20:20 +02:00
|
|
|
idle();
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2016-07-10 05:11:23 +02:00
|
|
|
return;
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2016-07-10 05:11:23 +02:00
|
|
|
#endif
|
2015-10-03 08:08:58 +02:00
|
|
|
//
|
|
|
|
// M100 F requests the code to return the number of free bytes in the memory pool along with
|
|
|
|
// other vital statistics that define the memory pool.
|
|
|
|
//
|
|
|
|
if (code_seen('F')) {
|
2017-04-10 22:20:20 +02:00
|
|
|
int max_cnt = -1, block_cnt = 0;
|
|
|
|
uint16_t max_addr=0;
|
2016-07-11 20:44:30 +02:00
|
|
|
ptr = __brkval ? __brkval : &__bss_end;
|
2015-10-03 08:08:58 +02:00
|
|
|
sp = top_of_stack();
|
|
|
|
n = sp - ptr;
|
|
|
|
// Scan through the range looking for the biggest block of 0xE5's we can find
|
|
|
|
for (i = 0; i < n; i++) {
|
2016-07-11 20:44:30 +02:00
|
|
|
if (*(ptr + i) == (char)0xe5) {
|
|
|
|
j = how_many_E5s_are_here(ptr + i);
|
2015-10-03 08:08:58 +02:00
|
|
|
if (j > 8) {
|
|
|
|
SERIAL_ECHOPAIR("Found ", j);
|
2017-03-29 02:45:54 +02:00
|
|
|
SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
|
2017-04-10 22:20:20 +02:00
|
|
|
if (j > max_cnt) {
|
|
|
|
max_cnt = j;
|
|
|
|
max_addr = (uint16_t) ptr + i;
|
|
|
|
}
|
2015-10-03 08:08:58 +02:00
|
|
|
i += j;
|
|
|
|
block_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 22:20:20 +02:00
|
|
|
if (block_cnt > 1) {
|
2016-06-28 01:29:35 +02:00
|
|
|
SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
|
2017-04-10 22:20:20 +02:00
|
|
|
SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
|
|
|
|
SERIAL_ECHOLNPAIR(" bytes big at 0x", hex_word(max_addr));
|
|
|
|
}
|
|
|
|
SERIAL_ECHOLNPAIR("free_memory_is_corrupted() = ", free_memory_is_corrupted());
|
2015-10-03 08:08:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption.
|
|
|
|
// This is useful to check the correctness of the M100 D and the M100 F commands.
|
|
|
|
//
|
2016-07-10 05:11:23 +02:00
|
|
|
#if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
|
|
|
|
if (code_seen('C')) {
|
|
|
|
int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
|
|
|
|
SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
|
|
|
|
ptr += 8;
|
|
|
|
sp = top_of_stack();
|
2017-04-10 22:20:20 +02:00
|
|
|
n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that
|
|
|
|
// has altered the stack.
|
2016-07-10 05:11:23 +02:00
|
|
|
j = n / (x + 1);
|
|
|
|
for (i = 1; i <= x; i++) {
|
|
|
|
*(ptr + (i * j)) = i;
|
2017-03-29 02:45:54 +02:00
|
|
|
SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)(ptr + i * j)));
|
2016-07-10 05:11:23 +02:00
|
|
|
}
|
|
|
|
SERIAL_ECHOLNPGM("\n");
|
|
|
|
return;
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2016-07-10 05:11:23 +02:00
|
|
|
#endif
|
2015-10-03 08:08:58 +02:00
|
|
|
//
|
|
|
|
// M100 I Initializes the free memory pool so it can be watched and prints vital
|
|
|
|
// statistics that define the free memory pool.
|
|
|
|
//
|
2016-07-11 20:44:30 +02:00
|
|
|
if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
|
|
|
|
SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
|
2017-04-10 22:20:20 +02:00
|
|
|
// Repeated M100 with no sub-command will not destroy the
|
|
|
|
// state of the initialized free memory pool.
|
2015-10-03 08:08:58 +02:00
|
|
|
ptr += 8;
|
|
|
|
SERIAL_ECHOLNPGM("\n");
|
2017-04-10 22:20:20 +02:00
|
|
|
n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that
|
2015-10-03 08:08:58 +02:00
|
|
|
// has altered the stack.
|
|
|
|
SERIAL_ECHO(n);
|
|
|
|
SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
|
|
|
|
for (i = 0; i < n; i++)
|
2016-07-11 20:44:30 +02:00
|
|
|
*(ptr + i) = (char)0xe5;
|
2015-10-03 08:08:58 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2016-07-11 20:44:30 +02:00
|
|
|
if (*(ptr + i) != (char)0xe5) {
|
2017-04-10 22:20:20 +02:00
|
|
|
SERIAL_ECHOPAIR("? address : ", hex_word(ptr+i) );
|
|
|
|
SERIAL_ECHOPAIR("=", hex_byte(*(ptr + i)) );
|
2015-10-03 08:08:58 +02:00
|
|
|
SERIAL_ECHOLNPGM("\n");
|
|
|
|
}
|
|
|
|
}
|
2016-07-11 20:44:30 +02:00
|
|
|
m100_not_initialized = false;
|
2015-10-03 08:08:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
2015-07-06 02:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// top_of_stack() returns the location of a variable on its stack frame. The value returned is above
|
|
|
|
// the stack once the function returns to the caller.
|
|
|
|
|
2016-07-11 20:44:30 +02:00
|
|
|
char* top_of_stack() {
|
|
|
|
char x;
|
2015-07-19 01:59:12 +02:00
|
|
|
return &x + 1; // x is pulled on return;
|
2015-07-06 02:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
|
|
|
|
// at the specified location. Having this logic as a function simplifies the search code.
|
|
|
|
//
|
2016-07-11 20:44:30 +02:00
|
|
|
int how_many_E5s_are_here(char* p) {
|
2015-10-03 08:08:58 +02:00
|
|
|
int n;
|
|
|
|
for (n = 0; n < 32000; n++) {
|
2016-07-11 20:44:30 +02:00
|
|
|
if (*(p + n) != (char)0xe5)
|
2015-10-03 08:08:58 +02:00
|
|
|
return n - 1;
|
|
|
|
}
|
|
|
|
return -1;
|
2015-07-06 02:42:13 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 22:20:20 +02:00
|
|
|
|
|
|
|
int free_memory_is_corrupted() {
|
|
|
|
char *sp, *ptr;
|
|
|
|
int block_cnt = 0, i, j, n;
|
|
|
|
|
|
|
|
ptr = __brkval ? __brkval : &__bss_end;
|
|
|
|
sp = top_of_stack();
|
|
|
|
|
|
|
|
n = sp - ptr;
|
|
|
|
|
|
|
|
// Scan through the range looking for the biggest block of 0xE5's we can find
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (*(ptr + i) == (char)0xe5) {
|
|
|
|
j = how_many_E5s_are_here(ptr + i);
|
|
|
|
if (j > 8) {
|
|
|
|
// SERIAL_ECHOPAIR("Found ", j);
|
|
|
|
// SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
|
|
|
|
|
|
|
|
i += j;
|
|
|
|
block_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (block_cnt > 1) {
|
|
|
|
// SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
|
|
|
|
// SERIAL_ECHOLNPAIR("\nLargest free block is ", max_cnt);
|
|
|
|
// }
|
|
|
|
return block_cnt;
|
|
|
|
}
|
|
|
|
|
2015-07-06 02:42:13 +02:00
|
|
|
#endif
|
|
|
|
|