krnl 1
Loading...
Searching...
No Matches
mutex3.ino
Go to the documentation of this file.
1#include <krnl.h>
2
3struct k_t *sem, *mutexSem, *pT1, *pT2;
4
5#define STK 100
6
7char stakH[STK], stakL[STK];
8
9
10// HOW TO DEBUG ??? :-)
11struct dataTp {
12 int i, j, k;
13};
14
15struct dataTp data;
16
17
18void initData() // must be called bef krnl is started
19{
20 data.i = data.j = data.k = 33;
21}
22
23void lowTask()
24{
25 int a, b, c;
26 while (1) {
27
28 k_wait(mutexSem, 0); // wait forever if needed
29 // >>>>>>> enter critical region
30 data.i++ ; data.j--; data.k; data.k = data.i + data.j;
31 // <<<<<<< leaving critical region
33
34 k_sleep(100);
35 }
36}
37
39{
40 int mya, myb, myc;
41
42 k_set_sem_timer(sem, 10); // let krnl send a signal to semaphore every 10th kernel timer tick
43
44 while (1) {
45 k_wait(sem, 0); // wait forever if ... for sampling
46
47 if (0 <= k_wait(mutexSem, -1) ) // = so no wait - might be dangerous bq you might miss region
48 {
49 // >>>>>>> enter critical region
50 mya = data.i; myb = data.j; myc = data.k;
51 // <<<<<<< leaving critical region
53 }
54
55
56 // do control stuff here with new data :-)
57 k_eat_time(5); // fake for doSamplingAlgorithmsAndControl();
58 }
59}
60void setup()
61{
62 initData();
63
64 k_init(2, 2, 0); // 2 tasks 2 semaphores
65 sem = k_crt_sem(0, 10);
66 mutexSem = k_crt_sem(1, 10);
67
70
71 k_start(1);
72
73}
74
75void loop() {}
76
77
#define STK
volatile int i
char a[150]
int k_signal(struct k_t *sem)
Signal a semaphore w eventually task shift.
Definition krnl.c:636
int k_sleep(int time)
let task sleep for a number of milliseconds
Definition krnl.c:445
int k_wait(struct k_t *sem, int timeout)
stand wait on semaphore call with timeout facility
Definition krnl.c:683
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_set_sem_timer(struct k_t *sem, int val)
attach a periodic timer to a semaphore, to be used for realtime
Definition krnl.c:586
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:544
int k_init(int nrTask, int nrSem, int nrMsg)
Definition krnl.c:1108
int k_start()
Definition krnl.c:1149
void initData()
Definition mutex3.ino:18
void setup()
Definition mutex3.ino:60
void controlTask()
Definition mutex3.ino:38
void lowTask()
Definition mutex3.ino:23
void loop()
Definition mutex3.ino:75
char stakL[100]
Definition mutex.ino:7
struct dataTp data
Definition mutex.ino:15
struct k_t * pT2
Definition mutex.ino:3
char stakH[100]
Definition mutex.ino:7
void controlTask()
Definition mutex.ino:38
struct k_t * pT1
Definition mutex.ino:3
void lowTask()
Definition mutex.ino:23
struct k_t * mutexSem
Definition mutex.ino:3
struct k_t * sem
Definition mutex.ino:3
volatile int k
Definition simpleisr.ino:7
volatile int j
Definition simpleisr.ino:7
Definition krnl.h:323