krnl 1
Loading...
Searching...
No Matches
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
9struct k_t *pt1, // pointer to hold reference
10 *pt2; // to taskdescriptor for t1 and t2
11
12char s1[200]; // stak for task t1
13char s2[200]; // stak for task t2
14
15void 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
27void 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
39void 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
71void loop(){ /* loop will never be called */ }
72
73
74
void setup()
void t2(void)
void t1(void)
void loop()
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
Definition krnl.h:323