/
krnl.c
1239 lines (958 loc) · 29.7 KB
/
krnl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************
* *
* *
* __ _ ___ ____ ____ ___ _ *
* | |/ ] / _]| \ | \ / _]| | *
* | ' / / [_ | D )| _ | / [_ | | *
* | \ | _]| / | | || _]| |___ *
* | || [_ | \ | | || [_ | | *
* | . || || . \| | || || | *
* |__|\_||_____||__|\_||__|__||_____||_____| *
* *
* *
* krnl.c part of kernel KRNL *
* based on "snot" *
* *
* June, 2014 *
* Feb 2015 *
* Author: jdn *
* *
******************************************************
* *
* (simple not - not ?! :-) ) *
* my own small KeRNeL adapted for Arduino *
* *
* this version adapted for Arduino *
* *
* (C) 2012,2013,2014 *
* *
* Jens Dalsgaard Nielsen <jdn@es.aau.dk> *
* http://es.aau.dk/staff/jdn *
* Section of Automation & Control *
* Aalborg University, *
* Denmark *
* *
* "THE BEER-WARE LICENSE" (frit efter PHK) *
* <jdn@es.aau.dk> wrote this file. As long as you *
* retain this notice you can do whatever you want *
* with this stuff. If we meet some day, and you think*
* this stuff is worth it ... *
* you can buy me a beer in return :-) *
* or if you are real happy then ... *
* single malt will be well received :-) *
* *
* Use it at your own risk - no warranty *
* *
* tested with duemilanove w/328, uno R3, *
* seeduino 1280 and mega2560 *
*****************************************************/
//http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus nice info
#include "krnl.h"
#include <avr/wdt.h>
// CPU frequency - for adjusting delays
#if (F_CPU == 16000000)
#pragma message ("krnl detected 16 MHz" )
#else
#pragma message ("krnl detected 8 MHz")
#endif
#if (KRNL_VRS != 2002)
#error "KRNL VERSION NOT UPDATED in krnl.c /JDN"
#endif
#include <avr/interrupt.h>
#include <stdlib.h>
/*
#ifdef __cplusplus
extern "C" {
#endif
*/
/* which timer to use for krnl heartbeat
* timer 0 ( 8 bit) is normally used by millis - avoid !
* timer 1 (16 bit) DEFAULT
* timer 2 ( 8 bit)
* timer 3 (16 bit) 1280/2560 only (MEGA)
* timer 4 (16 bit) 1280/2560 only (MEGA)
* timer 5 (16 bit) 1280/2560 only (MEGA)
*/
#if (KRNLTMR == 0)
// normally not goood bq of arduino sys timer so you wil get a compile error
// 8 bit timer !!!
#define KRNLTMRVECTOR TIMER0_OVF_vect
#define TCNTx TCNT0
#define TCCRxA TCCR0A
#define TCCRxB TCCR0B
#define TCNTx TCNT0
#define OCRxA OCR0A
#define TIMSKx TIMSK0
#define TOIEx TOIE0
#define PRESCALE 0x07
#define COUNTMAX 255
#define DIVV 15.625
#define DIVV8 7.812
#elif (KRNLTMR == 1)
#define KRNLTMRVECTOR TIMER1_OVF_vect
#define TCNTx TCNT1
#define TCCRxA TCCR1A
#define TCCRxB TCCR1B
#define TCNTx TCNT1
#define OCRxA OCR1A
#define TIMSKx TIMSK1
#define TOIEx TOIE1
#define PRESCALE 0x04
#define COUNTMAX 65535
#define DIVV 62.5
#define DIVV8 31.25
#elif (KRNLTMR == 2)
// 8 bit timer !!!
#define KRNLTMRVECTOR TIMER2_OVF_vect
#define TCNTx TCNT2
#define TCCRxA TCCR2A
#define TCCRxB TCCR2B
#define TCNTx TCNT2
#define OCRxA OCR2A
#define TIMSKx TIMSK2
#define TOIEx TOIE2
#define PRESCALE 0x07
#define COUNTMAX 255
#define DIVV 15.625
#define DIVV8 7.812
#elif (KRNLTMR == 3)
#define KRNLTMRVECTOR TIMER3_OVF_vect
#define TCNTx TCNT3
#define TCCRxA TCCR3A
#define TCCRxB TCCR3B
#define TCNTx TCNT3
#define OCRxA OCR3A
#define TIMSKx TIMSK3
#define TOIEx TOIE3
#define PRESCALE 0x04
#define COUNTMAX 65535
#define DIVV 62.5
#define DIVV8 31.25
#elif (KRNLTMR == 4)
#define KRNLTMRVECTOR TIMER4_OVF_vect
#define TCNTx TCNT4
#define TCCRxA TCCR4A
#define TCCRxB TCCR4B
#define TCNTx TCNT4
#define OCRxA OCR4A
#define TIMSKx TIMSK4
#define TOIEx TOIE4
#define PRESCALE 0x04
#define COUNTMAX 65535
#define DIVV 62.5
#define DIVV8 31.25
#elif (KRNLTMR == 5)
#define KRNLTMRVECTOR TIMER5_OVF_vect
#define TCNTx TCNT5
#define TCCRxA TCCR5A
#define TCCRxB TCCR5B
#define TCNTx TCNT5
#define OCRxA OCR5A
#define TIMSKx TIMSK5
#define TOIEx TOIE5
#define PRESCALE 0x04
#define COUNTMAX 65535
#define DIVV 62.5
#define DIVV8 31.25
#else
#pragma err "no valid tmr selected"
#endif
//----------------------------------------------------------------------------
struct k_t *task_pool, // array of descriptors for tasks
*sem_pool, // .. for semaphores
AQ, // Q head for active Q
*pmain_el, // procesdecriptor for main
*pAQ, // head of activeQ (AQ)
*pDmy, // ref to dummy task
*pRun, // who is running ?
*pSleepSem; // one semaphor for all to sleep at
struct k_msg_t *send_pool; // ptr to array for msg sem pool
int k_task, k_sem, k_msg; // how many did you request in k_init of descriptors ?
char nr_task = 0, nr_sem = 0, nr_send = 0; // counters for created KeRNeL items
volatile char krnl_preempt_flag = 1; //1: preempt, 0 : non preempt
volatile char k_running = 0, k_err_cnt = 0;
volatile unsigned int tcntValue; // counters for timer system
volatile int fakecnt, // counters for letting timer ISR go multipla faster than krnl timer
fakecnt_preset; // ...
static volatile char stopp = 0; // main will loop on stop as dummy task
unsigned long k_millis_counter = 0;
unsigned int k_tick_size;
int tmr_indx; // for travelling Qs in tmr isr
/**
* just for eating time
* eatTime in msec's
*/
void
k_eat_time (unsigned int eatTime)
{
// delayMicroseconds (in wiring.c) do busy waiting by running in a loop and execute
// a well known number og asm instruction with known time lenght
// so its only pure straight math
while (eatTime > 10) {
eatTime -= 10;
delayMicroseconds (10000);
}
delayMicroseconds (eatTime * 1000);
}
//---QOPS---------------------------------------------------------------------
void
enQ (struct k_t *Q, struct k_t *el)
{
el->next = Q;
el->pred = Q->pred;
Q->pred->next = el;
Q->pred = el;
}
//----------------------------------------------------------------------------
struct k_t *
deQ (struct k_t *el)
{
el->pred->next = el->next;
el->next->pred = el->pred;
return (el);
}
//----------------------------------------------------------------------------
void
prio_enQ (struct k_t *Q, struct k_t *el)
{
char prio = el->prio;
Q = Q->next; // bq first elm is Q head itself
while (Q->prio <= prio) { // find place before next with lower prio
Q = Q->next;
}
el->next = Q;
el->pred = Q->pred;
Q->pred->next = el;
Q->pred = el;
}
//---HW timer IRS-------------------------------------------------------------
//---HW timer IRS-------------------------------------------------------------
//---HW timer IRS-------------------------------------------------------------
//---HW timer IRS-------------------------------------------------------------
//---HW timer IRS-------------------------------------------------------------
//------------timer section---------------------------------------------------
/*
* The KRNL Timer is driven by timer
*
*
* Install the Interrupt Service Routine (ISR) for Timer2 overflow.
* This is normally done by writing the address of the ISR in the
* interrupt vector table but conveniently done by using ISR()
*
* Timer2 reload value, globally available
*/
struct k_t *pE;
ISR (KRNLTMRVECTOR, ISR_NAKED)
{
// no local vars ! I think
PUSHREGS ();
TCNTx = tcntValue; // Reload the timer
if (!k_running) { // obvious
goto exitt;
}
fakecnt--; // for very slow k_start values
//bq timer cant run so slow (8 bit timers at least)
if (0 < fakecnt) { // how often shall we run KeRNeL timer code ?
goto exitt;
}
fakecnt = fakecnt_preset; // now it's time for doing RT stuff
k_millis_counter += k_tick_size; // my own millis counter
// the following may look crazy: to go through all semaphores and tasks
// but you may have 3-4 tasks and 3-6 semaphores in your code
// so - seesm to be efficient :-)
// so - it's a good idea not to init krnl with more items
// (tasks/Sem/msg descriptors than needed)
pE = sem_pool; // Semaphore timer - check timers on semaphores - they may be cyclic
for (tmr_indx = 0; tmr_indx < nr_sem; tmr_indx++) {
if (0 < pE->cnt2) { // timer on semaphore ?
pE->cnt2--; // yep decrement it
if (pE->cnt2 <= 0) { // timeout ?
pE->cnt2 = pE->cnt3; // preset again - if cnt3 == 0 and >= 0 the rep timer
ki_signal (pE); //issue a signal to the semaphore
}
}
pE++;
}
pE = task_pool; // Chk timers on tasks - they may be one shoot waiting
for (tmr_indx = 0; tmr_indx < nr_task; tmr_indx++) {
if (0 < pE->cnt2) { // timer active on task ?
pE->cnt2--; // yep so let us do one down count
if (pE->cnt2 <= 0) { // timeout ? ( == 0 )
ki_signal ((struct k_t *) (pE->cnt3));
pE->cnt2 = -1; // indicate timeout in this semQ
}
}
pE++;
}
if (krnl_preempt_flag) {
prio_enQ (pAQ, deQ (pRun)); // round robbin
K_CHG_STAK ();
}
exitt:
POPREGS ();
RETI ();
}
//----------------------------------------------------------------------------
// inspired from ...
// http://arduinomega.blogspot.dk/2011/05/timer2-and-overflow-interrupt-lets-get.html
// Inspiration from http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/
// Inspiration from "Multitasking on an AVR" by Richard Barry, March 2004
// and http://www.control.aau.dk/~jdn/kernels/krnl/
//----------------------------------------------------------------------------
// avrfreaks.net
// and my old kernel from last century
// and a lot other stuff
// basic concept from my own very old kernels dated back bef millenium
void __attribute__ ((naked, noinline)) ki_task_shift (void)
{
PUSHREGS (); // push task regs on stak so we are rdy to task shift
K_CHG_STAK ();
POPREGS (); // restore regs
RETI (); // and do a reti NB this also enables interrupt !!!
}
//----------------------------------------------------------------------------
struct k_t *
k_crt_task (void (*pTask) (void), char prio, char *pStk, int stkSize)
{
struct k_t *pT;
int i;
char *s;
if ((k_running) || ((prio <= 0) || (DMY_PRIO < prio))
|| (k_task <= nr_task)) {
goto badexit;
}
// let's go
pT = task_pool + nr_task; // lets take a task descriptor
pT->nr = nr_task;
nr_task++;
pT->cnt2 = 0; // no time out running on you for the time being
pT->cnt3 = 0; // no time out semaphore
pT->cnt1 = (int) (pStk); // ref to my stack
// stack paint :-)
for (i = 0; i < stkSize; i++) { // put hash code on stak to be used by k_unused_stak()
pStk[i] = STAK_HASH;
}
s = pStk + stkSize - 1; // now we point on top of stak
*(s--) = 0x00; // 1 byte safety distance
// an interrupt do only push PC on stack by HW - can be 2 or 3 bytes
// depending of 368/.../1280/2560
*(s--) = lo8 (pTask); // so top now holds address of function
*(s--) = hi8 (pTask); // which is code body for task
// NB NB 2560 use 3 byte for call/ret addresses the rest only 2
#if defined (__AVR_ATmega2560__)
*(s--) = EIND; // best guess : 3 byte addresses !!!
#endif
// r1 is the socalled zero value register
// see https://gcc.gnu.org/wiki/avr-gcc
// can tmp be non zero (multiplication etc)
*(s--) = 0x00; // r1
*(s--) = 0x00; // r0
*(s--) = 0x00; // sreg
//1280 and 2560 need to save rampz reg just in case
#if defined (__AVR_ATmega2560__) || defined (__AVR_ATmega1280__)
*(s--) = RAMPZ; // best guess
*(s--) = EIND; // best guess
#endif
for (i = 0; i < 30; i++) { //r2-r31 = 30 regs
*(s--) = 0x00;
}
pT->sp_lo = lo8 (s); // now we just need to save stakptr
pT->sp_hi = hi8 (s); // in thread descriptor
// HW DEPENDENT PART - ENDE
pT->prio = prio; // maxv for holding org prio for inheritance
pT->maxv = (int) prio;
prio_enQ (pAQ, pT); // and put task in active Q
return (pT);
badexit:
k_err_cnt++;
return (NULL);
}
//----------------------------------------------------------------------------
int
freeRam (void)
{
extern int __heap_start, *__brkval;
int v;
// ho
return ((int) &v -
(__brkval == 0 ? (int) &__heap_start : (int) __brkval));
}
//----------------------------------------------------------------------------
int
k_sleep (int time)
{
return k_wait (pSleepSem, time);
}
//----------------------------------------------------------------------------
int
k_unused_stak (struct k_t *t)
{
int i = 0;
char *pstk;
if (t) { // another task or yourself - NO CHK of validity !
pstk = (char *) (t->cnt1);
} else {
pstk = (char *) (pRun->cnt1);
}
DI ();
// look for stack paint
while (*pstk == STAK_HASH) {
pstk++;
i++;
}
EI ();
return (i);
}
//----------------------------------------------------------------------------
int
k_set_prio (char prio)
{
int i;
if (!k_running) {
return (-1);
}
DI ();
if ((prio <= 0) || (DMY_PRIO <= prio)) {
// not legal value my friend
EI ();
return (-2);
}
i = pRun->prio;
pRun->prio = prio;
prio_enQ (pAQ, deQ (pRun));
ki_task_shift ();
EI ();
return (i);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
struct k_t *
k_crt_sem (char init_val, int maxvalue)
{
struct k_t *sem;
if (k_running) {
return (NULL);
}
if ((init_val < 0) || (32000 < init_val) || (maxvalue < -32000)
|| (32000 < maxvalue)) {
goto badexit;
}
if (k_sem <= nr_sem) {
goto badexit;
}
sem = sem_pool + nr_sem;
sem->nr = nr_sem;
nr_sem++;
sem->cnt2 = 0; // no timer running
sem->next = sem->pred = sem;
sem->prio = QHD_PRIO;
sem->cnt1 = init_val;
sem->maxv = maxvalue;
sem->clip = 0;
return (sem);
badexit:
k_err_cnt++;
return (NULL);
}
//----------------------------------------------------------------------------
int
k_set_sem_timer (struct k_t *sem, int val)
{
// there is no k_stop_sem_timer fct just call with val== 0
if (val < 0) {
return (-1); // bad value
}
DI ();
sem->cnt2 = sem->cnt3 = val; // if 0 then timer is not running - so
EI ();
return (0);
}
//----------------------------------------------------------------------------
int
ki_signal (struct k_t *sem)
{
DI (); // just in case
if (sem->cnt1 < sem->maxv) {
goto normal;
}
if (32000 > sem->clip) {
sem->clip++;
}
// here we are on bad clip failure no signal takes place
// signal is lost !!!
#ifdef KRNLBUG
k_sem_clip (sem->nr, sem->clip);
#endif
return (-1);
normal:
sem->cnt1++; // Salute to Dijkstra
if (sem->cnt1 <= 0) {
sem->next->cnt2 = 0; // return code == ok
prio_enQ (pAQ, deQ (sem->next));
return (0);
}
return (1); // just delivered a signal - no task was waiting
}
//----------------------------------------------------------------------------
int
k_prio_signal (struct k_t *sem, char prio)
{
int res;
DI ();
res = ki_signal (sem);
// set prio
pRun->prio = prio;
prio_enQ (pAQ, deQ (pRun));
ki_task_shift ();
EI ();
return (res);
}
//----------------------------------------------------------------------------
int
k_signal (struct k_t *sem)
{
int res;
DI ();
res = ki_signal (sem); // 1: ok no task to AQ, 0: ok task to AQ
if (res == 0) {
ki_task_shift (); // bq maybe started task has higher prio than me
}
EI ();
return (res);
}
//----------------------------------------------------------------------------
int
ki_wait (struct k_t *sem, int timeout)
{
// used by msg system
DI ();
if (0 < sem->cnt1) {
sem->cnt1--; // Salute to Dijkstra
return (1); // ok: 1 bq we are not suspended
}
if (timeout == -1) { // no luck, dont want to wait so bye bye
return (-1);
}
// from here we want to wait
pRun->cnt2 = timeout; // 0 == wait forever
if (timeout) {
pRun->cnt3 = (int) sem; // nasty keep ref to semaphore
}
// so we can be removed if timeout occurs
sem->cnt1--; // Salute to Dijkstra
enQ (sem, deQ (pRun));
ki_task_shift ();
// back again - have semaphore received signal or timeout ?
pRun->cnt3 = 0; // reset ref to timer semaphore
return ((char) (pRun->cnt2)); // 0: ok, -1: timeout
}
//----------------------------------------------------------------------------
int
k_wait (struct k_t *sem, int timeout)
{
int retval;
retval = ki_wait (sem, timeout);
EI ();
return retval; // 0: ok, -1: timeout
}
//----------------------------------------------------------------------------
int
k_wait_lost (struct k_t *sem, int timeout, int *lost)
{
DI ();
if (lost != NULL) {
*lost = sem->clip;
sem->clip = 0;
}
return k_wait (sem, timeout);
}
//----------------------------------------------------------------------------
int
k_sem_signals_lost (struct k_t *sem)
{
int x;
DI ();
x = sem->clip;
sem->clip = 0;
EI ();
return x;
}
//----------------------------------------------------------------------------
int
k_prio_wait (struct k_t *sem, int timeout, char prio)
{
int retval;
return -666; // no rdy for use
// copy of ki_wait just with EI()'s before leaving
DI ();
if (0 < sem->cnt1) { // lucky that we do not need to wait ?
sem->cnt1--; // Salute to Dijkstra
// set prio
pRun->prio = prio;
// no need bq we are alrdy in front prio_enQ (pAQ, deQ (pRun));
EI ();
return (0);
}
if (timeout == -1) { // no luck, dont want to wait so bye
EI ();
return (-2);
}
// from here we have to wait
pRun->cnt2 = timeout; // if 0 then wait forever
if (timeout) {
pRun->cnt3 = (int) sem; // nasty keep ref to semaphore,
}
// so we can be removed if timeout occurs
sem->cnt1--; // Salute to Dijkstra
enQ (sem, deQ (pRun));
ki_task_shift (); // call enables interrupt on return
pRun->cnt3 = 0; // reset ref to timer semaphore
retval = pRun->cnt2;
if (retval == 0) {
// set prio
pRun->prio = prio;
// no need prio_enQ (pAQ, deQ (pRun));
}
EI ();
return retval; // 0: ok, -1: timeout
}
//----------------------------------------------------------------------------
int
ki_semval (struct k_t *sem)
{
DI ();
return (sem->cnt1);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
struct k_msg_t *
k_crt_send_Q (int nr_el, int el_size, void *pBuf)
{
struct k_msg_t *pMsg;
if (k_running) {
return (NULL);
}
if (k_msg <= nr_send) {
goto errexit;
}
if (k_sem <= nr_sem) {
goto errexit;
}
pMsg = send_pool + nr_send;
pMsg->nr = nr_send; // I am element nr nr_send in msgQ pool
nr_send++;
pMsg->sem = k_crt_sem (0, nr_el);
if (pMsg->sem == NULL) {
goto errexit;
}
pMsg->pBuf = (char *) pBuf;
pMsg->r = pMsg->w = -1;
pMsg->el_size = el_size;
pMsg->nr_el = nr_el;
pMsg->lost_msg = 0;
pMsg->cnt = 0; // count nr elm in Q
return (pMsg);
errexit:
k_err_cnt++;
return (NULL);
}
//----------------------------------------------------------------------------
char
ki_send (struct k_msg_t *pB, void *el)
{
int i;
char *pSrc, *pDst;
if (pB->nr_el <= pB->cnt) {
// nope - no room for a putting new msg in Q ?
if (pB->lost_msg < 32000) {
pB->lost_msg++;
}
#ifdef KRNLBUG
k_send_Q_clip (pB->nr, pB->lost_msg);
#endif
return (-1); // nope
}
pB->cnt++;
pSrc = (char *) el;
pB->w++;
if (pB->nr_el <= pB->w) { // simple wrap around
pB->w = 0;
}
pDst = pB->pBuf + (pB->w * pB->el_size); // calculate where we shall put msg in ringbuf
for (i = 0; i < pB->el_size; i++) {
// copy to Q
*(pDst++) = *(pSrc++);
}
return (ki_signal (pB->sem)); // indicate a new msg is in Q
}
//----------------------------------------------------------------------------
char
k_send (struct k_msg_t *pB, void *el)
{
char res;
DI ();
res = ki_send (pB, el);
if (res == 0) { // if new task in AQ == someone was waiting for msg
ki_task_shift ();
}
EI ();
return (res);
}
//----------------------------------------------------------------------------
char
ki_receive (struct k_msg_t *pB, void *el, int *lost_msg)
{
int i;
char r, *pSrc, *pDst;
// can be called from ISR bq no blocking
DI (); // just to be sure
if ((r = ki_wait (pB->sem, -1)) >= 0) {
pDst = (char *) el;
pB->r++;
pB->cnt--; // got one
if (pB->nr_el <= pB->r) {
pB->r = 0;
}
pSrc = pB->pBuf + pB->r * pB->el_size;
for (i = 0; i < pB->el_size; i++) {
*(pDst++) = *(pSrc++);
}
if (lost_msg) {
*lost_msg = pB->lost_msg;
pB->lost_msg = 0;
}
return (r); // yes
}
return (-1); // nothing for you my friend
}
//----------------------------------------------------------------------------
char
k_receive (struct k_msg_t *pB, void *el, int timeout, int *lost_msg)
{
int i;
char r, *pSrc, *pDst;
DI ();
if (0 <= (r = ki_wait (pB->sem, timeout))) {
// ki_wait bq then intr is not enabled when coming back
pDst = (char *) el;
pB->r++;
pB->cnt--; // got one
if (pB->nr_el <= pB->r) {
pB->r = 0;
}
pSrc = pB->pBuf + pB->r * pB->el_size;
for (i = 0; i < pB->el_size; i++) {
*(pDst++) = *(pSrc++);
}
if (lost_msg) {
*lost_msg = pB->lost_msg;
pB->lost_msg = 0;
}
EI ();
return (r); // 1 if no suspension bq msg was already present, 0: ok if you have waited on msg
}
EI ();
return (-1); // nothing for you my friend
}
//----------------------------------------------------------------------------
void
k_round_robbin (void)
{
// reinsert running task in activeQ if round robbin is selected
DI ();
prio_enQ (pAQ, deQ (pRun));
ki_task_shift ();
EI ();
}