krnl 1
Loading...
Searching...
No Matches
k06syncsem.ino
Go to the documentation of this file.
1//220216
2#include <krnl.h>
3// one task loops and blink
4// k_wait on a semaphore ensures proper timing
5
6// task2 is using partly k_eat_ticks in the loop
7// Try to lower tasl2 priority to 11 and see what happens
8//?: what do you observe ?
9// hint k_eat_ticks mimic cpu usage so if a task uses 2000 ticks cpu time then it should not
10// have high(est) priority because lower priority task will then starve: not getting cpu time
11//
12// If you as starting point gives all same prioriryt there is a high chance taht all will more or less get
13// what the need.
14
15// even better is to give high priority task (like sampling and control) a high priority
16//
17// you can mimic high cpu usage for task1 with insertion of a k_eat_ticks in the code below (is commented out)
18
19// The commented out eat uses 90 ticks so task1 will use 90 out of 100 (the time in loop) of the CPU
20// or 90% of cpu power ...
21// Beware of high prority task using a lot of cpu power
22// life is life
23
24// ?: what will happen if equal priorities and the k_eat_ticks(90) in task1 ?
25// when task2 is u´ni the k_eat_ticks(2000) then it will be in competition with task1
26// the have same priority so they will share cpu time (round robbin)
27// So it will take 180 ticks for task1 to execute k_eat_ticks(90) and at same time task2 will et 90 ticks
28//
29// At the same time the krnl signals to semaphore s1 every 100 ticks. But a loop will take 180 ticks. So
30// will will come behind with 80 ticks for every loop(when task2 i in k_eat_ticks(2000)
31
32// you can see than k_Semval will never pass 50. This is because k_crt_sem is called with 50 as max.
33//
34// ?: can you model it ?
35
36
37struct k_t *p1, *p2 , *s1;
38
39
40int pendingWait = 0, loopCnt = 0;
41
42int er2;
43void task1()
44{
45
46 //let krnl send a signal to the semaphore every 100 ticks
47 // ensure real time capabilities
48
49 k_set_sem_timer(s1, 200);
50
51 while (1) {
52
53 loopCnt++;
54
55
56 // play with second parameter: timeout value 0, -1, pos value
57 // 2nd parm, 0: wait forever,
58 // pos num: timeout
59 // neg num : we will not just only eat a signal if one is already present
60 // second parameter is timeout: 0: wait forever if needed, -1 : no wait at all, pos int wat up until <value> ticks
61
62
63 // Question are we behind the period : timer generated signal to semaphore did occur before we entered the wait call waiting
64 // on next tick
65 //
66 // so return values:
67 // 0: we came before signal so we has been sleeping a bit - so we did arrive before the timer generated signal too the semaphore
68 // we are NOT behind
69
70 // 1: there was a signal(key) waiting on us - we did eat it - but we arrived after signal - we were behind ...
71
72 // -1: did not get a signal before timeout (if it was k_wait(s1,22) : we will at most 22 ticks before timeout
73
74 // -2: we will not wait and there was no signal for us: k_wait(s1,-1)
75
76
77
78 er2 = k_wait(s1, 100);
79
80
81 Serial.print(loopCnt);
82
83 Serial.print(" - ret from k_wait: "); Serial.println(er2);
84
85
86 k_eat_msec(100);
87
88 }
89}
90
91
92void task2()
93{
94 unsigned int loopNrCpy;
95 unsigned long tt;
96 while (1) {
97
98 k_sleep(200); // just eating time
99 k_eat_msec(150); // nasty !!! yo are requesting a heavy load on the processor
100 }
101}
102
103
104#define STK 110
105char a1[STK], a2[STK];
106
107void setup()
108{
109 int res;
110 Serial.begin(115200);
111 while (! Serial) ;
112 for (int i = 8 ; i < 14; i++) {
113 pinMode(i, OUTPUT);
114 digitalWrite(i, LOW);
115 }
116
117 k_init(2, 1, 0); // init with space for two tasks and one semaphore
118 // |--- no of mg Queues (0)
119 // |----- no of semaphores (0)
120 // |------- no of tasks (2)
121
122 // priority low number higher priority than higher number
123 p1 = k_crt_task(task1, 10, a1, STK); // task1 as task, priority 10, 100 B stak
124 p2 = k_crt_task(task2, 11, a2, STK); // task2 as task, priority 11 == lower than t1, 100 B stak
125
126 s1 = k_crt_sem(0, 1); // crt sem
127
128 res = k_start(); // 1 milli sec tick speed
129 // you will never return from k_start
130 Serial.print("ups an error occured: "); Serial.println(res);
131 while (1) ;
132}
133
134// TRY TO SET PRIORITIES EQUAL
135// TRY TO SET task2 higher thatn t1 (priority) stil at right time in task1 ?
136
137void loop() {}
138
139
140extern "C" {
141 void k_breakout() // called every task shift from dispatcher
142 {
143 PORTB = (1 << pRun->nr);
144 // on MEGA use PORTA
145 }
146}
#define STK
volatile int i
unsigned char s1[110]
Definition k03asleep.ino:25
struct k_t * p1
Definition k03asleep.ino:22
struct k_t * p2
Definition k03asleep.ino:22
char a1[110]
char a2[110]
void setup()
void k_breakout()
int er2
int loopCnt
int pendingWait
void task2()
void task1()
void loop()
void k_eat_msec(unsigned int eatTime)
eat milliseconds - to mimik time consuming code
Definition krnl.c:249
struct k_t * pRun
Definition krnl.c:151
int k_sleep(int time)
let task sleep for a number of milliseconds
Definition krnl.c:448
int k_wait(struct k_t *sem, int timeout)
stand wait on semaphore call with timeout facility
Definition krnl.c:686
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:331
int k_set_sem_timer(struct k_t *sem, int val)
attach a periodic timer to a semaphore, to be used for realtime
Definition krnl.c:589
struct k_t * k_crt_sem(int init_val, int maxvalue)
change task priority and reinserts it in task Q and do a task shift
Definition krnl.c:547
int k_init(int nrTask, int nrSem, int nrMsg)
Definition krnl.c:1111
int k_start()
Definition krnl.c:1152
Definition krnl.h:323