From 13593ecf4fe4b1aca72ba9d62fb068d822c0df69 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Tue, 26 Jan 2021 10:55:46 +0000 Subject: [PATCH] Timer working And slow wave crap removed --- DCC.cpp | 4 +-- DCC.h | 2 +- DCCEXParser.cpp | 4 --- DCCTimer.cpp | 69 +++++++++++++++++++++++-------------------------- DCCTimer.h | 2 +- DCCWaveform.cpp | 5 ---- DCCWaveform.h | 1 - 7 files changed, 36 insertions(+), 51 deletions(-) diff --git a/DCC.cpp b/DCC.cpp index b0973bc..3876e23 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -46,8 +46,8 @@ const byte FN_GROUP_5=0x10; FSH* DCC::shieldName=NULL; -void DCC::begin( FSH * motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver) { - shieldName=motorShieldName; +void DCC::begin(const FSH * motorShieldName, MotorDriver * mainDriver, MotorDriver* progDriver) { + shieldName=(FSH *)motorShieldName; DIAG(F("\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA)); // Load stuff from EEprom diff --git a/DCC.h b/DCC.h index 7099056..524c05c 100644 --- a/DCC.h +++ b/DCC.h @@ -64,7 +64,7 @@ const byte MAX_LOCOS = 50; class DCC { public: - static void begin(FSH * motorShieldName, MotorDriver *mainDriver, MotorDriver *progDriver); + static void begin(const FSH * motorShieldName, MotorDriver *mainDriver, MotorDriver *progDriver); static void loop(); // Public DCC API functions diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 04546a3..7fb6fa9 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -732,10 +732,6 @@ bool DCCEXParser::parseD(Print *stream, int params, int p[]) Diag::WITHROTTLE = onOff; return true; - case HASH_KEYWORD_DCC: - DCCWaveform::setDiagnosticSlowWave(params >= 1 && p[1] == HASH_KEYWORD_SLOW); - return true; - case HASH_KEYWORD_PROGBOOST: DCC::setProgTrackBoost(true); return true; diff --git a/DCCTimer.cpp b/DCCTimer.cpp index 15409ae..bbec1c1 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -28,50 +28,45 @@ */ #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_SLOW_TIME=58*512; // for command diagnostics +const long CLOCK_CYCLES=(F_CPU / 1000000 * DCC_SIGNAL_TIME) >>1; 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 - // Arduino unoWifi Rev2 and nanoEvery architectire - long clockCycles=slow? (14*512) : 14; // guesswork!!!! - DIAG(F("\nTimer unoWifi/nanoEvery F_CPU=%l c=%d"),F_CPU,clockCycles); - TCB0.CCMP = clockCycles; - TCB0.INTFLAGS = TCB_CAPT_bm; // clear interrupt request flag - TCB0.INTCTRL = TCB_CAPT_bm; // Enable the interrupt - TCB0.CNT = 0; - TCB0.CTRLA |= TCB_ENABLE_bm; // start - #define ISR_NAME TCB0_INT_vect + // Arduino unoWifi Rev2 and nanoEvery architectire + void DCCTimer::begin(INTERRUPT_CALLBACK callback) { + interruptHandler=callback; + noInterrupts(); + TCB0.CTRLB = TCB_CNTMODE_INT_gc & ~TCB_CCMPEN_bm; // timer compare mode with output disabled + TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // 8 MHz ~ 0.125 us + TCB0.CCMP = CLOCK_CYCLES -1; // 1 tick less for timer reset + TCB0.INTFLAGS = TCB_CAPT_bm; // clear interrupt request flag + TCB0.INTCTRL = TCB_CAPT_bm; // Enable the interrupt + TCB0.CNT = 0; + TCB0.CTRLA |= TCB_ENABLE_bm; // start + interrupts(); + } -#else - - // Arduino nano, uno, mega - long clockCycles=((F_CPU / 1000000) * (slow? DCC_SLOW_TIME : DCC_SIGNAL_TIME)) >>1; - 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 + // ISR called by timer interrupt every 58uS + ISR(TCB0_INT_vect){ + TCB0.INTFLAGS = TCB_CAPT_bm; + interruptHandler(); + } - 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(ISR_NAME) -{ -#ifdef ARDUINO_ARCH_MEGAAVR - TCB0.INTFLAGS = TCB_CAPT_bm; + ISR(TIMER1_OVF_vect){ interruptHandler(); } #endif - if (interruptHandler) interruptHandler(); -} diff --git a/DCCTimer.h b/DCCTimer.h index 7f99acc..a388349 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -6,7 +6,7 @@ typedef void (*INTERRUPT_CALLBACK)(); class DCCTimer { public: - static void begin(INTERRUPT_CALLBACK interrupt, bool slow=false); + static void begin(INTERRUPT_CALLBACK interrupt); private: }; diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 90db3ee..7d76e59 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -40,11 +40,6 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) { 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() { mainTrack.checkPowerOverload(); progTrack.checkPowerOverload(); diff --git a/DCCWaveform.h b/DCCWaveform.h index e3c05fd..c193545 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -46,7 +46,6 @@ class DCCWaveform { public: DCCWaveform( byte preambleBits, bool isMain); static void begin(MotorDriver * mainDriver, MotorDriver * progDriver); - static void setDiagnosticSlowWave(bool slow); static void loop(); static DCCWaveform mainTrack; static DCCWaveform progTrack;