Skip to content

Commit 812fbad

Browse files
committed
implement Scheduler
1 parent a816e70 commit 812fbad

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

cores/nRF5/rtos.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,45 @@ void yield(void)
4040
{
4141
taskYIELD();
4242
}
43+
44+
SchedulerRTOS Scheduler;
45+
46+
static void _redirect_task(void* arg)
47+
{
48+
SchedulerRTOS::taskfunc_t taskfunc = (SchedulerRTOS::taskfunc_t) arg;
49+
50+
while(1)
51+
{
52+
taskfunc();
53+
54+
// yield() anyway just in case user forgot
55+
taskYIELD();
56+
}
57+
}
58+
59+
SchedulerRTOS::SchedulerRTOS(void)
60+
{
61+
_num = 1; // loop is already created by default
62+
}
63+
64+
bool SchedulerRTOS::startLoop(taskfunc_t task, uint32_t stack_size)
65+
{
66+
char name[8] = "loop0";
67+
name[4] += _num;
68+
69+
if ( startLoop(task, name, stack_size) )
70+
{
71+
_num++;
72+
return true;
73+
}else
74+
{
75+
return false;
76+
}
77+
}
78+
79+
bool SchedulerRTOS::startLoop(taskfunc_t task, const char* name, uint32_t stack_size)
80+
{
81+
TaskHandle_t handle;
82+
return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_NORMAL, &handle);
83+
}
84+

cores/nRF5/rtos.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ static inline void* rtos_realloc(void* pv, size_t new_size)
7979
{
8080
return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? realloc(pv, new_size) : pvPortRealloc(pv, new_size);
8181
}
82+
#endif
83+
84+
#ifdef __cplusplus // Visible only with cplusplus
85+
86+
#define SCHEDULER_STACK_SIZE_DFLT (512*2)
87+
88+
class SchedulerRTOS
89+
{
90+
private:
91+
uint8_t _num;
92+
93+
public:
94+
typedef void (*taskfunc_t)(void);
95+
96+
SchedulerRTOS(void);
97+
98+
bool startLoop(taskfunc_t task, uint32_t stack_size = SCHEDULER_STACK_SIZE_DFLT);
99+
bool startLoop(taskfunc_t task, const char* name, uint32_t stack_size = SCHEDULER_STACK_SIZE_DFLT);
100+
};
101+
102+
extern SchedulerRTOS Scheduler;
82103

83104
#endif
84105
#endif /* RTOS_H_ */

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dbgHeapFree KEYWORD2
4545
dbgMemInfo KEYWORD2
4646
dbgPrintVersion KEYWORD2
4747

48+
startLoop KEYWORD2
49+
4850
#######################################
4951
# Adafruit_FIFO Methods (KEYWORD2)
5052
#######################################
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*********************************************************************
2+
This is an example for our nRF52 based Bluefruit LE modules
3+
4+
Pick one up today in the adafruit shop!
5+
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
15+
#include <Arduino.h>
16+
17+
/*
18+
* Sketch demonstate mutli-task using Scheduler. Demo create loop2() that
19+
* run in 'parallel' with loop().
20+
* - loop() toggle LED1 every 1 second
21+
* - loop2() toggle LED2 every half of second
22+
*/
23+
24+
int led1 = 17;
25+
int led2 = 19;
26+
27+
void setup()
28+
{
29+
// initialize digital pin as an output.
30+
pinMode(led1, OUTPUT);
31+
pinMode(led2, OUTPUT);
32+
33+
digitalWrite(led1, LOW);
34+
digitalWrite(led2, LOW);
35+
36+
// Create loop2() using Scheduler
37+
Scheduler.startLoop(loop2);
38+
}
39+
40+
/**
41+
* Toggle led1 every 1 second
42+
*/
43+
void loop()
44+
{
45+
digitalToggle(led1); // Toggle LED
46+
delay(1000); // wait for a second
47+
}
48+
49+
/**
50+
* Toggle led1 every 0.5 second
51+
*/
52+
void loop2()
53+
{
54+
digitalToggle(led2); // Toggle LED
55+
delay(500); // wait for a half second
56+
}
57+

0 commit comments

Comments
 (0)