dit projekt
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 
26 struct shDataTp getDataInCritRegion(void)
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 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() {}
struct k_t * s1
Definition: k07mutexsem.ino:6
void setup()
Definition: k07mutexsem.ino:68
void t2()
Definition: k07mutexsem.ino:52
void t1()
Definition: k07mutexsem.ino:39
char a2[STK]
Definition: k07mutexsem.ino:66
struct shDataTp sharedData
Definition: k07mutexsem.ino:14
struct k_t * p1
Definition: k07mutexsem.ino:6
void saveDataInCritRegion(int v)
Definition: k07mutexsem.ino:16
#define STK
Definition: k07mutexsem.ino:65
struct k_t * p2
Definition: k07mutexsem.ino:6
char a1[STK]
Definition: k07mutexsem.ino:66
struct shDataTp getDataInCritRegion(void)
Definition: k07mutexsem.ino:26
void loop()
Definition: k07mutexsem.ino:93
struct k_t * semmutex
Definition: k07mutexsem.ino:6
int k_signal(struct k_t *sem)
Definition: krnl.c:681
int k_sleep(int time)
Definition: krnl.c:491
struct k_t * k_crt_sem(int init_val, int maxvalue)
Definition: krnl.c:589
int k_wait(struct k_t *sem, int timeout)
Definition: krnl.c:728
int k_set_sem_timer(struct k_t *sem, int val)
Definition: krnl.c:631
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
Definition: krnl.c:374
int k_init(int nrTask, int nrSem, int nrMsg)
Definition: krnl.c:1152
int k_start()
Definition: krnl.c:1192
Definition: krnl.h:334