diff options
author | David Crocker <dcrocker@eschertech.com> | 2021-05-25 01:48:52 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2021-05-25 01:48:52 +0300 |
commit | 2d6866c9de072bf20e790991d93e4f3cae6d19bb (patch) | |
tree | c323c9e69de15db69797bb69b82bd1368f98341e | |
parent | 0dc6a333f2fd6f7156ec1b1586be5d7f01487faf (diff) |
Workaround for SAME70 transmit time stamp issue
-rw-r--r-- | src/CAN/CanInterface.cpp | 48 | ||||
-rw-r--r-- | src/Platform/TaskPriorities.h | 2 |
2 files changed, 31 insertions, 19 deletions
diff --git a/src/CAN/CanInterface.cpp b/src/CAN/CanInterface.cpp index 3b0644fa..484edfe8 100644 --- a/src/CAN/CanInterface.cpp +++ b/src/CAN/CanInterface.cpp @@ -38,9 +38,10 @@ const unsigned int NumCanBuffers = 2 * MaxCanBoards + 10; constexpr uint32_t MaxMotionSendWait = 20; // milliseconds constexpr uint32_t MaxUrgentSendWait = 20; // milliseconds -constexpr uint32_t MaxTimeSyncSendWait = 20; // milliseconds +constexpr uint32_t MaxTimeSyncSendWait = 2; // milliseconds constexpr uint32_t MaxResponseSendWait = 50; // milliseconds constexpr uint32_t MaxRequestSendWait = 50; // milliseconds +constexpr uint16_t MaxTimeSyncDelay = 300; // the maximum normal delay before a can time sync message is sent #define USE_BIT_RATE_SWITCH 0 @@ -63,13 +64,6 @@ static uint32_t lastTimeSent = 0; static uint32_t longestWaitTime = 0; static uint16_t longestWaitMessageType = 0; -// Time sync and transmit message markers -enum TxMarkerType : uint8_t -{ - MarkerNone = 0, // default marker, we don't get callbacks for Tx messages with this marker - MarkerTimeSync // marker for time sync messages -}; - static uint32_t peakTimeSyncTxDelay = 0; static volatile uint16_t timeSyncTxTimeStamp; static volatile bool gotTimeSyncTxTimeStamp = false; @@ -85,6 +79,8 @@ static CanAddress myAddress = CanId::MasterAddress; #endif +static uint8_t currentTimeSyncMarker = 0xFF; + #if SUPPORT_REMOTE_COMMANDS static bool inExpansionMode = false; #endif @@ -210,7 +206,7 @@ static void ReInit() noexcept // This is the function called by the transmit event handler when the message marker is nonzero void TxCallback(uint8_t marker, CanId id, uint16_t timeStamp) noexcept { - if (marker == MarkerTimeSync) + if (marker == currentTimeSyncMarker) { timeSyncTxTimeStamp = timeStamp; gotTimeSyncTxTimeStamp = true; @@ -395,31 +391,35 @@ extern "C" [[noreturn]] void CanClockLoop(void *) noexcept #endif { CanMessageTimeSync * const msg = buf.SetupBroadcastMessage<CanMessageTimeSync>(CanInterface::GetCanAddress()); - buf.marker = MarkerTimeSync; - can0dev->IsSpaceAvailable((CanDevice::TxBufferNumber)TxBufferIndexTimeSync, MaxTimeSyncSendWait); msg->lastTimeSent = lastTimeSent; + msg->lastTimeAcknowledgeDelay = 0; // assume we don't have the transmit delay available + + currentTimeSyncMarker = ((currentTimeSyncMarker + 1) & 0x0F) | 0xA0; + buf.marker = currentTimeSyncMarker; + buf.reportInFifo = 1; - can0dev->PollTxEventFifo(TxCallback); if (gotTimeSyncTxTimeStamp) { # if SAME70 // On the SAME70 the step clock is also the external time stamp counter const uint32_t timeSyncTxDelay = (timeSyncTxTimeStamp - (uint16_t)lastTimeSent) & 0xFFFF; # else - // On the SAME5x the time stamp counter counts CAN but times divided by 64 + // On the SAME5x the time stamp counter counts CAN bit times divided by 64 const uint32_t timeSyncTxDelay = (((timeSyncTxTimeStamp - lastTimeSyncTxPreparedStamp) & 0xFFFF) * CanInterface::GetTimeStampPeriod()) >> 6; # endif if (timeSyncTxDelay > peakTimeSyncTxDelay) { peakTimeSyncTxDelay = timeSyncTxDelay; } - msg->lastTimeAcknowledgeDelay = timeSyncTxDelay; + + // Occasionally on the SAME70 we get very large delays reported. These delays are not genuine. + if (timeSyncTxDelay < MaxTimeSyncDelay) + { + msg->lastTimeAcknowledgeDelay = timeSyncTxDelay; + } gotTimeSyncTxTimeStamp = false; } - else - { - msg->lastTimeAcknowledgeDelay = 0; - } + msg->isPrinting = reprap.GetGCodes().IsReallyPrinting(); // Send the real time just once a second @@ -449,6 +449,18 @@ extern "C" [[noreturn]] void CanClockLoop(void *) noexcept // Delay until it is time again vTaskDelayUntil(&lastWakeTime, CanClockIntervalMillis); + + // Check that the message was sent and get the time stamp + if (can0dev->IsSpaceAvailable((CanDevice::TxBufferNumber)TxBufferIndexTimeSync, 0)) // if the buffer is free already then the message was sent + { + can0dev->PollTxEventFifo(TxCallback); + } + else + { + (void)can0dev->IsSpaceAvailable((CanDevice::TxBufferNumber)TxBufferIndexTimeSync, MaxTimeSyncSendWait); // free the buffer + can0dev->PollTxEventFifo(TxCallback); // empty the fifo + gotTimeSyncTxTimeStamp = false; // ignore any values read from it + } } } diff --git a/src/Platform/TaskPriorities.h b/src/Platform/TaskPriorities.h index 816f1e1a..cc9ab004 100644 --- a/src/Platform/TaskPriorities.h +++ b/src/Platform/TaskPriorities.h @@ -27,9 +27,9 @@ namespace TaskPriority constexpr int LaserPriority = 5; constexpr int CanSenderPriority = 5; constexpr int CanReceiverPriority = 5; - constexpr int CanClockPriority = 5; constexpr int EthernetPriority = 5; constexpr int Accelerometer = 6; + constexpr int CanClockPriority = 7; } #endif /* SRC_TASKPRIORITIES_H_ */ |