Skip to content

Commit

Permalink
ensure master clock resynchronisation doesn't cause spurious cycle du…
Browse files Browse the repository at this point in the history
…ration measurements
  • Loading branch information
ZXGuesser committed May 12, 2024
1 parent 2cc5518 commit a8cfbf0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
12 changes: 7 additions & 5 deletions packetmag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ PacketMag::PacketMag(uint8_t mag, std::list<TTXPageStream>* pageSet, ttx::Config
_cycleDuration(-1)
{
//ctor
_lastCycle = {0,0};
_lastCycleTimestamp = {0,0};

for (int i=0;i<MAXPACKET29TYPES;i++)
{
Expand Down Expand Up @@ -160,12 +160,12 @@ Packet* PacketMag::GetPacket(Packet* p)
// get master clock singleton
MasterClock *mc = mc->Instance();
MasterClock::timeStruct t = mc->GetMasterClock();
if (_lastCycle.seconds){ // wait for real timestamps
int diffSeconds = difftime(t.seconds, _lastCycle.seconds); // truncates double to int
_cycleDuration = ((diffSeconds * 50) - _lastCycle.fields) + t.fields;
if (_lastCycleTimestamp.seconds){ // wait for real timestamps
int diffSeconds = difftime(t.seconds, _lastCycleTimestamp.seconds); // truncates double to int
_cycleDuration = ((diffSeconds * 50) - _lastCycleTimestamp.fields) + t.fields;
_debug->SetMagCycleDuration(_magNumber, _cycleDuration);
}
_lastCycle = t; // update timestamp
_lastCycleTimestamp = t; // update timestamp

// couldn't get a page to send so sent a time filling header
p->Header(_magNumber,0xFF,0x0000,0x8010,_headerTemplate);
Expand Down Expand Up @@ -435,6 +435,8 @@ bool PacketMag::IsReady(bool force)
ClearEvent(EVENT_FIELD);
if (_waitingForField > 0)
{
// _waitingForField is set to 2 when the last packet was a header but field event is not yet cleared
// on the next IsReady we clear the event and decrement it ready to wait for the next field event.
_waitingForField--;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packetmag.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace vbit
bool GetCustomHeaderFlag() { return _hasCustomHeader; };
void DeleteCustomHeader();

void InvalidateCycleTimestamp() { _lastCycleTimestamp = {0,0}; }; // reset cycle duration calculation
int GetCycleDuration() { return _cycleDuration; };

protected:
Expand Down Expand Up @@ -92,7 +93,7 @@ namespace vbit
bool _specialPagesFlipFlop; // toggle to alternate between special pages and normal pages
int _waitingForField;

MasterClock::timeStruct _lastCycle;
MasterClock::timeStruct _lastCycleTimestamp;
int _cycleDuration; // magazine cycle time in fields
};
}
Expand Down
7 changes: 5 additions & 2 deletions service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Service::Service(Configure *configure, vbit::Debug *debug, PageList *pageList, P
_packetServer(packetServer),
_fieldCounter(49) // roll over immediately
{
vbit::PacketMag **magList=_pageList->GetMagazines();
_magList=_pageList->GetMagazines();
// Register all the packet sources
for (uint8_t mag=0;mag<8;mag++)
{
vbit::PacketMag* m=magList[mag];
vbit::PacketMag* m=_magList[mag];
m->SetPriority(_configure->GetMagazinePriority(mag)); // set the mags to the desired priorities
_register(m); // use the PacketMags created in pageList rather than duplicating them
}
Expand Down Expand Up @@ -190,6 +190,9 @@ void Service::_updateEvents()
masterClock.seconds = now;

_debug->Log(Debug::LogLevels::logWARN,"[Service::_updateEvents] Resynchronising master clock");

for (int i=0;i<8;i++)
_magList[i]->InvalidateCycleTimestamp(); // reset magazine cycle duration calculations
}

if (masterClock.seconds%15==0) // TODO: how often do we want to trigger sending special packets?
Expand Down
1 change: 1 addition & 0 deletions service.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace ttx
Configure* _configure; /// Member reference to the configuration settings
vbit::Debug* _debug;
PageList* _pageList; /// Member reference to the pages list
vbit::PacketMag** _magList;
PacketServer* _packetServer;

// Member variables for event management
Expand Down

0 comments on commit a8cfbf0

Please sign in to comment.