From 87a82f7c181fffb0994e91deabafb2ee064c0ab2 Mon Sep 17 00:00:00 2001 From: David Cutting Date: Wed, 29 Jul 2020 17:03:09 -0600 Subject: [PATCH] Added free memory detection --- CommandStation-DCC.ino | 19 +++++++++++++++++++ Config.h | 4 ++-- FreeMemory.h | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 FreeMemory.h diff --git a/CommandStation-DCC.ino b/CommandStation-DCC.ino index cd2ef17..036bfac 100644 --- a/CommandStation-DCC.ino +++ b/CommandStation-DCC.ino @@ -22,6 +22,15 @@ #include #include "Config.h" +#include "FreeMemory.h" + +#if defined(ARDUINO_BOARD_UNO) +int ramLowWatermark = 16384; +#elif defined(ARDUINO_BOARD_MEGA_2560) +int ramLowWatermark = 64000; +#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAMC) +int ramLowWatermark = 256000; +#endif const uint8_t kIRQmicros = 29; const uint8_t kNumLocos = 50; @@ -38,6 +47,8 @@ DCCService* progTrack = DCCService::Create_Arduino_L298Shield_Prog(); #elif defined CONFIG_POLOLU_MOTOR_SHIELD DCCMain* mainTrack = DCCMain::Create_Pololu_MC33926Shield_Main(kNumLocos); DCCService* progTrack = DCCService::Create_Pololu_MC33926Shield_Prog(); +#else +#error "Cannot compile - no board selected in Config.h" #endif void waveform_IrqHandler() { @@ -73,8 +84,10 @@ void setup() { mainTrack->hdw.config_setTrackPowerCallback(DCCEXParser::trackPowerCallback); progTrack->hdw.config_setTrackPowerCallback(DCCEXParser::trackPowerCallback); + // Register the serial interface #if defined (ARDUINO_ARCH_SAMD) CommManager::registerInterface(new USBInterface(SerialUSB)); + while(!SerialUSB) {} // Wait for USB to come online (remove once wifi is implemented) Wire.begin(); // Needed for EEPROM to work #elif defined (ARDUINO_ARCH_SAMC) CommManager::registerInterface(new SerialInterface(Serial)); @@ -95,4 +108,10 @@ void loop() { CommManager::update(); mainTrack->loop(); progTrack->loop(); + + int freeNow=freeMemory(); + if (freeNow(sbrk(0)); +#else + return __brkval ? &top - __brkval : &top - __malloc_heap_start; +#endif // __arm__ +} +#endif \ No newline at end of file