2020-06-28 08:28:08 +02:00
|
|
|
/*
|
|
|
|
* main.cpp
|
|
|
|
*
|
|
|
|
* This file is part of CommandStation-DCC.
|
|
|
|
*
|
|
|
|
* CommandStation-DCC is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* CommandStation-DCC is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation-DCC. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-05-25 09:43:34 +02:00
|
|
|
#include <Arduino.h>
|
2020-08-14 19:59:03 +02:00
|
|
|
#include <DCC-EX-Lib.h>
|
2020-05-25 12:56:03 +02:00
|
|
|
#include <ArduinoTimers.h>
|
2020-05-25 09:43:34 +02:00
|
|
|
|
2020-06-28 08:28:08 +02:00
|
|
|
#include "Config.h"
|
2020-07-30 01:03:09 +02:00
|
|
|
#include "FreeMemory.h"
|
|
|
|
|
2020-07-31 02:27:04 +02:00
|
|
|
#if defined(ARDUINO_ARCH_AVR)
|
|
|
|
int ramLowWatermark = 32767;
|
|
|
|
#else
|
2020-07-30 01:17:34 +02:00
|
|
|
int ramLowWatermark = 256000;
|
2020-07-31 02:27:04 +02:00
|
|
|
#endif
|
2020-06-28 08:28:08 +02:00
|
|
|
|
2020-06-12 03:30:45 +02:00
|
|
|
const uint8_t kIRQmicros = 29;
|
|
|
|
const uint8_t kNumLocos = 50;
|
2020-06-03 00:37:58 +02:00
|
|
|
|
2020-07-18 14:18:06 +02:00
|
|
|
#if defined CONFIG_WSM_FIREBOX_MK1
|
|
|
|
DCCMain* mainTrack = DCCMain::Create_WSM_FireBox_MK1_Main(kNumLocos);
|
|
|
|
DCCService* progTrack = DCCService::Create_WSM_FireBox_MK1_Prog();
|
|
|
|
#elif defined CONFIG_WSM_FIREBOX_MK1S
|
|
|
|
DCCMain* mainTrack = DCCMain::Create_WSM_FireBox_MK1S_Main(kNumLocos);
|
|
|
|
DCCService* progTrack = DCCService::Create_WSM_FireBox_MK1S_Prog();
|
2020-06-28 08:28:08 +02:00
|
|
|
#elif defined CONFIG_ARDUINO_MOTOR_SHIELD
|
|
|
|
DCCMain* mainTrack = DCCMain::Create_Arduino_L298Shield_Main(kNumLocos);
|
|
|
|
DCCService* progTrack = DCCService::Create_Arduino_L298Shield_Prog();
|
|
|
|
#elif defined CONFIG_POLOLU_MOTOR_SHIELD
|
2020-06-12 03:30:45 +02:00
|
|
|
DCCMain* mainTrack = DCCMain::Create_Pololu_MC33926Shield_Main(kNumLocos);
|
2020-06-11 21:24:00 +02:00
|
|
|
DCCService* progTrack = DCCService::Create_Pololu_MC33926Shield_Prog();
|
2020-07-30 01:03:09 +02:00
|
|
|
#else
|
|
|
|
#error "Cannot compile - no board selected in Config.h"
|
2020-06-28 08:28:08 +02:00
|
|
|
#endif
|
2020-05-25 09:43:34 +02:00
|
|
|
|
2020-05-30 23:47:12 +02:00
|
|
|
void waveform_IrqHandler() {
|
2020-07-15 03:04:55 +02:00
|
|
|
bool mainInterrupt = mainTrack->interrupt1();
|
|
|
|
bool progInterrupt = progTrack->interrupt1();
|
|
|
|
if(mainInterrupt) mainTrack->interrupt2();
|
|
|
|
if(progInterrupt) progTrack->interrupt2();
|
2020-05-25 09:43:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 23:47:12 +02:00
|
|
|
#if defined(ARDUINO_ARCH_SAMD)
|
2020-06-02 00:31:23 +02:00
|
|
|
void SERCOM4_Handler()
|
2020-05-30 23:47:12 +02:00
|
|
|
{
|
2020-06-11 21:24:00 +02:00
|
|
|
mainTrack->railcom.getSerial()->IrqHandler();
|
2020-05-30 23:47:12 +02:00
|
|
|
}
|
2020-07-18 14:18:06 +02:00
|
|
|
#elif defined(ARDUINO_ARCH_SAMC)
|
|
|
|
void SERCOM0_Handler()
|
|
|
|
{
|
|
|
|
mainTrack->railcom.getSerial()->IrqHandler();
|
|
|
|
}
|
2020-05-30 23:47:12 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-25 09:43:34 +02:00
|
|
|
void setup() {
|
2020-06-12 03:30:45 +02:00
|
|
|
mainTrack->setup();
|
|
|
|
progTrack->setup();
|
2020-06-03 00:37:58 +02:00
|
|
|
|
2020-06-11 21:24:00 +02:00
|
|
|
// TimerA is TCC0 on SAMD21, Timer1 on MEGA2560, and Timer1 on MEGA328
|
|
|
|
// We will fire an interrupt every 29us to generate the signal on the track
|
|
|
|
TimerA.initialize();
|
2020-06-12 03:30:45 +02:00
|
|
|
TimerA.setPeriod(kIRQmicros);
|
2020-06-11 21:24:00 +02:00
|
|
|
TimerA.attachInterrupt(waveform_IrqHandler);
|
|
|
|
TimerA.start();
|
2020-05-26 11:16:05 +02:00
|
|
|
|
2020-07-18 14:18:06 +02:00
|
|
|
mainTrack->hdw.config_setTrackPowerCallback(DCCEXParser::trackPowerCallback);
|
|
|
|
progTrack->hdw.config_setTrackPowerCallback(DCCEXParser::trackPowerCallback);
|
|
|
|
|
2020-07-30 01:03:09 +02:00
|
|
|
// Register the serial interface
|
2020-05-26 11:16:05 +02:00
|
|
|
#if defined (ARDUINO_ARCH_SAMD)
|
2020-06-12 03:30:45 +02:00
|
|
|
CommManager::registerInterface(new USBInterface(SerialUSB));
|
2020-07-30 01:03:09 +02:00
|
|
|
while(!SerialUSB) {} // Wait for USB to come online (remove once wifi is implemented)
|
2020-06-11 21:24:00 +02:00
|
|
|
Wire.begin(); // Needed for EEPROM to work
|
2020-07-31 02:27:04 +02:00
|
|
|
EEStore::init(&SerialUSB);
|
2020-07-18 14:18:06 +02:00
|
|
|
#elif defined (ARDUINO_ARCH_SAMC)
|
|
|
|
CommManager::registerInterface(new SerialInterface(Serial));
|
|
|
|
Wire.begin(); // Needed for EEPROM to work
|
2020-07-31 02:27:04 +02:00
|
|
|
EEStore::init(&Serial);
|
2020-08-15 08:11:28 +02:00
|
|
|
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
2020-06-12 03:30:45 +02:00
|
|
|
CommManager::registerInterface(new SerialInterface(Serial));
|
2020-07-31 02:27:04 +02:00
|
|
|
EEStore::init(&Serial);
|
2020-05-25 09:43:34 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-31 02:27:04 +02:00
|
|
|
|
2020-05-26 11:16:05 +02:00
|
|
|
|
2020-06-12 03:30:45 +02:00
|
|
|
// Set up the string parser to accept commands from the interfaces
|
|
|
|
DCCEXParser::init(mainTrack, progTrack);
|
|
|
|
|
2020-06-11 21:24:00 +02:00
|
|
|
CommManager::showInitInfo();
|
2020-05-25 09:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2020-06-11 21:24:00 +02:00
|
|
|
CommManager::update();
|
|
|
|
mainTrack->loop();
|
|
|
|
progTrack->loop();
|
2020-07-30 01:03:09 +02:00
|
|
|
|
|
|
|
int freeNow=freeMemory();
|
|
|
|
if (freeNow<ramLowWatermark) {
|
|
|
|
ramLowWatermark=freeNow;
|
2020-07-31 02:27:04 +02:00
|
|
|
CommManager::broadcast(F("\nFree RAM=%d\n"),ramLowWatermark);
|
2020-07-30 01:03:09 +02:00
|
|
|
}
|
2020-05-30 11:03:19 +02:00
|
|
|
}
|