1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00

Added free memory detection

This commit is contained in:
David Cutting 2020-07-29 17:03:09 -06:00
parent 29b719ffd9
commit 87a82f7c18
3 changed files with 41 additions and 2 deletions

View File

@ -22,6 +22,15 @@
#include <ArduinoTimers.h>
#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<ramLowWatermark) {
ramLowWatermark=freeNow;
CommManager::printf(F("\nFree RAM=%d\n"),ramLowWatermark);
}
}

View File

@ -22,8 +22,8 @@
// Choose the motor shield that you want to use.
//#define CONFIG_WSM_FIREBOX_MK1
#define CONFIG_WSM_FIREBOX_MK1S
#define CONFIG_WSM_FIREBOX_MK1
//#define CONFIG_WSM_FIREBOX_MK1S
//#define CONFIG_ARDUINO_MOTOR_SHIELD
//#define CONFIG_POLOLU_MOTOR_SHIELD

20
FreeMemory.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef freeMemory_h
#define freeMemory_h
// thanks go to https://github.com/mpflaga/Arduino-MemoryFree
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#else
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
#endif