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