2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* This 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.
|
|
|
|
*
|
|
|
|
* It 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. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-24 00:02:54 +02:00
|
|
|
#include <Arduino.h>
|
2020-05-26 13:44:02 +02:00
|
|
|
#include "Hardware.h"
|
2020-05-24 00:02:54 +02:00
|
|
|
#include "DCCWaveform.h"
|
|
|
|
#include "DIAG.h"
|
2020-06-23 18:43:50 +02:00
|
|
|
|
2020-07-02 20:13:08 +02:00
|
|
|
DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true, (int)(MAIN_MAX_MILLIAMPS / MAIN_SENSE_FACTOR));
|
|
|
|
DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false, (int)(PROG_MAX_MILLIAMPS / PROG_SENSE_FACTOR));
|
2020-07-02 18:54:09 +02:00
|
|
|
|
|
|
|
const int ACK_MIN_PULSE_RAW=65 / PROG_SENSE_FACTOR;
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-07-12 01:11:30 +02:00
|
|
|
bool DCCWaveform::progTrackSyncMain=false;
|
2020-06-06 12:11:03 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
void DCCWaveform::begin() {
|
2020-05-26 13:44:02 +02:00
|
|
|
Hardware::init();
|
|
|
|
Hardware::setCallback(58, interruptHandler);
|
|
|
|
mainTrack.beginTrack();
|
|
|
|
progTrack.beginTrack();
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
void DCCWaveform::loop() {
|
|
|
|
mainTrack.checkPowerOverload();
|
|
|
|
progTrack.checkPowerOverload();
|
|
|
|
}
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
|
|
|
|
// static //
|
|
|
|
void DCCWaveform::interruptHandler() {
|
2020-05-26 13:44:02 +02:00
|
|
|
// call the timer edge sensitive actions for progtrack and maintrack
|
|
|
|
bool mainCall2 = mainTrack.interrupt1();
|
|
|
|
bool progCall2 = progTrack.interrupt1();
|
|
|
|
|
|
|
|
// call (if necessary) the procs to get the current bits
|
|
|
|
// these must complete within 50microsecs of the interrupt
|
|
|
|
// but they are only called ONCE PER BIT TRANSMITTED
|
|
|
|
// after the rising edge of the signal
|
|
|
|
if (mainCall2) mainTrack.interrupt2();
|
|
|
|
if (progCall2) progTrack.interrupt2();
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// An instance of this class handles the DCC transmissions for one track. (main or prog)
|
|
|
|
// Interrupts are marshalled via the statics.
|
|
|
|
// A track has a current transmit buffer, and a pending buffer.
|
2020-05-26 13:44:02 +02:00
|
|
|
// When the current buffer is exhausted, either the pending buffer (if there is one waiting) or an idle buffer.
|
2020-05-24 00:02:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
// This bitmask has 9 entries as each byte is trasmitted as a zero + 8 bits.
|
2020-05-26 13:44:02 +02:00
|
|
|
const byte bitMask[] = {0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
|
|
|
|
|
|
|
|
|
2020-07-02 18:54:09 +02:00
|
|
|
DCCWaveform::DCCWaveform( byte preambleBits, bool isMain, int rawCurrentTrip) {
|
2020-05-26 13:44:02 +02:00
|
|
|
// establish appropriate pins
|
2020-07-02 18:54:09 +02:00
|
|
|
rawCurrentTripValue=rawCurrentTrip;
|
2020-05-26 13:44:02 +02:00
|
|
|
isMainTrack = isMain;
|
|
|
|
packetPending = false;
|
|
|
|
memcpy(transmitPacket, idlePacket, sizeof(idlePacket));
|
|
|
|
state = 0;
|
2020-06-21 13:59:58 +02:00
|
|
|
// The +1 below is to allow the preamble generator to create the stop bit
|
|
|
|
// fpr the previous packet.
|
|
|
|
requiredPreambles = preambleBits+1;
|
2020-05-26 13:44:02 +02:00
|
|
|
bytes_sent = 0;
|
|
|
|
bits_sent = 0;
|
2020-06-16 12:06:36 +02:00
|
|
|
sampleDelay = 0;
|
|
|
|
lastSampleTaken = millis();
|
2020-07-02 13:49:35 +02:00
|
|
|
ackPending=false;
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
void DCCWaveform::beginTrack() {
|
|
|
|
setPowerMode(POWERMODE::ON);
|
2020-06-16 12:06:36 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
POWERMODE DCCWaveform::getPowerMode() {
|
2020-05-24 00:02:54 +02:00
|
|
|
return powerMode;
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCWaveform::setPowerMode(POWERMODE mode) {
|
|
|
|
powerMode = mode;
|
|
|
|
Hardware::setPower(isMainTrack, mode == POWERMODE::ON);
|
|
|
|
if (mode == POWERMODE::ON) delay(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
void DCCWaveform::checkPowerOverload() {
|
2020-07-11 10:06:34 +02:00
|
|
|
|
2020-06-16 12:06:36 +02:00
|
|
|
if (millis() - lastSampleTaken < sampleDelay) return;
|
|
|
|
lastSampleTaken = millis();
|
2020-07-12 01:11:30 +02:00
|
|
|
int tripValue= rawCurrentTripValue;
|
|
|
|
if (!isMainTrack && (ackPending || progTrackSyncMain)) tripValue=ACK_CURRENT_TRIP;
|
2020-06-16 12:06:36 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
switch (powerMode) {
|
2020-05-26 13:44:02 +02:00
|
|
|
case POWERMODE::OFF:
|
2020-06-16 12:06:36 +02:00
|
|
|
sampleDelay = POWER_SAMPLE_OFF_WAIT;
|
2020-05-24 00:02:54 +02:00
|
|
|
break;
|
2020-05-26 13:44:02 +02:00
|
|
|
case POWERMODE::ON:
|
|
|
|
// Check current
|
2020-07-02 18:54:09 +02:00
|
|
|
lastCurrent = Hardware::getCurrentRaw(isMainTrack);
|
2020-07-12 01:11:30 +02:00
|
|
|
if (lastCurrent <= tripValue) sampleDelay = POWER_SAMPLE_ON_WAIT;
|
2020-05-24 00:02:54 +02:00
|
|
|
else {
|
|
|
|
setPowerMode(POWERMODE::OVERLOAD);
|
2020-07-02 20:13:08 +02:00
|
|
|
unsigned int mA=Hardware::getCurrentMilliamps(isMainTrack,lastCurrent);
|
2020-07-12 01:11:30 +02:00
|
|
|
unsigned int maxmA=Hardware::getCurrentMilliamps(isMainTrack,tripValue);
|
2020-07-02 18:54:09 +02:00
|
|
|
DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d ***\n"), isMainTrack ? F("MAIN") : F("PROG"), mA, maxmA);
|
2020-06-16 12:06:36 +02:00
|
|
|
sampleDelay = POWER_SAMPLE_OVERLOAD_WAIT;
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-05-26 13:44:02 +02:00
|
|
|
case POWERMODE::OVERLOAD:
|
2020-05-24 00:02:54 +02:00
|
|
|
// Try setting it back on after the OVERLOAD_WAIT
|
|
|
|
setPowerMode(POWERMODE::ON);
|
2020-06-16 12:06:36 +02:00
|
|
|
sampleDelay = POWER_SAMPLE_ON_WAIT;
|
2020-05-24 00:02:54 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-06-16 12:06:36 +02:00
|
|
|
sampleDelay = 999; // cant get here..meaningless statement to avoid compiler warning.
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process time-edge sensitive part of interrupt
|
2020-05-26 13:44:02 +02:00
|
|
|
// return true if second level required
|
2020-05-24 00:02:54 +02:00
|
|
|
bool DCCWaveform::interrupt1() {
|
2020-05-26 13:44:02 +02:00
|
|
|
// NOTE: this must consume transmission buffers even if the power is off
|
|
|
|
// otherwise can cause hangs in main loop waiting for the pendingBuffer.
|
2020-05-24 00:02:54 +02:00
|
|
|
switch (state) {
|
|
|
|
case 0: // start of bit transmission
|
2020-07-12 01:11:30 +02:00
|
|
|
setSignal(HIGH);
|
2020-05-24 00:02:54 +02:00
|
|
|
state = 1;
|
2020-05-24 11:27:33 +02:00
|
|
|
return true; // must call interrupt2 to set currentBit
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-05-24 11:27:33 +02:00
|
|
|
case 1: // 58us after case 0
|
2020-05-24 00:02:54 +02:00
|
|
|
if (currentBit) {
|
2020-07-12 01:11:30 +02:00
|
|
|
setSignal(LOW);
|
2020-05-24 00:02:54 +02:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
else state = 2;
|
|
|
|
break;
|
2020-05-24 11:27:33 +02:00
|
|
|
case 2: // 116us after case 0
|
2020-07-12 01:11:30 +02:00
|
|
|
setSignal(LOW);
|
2020-05-24 00:02:54 +02:00
|
|
|
state = 3;
|
|
|
|
break;
|
2020-05-26 13:44:02 +02:00
|
|
|
case 3: // finished sending zero bit
|
2020-05-24 11:27:33 +02:00
|
|
|
state = 0;
|
2020-05-26 13:44:02 +02:00
|
|
|
break;
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
2020-07-03 18:12:53 +02:00
|
|
|
|
|
|
|
// ACK check is prog track only and will only be checked if
|
|
|
|
// this is not case(0) which needs relatively expensive packet change code to be called.
|
|
|
|
if (ackPending) checkAck();
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
return false;
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
2020-05-24 10:07:54 +02:00
|
|
|
|
2020-07-12 01:11:30 +02:00
|
|
|
void DCCWaveform::setSignal(bool high) {
|
|
|
|
if (progTrackSyncMain) {
|
|
|
|
if (!isMainTrack) return; // ignore PROG track waveform while in sync
|
|
|
|
// set both tracks to same signal
|
|
|
|
Hardware::setSignal(true, high);
|
|
|
|
Hardware::setSignal(false, high);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Hardware::setSignal(isMainTrack,high);
|
|
|
|
}
|
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
void DCCWaveform::interrupt2() {
|
|
|
|
// set currentBit to be the next bit to be sent.
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
if (remainingPreambles > 0 ) {
|
2020-05-26 13:44:02 +02:00
|
|
|
currentBit = true;
|
2020-05-24 00:02:54 +02:00
|
|
|
remainingPreambles--;
|
|
|
|
return;
|
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
// beware OF 9-BIT MASK generating a zero to start each byte
|
|
|
|
currentBit = transmitPacket[bytes_sent] & bitMask[bits_sent];
|
2020-05-24 00:02:54 +02:00
|
|
|
bits_sent++;
|
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
// If this is the last bit of a byte, prepare for the next byte
|
|
|
|
|
|
|
|
if (bits_sent == 9) { // zero followed by 8 bits of a byte
|
|
|
|
//end of Byte
|
|
|
|
bits_sent = 0;
|
|
|
|
bytes_sent++;
|
|
|
|
// if this is the last byte, prepere for next packet
|
|
|
|
if (bytes_sent >= transmitLength) {
|
|
|
|
// end of transmission buffer... repeat or switch to next message
|
|
|
|
bytes_sent = 0;
|
|
|
|
remainingPreambles = requiredPreambles;
|
|
|
|
|
|
|
|
if (transmitRepeats > 0) {
|
|
|
|
transmitRepeats--;
|
|
|
|
}
|
|
|
|
else if (packetPending) {
|
|
|
|
// Copy pending packet to transmit packet
|
|
|
|
for (int b = 0; b < pendingLength; b++) transmitPacket[b] = pendingPacket[b];
|
|
|
|
transmitLength = pendingLength;
|
|
|
|
transmitRepeats = pendingRepeats;
|
|
|
|
packetPending = false;
|
2020-06-06 12:11:03 +02:00
|
|
|
sentResetsSincePacket=0;
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Fortunately reset and idle packets are the same length
|
|
|
|
memcpy( transmitPacket, isMainTrack ? idlePacket : resetPacket, sizeof(idlePacket));
|
|
|
|
transmitLength = sizeof(idlePacket);
|
|
|
|
transmitRepeats = 0;
|
2020-06-07 14:48:42 +02:00
|
|
|
if (sentResetsSincePacket<250) sentResetsSincePacket++;
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 18:12:53 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:43:50 +02:00
|
|
|
|
2020-05-24 00:02:54 +02:00
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
// Wait until there is no packet pending, then make this pending
|
2020-05-24 00:02:54 +02:00
|
|
|
void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repeats) {
|
2020-05-26 13:44:02 +02:00
|
|
|
if (byteCount >= MAX_PACKET_SIZE) return; // allow for chksum
|
|
|
|
while (packetPending);
|
|
|
|
|
|
|
|
byte checksum = 0;
|
|
|
|
for (int b = 0; b < byteCount; b++) {
|
|
|
|
checksum ^= buffer[b];
|
|
|
|
pendingPacket[b] = buffer[b];
|
2020-05-24 00:02:54 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
pendingPacket[byteCount] = checksum;
|
|
|
|
pendingLength = byteCount + 1;
|
|
|
|
pendingRepeats = repeats;
|
|
|
|
packetPending = true;
|
2020-06-07 16:29:09 +02:00
|
|
|
sentResetsSincePacket=0;
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:20:36 +02:00
|
|
|
int DCCWaveform::getLastCurrent() {
|
|
|
|
return lastCurrent;
|
|
|
|
}
|
2020-07-02 13:49:35 +02:00
|
|
|
|
|
|
|
// Operations applicable to PROG track ONLY.
|
|
|
|
// (yes I know I could have subclassed the main track but...)
|
|
|
|
|
|
|
|
void DCCWaveform::setAckBaseline(bool debug) {
|
|
|
|
if (isMainTrack) return;
|
2020-07-02 18:54:09 +02:00
|
|
|
ackThreshold=Hardware::getCurrentRaw(false) + ACK_MIN_PULSE_RAW;
|
|
|
|
if (debug) DIAG(F("\nACK-BASELINE %d/%dmA"),ackThreshold,Hardware::getCurrentMilliamps(false,ackThreshold));
|
2020-07-02 13:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCWaveform::setAckPending(bool debug) {
|
|
|
|
if (isMainTrack) return;
|
|
|
|
(void)debug;
|
|
|
|
ackMaxCurrent=0;
|
|
|
|
ackPulseStart=0;
|
|
|
|
ackPulseDuration=0;
|
|
|
|
ackDetected=false;
|
|
|
|
ackCheckStart=millis();
|
|
|
|
ackPending=true; // interrupt routines will now take note
|
|
|
|
}
|
|
|
|
|
|
|
|
byte DCCWaveform::getAck(bool debug) {
|
|
|
|
if (ackPending) return (2); // still waiting
|
2020-07-02 18:54:09 +02:00
|
|
|
if (debug) DIAG(F("\nACK-%S after %dmS max=%d/%dmA pulse=%duS"),ackDetected?F("OK"):F("FAIL"), ackCheckDuration,
|
|
|
|
ackMaxCurrent,Hardware::getCurrentMilliamps(false,ackMaxCurrent), ackPulseDuration);
|
2020-07-02 13:49:35 +02:00
|
|
|
if (ackDetected) return (1); // Yes we had an ack
|
|
|
|
return(0); // pending set off but not detected means no ACK.
|
|
|
|
}
|
|
|
|
|
|
|
|
void DCCWaveform::checkAck() {
|
|
|
|
// This function operates in interrupt() time so must be fast and can't DIAG
|
|
|
|
|
|
|
|
if (sentResetsSincePacket > 6) { //ACK timeout
|
|
|
|
ackCheckDuration=millis()-ackCheckStart;
|
|
|
|
ackPending = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:54:09 +02:00
|
|
|
lastCurrent=Hardware::getCurrentRaw(false);
|
2020-07-02 13:49:35 +02:00
|
|
|
if (lastCurrent > ackMaxCurrent) ackMaxCurrent=lastCurrent;
|
2020-07-03 18:57:24 +02:00
|
|
|
// An ACK is a pulse lasting between MIN_ACK_PULSE_DURATION and MAX_ACK_PULSE_DURATION uSecs (refer @haba)
|
2020-07-02 13:49:35 +02:00
|
|
|
|
|
|
|
if (lastCurrent>ackThreshold) {
|
|
|
|
if (ackPulseStart==0) ackPulseStart=micros(); // leading edge of pulse detected
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in pulse
|
|
|
|
if (ackPulseStart==0) return; // keep waiting for leading edge
|
|
|
|
|
|
|
|
// detected trailing edge of pulse
|
|
|
|
ackPulseDuration=micros()-ackPulseStart;
|
|
|
|
|
2020-07-03 18:57:24 +02:00
|
|
|
if (ackPulseDuration>=MIN_ACK_PULSE_DURATION && ackPulseDuration<=MAX_ACK_PULSE_DURATION) {
|
2020-07-02 13:49:35 +02:00
|
|
|
ackCheckDuration=millis()-ackCheckStart;
|
|
|
|
ackDetected=true;
|
|
|
|
ackPending=false;
|
|
|
|
transmitRepeats=0; // shortcut remaining repeat packets
|
|
|
|
return; // we have a genuine ACK result
|
|
|
|
}
|
|
|
|
ackPulseStart=0; // We have detected a too-short or too-long pulse so ignore and wait for next leading edge
|
|
|
|
}
|