mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-25 13:41:23 +01:00
Timer working
And slow wave crap removed
This commit is contained in:
parent
a4b63013ba
commit
13593ecf4f
4
DCC.cpp
4
DCC.cpp
@ -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
2
DCC.h
@ -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
|
||||||
|
@ -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;
|
||||||
|
69
DCCTimer.cpp
69
DCCTimer.cpp
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISR called by timer interrupt every 58uS
|
||||||
|
ISR(TCB0_INT_vect){
|
||||||
|
TCB0.INTFLAGS = TCB_CAPT_bm;
|
||||||
|
interruptHandler();
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
// Arduino nano, uno, mega etc
|
||||||
// Arduino nano, uno, mega
|
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
|
||||||
long clockCycles=((F_CPU / 1000000) * (slow? DCC_SLOW_TIME : DCC_SIGNAL_TIME)) >>1;
|
interruptHandler=callback;
|
||||||
DIAG(F("\nTimer nano/uno/mega F_CPU=%l c=%d"),F_CPU,clockCycles);
|
noInterrupts();
|
||||||
TCCR1A = 0;
|
TCCR1A = 0;
|
||||||
ICR1 = clockCycles;
|
ICR1 = CLOCK_CYCLES;
|
||||||
TCNT1 = 0;
|
TCNT1 = 0;
|
||||||
TCCR1B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1
|
TCCR1B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1
|
||||||
TIMSK1 = _BV(TOIE1); // Enable Software interrupt
|
TIMSK1 = _BV(TOIE1); // Enable Software interrupt
|
||||||
#define ISR_NAME TIMER1_OVF_vect
|
interrupts();
|
||||||
#endif
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
@ -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:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user