forked from mikaelpatel/Arduino-Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathScheduler.cpp
275 lines (216 loc) · 6.28 KB
/
Scheduler.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
/**
* @file Scheduler.cpp
* @version 1.2
*
* @section License
* Copyright (C) 2015-2016, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "Scheduler.h"
#include <Arduino.h>
// Configuration: SRAM and heap handling
#if defined(TEENSYDUINO) && defined(__MK20DX256__)
#undef ARDUINO_ARCH_AVR
#define TEENSY_ARCH_ARM
#define RAMEND 0x20008000
#elif defined(ARDUINO_ARCH_AVR)
extern int __heap_start, *__brkval;
extern char* __malloc_heap_end;
extern size_t __malloc_margin;
#elif defined(ARDUINO_ARCH_SAM)
#define RAMEND 0x20088000
#elif defined(ARDUINO_ARCH_SAMD)
#define RAMEND 0x20008000
#elif defined(ARDUINO_ARCH_ESP8266)
// https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
#define RAMSTART 0x3FFE8000
#define RAMSIZE 0x14000
#define RAMEND RAMSTART + RAMSIZE - 1
//#define RAMEND 0x3fffffb0
#endif
// Single-ton
//SchedulerClass Scheduler;
SchedulerClass& sched(void)
{
static SchedulerClass ans = SchedulerClass();
return ans;
}
// Main task and run queue
SchedulerClass::task_t SchedulerClass::s_main = {
&SchedulerClass::s_main,
&SchedulerClass::s_main,
{ 0 },
NULL
};
// Reference running task
SchedulerClass::task_t* SchedulerClass::s_running = &SchedulerClass::s_main;
// Initial top stack for task allocation
size_t SchedulerClass::s_top = SchedulerClass::DEFAULT_STACK_SIZE;
// Initial number of tasks
size_t SchedulerClass::s_count = 0;
#if defined(ARDUINO_ARCH_ESP8266)
extern "C" {
#include "os_type.h"
//#include "signal.h"
#include "user_interface.h"
}
#include "Scheduler/Semaphore.h"
#define LOOP_TASK_PRIORITY 1
#define LOOP_QUEUE_SIZE 1
//void handler (int signum) { printf("SIGNAL\n"); };
extern "C" uint32_t system_get_time();
extern "C" void preloop_update_frequency();
static uint32_t g_micros_at_task_start = 0;
extern "C" void esp_yield(void){ yield(); }
extern "C" void optimistic_yield(uint32_t interval_us){ yield(); }
extern "C" void delay(unsigned long interval_ms)
{
uint32_t g_micros_at_task_start = system_get_time();
while((system_get_time() - g_micros_at_task_start) < interval_ms*1000)
{
system_soft_wdt_feed();
yield();
}
}
extern "C" void delay_until(uint32_t clock)
{
uint32_t current_clock;
while((current_clock = system_get_time()) < clock)
{
system_soft_wdt_feed();
yield();
}
if((current_clock - clock) > 100)
{
#ifdef DEBUG_SCHEDULER
printf_P(PSTR("deadline miss\n"));
#endif
}
}
extern "C" unsigned int get_clock()
{
return system_get_time();
}
extern "C" void loop_task(os_event_t *events)
{
static bool one = true;
g_micros_at_task_start = system_get_time();
//signal (SIGSEGV, handler);
if(one)
{
one = false;
preloop_update_frequency();
Scheduler.begin(0x0);
setup();
if(!Scheduler.start(NULL, loop, 1024))
{
panic();
}
}
yield();
ets_post(LOOP_TASK_PRIORITY, 0, 0);
}
static Semaphore spiffs(1);
extern "C" void esp_spiffs_lock(uint32_t* fs)
{
spiffs.wait(1);
}
extern "C" void esp_spiffs_unlock(uint32_t* fs)
{
spiffs.signal(1);
}
#endif
bool SchedulerClass::begin(size_t stackSize)
{
static bool initiated = true;
if(initiated)
{
initiated = false;
// Set main task stack size
s_top = stackSize;
}
#if defined(ARDUINO_ARCH_ESP8266)
// fill the remaining stack with a pattern
uint8_t * fr = (uint8_t*)RAMEND-STACK_MAX;
memset(fr, 0x08, STACK_MAX-stackSize);
#endif
return true;
}
bool SchedulerClass::start(func_t taskSetup, func_t taskLoop, size_t stackSize)
{
// Check called from main task and valid task loop function
if ((s_running != &s_main) || (taskLoop == NULL)) return (false);
// Adjust stack size with size of task context
stackSize += sizeof(task_t);
// Allocate stack(s) and check if main stack top should be set
size_t frame = RAMEND - (size_t) &frame;
#ifdef DEBUG_SCHEDULER
printf_P(PSTR("%p\n"), (size_t) &frame);
printf_P(PSTR("s_top - frame=%u\n"), s_top - frame);
#endif
uint8_t stack[s_top - frame];
if (s_main.stack == NULL) s_main.stack = stack;
#if defined(ARDUINO_ARCH_AVR)
// Check that the task can be allocated
int HEAPEND = (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
int STACKSTART = ((int) stack) - stackSize;
HEAPEND += __malloc_margin;
if (STACKSTART < HEAPEND) return (false);
// Adjust heap limit
__malloc_heap_end = (char*) STACKSTART;
#endif
#if defined(ARDUINO_ARCH_SAM) || \
defined(ARDUINO_ARCH_SAMD) || \
defined(TEENSY_ARCH_ARM) || \
defined(ARDUINO_ARCH_ESP8266)
// Check that the task can be allocated
if (s_top + stackSize > STACK_MAX) return (false);
#endif
// Adjust stack top for next task allocation
s_top += stackSize;
s_count++;
// Initiate task with given functions and stack top
init(taskSetup, taskLoop, stack - stackSize);
return (true);
}
void SchedulerClass::yield()
{
// Caller will continue here on yield
if (setjmp(s_running->context)) return;
// Next task in run queue will continue
s_running = s_running->next;
longjmp(s_running->context, true);
}
size_t SchedulerClass::stack()
{
unsigned char marker;
return (&marker - s_running->stack);
}
void SchedulerClass::init(func_t setup, func_t loop, const uint8_t* stack)
{
// Add task last in run queue (main task)
task_t task;
task.next = &s_main;
task.prev = s_main.prev;
s_main.prev->next = &task;
s_main.prev = &task;
task.stack = stack;
// Create context for new task, caller will return
if (setjmp(task.context)) {
if (setup != NULL) setup();
while (1) loop();
}
}
extern "C" void yield(void)
{
Scheduler.yield();
}