Merge pull request #4272 from AnHardt/m100-rep

Repair M100
This commit is contained in:
Scott Lahteine 2016-07-11 18:45:13 -07:00 committed by GitHub
commit 6a45aad893

View File

@ -43,23 +43,23 @@
#include "Marlin.h" #include "Marlin.h"
#if ENABLED(M100_FREE_MEMORY_WATCHER) #if ENABLED(M100_FREE_MEMORY_WATCHER)
extern void* __brkval; extern char* __brkval;
extern size_t __heap_start, __heap_end, __flp; extern size_t __heap_start, __heap_end, __flp;
extern char __bss_end;
// //
// Utility functions used by M100 to get its work done. // Utility functions used by M100 to get its work done.
// //
unsigned char* top_of_stack(); char* top_of_stack();
void prt_hex_nibble(unsigned int); void prt_hex_nibble(unsigned int);
void prt_hex_byte(unsigned int); void prt_hex_byte(unsigned int);
void prt_hex_word(unsigned int); void prt_hex_word(unsigned int);
int how_many_E5s_are_here(unsigned char*); int how_many_E5s_are_here(char*);
void gcode_M100() { void gcode_M100() {
static int m100_not_initialized = 1; static bool m100_not_initialized = true;
unsigned char* sp, *ptr; char* sp, *ptr;
int i, j, n; int i, j, n;
// //
// M100 D dumps the free memory block from __brkval to the stack pointer. // M100 D dumps the free memory block from __brkval to the stack pointer.
@ -72,19 +72,19 @@ void gcode_M100() {
// //
#if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
if (code_seen('D')) { if (code_seen('D')) {
ptr = (unsigned char*) __brkval; ptr = __brkval ? __brkval : &__bss_end;
// //
// We want to start and end the dump on a nice 16 byte boundry even though // 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. // the values we are using are not 16 byte aligned.
// //
SERIAL_ECHOPGM("\n__brkval : "); SERIAL_ECHOPGM("\nbss_end : ");
prt_hex_word((unsigned int) ptr); prt_hex_word((unsigned int) ptr);
ptr = (unsigned char*)((unsigned long) ptr & 0xfff0); ptr = (char*)((unsigned long) ptr & 0xfff0);
sp = top_of_stack(); sp = top_of_stack();
SERIAL_ECHOPGM("\nStack Pointer : "); SERIAL_ECHOPGM("\nStack Pointer : ");
prt_hex_word((unsigned int) sp); prt_hex_word((unsigned int) sp);
SERIAL_EOL; SERIAL_EOL;
sp = (unsigned char*)((unsigned long) sp | 0x000f); sp = (char*)((unsigned long) sp | 0x000f);
n = sp - ptr; n = sp - ptr;
// //
// This is the main loop of the Dump command. // This is the main loop of the Dump command.
@ -95,21 +95,17 @@ void gcode_M100() {
for (i = 0; i < 16; i++) { // and 16 data bytes for (i = 0; i < 16; i++) { // and 16 data bytes
prt_hex_byte(*(ptr + i)); prt_hex_byte(*(ptr + i));
SERIAL_CHAR(' '); SERIAL_CHAR(' ');
delay(2);
} }
SERIAL_CHAR('|'); // now show where non 0xE5's are SERIAL_CHAR('|'); // now show where non 0xE5's are
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {
delay(2); if (*(ptr + i) == (char)0xe5)
if (*(ptr + i) == 0xe5)
SERIAL_CHAR(' '); SERIAL_CHAR(' ');
else else
SERIAL_CHAR('?'); SERIAL_CHAR('?');
} }
SERIAL_EOL; SERIAL_EOL;
ptr += 16; ptr += 16;
delay(2);
} }
SERIAL_ECHOLNPGM("Done.");
return; return;
} }
#endif #endif
@ -119,17 +115,17 @@ void gcode_M100() {
// //
if (code_seen('F')) { if (code_seen('F')) {
#if 0 #if 0
int max_addr = (int) __brkval; int max_addr = (int) __brkval ? __brkval : &__bss_end;
int max_cnt = 0; int max_cnt = 0;
#endif #endif
int block_cnt = 0; int block_cnt = 0;
ptr = (unsigned char*) __brkval; ptr = __brkval ? __brkval : &__bss_end;
sp = top_of_stack(); sp = top_of_stack();
n = sp - ptr; n = sp - ptr;
// Scan through the range looking for the biggest block of 0xE5's we can find // Scan through the range looking for the biggest block of 0xE5's we can find
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (*(ptr + i) == (unsigned char) 0xe5) { if (*(ptr + i) == (char)0xe5) {
j = how_many_E5s_are_here((unsigned char*) ptr + i); j = how_many_E5s_are_here(ptr + i);
if (j > 8) { if (j > 8) {
SERIAL_ECHOPAIR("Found ", j); SERIAL_ECHOPAIR("Found ", j);
SERIAL_ECHOPGM(" bytes free at 0x"); SERIAL_ECHOPGM(" bytes free at 0x");
@ -148,7 +144,6 @@ void gcode_M100() {
} }
if (block_cnt > 1) if (block_cnt > 1)
SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area."); SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
SERIAL_ECHOLNPGM("\nDone.");
return; return;
} }
// //
@ -159,8 +154,8 @@ void gcode_M100() {
if (code_seen('C')) { if (code_seen('C')) {
int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
SERIAL_ECHOLNPGM("Corrupting free memory block.\n"); SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
ptr = (unsigned char*) __brkval; ptr = __brkval ? __brkval : &__bss_end;
SERIAL_ECHOPAIR("\n__brkval : ", ptr); SERIAL_ECHOPAIR("\nbss_end : ", ptr);
ptr += 8; ptr += 8;
sp = top_of_stack(); sp = top_of_stack();
SERIAL_ECHOPAIR("\nStack Pointer : ", sp); SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
@ -183,8 +178,8 @@ void gcode_M100() {
// //
if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time 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. SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
ptr = (unsigned char*) __brkval; // Repeated M100 with no sub-command will not destroy the ptr = __brkval ? __brkval : &__bss_end; // Repeated M100 with no sub-command will not destroy the
SERIAL_ECHOPAIR("\n__brkval : ", ptr); // state of the initialized free memory pool. SERIAL_ECHOPAIR("\nbss_end : ", ptr); // state of the initialized free memory pool.
ptr += 8; ptr += 8;
sp = top_of_stack(); sp = top_of_stack();
SERIAL_ECHOPAIR("\nStack Pointer : ", sp); SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
@ -194,16 +189,15 @@ void gcode_M100() {
SERIAL_ECHO(n); SERIAL_ECHO(n);
SERIAL_ECHOLNPGM(" bytes of memory initialized.\n"); SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
*(ptr + i) = (unsigned char) 0xe5; *(ptr + i) = (char)0xe5;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (*(ptr + i) != (unsigned char) 0xe5) { if (*(ptr + i) != (char)0xe5) {
SERIAL_ECHOPAIR("? address : ", ptr + i); SERIAL_ECHOPAIR("? address : ", ptr + i);
SERIAL_ECHOPAIR("=", *(ptr + i)); SERIAL_ECHOPAIR("=", *(ptr + i));
SERIAL_ECHOLNPGM("\n"); SERIAL_ECHOLNPGM("\n");
} }
} }
m100_not_initialized = 0; m100_not_initialized = false;
SERIAL_ECHOLNPGM("Done.\n");
return; return;
} }
return; return;
@ -212,8 +206,8 @@ void gcode_M100() {
// top_of_stack() returns the location of a variable on its stack frame. The value returned is above // 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. // the stack once the function returns to the caller.
unsigned char* top_of_stack() { char* top_of_stack() {
unsigned char x; char x;
return &x + 1; // x is pulled on return; return &x + 1; // x is pulled on return;
} }
@ -226,7 +220,6 @@ void prt_hex_nibble(unsigned int n) {
SERIAL_ECHO(n); SERIAL_ECHO(n);
else else
SERIAL_ECHO((char)('A' + n - 10)); SERIAL_ECHO((char)('A' + n - 10));
delay(2);
} }
void prt_hex_byte(unsigned int b) { void prt_hex_byte(unsigned int b) {
@ -242,10 +235,10 @@ void prt_hex_word(unsigned int w) {
// how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are // 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. // at the specified location. Having this logic as a function simplifies the search code.
// //
int how_many_E5s_are_here(unsigned char* p) { int how_many_E5s_are_here(char* p) {
int n; int n;
for (n = 0; n < 32000; n++) { for (n = 0; n < 32000; n++) {
if (*(p + n) != (unsigned char) 0xe5) if (*(p + n) != (char)0xe5)
return n - 1; return n - 1;
} }
return -1; return -1;