-
Notifications
You must be signed in to change notification settings - Fork 7.6k
freeRTOS based schedule #1516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
freeRTOS based schedule #1516
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e97a0b
Add Schedule and Functional/Scheduled Ticker
hreintke 90a1413
Update Schedule to use freeRTOS tasks & queues (WIP)
hreintke 4d6905c
Finalize Function Queue Implementation
hreintke 1f99fcf
Update freeRTOS.h to FreeRTOS.h
hreintke 46e1ae8
Update slashes in includes
hreintke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* FunctionQueue.cpp | ||
* | ||
* Created on: 9 jun. 2018 | ||
* Author: Herman | ||
*/ | ||
|
||
#include <FunctionQueue.h> | ||
#include <functional> | ||
#include "Arduino.h" | ||
#include "freertos/event_groups.h" | ||
|
||
EventGroupHandle_t FunctionQueue::loopEventHandle = xEventGroupCreate(); | ||
uint8_t FunctionQueue::allSynced = 0; // Loop is always in the sync group | ||
const uint8_t FunctionQueue::loopIndex = 1; | ||
SemaphoreHandle_t FunctionQueue::syncedMutex = xSemaphoreCreateMutex(); | ||
|
||
FunctionQueue::FunctionQueue() | ||
:FunctionQueue(false) | ||
{ | ||
} | ||
|
||
FunctionQueue::FunctionQueue(bool loopSynced) | ||
{ | ||
if (loopSynced) | ||
{ | ||
syncIndex = 0; | ||
for (int ix = loopIndex;ix < 8;ix++) | ||
{ | ||
if (((allSynced >> ix) & 1U) == 0) | ||
{ | ||
syncIndex = ix; | ||
break; | ||
} | ||
} | ||
if (syncIndex) | ||
{ | ||
xSemaphoreTake(syncedMutex, portMAX_DELAY); | ||
allSynced |= (1u << syncIndex); | ||
xSemaphoreGive(syncedMutex); | ||
} | ||
else | ||
{ | ||
// To many synced FQ's | ||
} | ||
} | ||
|
||
functionQueue = xQueueCreate( 10,sizeof(QueuedItem*) ); | ||
// ARDUINO_RUNNING_CORE is defined in main.cpp core is here hardcoded to 1 | ||
// Priority 1 is identical to the prio of the looptask | ||
xTaskCreatePinnedToCore(staticFunction, "FunctionQueue", 8192, this, 1, &this->functionTask, 1); | ||
} | ||
|
||
FunctionQueue::~FunctionQueue() | ||
{ | ||
if (syncIndex != 0) | ||
{ | ||
xSemaphoreTake(syncedMutex, portMAX_DELAY); | ||
allSynced &= ~(1u << syncIndex); | ||
xSemaphoreGive(syncedMutex); | ||
} | ||
xEventGroupSetBits(loopEventHandle, (1u << syncIndex)); //Loop still may be waiting on this | ||
vQueueDelete(functionQueue); | ||
vTaskDelete(functionTask); | ||
} | ||
|
||
bool FunctionQueue::scheduleFunction(std::function<void(void)> sf) | ||
{ | ||
QueuedItem* queuedItem = new QueuedItem; | ||
queuedItem->queuedFunction = sf; | ||
xQueueSendToBack(functionQueue,&queuedItem,portMAX_DELAY); | ||
|
||
return true; | ||
} | ||
|
||
void FunctionQueue::processQueueItem() | ||
{ | ||
QueuedItem* queuedItem = nullptr; | ||
if (xQueueReceive(functionQueue, &queuedItem, portMAX_DELAY) == pdTRUE){ | ||
queuedItem->queuedFunction(); | ||
delete queuedItem; | ||
} | ||
} | ||
|
||
void FunctionQueue::staticFunction(void* pvParameters) | ||
{ | ||
FunctionQueue* _this = static_cast<FunctionQueue*>(pvParameters); | ||
for (;;) { | ||
if (_this->syncIndex != 0) | ||
{ | ||
uint16_t er = xEventGroupSync(loopEventHandle,(1u << _this->syncIndex),0x01,portMAX_DELAY); | ||
|
||
int mc = uxQueueMessagesWaiting(_this->functionQueue); | ||
// only run already queued functions to allow recursive | ||
while (mc-- > 0) | ||
{ | ||
_this->processQueueItem(); // Will always return | ||
} | ||
} | ||
else | ||
{ | ||
_this->processQueueItem(); // Will block when queue is empty | ||
} | ||
} | ||
vTaskDelete(NULL); | ||
} | ||
|
||
FunctionQueue FQ; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* FunctionQueue.h | ||
* | ||
* Created on: 9 jun. 2018 | ||
* Author: Herman | ||
*/ | ||
|
||
#ifndef CORE_CORE_FUNCTIONQUEUE_H_ | ||
#define CORE_CORE_FUNCTIONQUEUE_H_ | ||
|
||
#include <functional> | ||
|
||
extern "C" | ||
{ | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "freertos/queue.h" | ||
#include "freertos/semphr.h" | ||
#include "freertos/event_groups.h" | ||
} | ||
|
||
class FunctionQueue { | ||
public: | ||
|
||
struct QueuedItem | ||
{ | ||
std::function<void(void)> queuedFunction; | ||
}; | ||
|
||
FunctionQueue(); | ||
FunctionQueue(bool); | ||
virtual ~FunctionQueue(); | ||
|
||
static void staticFunction(void* pvParameters); | ||
static EventGroupHandle_t loopEventHandle; | ||
static uint8_t allSynced; | ||
static SemaphoreHandle_t syncedMutex; | ||
static const uint8_t loopIndex; | ||
|
||
void processQueueItem(); | ||
|
||
bool scheduleFunction(std::function<void(void)>); | ||
|
||
uint8_t syncIndex; | ||
|
||
QueueHandle_t functionQueue; | ||
TaskHandle_t functionTask; | ||
|
||
}; | ||
|
||
extern FunctionQueue FQ; | ||
|
||
#endif /* CORE_CORE_FUNCTIONQUEUE_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other libraries allow to not automatically instantiate global variables.
Do we need to obey NO_GLOBAL_INSTANCES here?