F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
PassiveRateGroup.cpp
Go to the documentation of this file.
1 /*
2  * \author: Tim Canham
3  * \file:
4  * \brief
5  *
6  * This file implements the PassiveRateGroup component,
7  * which invokes a set of components the comprise the rate group.
8  *
9  * Copyright 2014-2015, by the California Institute of Technology.
10  * ALL RIGHTS RESERVED. United States Government Sponsorship
11  * acknowledged.
12  */
13 
14 #include <Fw/FPrimeBasicTypes.hpp>
15 #include <Fw/Types/Assert.hpp>
16 #include <Os/Console.hpp>
18 #include <config/PassiveRateGroupCfg.hpp>
20 
21 namespace Svc {
24  m_cycles(0),
25  m_maxTime(0),
26  m_portCycleTimeHWMUsec{}, // Zero-initialize atomic array
27  m_rawTimeSource(Os::RAWTIME_DEFAULT),
28  m_numContexts(0) {}
29 
31 
32 Os::RawTime PassiveRateGroup::createRawTime() const {
33  return Os::RawTime(this->m_rawTimeSource);
34 }
35 
36 void PassiveRateGroup::configure(const ContextArray& contexts, const Os::RawTimeSource rawTimeSource) {
38  "Context table size must match the number of rate group member output ports");
39 
40  this->m_numContexts = CONNECTION_COUNT_MAX;
41  // copy context values
42  for (FwIndexType entry = 0; entry < this->m_numContexts; entry++) {
43  this->m_contexts[entry] = contexts[static_cast<FwSizeType>(entry)];
44  }
45 
46  this->m_rawTimeSource = rawTimeSource;
47 
48  // Write initial telemetry so that every channel has a value before the first cycle runs
49  // (ports must already be connected for these writes to be delivered)
51  PassiveRateGroup_CycleTime portCycleTimeHWM;
52  for (FwIndexType port = 0; port < this->getNum_RateGroupMemberOut_OutputPorts(); port++) {
53  portCycleTimeHWM[static_cast<FwSizeType>(port)] =
54  this->m_portCycleTimeHWMUsec[static_cast<FwSizeType>(port)].load(std::memory_order_relaxed);
55  }
56  this->tlmWrite_PortCycleTimeHWM(portCycleTimeHWM);
57  }
58  this->tlmWrite_MaxCycleTime(this->m_maxTime.load(std::memory_order_relaxed));
59  this->tlmWrite_CycleTime(0);
60  this->tlmWrite_CycleCount(this->m_cycles);
61 }
62 
63 void PassiveRateGroup::configure(const U32 contexts[], const FwIndexType numContexts) {
64  FW_ASSERT(contexts != nullptr);
65  FW_ASSERT(numContexts == this->getNum_RateGroupMemberOut_OutputPorts(), static_cast<FwAssertArgType>(numContexts),
66  static_cast<FwAssertArgType>(this->getNum_RateGroupMemberOut_OutputPorts()));
67 
68  ContextArray contextArray;
69  for (FwIndexType entry = 0; entry < numContexts; entry++) {
70  contextArray[static_cast<FwSizeType>(entry)] = static_cast<U32>(contexts[entry]);
71  }
72  this->configure(contextArray);
73 }
74 
75 void PassiveRateGroup::CycleIn_handler(FwIndexType portNum, Os::RawTime& cycleStart) {
76  Os::RawTime endTime = this->createRawTime();
77  FW_ASSERT(this->m_numContexts != 0);
78 
79  // Pre-allocate RawTime objects outside loop to avoid repeated constructor calls
80  Os::RawTime portStart = this->createRawTime();
81  Os::RawTime portEnd = this->createRawTime();
82  PassiveRateGroup_CycleTime portTimes;
83 
84  // invoke any members of the rate group
85  for (FwIndexType port = 0; port < this->getNum_RateGroupMemberOut_OutputPorts(); port++) {
88  (void)portStart.now();
89  }
90 
91  this->RateGroupMemberOut_out(port, this->m_contexts[port]);
92 
94  (void)portEnd.now();
95  U32 cycleTime = 0;
96  (void)portEnd.getDiffUsec(portStart, cycleTime);
97  portTimes[static_cast<FwSizeType>(port)] = cycleTime;
98  // Update high water mark if current cycle time exceeds it (lock-free atomic)
99  U32 currentHWM =
100  this->m_portCycleTimeHWMUsec[static_cast<FwSizeType>(port)].load(std::memory_order_relaxed);
101  while (cycleTime > currentHWM) {
102  if (this->m_portCycleTimeHWMUsec[static_cast<FwSizeType>(port)].compare_exchange_weak(
103  currentHWM, cycleTime, std::memory_order_relaxed)) {
104  break;
105  }
106  }
107  }
108  }
109  }
110 
111  // grab timer for endTime of cycle
112  (void)endTime.now();
113 
114  // get rate group execution time
115  U32 cycleTime = 0;
116  // Cast to void: overflow is capped to U32 max by getDiffUsec, and a mismatched
117  // RawTimeSource (see sdd.md) leaves cycleTime at zero
118  (void)endTime.getDiffUsec(cycleStart, cycleTime);
119 
120  // Update max time atomically (lock-free, ISR-safe)
121  U32 currentMax = this->m_maxTime.load(std::memory_order_relaxed);
122  while (cycleTime > currentMax) {
123  if (this->m_maxTime.compare_exchange_weak(currentMax, cycleTime, std::memory_order_relaxed)) {
124  break;
125  }
126  }
127 
128  U32 maxTime = this->m_maxTime.load(std::memory_order_relaxed);
129  U32 cycles = ++this->m_cycles;
130 
132  // Copy atomic array to telemetry structure for sending
133  PassiveRateGroup_CycleTime portCycleTimeHWM;
134  for (FwIndexType port = 0; port < this->getNum_RateGroupMemberOut_OutputPorts(); port++) {
135  portCycleTimeHWM[static_cast<FwSizeType>(port)] =
136  this->m_portCycleTimeHWMUsec[static_cast<FwSizeType>(port)].load(std::memory_order_relaxed);
137  }
138  this->tlmWrite_PortCycleTime(portTimes);
139  this->tlmWrite_PortCycleTimeHWM(portCycleTimeHWM);
140  }
141 
142  this->tlmWrite_MaxCycleTime(maxTime);
143  this->tlmWrite_CycleTime(cycleTime);
144  this->tlmWrite_CycleCount(cycles);
145 }
146 
147 void PassiveRateGroup::CLEAR_STATISTICS_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
148  // Clear max cycle time (lock-free atomic)
149  this->m_maxTime.store(0, std::memory_order_relaxed);
150  // Note: m_cycles is intentionally NOT cleared - it's a running total
151 
152  // Clear all port cycle time high water marks (lock-free atomic)
153  for (FwIndexType port = 0; port < this->getNum_RateGroupMemberOut_OutputPorts(); port++) {
154  this->m_portCycleTimeHWMUsec[static_cast<FwSizeType>(port)].store(0, std::memory_order_relaxed);
155  }
156 
157  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
158 }
159 
160 } // namespace Svc
PassiveRateGroup(const char *compName)
PassiveRateGroupImpl constructor.
FwIdType FwOpcodeType
The type of a command opcode.
PlatformSizeType FwSizeType
bool isConnected_RateGroupMemberOut_OutputPort(FwIndexType portNum) const
static constexpr FwIndexType getNum_RateGroupMemberOut_OutputPorts()
Status now() override
Get the current time.
void RateGroupMemberOut_out(FwIndexType portNum, U32 context) const
Invoke output port RateGroupMemberOut.
void tlmWrite_CycleTime(U32 arg, Fw::Time _tlmTime=Fw::Time()) const
Fw::Array< U32, CONNECTION_COUNT_MAX > ContextArray
Array of context values for rate group members, indexed by output port number.
static constexpr FwIndexType CONNECTION_COUNT_MAX
RawTimeSource
Timer source selection for RawTime.
Auto-generated base for PassiveRateGroup component.
void configure(const ContextArray &contexts, const Os::RawTimeSource rawTimeSource=Os::RAWTIME_DEFAULT)
PassiveRateGroupImpl initialization function.
Command successfully executed.
void tlmWrite_MaxCycleTime(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_PortCycleTimeHWM(const Svc::PassiveRateGroup_CycleTime &arg, Fw::Time _tlmTime=Fw::Time())
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
void tlmWrite_CycleCount(U32 arg, Fw::Time _tlmTime=Fw::Time()) const
PlatformIndexType FwIndexType
Status getDiffUsec(const RawTime &other, U32 &result) const override
Calculate the difference in microseconds between two RawTime objects.
#define FW_NUM_ARRAY_ELEMENTS(a)
number of elements in an array
Definition: BasicTypes.h:94
~PassiveRateGroup()
PassiveRateGroupImpl destructor.
Platform&#39;s default timer.
DelegateRawTime RawTime
Definition: OsSelection.hpp:99
RateGroupDivider component implementation.
void tlmWrite_PortCycleTime(const Svc::PassiveRateGroup_CycleTime &arg, Fw::Time _tlmTime=Fw::Time()) const
Write telemetry channel PortCycleTime.
#define FW_ASSERT(...)
Definition: Assert.hpp:14