dit projekt
examples
k03asleep
k03asleep.ino
Go to the documentation of this file.
1
//220216
2
#include <
krnl.h
>
3
// one task loops and blink
4
5
6
// hint using k_sleep instead og k_eat_ticks
7
// k_eat_ticks eat your cpu time
8
9
// k_eat_ticks is busy waiting meaning it is using cpu time
10
// See in krnl.c approx line 597
11
// In contradition k_sleep deactivate the calling task for the time given as parameter
12
// and is reactivated (reinserted in the activeQ) after timeout
13
14
// ?: how much cputime does t2 used in one loop
15
// ?: how much time does t1 use in one loop
16
// ?: what will happen if you increase t1 priority to 9 ?
17
// ?: why is t2 running in bursts
18
// ?: Can you predict the minimum time it takes to k_eat_ticks(2000) if you do not know
19
// what other tasks are doing (a:no)
20
// ?: Can you predict minimu time it takes to k_sleep(2000); (a: yes)
21
22
struct
k_t
*
p1
, *
p2
;
23
#define STK 110
24
25
unsigned
char
s1
[
STK
],
s2
[
STK
];
26
27
void
t1
()
28
{
29
int
v;
30
31
while
(1) {
32
k_eat_msec
(2000);
// burn of cpu time for 2000 ticks
33
k_sleep
(2000);
// sleep - no cpu eat for 2000 ticks
34
}
35
}
36
37
void
t2
()
38
{
39
int
i
= 0;
40
while
(1) {
41
digitalWrite(13, HIGH);
42
k_sleep
(50);
43
digitalWrite(13, LOW);
44
//k_sleep(50);
45
//or
46
k_eat_msec
(50);
47
}
48
// do we keep up in pace ?
49
}
50
51
52
void
setup
()
53
{
54
int
res;
55
Serial.begin(115200);
56
while
(! Serial) ;
57
pinMode(13, OUTPUT);
58
59
k_init
(2, 0, 0);
// init with space for one task
60
// |--- no of mg Queues (0)
61
// |----- no of semaphores (0)
62
// |------- no of tasks (2)
63
64
// priority low number higher priority than higher number
65
p1
=
k_crt_task
(
t1
, 10,
s1
,
STK
);
// t1 as task, priority 9, 100 B stak
66
p2
=
k_crt_task
(
t2
, 10,
s2
,
STK
);
// t2 as task, priority 10, 100 B stak
67
68
Serial.println(
"bef start"
);
69
res =
k_start
();
// 1 milli sec tick speed
70
// you will never return from k_start
71
Serial.print(
"ups an error occured: "
); Serial.println(res);
72
while
(1) ;
73
}
74
75
void
loop
() {}
76
i
volatile int i
Definition:
k02twotasks.ino:29
s1
unsigned char s1[STK]
Definition:
k03asleep.ino:25
setup
void setup()
Definition:
k03asleep.ino:52
t2
void t2()
Definition:
k03asleep.ino:37
t1
void t1()
Definition:
k03asleep.ino:27
s2
unsigned char s2[STK]
Definition:
k03asleep.ino:25
p1
struct k_t * p1
Definition:
k03asleep.ino:22
STK
#define STK
Definition:
k03asleep.ino:23
p2
struct k_t * p2
Definition:
k03asleep.ino:22
loop
void loop()
Definition:
k03asleep.ino:75
k_eat_msec
void k_eat_msec(unsigned int eatTime)
Definition:
krnl.c:292
k_sleep
int k_sleep(int time)
Definition:
krnl.c:491
k_crt_task
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
Definition:
krnl.c:374
k_init
int k_init(int nrTask, int nrSem, int nrMsg)
Definition:
krnl.c:1152
k_start
int k_start()
Definition:
krnl.c:1192
krnl.h
k_t
Definition:
krnl.h:334
Generated by
1.9.1