Keep "astyled" elements in M100_*.cpp

This commit is contained in:
Scott Lahteine
2015-10-02 23:21:19 -07:00
parent 2973e2a504
commit 7c84ba64a1

View File

@@ -24,7 +24,7 @@
#include "Marlin.h" #include "Marlin.h"
#if ENABLED(M100_FREE_MEMORY_WATCHER) #if ENABLED(M100_FREE_MEMORY_WATCHER)
extern void *__brkval; extern void* __brkval;
extern size_t __heap_start, __heap_end, __flp; extern size_t __heap_start, __heap_end, __flp;
@@ -34,12 +34,12 @@ extern size_t __heap_start, __heap_end, __flp;
float code_value(); float code_value();
long code_value_long(); long code_value_long();
bool code_seen(char ); bool code_seen(char);
void serial_echopair_P(const char *, float ); void serial_echopair_P(const char*, float);
void serial_echopair_P(const char *, double ); void serial_echopair_P(const char*, double);
void serial_echopair_P(const char *, unsigned long ); void serial_echopair_P(const char*, unsigned long);
void serial_echopair_P(const char *, int ); void serial_echopair_P(const char*, int);
void serial_echopair_P(const char *, long ); void serial_echopair_P(const char*, long);
@@ -48,72 +48,64 @@ void serial_echopair_P(const char *, long );
// 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(); unsigned 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(unsigned char*);
void gcode_M100() void gcode_M100() {
{ static int m100_not_initialized = 1;
static int m100_not_initialized=1; unsigned char* sp, *ptr;
unsigned char *sp, *ptr; int i, j, n;
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
// M100 D dumps the free memory block from __brkval to the stack pointer. // up from the bottom of the block. Solid 0xE5's indicate nothing has
// malloc() eats memory from the start of the block and the stack grows // used that memory yet. There should not be anything but 0xE5's within
// up from the bottom of the block. Solid 0xE5's indicate nothing has // the block of 0xE5's. If there is, that would indicate memory corruption
// used that memory yet. There should not be anything but 0xE5's within // probably caused by bad pointers. Any unexpected values will be flagged in
// the block of 0xE5's. If there is, that would indicate memory corruption // the right hand column to help spotting them.
// probably caused by bad pointers. Any unexpected values will be flagged in //
// the right hand column to help spotting them.
//
#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 = (unsigned char*) __brkval;
//
// // 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("\n__brkval : ");
prt_hex_word( (unsigned int) ptr ); prt_hex_word((unsigned int) ptr);
ptr = (unsigned char *) ((unsigned long) ptr & 0xfff0); ptr = (unsigned 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_ECHOPGM("\n"); SERIAL_ECHOPGM("\n");
sp = (unsigned char*)((unsigned long) sp | 0x000f);
sp = (unsigned 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.
// //
while ( ptr < sp ) { while (ptr < sp) {
prt_hex_word( (unsigned int) ptr); // Print the address prt_hex_word((unsigned int) ptr); // Print the address
SERIAL_ECHOPGM(":"); SERIAL_ECHOPGM(":");
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_ECHOPGM(" "); SERIAL_ECHOPGM(" ");
delay(2); delay(2);
} }
SERIAL_ECHO("|"); // now show where non 0xE5's are SERIAL_ECHO("|"); // now show where non 0xE5's are
for(i=0; i<16; i++) { for (i = 0; i < 16; i++) {
delay(2); delay(2);
if ( *(ptr+i)==0xe5) if (*(ptr + i) == 0xe5)
SERIAL_ECHOPGM(" "); SERIAL_ECHOPGM(" ");
else else
SERIAL_ECHOPGM("?"); SERIAL_ECHOPGM("?");
} }
SERIAL_ECHO("\n"); SERIAL_ECHO("\n");
ptr += 16; ptr += 16;
delay(2); delay(2);
} }
@@ -121,101 +113,89 @@ int i, j, n;
return; return;
} }
#endif #endif
//
// // M100 F requests the code to return the number of free bytes in the memory pool along with
// 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.
// other vital statistics that define the memory pool. //
// if (code_seen('F')) {
if ( code_seen('F') ) {
int max_addr = (int) __brkval; int max_addr = (int) __brkval;
int max_cnt = 0; int max_cnt = 0;
int block_cnt = 0; int block_cnt = 0;
ptr = (unsigned char *) __brkval; ptr = (unsigned char*) __brkval;
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++) {
if (*(ptr + i) == (unsigned char) 0xe5) {
for(i=0; i<n; i++) { j = how_many_E5s_are_here((unsigned char*) ptr + i);
if ( *(ptr+i) == (unsigned char) 0xe5) { if (j > 8) {
j = how_many_E5s_are_here( (unsigned char *) ptr+i ); SERIAL_ECHOPAIR("Found ", j);
if ( j>8) {
SERIAL_ECHOPAIR("Found ", j );
SERIAL_ECHOPGM(" bytes free at 0x"); SERIAL_ECHOPGM(" bytes free at 0x");
prt_hex_word( (int) ptr+i ); prt_hex_word((int) ptr + i);
SERIAL_ECHOPGM("\n"); SERIAL_ECHOPGM("\n");
i += j; i += j;
block_cnt++; block_cnt++;
} }
if ( j>max_cnt) { // We don't do anything with this information yet if (j > max_cnt) { // We don't do anything with this information yet
max_cnt = j; // but we do know where the biggest free memory block is. max_cnt = j; // but we do know where the biggest free memory block is.
max_addr = (int) ptr+i; max_addr = (int) ptr + i;
} }
} }
} }
if (block_cnt>1) if (block_cnt > 1)
SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.\n"); SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.\n");
SERIAL_ECHO("\nDone.\n"); SERIAL_ECHO("\nDone.\n");
return; return;
} }
// //
// M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption. // 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. // This is useful to check the correctness of the M100 D and the M100 F commands.
// //
#if ENABLED(M100_FREE_MEMORY_CORRUPTOR) #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
if ( code_seen('C') ) { if (code_seen('C')) {
int x; // x gets the # of locations to corrupt within the memory pool int x; // x gets the # of locations to corrupt within the memory pool
x = code_value(); x = code_value();
SERIAL_ECHOLNPGM("Corrupting free memory block.\n"); SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
ptr = (unsigned char *) __brkval; ptr = (unsigned char*) __brkval;
SERIAL_ECHOPAIR("\n__brkval : ",(long) ptr ); SERIAL_ECHOPAIR("\n__brkval : ", (long) ptr);
ptr += 8; ptr += 8;
sp = top_of_stack(); sp = top_of_stack();
SERIAL_ECHOPAIR("\nStack Pointer : ",(long) sp ); SERIAL_ECHOPAIR("\nStack Pointer : ", (long) sp);
SERIAL_ECHOLNPGM("\n"); SERIAL_ECHOLNPGM("\n");
n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
// has altered the stack. // has altered the stack.
j = n / (x+1); j = n / (x + 1);
for(i=1; i<=x; i++) { for (i = 1; i <= x; i++) {
*(ptr+(i*j)) = i; *(ptr + (i * j)) = i;
SERIAL_ECHO("\nCorrupting address: 0x"); SERIAL_ECHO("\nCorrupting address: 0x");
prt_hex_word( (unsigned int) (ptr+(i*j)) ); prt_hex_word((unsigned int)(ptr + (i * j)));
} }
SERIAL_ECHOLNPGM("\n"); SERIAL_ECHOLNPGM("\n");
return; return;
} }
#endif #endif
//
// // M100 I Initializes the free memory pool so it can be watched and prints vital
// M100 I Initializes the free memory pool so it can be watched and prints vital // statistics that define the free memory pool.
// statistics that define the free memory pool. //
// 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 = (unsigned char*) __brkval; // Repeated M100 with no sub-command will not destroy the
SERIAL_ECHOPAIR("\n__brkval : ",(long) ptr ); // state of the initialized free memory pool. SERIAL_ECHOPAIR("\n__brkval : ", (long) ptr); // state of the initialized free memory pool.
ptr += 8; ptr += 8;
sp = top_of_stack(); sp = top_of_stack();
SERIAL_ECHOPAIR("\nStack Pointer : ",(long) sp ); SERIAL_ECHOPAIR("\nStack Pointer : ", (long) sp);
SERIAL_ECHOLNPGM("\n"); SERIAL_ECHOLNPGM("\n");
n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
// has altered the stack. // has altered the stack.
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) = (unsigned char) 0xe5; for (i = 0; i < n; i++) {
if (*(ptr + i) != (unsigned char) 0xe5) {
for(i=0; i<n; i++) { SERIAL_ECHOPAIR("? address : ", (unsigned long) ptr + i);
if ( *(ptr+i) != (unsigned char) 0xe5 ) { SERIAL_ECHOPAIR("=", *(ptr + i));
SERIAL_ECHOPAIR("? address : ", (unsigned long) ptr+i );
SERIAL_ECHOPAIR("=", *(ptr+i) );
SERIAL_ECHOLNPGM("\n"); SERIAL_ECHOLNPGM("\n");
} }
} }
@@ -229,7 +209,7 @@ int i, j, n;
// 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() { unsigned char* top_of_stack() {
unsigned char x; unsigned char x;
return &x + 1; // x is pulled on return; return &x + 1; // x is pulled on return;
} }
@@ -238,37 +218,32 @@ unsigned char *top_of_stack() {
// 3 support routines to print hex numbers. We can print a nibble, byte and word // 3 support routines to print hex numbers. We can print a nibble, byte and word
// //
void prt_hex_nibble( unsigned int n ) void prt_hex_nibble(unsigned int n) {
{ if (n <= 9)
if ( n <= 9 )
SERIAL_ECHO(n); SERIAL_ECHO(n);
else else
SERIAL_ECHO( (char) ('A'+n-10) ); SERIAL_ECHO((char)('A' + n - 10));
delay(2); delay(2);
} }
void prt_hex_byte(unsigned int b) void prt_hex_byte(unsigned int b) {
{ prt_hex_nibble((b & 0xf0) >> 4);
prt_hex_nibble( ( b & 0xf0 ) >> 4 ); prt_hex_nibble(b & 0x0f);
prt_hex_nibble( b & 0x0f );
} }
void prt_hex_word(unsigned int w) void prt_hex_word(unsigned int w) {
{ prt_hex_byte((w & 0xff00) >> 8);
prt_hex_byte( ( w & 0xff00 ) >> 8 ); prt_hex_byte(w & 0x0ff);
prt_hex_byte( w & 0x0ff );
} }
// 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(unsigned char* p) {
{ int n;
int n; for (n = 0; n < 32000; n++) {
if (*(p + n) != (unsigned char) 0xe5)
for(n=0; n<32000; n++) { return n - 1;
if ( *(p+n) != (unsigned char) 0xe5)
return n-1;
} }
return -1; return -1;
} }