Skip to content

Commit

Permalink
pendingPacket overwrite protection busy wait test
Browse files Browse the repository at this point in the history
  • Loading branch information
habazut committed Jan 16, 2025
1 parent 0476b9c commit 14cc2c7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions DCC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,15 @@ void DCC::setAccessory(int address, byte port, bool gate, byte onoff /*= 2*/) {
b[0] = address % 64 + 128;
b[1] = ((((address / 64) % 8) << 4) + (port % 4 << 1) + gate % 2) ^ 0xF8;
if (onoff != 0) {
DIAG(F("DCC::setAccessory(%d,%d,%d) ON"), address, port, gate);
DCCWaveform::mainTrack.schedulePacket(b, 2, 3); // Repeat on packet three times
#if defined(EXRAIL_ACTIVE)
RMFT2::activateEvent(address<<2|port,gate);
#endif
}
if (onoff != 1) {
b[1] &= ~0x08; // set C to 0
DIAG(F("DCC::setAccessory(%d,%d,%d) OFF"), address, port, gate);
DCCWaveform::mainTrack.schedulePacket(b, 2, 3); // Repeat off packet three times
}
}
Expand Down
28 changes: 24 additions & 4 deletions DCCRMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void setDCCBit0Long(rmt_item32_t* item) {
item->duration1 = DCC_0_HALFPERIOD + DCC_0_HALFPERIOD/10;
}

// special short one to trigger scope
void setDCCBit1Short(rmt_item32_t* item) {
item->level0 = 1;
item->duration0 = DCC_1_HALFPERIOD/2;
item->level1 = 0;
item->duration1 = DCC_1_HALFPERIOD/2;
}

void setEOT(rmt_item32_t* item) {
item->val = 0;
}
Expand Down Expand Up @@ -135,11 +143,17 @@ RMTChannel::RMTChannel(pinpair pins, bool isMain) {
// preamble
preambleLen = plen+2; // plen 1 bits, one 0 bit and one EOF marker
preamble = (rmt_item32_t*)malloc(preambleLen*sizeof(rmt_item32_t));
for (byte n=0; n<plen; n++)
setDCCBit1(preamble + n); // preamble bits
#ifdef SCOPE
for (byte n=0; n<plen; n++) {
if (n == 0)
setDCCBit1Short(preamble + n); // start of preamble 1 bit short version
else
setDCCBit1(preamble + n);
}
setDCCBit0Long(preamble + plen); // start of packet 0 bit long version
#else
for (byte n=0; n<plen; n++) {
setDCCBit1(preamble + n); // preamble bits
setDCCBit0(preamble + plen); // start of packet 0 bit normal version
#endif
setEOT(preamble + plen + 1); // EOT marker
Expand Down Expand Up @@ -204,16 +218,22 @@ void RMTChannel::RMTprefill() {
const byte transmitMask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

int RMTChannel::RMTfillData(const byte buffer[], byte byteCount, byte repeatCount=0) {
int ret = 0;
//int RMTChannel::RMTfillData(dccPacket packet) {
// dataReady: Signals to then interrupt routine. It is set when
// we have data in the channel buffer which can be copied out
// to the HW. dataRepeat on the other hand signals back to
// the caller of this function if the data has been sent enough
// times (0 to 3 means 1 to 4 times in total).
if (dataRepeat > 0) // we have still old work to do
return dataRepeat;
ret = dataRepeat;
if (dataReady == true) // the packet is not copied out yet
return 1000;
ret = 1000;
if (buffer[0] == 129) {
DIAG(F("RMTfillData 129 %d %d"), buffer[1], ret);
}
if (ret > 0)
return ret;
if (DATA_LEN(byteCount) > maxDataLen) { // this would overun our allocated memory for data
DIAG(F("Can not convert DCC bytes # %d to DCC bits %d, buffer too small"), byteCount, maxDataLen);
return -1; // something very broken, can not convert packet
Expand Down
7 changes: 7 additions & 0 deletions DCCRMT.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class RMTChannel {
void RMTprefill();
//int RMTfillData(dccPacket packet);
int RMTfillData(const byte buffer[], byte byteCount, byte repeatCount);
inline void waitForDataCopy() {
while(1) {
if (dataReady == false)
break;
//DIAG(F("%d %d"), dataReady, dataRepeat);
} //do nothing and wait for interrupt to happen
};
inline bool busy() {
if (dataRepeat > 0) // we have still old work to do
return true;
Expand Down
7 changes: 7 additions & 0 deletions DCCWaveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ void DCCWaveform::schedulePacket(const byte buffer[], byte byteCount, byte repea
if (byteCount > MAX_PACKET_SIZE) return; // allow for chksum

byte checksum = 0;
if(isMainTrack) {
if (rmtMainChannel != NULL)
rmtMainChannel->waitForDataCopy();
} else {
if (rmtProgChannel != NULL)
rmtProgChannel->waitForDataCopy();
}
for (byte b = 0; b < byteCount; b++) {
checksum ^= buffer[b];
pendingPacket[b] = buffer[b];
Expand Down
2 changes: 1 addition & 1 deletion GITHUB_SHA.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define GITHUB_SHA "c389fe9"
#define GITHUB_SHA "master-packetloss-202501161204Z"
3 changes: 2 additions & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include "StringFormatter.h"

#define VERSION "5.4.0"
#define VERSION "5.4.X"
// 5.4.X - bugfix branch
// 5.4.0 - New version on master
// 5.2.96 - EXRAIL additions XFWD() and XREV()
// 5.2.95 - Release candidate for 5.4
Expand Down

0 comments on commit 14cc2c7

Please sign in to comment.