Skip to content

Commit 876cbb0

Browse files
committed
add avr in library.properties
fix malloc(It will be strengthened as soon as possible), fix interrupt, need clean code prepared sizing loop , remove conflict malloc , insert stack min use standard malloc and resolve bug, add start stop task, priority, semaphore, mutex and example
1 parent aee3187 commit 876cbb0

File tree

9 files changed

+1080
-95
lines changed

9 files changed

+1080
-95
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Scheduler View Memory Organization
3+
4+
Demonstrates to view and debug task memory
5+
6+
created 21 gen 2016
7+
*/
8+
9+
// Include Scheduler since we want to manage multiple tasks.
10+
#include <Scheduler.h>
11+
12+
// loop2 static memory
13+
// set memory size
14+
const int taskMemory = DEFAULT_STACK_SIZE;
15+
// Create memory for loop2
16+
TaskContext taskContext2;
17+
TaskStack taskStack2[ taskMemory ];
18+
19+
20+
//vector of structure contiene dati di ogni processo, sp spend context heap start heapend.
21+
22+
typedef enum {TYPE_SPEND, TYPE_SP, TYPE_CONTEXT, TYPE_CONTEXTEND, TYPE_HEAP, TYPE_HEAPEND, TYPE_COUNT} AddressType;
23+
24+
struct MemoryDump {
25+
uint32_t address;
26+
AddressType type;
27+
char name[7];
28+
} dump[14];
29+
byte curDump = 0;
30+
31+
32+
33+
void setup() {
34+
Serial.begin(9600);
35+
36+
// Add "loop2" in global memory and "loop3" in heap memory.
37+
// "loop" is always started by default, use global memory for the context and is unico can be used the stack.
38+
Scheduler.startLoop(loop2, taskMemory, taskStack2, taskContext2);
39+
Scheduler.startLoop(loop3);
40+
}
41+
42+
// Task no.1: passo subito l'esecuzione agli altri task che salveranno i dati
43+
void loop() {
44+
yield();
45+
46+
dump[curDump].address = Scheduler.stackPtr();
47+
dump[curDump].type = TYPE_SP;
48+
strcpy(dump[curDump].name, "loop");
49+
++curDump;
50+
51+
dump[curDump].address = Scheduler.stackEnd();
52+
dump[curDump].type = TYPE_SPEND;
53+
strcpy(dump[curDump].name, "loop");
54+
++curDump;
55+
56+
dump[curDump].address = Scheduler.contextPtr();
57+
dump[curDump].type = TYPE_CONTEXT;
58+
strcpy(dump[curDump].name, "loop");
59+
++curDump;
60+
61+
dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
62+
dump[curDump].type = TYPE_CONTEXTEND;
63+
strcpy(dump[curDump].name, "loop");
64+
++curDump;
65+
66+
dump[curDump].address = Scheduler.heapStart();
67+
dump[curDump].type = TYPE_HEAP;
68+
strcpy(dump[curDump].name, "sram");
69+
++curDump;
70+
71+
dump[curDump].address = Scheduler.heapEnd();
72+
dump[curDump].type = TYPE_HEAPEND;
73+
strcpy(dump[curDump].name, "sram");
74+
++curDump;
75+
76+
//riordino dati
77+
struct MemoryDump temp;
78+
byte swap;
79+
do {
80+
byte i;
81+
for( swap = 0, i = 0; i < curDump - 1; ++i ) {
82+
if ( dump[i].address > dump[i + 1].address ) {
83+
temp = dump[i];
84+
dump[i] = dump[i + 1];
85+
dump[i + 1] = temp;
86+
swap = 1;
87+
}
88+
}
89+
}while(swap);
90+
91+
const char* memName[] = { "stack end", "stack pointer", "context start", "context end", "heap start", "heap end"};
92+
char adr[8];
93+
byte i;
94+
95+
Serial.println(F(" --------------"));
96+
Serial.println(F(" ---- SRAM ----"));
97+
Serial.println(F(" --------------"));
98+
Serial.println(F(" ______________"));
99+
for( swap = 0, i = 0; i < curDump; ++i ) {
100+
Serial.print(F(" | "));
101+
sprintf(adr,"0x%.8X",(unsigned)dump[i].address);
102+
Serial.print(adr);
103+
Serial.print(F(" |<--"));
104+
Serial.print(memName[dump[i].type]);
105+
Serial.print(" ");
106+
Serial.println(dump[i].name);
107+
if ( dump[i].type & 1 || dump[i].type == 4)
108+
Serial.println(F(" |____________|"));
109+
else
110+
Serial.println(F(" | |"));
111+
}
112+
Serial.println(F(" --------------"));
113+
114+
while(1);
115+
}
116+
117+
// Task no.2: save data
118+
void loop2() {
119+
dump[curDump].address = Scheduler.stackPtr();
120+
dump[curDump].type = TYPE_SP;
121+
strcpy(dump[curDump].name, "loop2");
122+
++curDump;
123+
124+
dump[curDump].address = Scheduler.stackEnd();
125+
dump[curDump].type = TYPE_SPEND;
126+
strcpy(dump[curDump].name, "loop2");
127+
++curDump;
128+
129+
dump[curDump].address = Scheduler.contextPtr();
130+
dump[curDump].type = TYPE_CONTEXT;
131+
strcpy(dump[curDump].name, "loop2");
132+
++curDump;
133+
134+
dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
135+
dump[curDump].type = TYPE_CONTEXTEND;
136+
strcpy(dump[curDump].name, "loop2");
137+
++curDump;
138+
139+
yield();
140+
while(1);
141+
}
142+
143+
// Task no.3:
144+
void loop3() {
145+
dump[curDump].address = Scheduler.stackPtr();
146+
dump[curDump].type = TYPE_SP;
147+
strcpy(dump[curDump].name, "loop3");
148+
++curDump;
149+
150+
dump[curDump].address = Scheduler.stackEnd();
151+
dump[curDump].type = TYPE_SPEND;
152+
strcpy(dump[curDump].name, "loop3");
153+
++curDump;
154+
155+
dump[curDump].address = Scheduler.contextPtr();
156+
dump[curDump].type = TYPE_CONTEXT;
157+
strcpy(dump[curDump].name, "loop3");
158+
++curDump;
159+
160+
dump[curDump].address = Scheduler.contextPtr() + TASK_CONTEXT_SIZE;
161+
dump[curDump].type = TYPE_CONTEXTEND;
162+
strcpy(dump[curDump].name, "loop3");
163+
++curDump;
164+
165+
yield();
166+
while(1);
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Scheduler Priority
3+
4+
Demonstrates the use of the priority
5+
6+
Hardware required :
7+
Serial
8+
9+
Serial Monitor:
10+
Send 'r' to reverse priority and send 'n' to reset priority
11+
12+
created 3 feb 2016
13+
*/
14+
15+
16+
// Include Scheduler since we want to manage multiple tasks.
17+
#include <Scheduler.h>
18+
19+
TaskContext taskContext2;
20+
TaskContext taskContext3;
21+
TaskStack taskStack2[ DEFAULT_STACK_SIZE ];
22+
TaskStack taskStack3[ DEFAULT_STACK_SIZE ];
23+
tid_t loop2ID;
24+
tid_t loop3ID;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
//Default priority loop == 7(max)
30+
//Default priority Task == 0(min)
31+
32+
//First set priority and after start task
33+
Scheduler.setPriority(5);
34+
loop2ID = Scheduler.startLoop(loop2, sizeof taskStack2, taskStack2, taskContext2);
35+
Scheduler.setPriority(3);
36+
loop3ID = Scheduler.startLoop(loop3, sizeof taskStack3, taskStack3, taskContext3);
37+
//execution:
38+
//loop(7) -> loop2(5) -> loop3(3)
39+
}
40+
41+
void loop() {
42+
Serial.println(F("loop"));
43+
if ( Serial.available() ) {
44+
switch ( Serial.read() ) {
45+
case 'r':
46+
//reverse priority
47+
Scheduler.taskPriority(loop3ID, 7);
48+
//0 is current task
49+
Scheduler.taskPriority(0, 3);
50+
break;
51+
52+
case 'n':
53+
//normal priority
54+
Scheduler.taskPriority(loop3ID, 3);
55+
//0 is current task
56+
Scheduler.taskPriority(0, 7);
57+
break;
58+
}
59+
60+
}
61+
62+
//delay without yield
63+
Scheduler.wait(800);
64+
yield();
65+
}
66+
67+
void loop2() {
68+
Serial.println(F("loop2"));
69+
//delay without yield
70+
Scheduler.wait(800);
71+
yield();
72+
}
73+
74+
void loop3() {
75+
Serial.println(F("loop3"));
76+
Serial.println("");
77+
//delay without yield
78+
Scheduler.wait(800);
79+
yield();
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Scheduler Resume Stop Task
3+
4+
Demonstrates to stop and resume task
5+
6+
Hardware required :
7+
Serial
8+
9+
Send 1 to resume task, 0 to stop task.
10+
11+
created 2 fed 2016
12+
*/
13+
14+
// Include Scheduler since we want to manage multiple tasks.
15+
#include <Scheduler.h>
16+
17+
// loop2
18+
TaskContext taskContext2;
19+
TaskStack taskStack2[ DEFAULT_STACK_SIZE ];
20+
tid_t loop2ID;
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
pinMode(13, OUTPUT);
25+
26+
//start loop2 and get task id
27+
loop2ID = Scheduler.startLoop(loop2, DEFAULT_STACK_SIZE, taskStack2, taskContext2);
28+
}
29+
30+
void loop() {
31+
if ( Serial.available() ) {
32+
char cmd = Serial.read();
33+
34+
switch ( cmd ) {
35+
case '0':
36+
Scheduler.taskStop(loop2ID);
37+
break;
38+
39+
case '1':
40+
Scheduler.taskResume(loop2ID);
41+
break;
42+
}
43+
}
44+
yield();
45+
}
46+
47+
void loop2() {
48+
digitalWrite(13, HIGH);
49+
delay(500);
50+
digitalWrite(13, LOW);
51+
delay(500);
52+
}
53+

0 commit comments

Comments
 (0)