krnl
1
Loading...
Searching...
No Matches
examples
oldexamples
k03priorityequalfixed
k03priorityequalfixed.ino
Go to the documentation of this file.
1
//220216
2
#include <
krnl.h
>
3
//
4
// sleppy waiting k_sleep("sleeptime in msec");
5
//
6
7
// Set t1 priority to 9 == highest priority
8
// What will happen ? What do you see on the led and the measurements
9
// REALTIME ?
10
// change priority of t1 to 9 (higher than t2) : does it matters ?
11
12
// gives a guess how cpu time is used in krnl "dummy time eating"
13
14
struct
k_t
*
p1
, *
p2
;
15
16
#define SS 140
17
unsigned
char
s1
[
SS
],
s2
[
SS
];
18
19
20
void
t1
()
21
{
22
int
v;
23
24
while
(1) {
25
k_sleep
(2000);
// eat time for 2000 ticks so mimic cpu usage == 2000 msec
26
}
27
}
28
29
unsigned
long
tStart
,
tStop
;
30
void
t2
()
31
{
32
int
i
= 0;
33
while
(1) {
34
Serial.println(
"start"
);
35
36
tStart
=
k_millis
();
37
38
for
(
int
i
= 0;
i
< 10 ;
i
++) {
// 10 loops of 100 milliseconds == 1 second ! ?
39
digitalWrite(13, HIGH);
40
k_sleep
(50);
41
digitalWrite(13, LOW);
42
k_sleep
(50);
43
}
44
45
tStop
=
k_millis
();
46
47
Serial.print( (
tStop
-
tStart
) / 1000.0);
48
Serial.println(
" did it take 1 second ?? :-)?"
);
49
}
50
}
51
52
// we do sleep instead of busy waiting so others can use cpu
53
// added breakout so led 13 is ON when dummy task is running
54
55
56
void
setup
()
57
{
58
int
res;
59
Serial.begin(115200);
60
while
(! Serial) ;
61
62
pinMode(13, OUTPUT);
63
/* using analyser(like analog discovery) D8 will be dummy running, D9 when first created task is running ,... */
64
/* remember to change in k_breakout*/
65
/* for (int i=8; i< 14; i++) { pinMode(i,OUTPUT); digitalWrite(i,LOW); } */
66
67
68
k_init
(2, 0, 0);
// init with space for one task
69
// |--- no of mg Queues (0)
70
// |----- no of semaphores (0)
71
// |------- no of tasks (2)
72
73
// priority low number higher priority than higher number
74
p1
=
k_crt_task
(
t1
, 10,
s1
,
SS
);
// t1 as task, priority 9, 100 B stak
75
p2
=
k_crt_task
(
t2
, 10,
s2
,
SS
);
// t2 as task, priority 10, 100 B stak
76
77
Serial.println(
"bef start"
);
78
res =
k_start
();
// 1 milli sec tick speed
79
// you will never return from k_start
80
Serial.print(
"ups an error occured: "
); Serial.println(res);
81
while
(1) ;
82
}
83
84
void
loop
() {}
85
86
87
extern
"C"
{
88
89
void
k_breakout
()
// called every task shift from dispatcher
90
{
91
92
93
if
(
pRun
->nr == 0)
94
{
95
PORTB = PORTB | B00100000;
// led13 (bit 5) on let the rest be untouched
96
}
97
else
{
98
PORTB = PORTB & B11011111;
// led off uno
99
}
100
}
101
/* using D8-D13 use following instead of teh code above*/
102
/* PORTB = (1 << pRun->nr); */
103
}
104
105
// on mega led is on portb bit 7 so use B10000000 and B011111111 above
i
volatile int i
Definition
k02twotasks.ino:29
s2
unsigned char s2[110]
Definition
k03asleep.ino:25
p1
struct k_t * p1
Definition
k03asleep.ino:22
p2
struct k_t * p2
Definition
k03asleep.ino:22
s1
unsigned char s1[110]
Definition
k03asleep.ino:25
tStart
unsigned long tStart
Definition
k03priorityequal.ino:28
SS
#define SS
Definition
k03priorityequal.ino:15
tStop
unsigned long tStop
Definition
k03priorityequal.ino:28
setup
void setup()
Definition
k03priorityequalfixed.ino:56
k_breakout
void k_breakout()
Definition
k03priorityequalfixed.ino:89
t2
void t2()
Definition
k03priorityequalfixed.ino:30
t1
void t1()
Definition
k03priorityequalfixed.ino:20
loop
void loop()
Definition
k03priorityequalfixed.ino:84
k_millis
unsigned long k_millis(void)
return no of millisec sincs start NB no leap seconds - its clean
Definition
krnl.c:1247
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
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