diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index 5cdf03c9..46070117 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -40,3 +40,45 @@ void yield(void) { taskYIELD(); } + +SchedulerRTOS Scheduler; + +static void _redirect_task(void* arg) +{ + SchedulerRTOS::taskfunc_t taskfunc = (SchedulerRTOS::taskfunc_t) arg; + + while(1) + { + taskfunc(); + + // yield() anyway just in case user forgot + taskYIELD(); + } +} + +SchedulerRTOS::SchedulerRTOS(void) +{ + _num = 1; // loop is already created by default +} + +bool SchedulerRTOS::startLoop(taskfunc_t task, uint32_t stack_size) +{ + char name[8] = "loop0"; + name[4] += _num; + + if ( startLoop(task, name, stack_size) ) + { + _num++; + return true; + }else + { + return false; + } +} + +bool SchedulerRTOS::startLoop(taskfunc_t task, const char* name, uint32_t stack_size) +{ + TaskHandle_t handle; + return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_NORMAL, &handle); +} + diff --git a/cores/nRF5/rtos.h b/cores/nRF5/rtos.h index 9c3f541e..6592fe81 100644 --- a/cores/nRF5/rtos.h +++ b/cores/nRF5/rtos.h @@ -79,6 +79,27 @@ static inline void* rtos_realloc(void* pv, size_t new_size) { return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? realloc(pv, new_size) : pvPortRealloc(pv, new_size); } +#endif + +#ifdef __cplusplus // Visible only with cplusplus + +#define SCHEDULER_STACK_SIZE_DFLT (512*2) + +class SchedulerRTOS +{ +private: + uint8_t _num; + +public: + typedef void (*taskfunc_t)(void); + + SchedulerRTOS(void); + + bool startLoop(taskfunc_t task, uint32_t stack_size = SCHEDULER_STACK_SIZE_DFLT); + bool startLoop(taskfunc_t task, const char* name, uint32_t stack_size = SCHEDULER_STACK_SIZE_DFLT); +}; + +extern SchedulerRTOS Scheduler; #endif #endif /* RTOS_H_ */ diff --git a/keywords.txt b/keywords.txt index 50239a2f..6428075c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -45,6 +45,8 @@ dbgHeapFree KEYWORD2 dbgMemInfo KEYWORD2 dbgPrintVersion KEYWORD2 +startLoop KEYWORD2 + ####################################### # Adafruit_FIFO Methods (KEYWORD2) ####################################### diff --git a/libraries/Bluefruit52Lib/examples/Hardware/rtos_scheduler/rtos_scheduler.ino b/libraries/Bluefruit52Lib/examples/Hardware/rtos_scheduler/rtos_scheduler.ino new file mode 100644 index 00000000..f47a97cc --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Hardware/rtos_scheduler/rtos_scheduler.ino @@ -0,0 +1,57 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include + +/* + * Sketch demonstate mutli-task using Scheduler. Demo create loop2() that + * run in 'parallel' with loop(). + * - loop() toggle LED1 every 1 second + * - loop2() toggle LED2 every half of second + */ + +int led1 = 17; +int led2 = 19; + +void setup() +{ + // initialize digital pin as an output. + pinMode(led1, OUTPUT); + pinMode(led2, OUTPUT); + + digitalWrite(led1, LOW); + digitalWrite(led2, LOW); + + // Create loop2() using Scheduler + Scheduler.startLoop(loop2); +} + +/** + * Toggle led1 every 1 second + */ +void loop() +{ + digitalToggle(led1); // Toggle LED + delay(1000); // wait for a second +} + +/** + * Toggle led1 every 0.5 second + */ +void loop2() +{ + digitalToggle(led2); // Toggle LED + delay(500); // wait for a half second +} +