implement Scheduler

This commit is contained in:
hathach committed 2017-03-16 00:01:33 +07:00
1 parent a816e70e8f
commit 812fbad07d
4 files changed
+122

No files matched your search

+42
View File
@@ -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);
}
+21
View File
@@ -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_ */
+2
View File
@@ -45,6 +45,8 @@ dbgHeapFree KEYWORD2
dbgMemInfo KEYWORD2
dbgPrintVersion KEYWORD2
startLoop KEYWORD2
#######################################
# Adafruit_FIFO Methods (KEYWORD2)
#######################################
@@ -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 <Arduino.h>
/*
* 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
}