Skip to content

Commit d8203c8

Browse files
committed
Scheduler library for Arduino Due to the new format
0 parents  commit d8203c8

File tree

5 files changed

+306
-0
lines changed

5 files changed

+306
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Multiple Blinks
3+
4+
Demonstrates the use of the Scheduler library for the Arduino Due
5+
6+
Hardware required :
7+
* LEDs connected to pins 11, 12, and 13
8+
9+
created 8 Oct 2012
10+
by Cristian Maglie
11+
Modified by
12+
Scott Fitzgerald 19 Oct 2012
13+
14+
This example code is in the public domain
15+
16+
http://arduino.cc/en/Tutorial/MultipleBlinks
17+
*/
18+
19+
// Include Scheduler since we want to manage multiple tasks.
20+
#include <Scheduler.h>
21+
22+
int led1 = 13;
23+
int led2 = 12;
24+
int led3 = 11;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
// Setup the 3 pins as OUTPUT
30+
pinMode(led1, OUTPUT);
31+
pinMode(led2, OUTPUT);
32+
pinMode(led3, OUTPUT);
33+
34+
// Add "loop2" and "loop3" to scheduling.
35+
// "loop" is always started by default.
36+
Scheduler.startLoop(loop2);
37+
Scheduler.startLoop(loop3);
38+
}
39+
40+
// Task no.1: blink LED with 1 second delay.
41+
void loop() {
42+
digitalWrite(led1, HIGH);
43+
44+
// IMPORTANT:
45+
// When multiple tasks are running 'delay' passes control to
46+
// other tasks while waiting and guarantees they get executed.
47+
delay(1000);
48+
49+
digitalWrite(led1, LOW);
50+
delay(1000);
51+
}
52+
53+
// Task no.2: blink LED with 0.1 second delay.
54+
void loop2() {
55+
digitalWrite(led2, HIGH);
56+
delay(100);
57+
digitalWrite(led2, LOW);
58+
delay(100);
59+
}
60+
61+
// Task no.3: accept commands from Serial port
62+
// '0' turns off LED
63+
// '1' turns on LED
64+
void loop3() {
65+
if (Serial.available()) {
66+
char c = Serial.read();
67+
if (c=='0') {
68+
digitalWrite(led3, LOW);
69+
Serial.println("Led turned off!");
70+
}
71+
if (c=='1') {
72+
digitalWrite(led3, HIGH);
73+
Serial.println("Led turned on!");
74+
}
75+
}
76+
77+
// IMPORTANT:
78+
// We must call 'yield' at a regular basis to pass
79+
// control to other tasks.
80+
yield();
81+
}

keywords.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map For Scheduler
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Scheduler KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
startLoop KEYWORD2
16+
17+
#######################################
18+
# Constants (LITERAL1)
19+
#######################################
20+

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Scheduler
2+
author=The Android Open Source Project
3+
email=info@arduino.cc
4+
sentence=The Scheduler library enables the Arduino Due to run multiple functions at the same time. This allows tasks to happen without interrupting each other.
5+
paragraph=The Scheduler library enables the Arduino Due to run multiple functions at the same time. This allows tasks to happen without interrupting each other.</br>This is a cooperative scheduler in that the CPU switches from one task to another. The library includes methods for passing control between tasks.
6+
url=http://arduino.cc/en/Reference/Scheduler
7+
architectures=*
8+
version=0.4
9+
dependencies= none
10+
core-dependencies=arduino (>=1.5.0)

src/Scheduler.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Scheduler.h"
18+
19+
extern "C" {
20+
21+
#define NUM_REGS 10 // r4-r11, sp, pc
22+
23+
typedef struct CoopTask {
24+
uint32_t regs[NUM_REGS];
25+
void* stackPtr;
26+
struct CoopTask* next;
27+
struct CoopTask* prev;
28+
} CoopTask;
29+
30+
static CoopTask *cur = 0;
31+
32+
static CoopTask* __attribute__((noinline)) coopSchedule(char taskDied) {
33+
CoopTask* next = cur->next;
34+
35+
if (taskDied) {
36+
// Halt if last task died.
37+
if (next == cur)
38+
while (1)
39+
;
40+
41+
// Delete task
42+
if (cur->stackPtr)
43+
free(cur->stackPtr);
44+
cur->next->prev = cur->prev;
45+
cur->prev->next = cur->next;
46+
free(cur);
47+
}
48+
cur = next;
49+
return next;
50+
}
51+
52+
static void __attribute__((naked)) __attribute__((noinline)) coopTaskStart(void) {
53+
asm (
54+
"mov r0, r5;"
55+
"blx r4;"
56+
"mov r0, #1;"
57+
"bl coopSchedule;"
58+
"ldmia r0, {r4-r12, lr};"
59+
"mov sp, r12;"
60+
"bx lr;"
61+
);
62+
}
63+
64+
static void __attribute__((naked)) __attribute__((noinline)) coopDoYield(CoopTask* curTask) {
65+
asm (
66+
"mov r12, sp;"
67+
"stmia r0, {r4-r12, lr};"
68+
"mov r0, #0;"
69+
"bl coopSchedule;"
70+
"ldmia r0, {r4-r12, lr};"
71+
"mov sp, r12;"
72+
"bx lr;"
73+
);
74+
}
75+
76+
static int coopInit(void) {
77+
CoopTask* task;
78+
79+
task = reinterpret_cast<CoopTask *>(malloc(sizeof(CoopTask)));
80+
if (!task)
81+
return 0;
82+
task->next = task;
83+
task->prev = task;
84+
task->stackPtr = 0;
85+
cur = task;
86+
87+
return 1;
88+
}
89+
90+
static int coopSpawn(SchedulerParametricTask taskF, void* taskData, uint32_t stackSz) {
91+
uint8_t *stack = (uint8_t*)malloc(stackSz);
92+
if (!stack)
93+
return 0;
94+
95+
CoopTask *task = reinterpret_cast<CoopTask *>(malloc(sizeof(CoopTask)));
96+
if (!task) {
97+
free(stack);
98+
return 0;
99+
}
100+
task->stackPtr = stack;
101+
task->regs[0] = (uint32_t) taskF;
102+
task->regs[1] = (uint32_t) taskData;
103+
task->regs[8] = ((uint32_t)(stack + stackSz)) & ~7;
104+
task->regs[9] = (uint32_t) & coopTaskStart;
105+
106+
task->prev = cur;
107+
task->next = cur->next;
108+
cur->next->prev = task;
109+
cur->next = task;
110+
111+
// These are here so compiler is sure that function is
112+
// referenced in both variants (cancels a warning)
113+
if (stackSz == 0xFFFFFFFF)
114+
coopSchedule(0);
115+
if (stackSz == 0xFFFFFFFE)
116+
coopSchedule(1);
117+
118+
return 1;
119+
}
120+
121+
void yield(void) {
122+
coopDoYield(cur);
123+
}
124+
125+
}; // extern "C"
126+
127+
SchedulerClass::SchedulerClass() {
128+
coopInit();
129+
}
130+
131+
static void startLoopHelper(void *taskData) {
132+
SchedulerTask task = reinterpret_cast<SchedulerTask>(taskData);
133+
while (true)
134+
task();
135+
}
136+
137+
void SchedulerClass::startLoop(SchedulerTask task, uint32_t stackSize) {
138+
coopSpawn(startLoopHelper, reinterpret_cast<void *>(task), stackSize);
139+
}
140+
141+
static void startTaskHelper(void *taskData) {
142+
SchedulerTask task = reinterpret_cast<SchedulerTask>(taskData);
143+
task();
144+
}
145+
146+
void SchedulerClass::start(SchedulerTask task, uint32_t stackSize) {
147+
coopSpawn(startTaskHelper, reinterpret_cast<void *>(task), stackSize);
148+
}
149+
150+
void SchedulerClass::start(SchedulerParametricTask task, void *taskData, uint32_t stackSize) {
151+
coopSpawn(task, taskData, stackSize);
152+
}
153+
154+
SchedulerClass Scheduler;
155+

src/Scheduler.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _SCHEDULDER_H_
18+
#define _SCHEDULDER_H_
19+
20+
#include <Arduino.h>
21+
22+
extern "C" {
23+
typedef void (*SchedulerTask)(void);
24+
typedef void (*SchedulerParametricTask)(void *);
25+
}
26+
27+
class SchedulerClass {
28+
public:
29+
SchedulerClass();
30+
static void startLoop(SchedulerTask task, uint32_t stackSize = 1024);
31+
static void start(SchedulerTask task, uint32_t stackSize = 1024);
32+
static void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024);
33+
34+
static void yield() { ::yield(); };
35+
};
36+
37+
extern SchedulerClass Scheduler;
38+
39+
#endif
40+

0 commit comments

Comments
 (0)