krnl
1
Loading...
Searching...
No Matches
examples
oldexamples
k01myfirstprogram
k01myfirstprogram.ino
Go to the documentation of this file.
1
#include <
krnl.h
>
2
3
// A small krnl program with two independent tasks
4
// They run at same priority so krnl will do timeslicing between them
5
// Watch LED and Serial TX
6
7
// NB only one task must use print if you dont protect the serial port by a critical section
8
9
struct
k_t
*
pt1
,
// pointer to hold reference
10
*
pt2
;
// to taskdescriptor for t1 and t2
11
12
char
s1
[200];
// stak for task t1
13
char
s2
[200];
// stak for task t2
14
15
void
t1
(
void
)
16
{
17
// a task must have an endless loop
18
// if you end and leave the task function - a crash will occur!!
19
// so this loop is the code body for task 1
20
21
while
(1) {
22
Serial.println(
"Hello World"
);
23
k_sleep
(1000);
// let task sleep for 1000 kernel ticks
24
}
// lenght of ticks in millisec is specified in
25
}
// k_start call called from setup
26
27
void
t2
(
void
)
28
{
29
// and task body for task 2
30
// runs independent of task t1
31
while
(1) {
32
digitalWrite(13,HIGH);
// led 13 ON
33
k_sleep
(500);
// sleep 500 ticks
34
digitalWrite(13,LOW);
// led 13 OFF
35
k_sleep
(500);
// and sleep
36
}
37
}
38
39
void
setup
()
40
{
41
Serial.begin(9600);
// for output from task 1
42
pinMode(13,OUTPUT);
// for blink on LED from task 2
43
44
// init krnl so you can create 2 tasks, no semaphores and no message queues
45
k_init
(2,0,0);
46
47
// two task are created
48
// |------------ function used for body code for task
49
// | |--------- priority (lower number= higher prio
50
// | | |------- array used for stak for task
51
// | | | |--- staksize for array s1
52
53
pt1
=
k_crt_task
(
t1
,11,
s1
,200);
54
pt2
=
k_crt_task
(
t2
,11,
s2
,200);
55
56
57
// NB-1 remember an Arduino has only 2-8 kByte RAM
58
// NB-2 remember that stak is used in function calls for
59
// - return address
60
// - registers stakked
61
// - local variabels in a function
62
// So having 200 Bytes of stak excludes a local variable like ...
63
// int arr[400];
64
// krnl call k_unused_stak returns size of unused stak
65
// Both task has same priority so krnl will shift between the
66
// tasks every 10 milli second (speed set in k_start)
67
68
k_start
(1);
// start kernel with tick speed 1 milli seconds
69
}
70
71
void
loop
(){
/* loop will never be called */
}
72
73
74
setup
void setup()
Definition
k01myfirstprogram.ino:39
t2
void t2(void)
Definition
k01myfirstprogram.ino:27
t1
void t1(void)
Definition
k01myfirstprogram.ino:15
loop
void loop()
Definition
k01myfirstprogram.ino:71
pt2
struct k_t * pt2
Definition
k02twotasks.ino:7
pt1
struct k_t * pt1
Definition
k02twotasks.ino:6
s2
unsigned char s2[110]
Definition
k03asleep.ino:25
s1
unsigned char s1[110]
Definition
k03asleep.ino:25
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