krnl 1
Loading...
Searching...
No Matches
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
#define STK
volatile int i
unsigned char s2[110]
Definition k03asleep.ino:25
void setup()
Definition k03asleep.ino:52
void t2()
Definition k03asleep.ino:37
void t1()
Definition k03asleep.ino:27
struct k_t * p1
Definition k03asleep.ino:22
struct k_t * p2
Definition k03asleep.ino:22
unsigned char s1[110]
Definition k03asleep.ino:25
void loop()
Definition k03asleep.ino:75
void k_eat_msec(unsigned int eatTime)
eat milliseconds - to mimik time consuming code
Definition krnl.c:246
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