krnl
k07mutexsem.ino
Go to the documentation of this file.
1 //220216
2 #include <krnl.h>
3 // one task loops and blink
4 // k_wait ensures proper timing
5 
6 struct k_t *p1, *p2, *s1, *semmutex;
7 
8 // shared data
9 struct shDataTp {
10  int v;
11  int counter;
12 };
13 
14 struct shDataTp sharedData = {0, 0};
15 
17 {
18  k_wait(semmutex, 0);
19  {
20  sharedData.v = v;
22  }
24 }
25 
27 {
28  struct shDataTp tmp;
29  k_wait(semmutex, 0);
30 
32  tmp = sharedData;
33 
35  return tmp;
36 }
37 
38 // the sampler task
39 void t1()
40 {
41  int v;
42  k_set_sem_timer(s1, 100);
43  while (1) {
44 
45  k_wait(s1, 0); //wait until a kick comes
46 
47  v = analogRead(A0);
49  }
50 }
51 
52 void t2()
53 {
54  static struct shDataTp v;
55  while (1) {
56 
57  k_sleep(345);
59  Serial.print(v.v);
60  Serial.print(" ");
61  Serial.println(v.counter);
62  } // do we keep up in pace ?
63 }
64 
65 #define STK 110
66 char a1[STK],a2[STK];
67 
68 void setup()
69 {
70  int res;
71  Serial.begin(115200);
72  while (! Serial) ;
73  pinMode(13, OUTPUT);
74 
75  k_init(2, 2, 0); // init with space for one task
76  // |--- no of mg Queues (0)
77  // |----- no of semaphores (0)
78  // |------- no of tasks (2)
79 
80  // priority low number higher priority than higher number
81  p1 = k_crt_task(t1, 10, a1,STK); // t1 as task, priority 10, 100 B stak
82  p2 = k_crt_task(t2, 10, a2,STK); // t1 as task, priority 10, 100 B stak
83 
84  s1 = k_crt_sem(0, 10);
85  semmutex = k_crt_sem(1, 10); // must be 1 otherwise no one can come inside
86  Serial.println("bef start");
87  res = k_start(1); // 1 milli sec tick speed
88  // you will never return from k_start
89  Serial.print("ups an error occured: "); Serial.println(res);
90  while (1) ;
91 }
92 
93 void loop() {}
t1
void t1()
Definition: k07mutexsem.ino:39
p1
struct k_t * p1
Definition: k07mutexsem.ino:6
saveDataInCritRegion
void saveDataInCritRegion(int v)
Definition: k07mutexsem.ino:16
loop
void loop()
Definition: k07mutexsem.ino:93
p2
struct k_t * p2
Definition: k07mutexsem.ino:6
k_sleep
int k_sleep(int time)
Definition: krnl.c:532
s1
struct k_t * s1
Definition: k07mutexsem.ino:6
k_crt_task
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
Definition: krnl.c:421
STK
#define STK
Definition: k07mutexsem.ino:65
a2
char a2[STK]
Definition: k07mutexsem.ino:66
shDataTp
Definition: k07mutexsem.ino:9
setup
void setup()
Definition: k07mutexsem.ino:68
k_signal
int k_signal(struct k_t *sem)
Definition: krnl.c:704
k_init
int k_init(int nrTask, int nrSem, int nrMsg)
Definition: krnl.c:1172
a1
char a1[STK]
Definition: k07mutexsem.ino:66
k_start
int k_start(int tm)
Definition: krnl.c:1213
k_t
Definition: krnl.h:329
semmutex
struct k_t * semmutex
Definition: k07mutexsem.ino:6
k_set_sem_timer
int k_set_sem_timer(struct k_t *sem, int val)
Definition: krnl.c:652
k_wait
int k_wait(struct k_t *sem, int timeout)
Definition: krnl.c:754
shDataTp::counter
int counter
Definition: k07mutexsem.ino:11
k_crt_sem
struct k_t * k_crt_sem(char init_val, int maxvalue)
Definition: krnl.c:610
krnl.h
shDataTp::v
int v
Definition: k07mutexsem.ino:10
sharedData
struct shDataTp sharedData
Definition: k07mutexsem.ino:14
getDataInCritRegion
struct shDataTp getDataInCritRegion(void)
Definition: k07mutexsem.ino:26
t2
void t2()
Definition: k07mutexsem.ino:52