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

Timer working

And slow wave crap removed
This commit is contained in:
Asbelos 2021-01-26 10:55:46 +00:00
parent a4b63013ba
commit 13593ecf4f
7 changed files with 36 additions and 51 deletions

View File

@ -46,8 +46,8 @@ const byte FN_GROUP_5=0x10;
FSH* DCC::shieldName=NULL; FSH* DCC::shieldName=NULL;
void DCC::begin( FSH * motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver) { void DCC::begin(const FSH * motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver) {
shieldName=motorShieldName; shieldName=(FSH *)motorShieldName;
DIAG(F("<iDCC-EX V-%S / %S / %S G-%S>\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA)); DIAG(F("<iDCC-EX V-%S / %S / %S G-%S>\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA));
// Load stuff from EEprom // Load stuff from EEprom

2
DCC.h
View File

@ -64,7 +64,7 @@ const byte MAX_LOCOS = 50;
class DCC class DCC
{ {
public: public:
static void begin(FSH * motorShieldName, MotorDriver *mainDriver, MotorDriver *progDriver); static void begin(const FSH * motorShieldName, MotorDriver *mainDriver, MotorDriver *progDriver);
static void loop(); static void loop();
// Public DCC API functions // Public DCC API functions

View File

@ -732,10 +732,6 @@ bool DCCEXParser::parseD(Print *stream, int params, int p[])
Diag::WITHROTTLE = onOff; Diag::WITHROTTLE = onOff;
return true; return true;
case HASH_KEYWORD_DCC:
DCCWaveform::setDiagnosticSlowWave(params >= 1 && p[1] == HASH_KEYWORD_SLOW);
return true;
case HASH_KEYWORD_PROGBOOST: case HASH_KEYWORD_PROGBOOST:
DCC::setProgTrackBoost(true); DCC::setProgTrackBoost(true);
return true; return true;

View File

@ -28,50 +28,45 @@
*/ */
#include "DCCTimer.h" #include "DCCTimer.h"
#include "DIAG.h"
const int DCC_SIGNAL_TIME=58; // this is the 58uS DCC 1-bit waveform half-cycle const int DCC_SIGNAL_TIME=58; // this is the 58uS DCC 1-bit waveform half-cycle
const int DCC_SLOW_TIME=58*512; // for <D DCC SLOW> command diagnostics const long CLOCK_CYCLES=(F_CPU / 1000000 * DCC_SIGNAL_TIME) >>1;
INTERRUPT_CALLBACK interruptHandler=0; INTERRUPT_CALLBACK interruptHandler=0;
void DCCTimer::begin(INTERRUPT_CALLBACK callback, bool slow) {
interruptHandler=callback;
// Initialise timer1 to trigger every 58us (DCC_SIGNAL_TIME)
noInterrupts();
#ifdef ARDUINO_ARCH_MEGAAVR #ifdef ARDUINO_ARCH_MEGAAVR
// Arduino unoWifi Rev2 and nanoEvery architectire // Arduino unoWifi Rev2 and nanoEvery architectire
long clockCycles=slow? (14*512) : 14; // guesswork!!!! void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
DIAG(F("\nTimer unoWifi/nanoEvery F_CPU=%l c=%d"),F_CPU,clockCycles); interruptHandler=callback;
TCB0.CCMP = clockCycles; noInterrupts();
TCB0.INTFLAGS = TCB_CAPT_bm; // clear interrupt request flag TCB0.CTRLB = TCB_CNTMODE_INT_gc & ~TCB_CCMPEN_bm; // timer compare mode with output disabled
TCB0.INTCTRL = TCB_CAPT_bm; // Enable the interrupt TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // 8 MHz ~ 0.125 us
TCB0.CNT = 0; TCB0.CCMP = CLOCK_CYCLES -1; // 1 tick less for timer reset
TCB0.CTRLA |= TCB_ENABLE_bm; // start TCB0.INTFLAGS = TCB_CAPT_bm; // clear interrupt request flag
#define ISR_NAME TCB0_INT_vect TCB0.INTCTRL = TCB_CAPT_bm; // Enable the interrupt
TCB0.CNT = 0;
TCB0.CTRLA |= TCB_ENABLE_bm; // start
interrupts();
}
#else // ISR called by timer interrupt every 58uS
ISR(TCB0_INT_vect){
// Arduino nano, uno, mega TCB0.INTFLAGS = TCB_CAPT_bm;
long clockCycles=((F_CPU / 1000000) * (slow? DCC_SLOW_TIME : DCC_SIGNAL_TIME)) >>1; interruptHandler();
DIAG(F("\nTimer nano/uno/mega F_CPU=%l c=%d"),F_CPU,clockCycles); }
TCCR1A = 0;
ICR1 = clockCycles;
TCNT1 = 0;
TCCR1B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1
TIMSK1 = _BV(TOIE1); // Enable Software interrupt
#define ISR_NAME TIMER1_OVF_vect
#endif
interrupts(); #else
} // Arduino nano, uno, mega etc
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
interruptHandler=callback;
noInterrupts();
TCCR1A = 0;
ICR1 = CLOCK_CYCLES;
TCNT1 = 0;
TCCR1B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1
TIMSK1 = _BV(TOIE1); // Enable Software interrupt
interrupts();
}
// ISR called by timer interrupt every 58uS // ISR called by timer interrupt every 58uS
ISR(ISR_NAME) ISR(TIMER1_OVF_vect){ interruptHandler(); }
{
#ifdef ARDUINO_ARCH_MEGAAVR
TCB0.INTFLAGS = TCB_CAPT_bm;
#endif #endif
if (interruptHandler) interruptHandler();
}

View File

@ -6,7 +6,7 @@ typedef void (*INTERRUPT_CALLBACK)();
class DCCTimer { class DCCTimer {
public: public:
static void begin(INTERRUPT_CALLBACK interrupt, bool slow=false); static void begin(INTERRUPT_CALLBACK interrupt);
private: private:
}; };

View File

@ -40,11 +40,6 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) {
DCCTimer::begin(DCCWaveform::interruptHandler); DCCTimer::begin(DCCWaveform::interruptHandler);
} }
void DCCWaveform::setDiagnosticSlowWave(bool slow) {
DCCTimer::begin(DCCWaveform::interruptHandler, slow);
DIAG(F("\nDCC SLOW WAVE %S\n"),slow?F("SET. DO NOT ADD LOCOS TO TRACK"):F("RESET"));
}
void DCCWaveform::loop() { void DCCWaveform::loop() {
mainTrack.checkPowerOverload(); mainTrack.checkPowerOverload();
progTrack.checkPowerOverload(); progTrack.checkPowerOverload();

View File

@ -46,7 +46,6 @@ class DCCWaveform {
public: public:
DCCWaveform( byte preambleBits, bool isMain); DCCWaveform( byte preambleBits, bool isMain);
static void begin(MotorDriver * mainDriver, MotorDriver * progDriver); static void begin(MotorDriver * mainDriver, MotorDriver * progDriver);
static void setDiagnosticSlowWave(bool slow);
static void loop(); static void loop();
static DCCWaveform mainTrack; static DCCWaveform mainTrack;
static DCCWaveform progTrack; static DCCWaveform progTrack;