krnl 1
Loading...
Searching...
No Matches
rt-arduino-realtimeloop.ino
Go to the documentation of this file.
1/* critical region - by semaphores - aka mutex
2 JDN
3 * */
4
5#include <krnl.h>
6
7#define STK 150
8
9#define TASKPRIO 10
10
11
12struct k_t * pTask1;
13
14
15struct k_t *timedSem1;
16
17void task1()
18{
19 k_set_sem_timer(timedSem1, 100); // 100 tick aka 100 msec realtime signal from krnl
20
21 while (1) {
22 k_wait(timedSem1, 0); // wait until kick
23
24 k_eat_ticks(50); // simulate realtime code
25 Serial.println(k_millis()); // NB k_millis does NOT insert leap msec as std millis
26
27 }
28}
29
30
31void setup() {
32
33 Serial.begin(115200);
34 delay(2000);
35
36 Serial.println("just bef init part");
37
38 k_init(1,1, 0); // 2 task, 1 semaphores, 0 messaegQueues */
39
40 pTask1 = k_crt_task(task1, 15, STK);
41
42 timedSem1 = k_crt_sem(0, 1); // 1: start value, 10: max value (clipping)
43
44 Serial.println("just bef k_start");
45
46 k_start(1); /* start krnl timer speed 1 milliseconds*/
47
48 Serial.println("If you see this then krnl didnt start :-( ");
49}
50
51void loop() {}
52
53
54/*
55 * from arduino runtimelibrary - insertino of leap due to not exact 1msec
56 * timer0_millis += MILLIS_INC;
57 timer0_fractt += FRACT_INC;
58 if (timer0_fractt >= FRACT_MAX) {
59 timer0_fractt -= FRACT_MAX;
60 timer0_millis += 1;
61 }
62 timer0_overflow_count++;
63
64
65#define MICROSECONDS_PER_TIMER0_OVERFLOW (1024) //clockCyclesToMicroseconds(64 * 256))
66
67// the whole number of milliseconds per timer0 overflow
68#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
69
70// the fractional number of milliseconds per timer0 overflow. we shift right
71// by three to fit these numbers into a byte. (for the clock speeds we care
72// about - 8 and 16 MHz - this doesn't lose precision.)
73#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
74#define FRACT_MAX (1000 >> 3)
75
76 */
#define STK
void task1()
struct k_t * timedSem1
Definition k08isrsem.ino:21
struct k_t * pTask1
Definition k08isrsem.ino:17
unsigned long k_millis(void)
return no of millisec sincs start NB no leap seconds - its clean
Definition krnl.c:1247
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
Definition krnl.h:323