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