krnl
1
Loading...
Searching...
No Matches
examples
k02twotasks
k02twotasks.ino
Go to the documentation of this file.
1
//220216
2
#include <
krnl.h
>
3
4
const
int
STKSZ
= 200;
5
6
struct
k_t
*
pt1
,
// pointer to hold reference
7
*
pt2
;
// to taskdescriptor for t1 and t2
8
9
char
stak1
[
STKSZ
],
stak2
[
STKSZ
];
10
11
void
sampl
() {}
// for time being empty
12
13
void
alg01
() {}
// for time being empty
14
15
int
loopNr
= 0;
16
17
void
aktuer01
() {
18
Serial.println(
"aktuer"
);
19
Serial.println(
loopNr
);
20
loopNr
=
loopNr
+ 1;
21
}
22
void
aktuer02
() {
23
static
int
loopNr
= 0;
24
Serial.println(
"AAAAaktuer"
);
25
Serial.println(
loopNr
);
26
loopNr
=
loopNr
+ 1;
27
}
28
29
volatile
int
i
= 1000;
33
void
t1
(
void
) {
34
while
(1) {
35
/* k_sleep(1000);
36
k_eat_msec(500);
37
sampl();
38
alg01();
39
*/
40
aktuer01
();
41
}
42
}
43
44
45
46
void
t2
() {
47
while
(1) {
48
// digitalWrite(12, !digitalRead(13));
49
//k_sleep(300);
50
aktuer02
();
51
}
52
}
53
54
void
setup
() {
55
56
Serial.begin(115200);
57
pinMode(13, OUTPUT);
58
59
// init krnl so you can create 2 tasks, no semaphores and no message queues
60
k_init
(2, 0, 0);
61
62
63
// |------------ function used for body code for task
64
// | |--------- priority (lower number= higher prio
65
// | | |----- staksize for array s1
66
// |-- array used for stak
67
pt1
=
k_crt_task
(
t1
, 11,
stak1
,
STKSZ
);
68
pt2
=
k_crt_task
(
t2
, 11,
stak2
,
STKSZ
);
69
70
71
// NB-1 remember an Arduino has only 2-8 kByte RAM
72
// NB-2 remember that stak is used in function calls for
73
// - return address
74
// - registers stakked
75
// - local variabels in a function
76
// So having 200 Bytes of stak excludes a local variable like ...
77
// int arr[400];
78
// krnl call k_unused_stak returns size of unused stak
79
// Both task has same priority so krnl will shift between the
80
// tasks every 10 milli second (speed set in k_start)
81
82
k_start
();
// start kernel with tick speed 1 milli seconds
83
}
84
85
void
loop
() {
86
/* loop will never be called */
87
}
88
89
90
91
extern
"C"
{
92
93
void
k_breakout
()
// called every task shift from dispatcher
94
{
95
96
97
if
(
pRun
->nr == 0)
// 0 is dummy task - the eater of excessive CPU when all user tasks are idling
98
{
99
PORTB = PORTB | B00100000;
// led13 (bit 5) on let the rest be untouched
100
}
101
else
{
102
PORTB = PORTB & B11011111;
// led13 off uno
103
}
104
/* using D8-D13 use following instead of teh code above*/
105
/* PORTB = (1 << pRun->nr); */
106
}
107
108
}
109
110
/*
111
* README README
112
* LED 13 will be ON when dummy is running and OFF when all other tasks are running
113
* See k_breakout function above
114
*
115
* 0) Cleanup
116
* CODE for task t1 and t2 to begin with - here is the code for teh two functions and
117
* k_crt_task calls in setup - if you have changed it/played with the code
118
*
119
*
120
*
121
* void t1(void)
122
* {
123
* while (1) {
124
*
125
* Serial.println(i++);
126
* k_eat_msec(1000); // eat 1000 msec time
127
* //k_sleep(1000); // sleep for 1000 msec
128
*
129
* } // lenght of ticks in millisec is specified in
130
* } // k_start call called from setup - USE 1 msec :-)
131
*
132
* void t2(void)
133
* {
134
* // and task body for task 2
135
* // runs independent of task t1
136
* while (1) {
137
* k_eat_msec(1000); // simulating algorithms running for 1 sec
138
* //k_sleep(1000); // sleep for 1 sec
139
* }
140
* }
141
*
142
* and in setup:
143
*
144
* pt1 = k_crt_task(t1, 11, STKSZ, stak1);
145
* //pt2 = k_crt_task(t2, 11, STKSZ, stak2);
146
*
147
*
148
********************************
149
*
150
* 1) Run code. You will see LED13 is OFF
151
* Why ? because t1 is running all the time
152
*
153
* 2) remove comment in front of k_sleep(1000) in t1
154
* Now LED13 will be ON for 1sec and off for 1sec bq whenin k_sleep t1 is not in active Q
155
* and dummy task will run
156
*
157
* You will see when LED13 is going OFF there is a short blink on TX LED.
158
* This is because t1 is printing an int just after comming back from k_sleep(1000) and looping arounf in the while loop
159
*
160
* 3) Go back and comment out k_sleep in t1 and remove comment in front of pt2 = k_crt_task(t2 - so you will now have two tasks
161
* Both are doing a k_eat_msec and t1 is also printing.
162
* LED13 will never be ON.
163
* TX LED will blink every 2 sec not every 1 sec
164
* Why? because we all the time has two task wanting to use the CPU
165
* So eating 1000 msec takes 2000 msec because they run 1 msec each and then do round robbin
166
* So we are basicly wasting more than 99% of the CPU
167
*
168
* 4) In t1 and t2 comment out k_eat_msec and active (remove comment) from k_sleep
169
* Now LED 13 will be ON all time (if looking in logic analyser you can see that LED 13 will be OFF very shortly when t1 is printing
170
* and when t2 is leaving k_sleep and loop arouond in the while loop and again calling k_sleep)
171
* you cant see it on LED13 with you eye bq it is so short in time
172
* An now TX LED is blinking every 1 sec so task t1 is doing what we want it to do . print and sleep for 1 sec over and over
173
*
174
* Change from k_eat_msec to k_sleep
175
* What do we see
176
*
177
* 5) Change task priority
178
* Go back and comment out k_sleep in t1 and remove comment in front of pt2 = k_vcrt_task(t2 - so you will now have two tasks
179
* Both are doing a k_eat_msec and t1 is also printing.
180
* Change task t2 priority to 8
181
* You will see t1 stops printg == stops running
182
* Why ? because t2 has highest priority
183
*
184
*
185
* ON when task t1 i in k_sleep(1000) then no load on CPU for 1sec
186
* OFF when task t1 is either printing or eating CPU time in k_eat_msec(1000)
187
* A guess i that printing takes less than 1 msec. so looptime will be 1+1000 [msec]
188
*
189
* 2) In function setup remove comment in front of creating of task t2 (pt2 = k_crt_task(t2,...
190
*
191
*/
aktuer01
void aktuer01()
Definition
k02twotasks.ino:17
alg01
void alg01()
Definition
k02twotasks.ino:13
stak1
char stak1[STKSZ]
Definition
k02twotasks.ino:9
setup
void setup()
Definition
k02twotasks.ino:54
i
volatile int i
Definition
k02twotasks.ino:29
k_breakout
void k_breakout()
Definition
k02twotasks.ino:93
t2
void t2()
Definition
k02twotasks.ino:46
pt2
struct k_t * pt2
Definition
k02twotasks.ino:7
pt1
struct k_t * pt1
Definition
k02twotasks.ino:6
aktuer02
void aktuer02()
Definition
k02twotasks.ino:22
t1
void t1(void)
Definition
k02twotasks.ino:33
sampl
void sampl()
Definition
k02twotasks.ino:11
loopNr
int loopNr
Definition
k02twotasks.ino:15
stak2
char stak2[STKSZ]
Definition
k02twotasks.ino:9
loop
void loop()
Definition
k02twotasks.ino:85
pRun
struct k_t * pRun
Definition
krnl.c:148
k_crt_task
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
create a task - only to be called before k_start creates a task and put it in the active Q
Definition
krnl.c:328
k_init
int k_init(int nrTask, int nrSem, int nrMsg)
Definition
krnl.c:1108
k_start
int k_start()
Definition
krnl.c:1149
krnl.h
STKSZ
#define STKSZ
Definition
manyinone.ino:1241
t1
struct k_t * t1
Definition
krnlgen.ino:3
t2
struct k_t * t2
Definition
krnlgen.ino:3
k_t
Definition
krnl.h:323
Generated by
1.15.0