Skip to content

porting scheduler for Avr #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Scheduler Library for Arduino =

The Scheduler library enables the Arduino Due to run multiple functions at the same time. This allows tasks to happen without interrupting each other.
The Scheduler library enables the Arduino to run multiple functions at the same time. This allows tasks to happen without interrupting each other.

For more information about this library please visit us at
http://www.arduino.cc/en/Reference/Scheduler
Expand Down
8 changes: 8 additions & 0 deletions examples/MultiplePWM/Loop2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// Task no.2

void loop2 () {
analogWrite(LED2, counter1);
counter1 += 50;
delay(500);
}
8 changes: 8 additions & 0 deletions examples/MultiplePWM/Loop3.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// Task no.3

void loop3 () {
analogWrite(LED3, counter2);
counter2++;
delay(7);
}
54 changes: 54 additions & 0 deletions examples/MultiplePWM/MultiplePWM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Multiple PWM

Demonstrates the use of the Scheduler library

Hardware required :
* LEDs connected to PWM pins

created 28 Dic 2015
by Testato

*/


// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>

#define LED1 13
#define LED2 10
#define LED3 11
byte counter1;
byte counter2;


void setup() {

// Setup the led pins as OUTPUT
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

// Add "loop2 and loop3" to scheduler
// "loop" is always started by default
Scheduler.startLoop(loop2);
Scheduler.startLoop(loop3);
}


// Task no.1 (standard Arduino loop() )
void loop() {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);

// When multiple tasks are running, 'delay' passes control to
// other tasks while waiting and guarantees they get executed
// It is not necessary to call yield() when using delay()
}





51 changes: 51 additions & 0 deletions examples/MultipleSketch/MultipleSketch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Multiple Sketch

Demonstrates the use of .start and .startLoop method of the Scheduler library

Hardware required :
* LEDs connected to pins

created 07 Gen 2016
by Testato

*/


// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>

#define LED1 13
#define LED2 10
#define LED3 11


// Sketch no.1

void setup() {
// Setup the led pin as OUTPUT
pinMode(LED1, OUTPUT);

// Add "setup2 and setup3" to scheduler
Scheduler.start(setup2);
Scheduler.start(setup3);
// Add "loop2 and loop3" to scheduler
Scheduler.startLoop(loop2);
Scheduler.startLoop(loop3);
}


void loop() {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);

// When multiple tasks are running, 'delay' passes control to
// other tasks while waiting and guarantees they get executed
// It is not necessary to call yield() when using delay()
}




13 changes: 13 additions & 0 deletions examples/MultipleSketch/Sketch2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// Sketch no.2

void setup2 () {
pinMode(LED2, OUTPUT);
}

void loop2 () {
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED2, LOW);
delay(500);
}
13 changes: 13 additions & 0 deletions examples/MultipleSketch/Sketch3
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// Sketch no.3

void setup3 () {
pinMode(LED3, OUTPUT);
}

void loop3 () {
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED3, LOW);
delay(250);
}
167 changes: 167 additions & 0 deletions examples/SchedulerMemoryDump/SchedulerMemoryDump.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Scheduler View Memory Organization

Demonstrates to view and debug task memory

created 21 gen 2016
*/

// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>

// loop2 static memory
// set memory size
const int taskMemory = DEFAULT_STACK_SIZE;
// Create memory for loop2
TaskContext taskContext2;
TaskStack taskStack2[ taskMemory ];


//vector of structure contiene dati di ogni processo, sp spend context heap start heapend.

typedef enum {TYPE_SPEND, TYPE_SP, TYPE_CONTEXT, TYPE_CONTEXTEND, TYPE_HEAP, TYPE_HEAPEND, TYPE_COUNT} AddressType;

struct MemoryDump {
uint32_t address;
AddressType type;
char name[7];
} dump[14];
byte curDump = 0;



void setup() {
Serial.begin(9600);

// Add "loop2" in global memory and "loop3" in heap memory.
// "loop" is always started by default, use global memory for the context and is unico can be used the stack.
Scheduler.startLoop(loop2, taskMemory, taskStack2, taskContext2);
Scheduler.startLoop(loop3);
}

// Task no.1: passo subito l'esecuzione agli altri task che salveranno i dati
void loop() {
yield();

dump[curDump].address = Scheduler.stackPtr();
dump[curDump].type = TYPE_SP;
strcpy(dump[curDump].name, "loop");
++curDump;

dump[curDump].address = Scheduler.stackEnd();
dump[curDump].type = TYPE_SPEND;
strcpy(dump[curDump].name, "loop");
++curDump;

dump[curDump].address = Scheduler.contextPtr();
dump[curDump].type = TYPE_CONTEXT;
strcpy(dump[curDump].name, "loop");
++curDump;

dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
dump[curDump].type = TYPE_CONTEXTEND;
strcpy(dump[curDump].name, "loop");
++curDump;

dump[curDump].address = Scheduler.heapStart();
dump[curDump].type = TYPE_HEAP;
strcpy(dump[curDump].name, "sram");
++curDump;

dump[curDump].address = Scheduler.heapEnd();
dump[curDump].type = TYPE_HEAPEND;
strcpy(dump[curDump].name, "sram");
++curDump;

//riordino dati
struct MemoryDump temp;
byte swap;
do {
byte i;
for( swap = 0, i = 0; i < curDump - 1; ++i ) {
if ( dump[i].address > dump[i + 1].address ) {
temp = dump[i];
dump[i] = dump[i + 1];
dump[i + 1] = temp;
swap = 1;
}
}
}while(swap);

const char* memName[] = { "stack end", "stack pointer", "context start", "context end", "heap start", "heap end"};
char adr[8];
byte i;

Serial.println(F(" --------------"));
Serial.println(F(" ---- SRAM ----"));
Serial.println(F(" --------------"));
Serial.println(F(" ______________"));
for( swap = 0, i = 0; i < curDump; ++i ) {
Serial.print(F(" | "));
sprintf(adr,"0x%.8X",(unsigned)dump[i].address);
Serial.print(adr);
Serial.print(F(" |<--"));
Serial.print(memName[dump[i].type]);
Serial.print(" ");
Serial.println(dump[i].name);
if ( dump[i].type & 1 || dump[i].type == 4)
Serial.println(F(" |____________|"));
else
Serial.println(F(" | |"));
}
Serial.println(F(" --------------"));

while(1);
}

// Task no.2: save data
void loop2() {
dump[curDump].address = Scheduler.stackPtr();
dump[curDump].type = TYPE_SP;
strcpy(dump[curDump].name, "loop2");
++curDump;

dump[curDump].address = Scheduler.stackEnd();
dump[curDump].type = TYPE_SPEND;
strcpy(dump[curDump].name, "loop2");
++curDump;

dump[curDump].address = Scheduler.contextPtr();
dump[curDump].type = TYPE_CONTEXT;
strcpy(dump[curDump].name, "loop2");
++curDump;

dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
dump[curDump].type = TYPE_CONTEXTEND;
strcpy(dump[curDump].name, "loop2");
++curDump;

yield();
while(1);
}

// Task no.3:
void loop3() {
dump[curDump].address = Scheduler.stackPtr();
dump[curDump].type = TYPE_SP;
strcpy(dump[curDump].name, "loop3");
++curDump;

dump[curDump].address = Scheduler.stackEnd();
dump[curDump].type = TYPE_SPEND;
strcpy(dump[curDump].name, "loop3");
++curDump;

dump[curDump].address = Scheduler.contextPtr();
dump[curDump].type = TYPE_CONTEXT;
strcpy(dump[curDump].name, "loop3");
++curDump;

dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
dump[curDump].type = TYPE_CONTEXTEND;
strcpy(dump[curDump].name, "loop3");
++curDump;

yield();
while(1);
}
Loading