dit projekt
k04periodic-clip.ino
Go to the documentation of this file.
1 //220216
2 #include <krnl.h>
3 
4 #define STK 110
5 struct k_t *p1, *p2, *sem1, *sem2;
6 
7 char st1[STK], st2[STK];
8 
9 // HERE WE WILL LIGNT ON LED IF OVERFLOW ON sem1 == tperiodic is behind
10 
11 int x = 0;
12 int clipp;
13 void tperiodic()
14 {
15  // simple periodic sampler/controller/...
16  k_set_sem_timer(sem1, 200);
17 
18  while (1) {
19  k_wait(sem1, 0);
20 
21  k_eat_msec(140); // we eat cpu time
22 
23  Serial.print(x++);
24  Serial.print(" nr of clip on sem1 ");
25 
26  DI(); // silencio disable interrupt
27  clipp = sem1->clip;
28  EI();
29 
30  Serial.println(clipp);
31 
32  // reset bit 5 LED13 if there has been an overflow on the semaphore
33  DI();
34  PORTB &= B11011111; // reset bit 5 == led13 on uno (on mega its bit7) 0x80 or B10000000
35  EI();
36  }
37 }
38 
39 
40 // sem->clip
41 
42 void tnoise()
43 {
44  while (1) {
45  k_eat_msec( 600 ); // we eat between 30 and 200 msec of time
46  k_sleep(1000); // and sleep for 500 msec
47  }
48 }
49 
50 int err;
51 
52 void setup()
53 {
54 
55  for (int i = 8; i < 14; i++) {
56  pinMode(i, OUTPUT);
57  digitalWrite(i, LOW);
58  }
59 
60  Serial.begin(115200);
61 
62  k_init(2, 1, 0); // init with space for three tasks
63 
64  // priority low number higher priority than higher number
65  //Task 1
66  p1 = k_crt_task(tperiodic, 10, st1, STK);
67 
68  //Task 2
69  p2 = k_crt_task(tnoise, 11 , st2, STK);
70 
71  sem1 = k_crt_sem(0, 1);
72 
73  err = k_start(); // 1 milli sec tick speed
74 
75  Serial.print("start error ");
76  Serial.print(err); // if error is in -1 to -20 its because you need to adjust in k_init
77 
78 }
79 
80 
81 // LED13 will go ON if overflow på semaphore sem1
82 // YOU can see nr of clip/saturatino situation in the printotu (terminal)
83 
84 // FOr tnoise:
85 // prio = 11 no overflow bq tperodic has highest prio
86 // prio =10 overflow willl occur bq tnoise is eating 600 msec and therefore share CPU
87 // 50/50 with tperiodic so eating 140 msec in every 200 msec period can take up to 280 msec(guessing) and therfore its are being
88 // tnoise prio < 10 makes even worse
89 
90 // Hack in tperiodic: switch off led 13 by PORTB = .... so we can observe reoccurence of clip
91 
92 
93 
94 
95 void loop() {}
96 
97 
98 extern "C" {
99 
100  void k_sem_clip(unsigned char nr, int nrClip)
101  {
102  PORTB = 0x20; //led13
103  /* mega PORTB=0x80; // bit 7 */
104  }
105 }
volatile int i
Definition: k02twotasks.ino:29
void tnoise()
struct k_t * sem1
char st2[STK]
void setup()
int x
int err
struct k_t * p1
void tperiodic()
#define STK
struct k_t * p2
struct k_t * sem2
char st1[STK]
void k_sem_clip(unsigned char nr, int nrClip)
int clipp
void loop()
void k_eat_msec(unsigned int eatTime)
Definition: krnl.c:292
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
#define DI()
Definition: krnl.h:489
#define EI()
Definition: krnl.h:490
void int nrClip
Definition: krnl.h:1110
Definition: krnl.h:334
unsigned char nr
Definition: krnl.h:338
volatile int clip
Definition: krnl.h:350