-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeMan.cpp
275 lines (228 loc) · 8.59 KB
/
timeMan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "timeMan.h"
TimingManager* TimingManager::instance = nullptr;
TimingManager::TimingManager()
{
outputWork = false;
memset(linkedListCoreHead, 0x00, sizeof(linkedListCoreHead));
}
TimingManager::~TimingManager()
{
kill();
if (outputWork) Serial.println("Deconstructor called!");
instance = nullptr;
}
bool TimingManager::isJobFinished(functionData* currJob)
{
bool finished = false;
if (currJob->runTimes != 0) {
if (currJob->runTimes <= currJob->timesRan)
{
finished = true;
}
}
return finished;
}
int TimingManager::clearTaskList(Core core)
{
unsigned int clearedNodes = 0;
FunctionNode* currentNode = linkedListCoreHead[core];
while (currentNode != nullptr) {
FunctionNode* nextNode = currentNode->next;
if (outputWork) Serial.println(getCorePrefix(core) + " Freeing 0x" + String((unsigned long)currentNode));
free(currentNode);
currentNode = nextNode;
++clearedNodes;
}
linkedListCoreHead[core] = nullptr;
if (outputWork) Serial.println(getCorePrefix(core) + "Freed " + String(clearedNodes) + " nodes");
return clearedNodes;
}
String TimingManager::getCorePrefix(Core core)
{
return "[CORE" + String(core) + "]";
}
void TimingManager::performWork(TimingManager* tmObj, Core core)
{
unsigned int disabledThisRound = 0;
#if automaticTicking
++tmObj->counter[core];
#endif // automaticTicking
FunctionNode* currentNode = tmObj->linkedListCoreHead[core];
while (!(tmObj->coreDeathSignal)
&& currentNode != nullptr
&& currentNode->data.functionReference != nullptr)
{
functionData* currJob = ¤tNode->data;
if (currJob->typeOfCount == CYCLEJOB) {
if (tmObj->counter[core] % currJob->goal == 0) {
currJob->functionReference(currJob->addressOfData);
++currJob->timesRan;
}
}
else if (currJob->typeOfCount == MILISEC) {
if (millis() >= (currJob->lastTimeRan + currJob->goal)) {
currJob->lastTimeRan = millis(); //Using the newest time (BEFORE) the function has been executed. [Add a way to choose between before and after the execution?]
if (tmObj->outputWork) Serial.println("\nPerformed 0x" + String((unsigned long)currentNode) + " (Function reference:0x" + String((long)&currJob->functionReference) + ") on core" + xPortGetCoreID());
currJob->functionReference(currJob->addressOfData);
++currJob->timesRan;
}
}
if (!(tmObj->coreDeathSignal) && isJobFinished(currJob)) { //Once the deathSignal has been given, the core should not handle the removal of tasks anymore.
FunctionNode* nextNode = currentNode->next;
if (currentNode->prev != nullptr)
{
currentNode->prev->next = nextNode;
}
else {
tmObj->linkedListCoreHead[core] = nextNode;
}
if (nextNode != nullptr)
{
nextNode->prev = currentNode->prev;
}
if (tmObj->outputWork) Serial.println("[CORE" + String(core) + "|" + String(++disabledThisRound) + "]: 0x" + String((unsigned long)currentNode) + " (Function reference:0x" + String((long)&currJob->functionReference) + ") - task is now disabled..");
free(currentNode);
if (nextNode != nullptr)
{
currentNode = nextNode;
}
else {
currentNode = nullptr;
}
}
else {
currentNode = currentNode->next;
}
}
}
void TimingManager::secondCoreLoop(void* _tmObj)
{
TimingManager* tmObj = (TimingManager*)_tmObj;
while (tmObj->coreReady[CORE0] && !tmObj->coreDeathSignal)
{
performWork(tmObj, CORE0);
/////////////////////////////////////////////
/*
Problem: Task watchdog got triggered. The following tasks did not reset the watchdog in time: [...]
Solution: https://github.com/espressif/esp-idf/issues/1646#issuecomment-413829778
*/
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed = 1;
TIMERG0.wdt_wprotect = 0;
/////////////////////////////////////////////
}
if (tmObj->outputWork) Serial.println("Killing own task (core0)");
vTaskDelete(NULL);
}
void TimingManager::primaryCoreLoop(void* _tmObj) {
TimingManager* tmObj = (TimingManager*)_tmObj;
while (tmObj->coreReady[CORE1] && !tmObj->coreDeathSignal) {
performWork(tmObj, CORE1);
}
if (tmObj->outputWork) Serial.println("Killing own task (core1)");
vTaskDelete(NULL);
}
void TimingManager::startHandlingPrimaryCore(bool killArduinoTask) { //Whether or not to kill the Arduino task.
if (!coreReady[CORE1]) {
coreReady[CORE1] = true;
if (outputWork) Serial.println("Added task to primary core!");
xTaskCreatePinnedToCore(primaryCoreLoop, "coreOneTask", 10000, this, 1, NULL, CORE1); //Description: Function to run - name - size of stack - parameter (Reference to the task holding) - priority of this coretask - reference to the taskHandle - the core.
if (killArduinoTask) {
vTaskDelete(NULL);
}
}
}
void TimingManager::printChain()
{
String startOfChainMessage = "#################Start of chain#################";
Serial.println(startOfChainMessage);
int totalMemPerNode = sizeof(functionData) + sizeof(FunctionNode);
int freeHeapMem = xPortGetFreeHeapSize();
Serial.println("Size of node: " + String(sizeof(FunctionNode)) + " bytes");
Serial.println("Size of task: " + String(sizeof(functionData)) + " bytes");
Serial.println("Total: " + String(totalMemPerNode) + " bytes");
Serial.println("Size of this: " + String(sizeof(*this)) + " bytes");
Serial.println(String(freeHeapMem) + " bytes free");
Serial.println("Theoretical limit: " + String(floor(freeHeapMem / totalMemPerNode)));
for (int kerne = 0; kerne < 2; kerne++)
{
Serial.println("CORE " + String(kerne));
FunctionNode* currentNode = linkedListCoreHead[kerne];
while (currentNode != nullptr) {
Serial.println("[prev] 0x" + String((unsigned long)currentNode->prev) + " -> [node] 0x" + String((unsigned long)currentNode) + " -> [next] 0x" + String((unsigned long)currentNode->next));
currentNode = currentNode->next;
}
}
Serial.print("\n");
for (int i = 0; i < startOfChainMessage.length(); i++) Serial.print("#");
Serial.print("\n");
}
void TimingManager::setOutputWork(bool state)
{
outputWork = state;
}
TimingManager* TimingManager::getInstance()
{
if (instance == nullptr)
{
instance = new TimingManager();
}
return instance;
}
void TimingManager::addFunction(RunType type, unsigned int activator, void (*referencToFunction)(void*), void* _addressOfData, int offset, Core core, unsigned int runCount)
{
functionData newJob;
newJob.goal = activator;
newJob.addressOfData = _addressOfData;
newJob.functionReference = referencToFunction;
newJob.typeOfCount = type;
newJob.lastTimeRan = millis() + offset;
newJob.runTimes = runCount;
if (outputWork) Serial.println("Adding task to CORE" + String(core));
FunctionNode* previousNode = linkedListCoreHead[core];
FunctionNode* functionNodeToAdd = new FunctionNode();
if (outputWork) Serial.println("Created new object in HEAP memory.");
functionNodeToAdd->data = newJob;
if (outputWork) Serial.print("\n");
if (previousNode == nullptr) {
if (outputWork) Serial.print("Set the head of the LinkedList");
}
else {
functionNodeToAdd->next = linkedListCoreHead[core];
linkedListCoreHead[core]->prev = functionNodeToAdd;
if (outputWork) Serial.println(String((unsigned long)functionNodeToAdd->prev) + ":|" + String((unsigned long)functionNodeToAdd) + "|:" + String((unsigned long)functionNodeToAdd->next));
if (outputWork) Serial.print("Changed the head of the LinkedList");
}
if (outputWork) Serial.println("\nCORE" + String(core) + " -> " + String((unsigned long)functionNodeToAdd->prev) + ":|" + String((unsigned long)functionNodeToAdd) + "|:" + String((unsigned long)functionNodeToAdd->next));
linkedListCoreHead[core] = functionNodeToAdd;
if (outputWork) Serial.println("[" + String(core) + "]");
if (core == CORE0 && !coreReady[CORE0]) {
coreReady[CORE0] = true;
xTaskCreatePinnedToCore(secondCoreLoop, "coreZeroTask", 10000, this, 0, NULL, CORE0); //Description: Function to run - name - size of stack - parameter (Reference to the task holding) - priority of this coretask - reference to the taskHandle - the core.
if (outputWork) Serial.println("Added task to CORE0");
}
}
void TimingManager::tick(Core coreToTick) {
++counter[coreToTick];
}
void TimingManager::cycle()
{
performWork(this, CORE1);
}
void TimingManager::clear()
{
coreDeathSignal = true;
if (linkedListCoreHead[1] != nullptr)
{
clearTaskList(Core::CORE1);
}
if (linkedListCoreHead[0] != nullptr)
{
clearTaskList(Core::CORE0);
}
}
void TimingManager::kill()
{
clear();
memset(coreReady, 0x00, sizeof(coreReady));
}