krnl 1
Loading...
Searching...
No Matches
k04periodic-clip.ino
Go to the documentation of this file.
1//220216
2#include <krnl.h>
3
4#define STK 110
5struct k_t *p1, *p2, *sem1, *sem2;
6
7char st1[STK], st2[STK];
8
9// HERE WE WILL LIGNT ON LED IF OVERFLOW ON sem1 == tperiodic is behind
10
11int x = 0;
14{
15 // simple periodic sampler/controller/...
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
42void 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
50int err;
51
52void 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
95void loop() {}
96
97
98extern "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}
#define STK
volatile int i
struct k_t * p1
Definition k03asleep.ino:22
struct k_t * p2
Definition k03asleep.ino:22
void tnoise()
struct k_t * sem1
char st2[110]
void setup()
int x
int err
char st1[110]
void tperiodic()
struct k_t * sem2
void k_sem_clip(unsigned char nr, int nrClip)
int clipp
void loop()
void k_eat_msec(unsigned int eatTime)
eat milliseconds - to mimik time consuming code
Definition krnl.c:246
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
#define DI()
Definition krnl.h:478
#define EI()
Definition krnl.h:479
Definition krnl.h:323
unsigned char nr
Definition krnl.h:327