diff --git a/DCC.cpp b/DCC.cpp index 6d19a26..25d56b5 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -36,6 +36,7 @@ #include "EXRAIL2.h" #include "CommandDistributor.h" #include "TrackManager.h" +#include "DCCTimer.h" // This module is responsible for converting API calls into // messages to be sent to the waveform generator. diff --git a/DCC.h b/DCC.h index 0effee2..db27b17 100644 --- a/DCC.h +++ b/DCC.h @@ -42,12 +42,10 @@ const uint16_t LONG_ADDR_MARKER = 0x4000; // Allocations with memory implications..! // Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created -#if defined(ARDUINO_AVR_UNO) -const byte MAX_LOCOS = 20; -#elif defined(ARDUINO_AVR_NANO) -const byte MAX_LOCOS = 30; -#else +#if defined(HAS_ENOUGH_MEMORY) const byte MAX_LOCOS = 50; +#else +const byte MAX_LOCOS = 30; #endif class DCC @@ -131,31 +129,4 @@ private: static const byte BIT_OFF = 0x00; }; -#ifdef ARDUINO_AVR_MEGA // is using Mega 1280, define as Mega 2560 (pinouts and functionality are identical) -#define ARDUINO_AVR_MEGA2560 -#endif - -#if defined(ARDUINO_AVR_UNO) -#define ARDUINO_TYPE "UNO" -#elif defined(ARDUINO_AVR_NANO) -#define ARDUINO_TYPE "NANO" -#elif defined(ARDUINO_AVR_MEGA2560) -#define ARDUINO_TYPE "MEGA" -#elif defined(ARDUINO_ARCH_MEGAAVR) -#define ARDUINO_TYPE "MEGAAVR" -#elif defined(ARDUINO_TEENSY32) -#define ARDUINO_TYPE "TEENSY32" -#elif defined(ARDUINO_TEENSY35) -#define ARDUINO_TYPE "TEENSY35" -#elif defined(ARDUINO_TEENSY36) -#define ARDUINO_TYPE "TEENSY36" -#elif defined(ARDUINO_TEENSY40) -#define ARDUINO_TYPE "TEENSY40" -#elif defined(ARDUINO_TEENSY41) -#define ARDUINO_TYPE "TEENSY41" -#else -#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560 -#endif - - #endif diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index ae2a109..ab1f122 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -40,15 +40,6 @@ #include "DCCTimer.h" #include -//////////////////////////////////////////////////////////////////////////////// -// -// Figure out if we have enough memory for advanced features -// -#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) -// nope -#else -#define HAS_ENOUGH_MEMORY -#endif // These keywords are used in the <1> command. The number is what you get if you use the keyword as a parameter. // To discover new keyword numbers , use the <$ YOURKEYWORD> command diff --git a/DCCTimer.h b/DCCTimer.h index e4ebf5e..f87d967 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -86,4 +86,35 @@ private: }; +//////////////////////////////////////////////////////////////////////////////// +// Create a cpu type we can share and +// gigure out if we have enough memory for advanced features +// so define HAS_ENOUGH_MEMORY until proved otherwise. +#define HAS_ENOUGH_MEMORY + +#if defined(ARDUINO_AVR_UNO) +#define ARDUINO_TYPE "UNO" +#undef HAS_ENOUGH_MEMORY +#elif defined(ARDUINO_AVR_NANO) +#define ARDUINO_TYPE "NANO" +#undef HAS_ENOUGH_MEMORY +#elif defined(ARDUINO_AVR_MEGA) +#define ARDUINO_TYPE "MEGA" +#elif defined(ARDUINO_AVR_MEGA2560) +#define ARDUINO_TYPE "MEGA" +#elif defined(ARDUINO_ARCH_MEGAAVR) +#define ARDUINO_TYPE "MEGAAVR" +#elif defined(ARDUINO_TEENSY32) +#define ARDUINO_TYPE "TEENSY32" +#elif defined(ARDUINO_TEENSY35) +#define ARDUINO_TYPE "TEENSY35" +#elif defined(ARDUINO_TEENSY36) +#define ARDUINO_TYPE "TEENSY36" +#elif defined(ARDUINO_TEENSY40) +#define ARDUINO_TYPE "TEENSY40" +#elif defined(ARDUINO_TEENSY41) +#define ARDUINO_TYPE "TEENSY41" +#else +#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560 +#endif #endif diff --git a/DCCTimerMEGAAVR.cpp b/DCCTimerMEGAAVR.cpp index 624008a..8b43340 100644 --- a/DCCTimerMEGAAVR.cpp +++ b/DCCTimerMEGAAVR.cpp @@ -96,6 +96,19 @@ extern char *__malloc_heap_start; mac[0] |= 0x02; } +volatile int DCCTimer::minimum_free_memory=__INT_MAX__; + +// Return low memory value... +int DCCTimer::getMinimumFreeMemory() { + noInterrupts(); // Disable interrupts to get volatile value + int retval = minimum_free_memory; + interrupts(); + return retval; +} + +extern char *__brkval; +extern char *__malloc_heap_start; + int DCCTimer::freeMemory() { char top; return __brkval ? &top - __brkval : &top - __malloc_heap_start; diff --git a/MotorDriver.h b/MotorDriver.h index ca30706..0e4af4f 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -103,16 +103,5 @@ class MotorDriver { unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned int power_good_counter = 0; -#if defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) - static bool disableInterrupts() { - uint32_t primask; - __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::); - __disable_irq(); - return (primask == 0) ? true : false; - } - static void enableInterrupts(bool doit) { - if (doit) __enable_irq(); - } -#endif }; #endif diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 82c1064..ace3d16 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -56,6 +56,7 @@ #include "EXRAIL2.h" #include "CommandDistributor.h" #include "TrackManager.h" +#include "DCCTimer.h" #define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco