1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-04-21 20:41:19 +02:00

Compare commits

..

2 Commits

Author SHA1 Message Date
Travis Farmer
e5965043bf
Merge 764380ae5791d04375cc4db9f9cf9665176c4c4c into 818240b3496db750a485e08d06a1be26ebf79e8f 2024-12-29 11:02:14 +00:00
travis-farmer
764380ae57
save current, works for now 2024-12-29 06:02:06 -05:00
2 changed files with 26 additions and 10 deletions

View File

@ -30,13 +30,13 @@ static const byte PAYLOAD_STRING = 2;
************************************************************/ ************************************************************/
// Constructor for EXIO485 // Constructor for EXIO485
EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime) { EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin) {
_serial = &serial; _serial = &serial;
_baud = baud; _baud = baud;
_txPin = txPin; _txPin = txPin;
_busNo = busNo; _busNo = busNo;
_cycleTime = cycleTime * 1000UL; _retryTime = 1000000UL; // 1 second
bufferLength=0; bufferLength=0;
inCommandPayload=PAYLOAD_FALSE; inCommandPayload=PAYLOAD_FALSE;
// Add device to HAL device chain // Add device to HAL device chain
@ -89,10 +89,21 @@ void EXIO485::_loop(unsigned long currentMicros) {
} }
if ( hasTasks()){ // do we have any tasks on the docket if ( hasTasks()){ // do we have any tasks on the docket
_cycleStartTimeA = _currentMicros;
if (CurrentTaskID == -1) CurrentTaskID = getNextTaskId();
if (CurrentTaskID == -1) CurrentTaskID = getNextTaskId();
Task* currentTask = getTaskById(CurrentTaskID); Task* currentTask = getTaskById(CurrentTaskID);
if (currentTask == nullptr) return; // dead task
if (!currentTask->completed && _currentMicros - _cycleStartTimeA >= _retryTime) { // after CRC pass, timer is reset
if (currentTask->currentRetryTimer == 0UL) { // first trigger
currentTask->currentRetryTimer = _currentMicros; // set timer
} else if (_currentMicros - currentTask->currentRetryTimer >= _retryTime) {
currentTask->currentRetryTimer = _currentMicros; // reset timer
currentTask->rxMode = false; // resend data
DIAG(F("EX-IOExplorer485: Fail RX, Retry TX. Task: %d"), CurrentTaskID);
}
}
if (!currentTask->rxMode) { if (!currentTask->rxMode) {
currentTask->crcPassFail = 0; currentTask->crcPassFail = 0;
@ -167,6 +178,7 @@ void EXIO485::_loop(unsigned long currentMicros) {
int nodeTo = received_data[0]; int nodeTo = received_data[0];
if (nodeTo == 0) { // for master. if (nodeTo == 0) { // for master.
flagProc = true; flagProc = true;
} }
} }
@ -258,6 +270,7 @@ void EXIO485::_loop(unsigned long currentMicros) {
break; break;
} }
} }
_cycleStartTimeA = currentMicros; // reset timer so we do not resend data
} }
} }
} }

View File

@ -371,7 +371,7 @@ private:
unsigned long _cycleStartTime = 0; unsigned long _cycleStartTime = 0;
unsigned long _cycleStartTimeA = 0; unsigned long _cycleStartTimeA = 0;
unsigned long _timeoutStart = 0; unsigned long _timeoutStart = 0;
unsigned long _cycleTime; // target time between successive read/write cycles, microseconds unsigned long _retryTime; // target time between successive read/write cycles, microseconds
unsigned long _timeoutPeriod; // timeout on read responses, in microseconds. unsigned long _timeoutPeriod; // timeout on read responses, in microseconds.
unsigned long _currentMicros; // last value of micros() from _loop function. unsigned long _currentMicros; // last value of micros() from _loop function.
unsigned long _postDelay; // delay time after transmission before switching off transmitter (in us) unsigned long _postDelay; // delay time after transmission before switching off transmitter (in us)
@ -418,12 +418,13 @@ struct Task {
int crcPassFail; int crcPassFail;
bool completed; bool completed;
bool processed; bool processed;
unsigned long currentRetryTimer;
}; };
static const int MAX_TASKS = 1000; static const int MAX_TASKS = 100; // we don't want to run out of task slots, but memory?
long taskIDCntr = 1; long taskIDCntr = 1;
long CurrentTaskID = -1; long CurrentTaskID = -1;
int taskResendCount = 0; int taskResendCount = 0;
Task taskBuffer[MAX_TASKS]; // Buffer to hold up to 100 tasks Task taskBuffer[MAX_TASKS];
int currentTaskIndex = 0; int currentTaskIndex = 0;
void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) { void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) {
@ -448,6 +449,7 @@ struct Task {
taskBuffer[emptySlot].gotCallback = false; taskBuffer[emptySlot].gotCallback = false;
taskBuffer[emptySlot].completed = false; taskBuffer[emptySlot].completed = false;
taskBuffer[emptySlot].processed = false; taskBuffer[emptySlot].processed = false;
taskBuffer[emptySlot].currentRetryTimer = 0UL;
taskIDCntr++; taskIDCntr++;
if (taskIDCntr >= 5000000) taskIDCntr = 1; if (taskIDCntr >= 5000000) taskIDCntr = 1;
taskBuffer[emptySlot].taskID = taskIDCntr; taskBuffer[emptySlot].taskID = taskIDCntr;
@ -485,6 +487,7 @@ struct Task {
if (taskBuffer[i].taskID == id) { if (taskBuffer[i].taskID == id) {
taskBuffer[i].completed = true; // completed taskBuffer[i].completed = true; // completed
taskBuffer[i].taskID = -1; // unassigned taskBuffer[i].taskID = -1; // unassigned
taskBuffer[i].currentRetryTimer = 0UL; // stop timer
CurrentTaskID = getNextTaskId(); CurrentTaskID = getNextTaskId();
break; break;
} }
@ -520,8 +523,8 @@ struct Task {
EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM) EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM)
EXIOERR = 0xEF, // Flag we've received an error EXIOERR = 0xEF, // Flag we've received an error
}; };
static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1, int cycleTime=500) { static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1) {
new EXIO485(busNo, serial, baud, txPin, cycleTime); new EXIO485(busNo, serial, baud, txPin);
} }
HardwareSerial* _serial; HardwareSerial* _serial;
int _CommMode = 0; int _CommMode = 0;
@ -599,7 +602,7 @@ struct Task {
} }
protected: protected:
EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime); EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin);
public: public: