F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ActiveRateGroup.cpp
Go to the documentation of this file.
1 /*
2  * \author: Tim Canham
3  * \file:
4  * \brief
5  *
6  * This file implements the ActiveRateGroup 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 
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include <Fw/Types/Assert.hpp>
17 #include <Os/Console.hpp>
19 #include <config/ActiveRateGroupCfg.hpp>
20 
21 namespace Svc {
22 
23 // The cycle-started flag is stored from the cycle caller's thread, which may be interrupt context;
24 // a std::atomic implemented with a hidden lock would not be safe there.
25 static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "std::atomic<bool> must be lock-free for the cycle-started flag");
26 
27 ActiveRateGroup::ActiveRateGroup(const char* compName)
28  : ActiveRateGroupComponentBase(compName),
29  m_cycles(0),
30  m_maxTime(0),
31  m_cycleStarted(false),
32  m_numContexts(0),
33  m_overrunThrottle(0),
34  m_cycleSlips(0) {}
35 
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 
47 void ActiveRateGroup::configure(const U32 contexts[], const FwIndexType numContexts) {
48  FW_ASSERT(contexts != nullptr);
49  FW_ASSERT(numContexts == this->getNum_RateGroupMemberOut_OutputPorts(), static_cast<FwAssertArgType>(numContexts),
50  static_cast<FwAssertArgType>(this->getNum_RateGroupMemberOut_OutputPorts()));
51 
52  ContextArray contextArray;
53  for (FwIndexType entry = 0; entry < numContexts; entry++) {
54  contextArray[static_cast<FwSizeType>(entry)] = contexts[entry];
55  }
56  this->configure(contextArray);
57 }
58 
60 
61 void ActiveRateGroup::preamble() {
63 }
64 
65 void ActiveRateGroup::CycleIn_handler(FwIndexType portNum, Os::RawTime& cycleStart) {
66  // Make sure it's been configured
67  FW_ASSERT(this->m_numContexts != 0);
68 
69  Os::RawTime endTime;
70 
71  // Clear the flag set by this cycle's CycleIn_preMsgHook. A cycle message whose hook ran
72  // between the dequeue and this store is coalesced with this cycle: one slip per delay.
73  this->m_cycleStarted.store(false, std::memory_order_relaxed);
74 
75  // invoke any members of the rate group
76  for (FwIndexType port = 0; port < this->m_numContexts; port++) {
78  this->RateGroupMemberOut_out(port, static_cast<U32>(this->m_contexts[port]));
79  }
80  }
81 
82  // grab timer for endTime of cycle
83  Os::RawTime::Status timeStatus = endTime.now();
84  if (timeStatus != Os::RawTime::Status::OP_OK) {
85  this->log_WARNING_HI_RateGroupTimeGetError(Os::RawTimeStatus(static_cast<Os::RawTimeStatus::T>(timeStatus)));
86  }
87 
88  // get rate group execution time
89  U32 cycleTime;
90  // Cast to void as the only possible error is overflow, which we can't handle other
91  // than capping cycleTime to max value of U32 (which is done in getDiffUsec anyways)
92  (void)endTime.getDiffUsec(cycleStart, cycleTime);
93 
94  // check to see if the time has exceeded the previous maximum
95  if (cycleTime > this->m_maxTime) {
96  this->m_maxTime = cycleTime;
97  }
98 
99  // update cycle telemetry
100  this->tlmWrite_RgMaxTime(this->m_maxTime);
101 
102  // check for cycle slip. That will happen if new cycle message has been received
103  // which will cause flag will be set again.
104  if (this->m_cycleStarted.load(std::memory_order_relaxed)) {
105  this->m_cycleSlips++;
106  if (this->m_overrunThrottle < ACTIVE_RATE_GROUP_OVERRUN_THROTTLE) {
107  this->log_WARNING_HI_RateGroupCycleSlip(this->m_cycles);
108  this->m_overrunThrottle++;
109  }
110  // update cycle slips
111  this->tlmWrite_RgCycleSlips(this->m_cycleSlips);
112  } else { // if cycle is okay start decrementing throttle value
113  if (this->m_overrunThrottle > 0) {
114  this->m_overrunThrottle--;
115  }
116  }
117 
118  // increment cycle
119  this->m_cycles++;
120 }
121 
122 void ActiveRateGroup::CycleIn_preMsgHook(FwIndexType portNum, Os::RawTime& cycleStart) {
123  // set flag to indicate cycle has started. Check in thread for overflow.
124  this->m_cycleStarted.store(true, std::memory_order_relaxed);
125 }
126 
127 void ActiveRateGroup::PingIn_handler(FwIndexType portNum, U32 key) {
128  // return the key to health
129  this->PingOut_out(0, key);
130 }
131 
132 } // namespace Svc
void configure(const ContextArray &contexts)
ActiveRateGroup configuration function.
void tlmWrite_RgCycleSlips(U32 arg, Fw::Time _tlmTime=Fw::Time())
Operation succeeded.
Definition: Os.hpp:27
ActiveRateGroup(const char *compName)
ActiveRateGroup constructor.
FPP shadow-enum representing Os::RawTime::Status.
PlatformSizeType FwSizeType
static constexpr FwIndexType getNum_RateGroupMemberOut_OutputPorts()
void log_WARNING_HI_RateGroupTimeGetError(const Os::RawTimeStatus &status)
Status now() override
Get the current time.
void PingOut_out(FwIndexType portNum, U32 key) const
Invoke output port PingOut.
~ActiveRateGroup()
ActiveRateGroup destructor.
void tlmWrite_RgMaxTime(U32 arg, Fw::Time _tlmTime=Fw::Time())
void RateGroupMemberOut_out(FwIndexType portNum, U32 context) const
Invoke output port RateGroupMemberOut.
static constexpr FwIndexType CONNECTION_COUNT_MAX
Auto-generated base for ActiveRateGroup component.
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
RateGroupDivider component implementation.
Fw::Array< U32, CONNECTION_COUNT_MAX > ContextArray
Array of context values for rate group members, indexed by output port number.
bool isConnected_RateGroupMemberOut_OutputPort(FwIndexType portNum) const
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Number of overruns allowed before overrun event is throttled.