krnl 1
Loading...
Searching...
No Matches
ka03isrmsg.ino
Go to the documentation of this file.
1//260414
2
3/*
4* ISR INTEGRATION
5
6* WITH ADN WITHOUT DEBOUNCE
7* pro: we do use the standard atatchInterrupt from Arduino , easy etc
8*
9* ISR PINS TO USE
10
11UNO(328P): https://jensd.dk/doc/arduino/pins/uno.png
12
13pin 2 (INT0)
14pin 3 (INT1)
15
16MEGA(2560) https://jensd.dk/doc/arduino/pins/mega.png
17 pin 21 INT0 NB: I2C SCL pin as well
18 pin 20 INT1 NB: I2C SDA pin as well
19 pin 19 INT2
20 pin 18 INT3
21 pin 2 INT4
22 pin 3 INT5
23
24 Atmega32u4 based micro
25 intr 0,1,2,3, intr is special - it you used it beware
26Arduino micro(32u4 based)
27pin 18 INT0
28pin 19 INT1
29pin 20 INT2
30pin 21 INT3
31pin 1 INT6
32
33 NB NB NB
34 Only krnl calls allowd in isr: ki_signal and ki_send
35 When using Arduino install of interrupts( attachInterrupt) you are not allowed to call krnls scheduler.
36
37 So in worst case the task started by a interrupt krnl call (ki_signal, ki_send) will be running withn1 msec (at next krnl timer tick)
38
39 SO NO K_CHG_STAK(); PUSHREGS(); POPREGS(); RETI();
40}
41*
42*/
43
44#include <krnl.h>
45
46// stak size
47#define STK 150
48
49
51
52#define TASKPRIO 10
53
54struct k_t
58
59// message queue
60const int msgQSize = 10;
61struct k_msg_t *msgQ;
62unsigned long dataBufForMsgQ[msgQSize]; // 10 long for time
63
64
65volatile unsigned long isrCount = 0, localCpy = 0;
66
67unsigned long lastRun;
68const unsigned long debounceTime = 1000;
69
71 // https://jensd.dk/doc/time/simpletime.html
72 if (debounceTime < (ki_millis() - lastRun)) {
74 isrCount++;
75 ki_send(msgQ, (void *)&lastRun);
76 }
77}
78
80 unsigned long t = ki_millis();
81 isrCount++;
82 ki_send(msgQ, (void *)&t);
83}
84
85
86void task1() {
87
88 int cn = 0;
89
91 while (1) {
92 k_wait(timedSem, 0);
93 Serial.print("isr counter ");
94 Serial.print(cn++);
95 Serial.print(" ");
96 Serial.println(localCpy);
97 }
98}
99
100
101void task2() {
102 unsigned long ii;
103 int res, lostM;
104
105 while (1) {
106 res = k_receive(msgQ, &ii, 0, &lostM);
107
108 DI(); // disable interrupt - takes approx 62.5 nanosec
109 localCpy = ii;
110 EI(); // enable interrupt - takes approx 62.5 nanosec
111 }
112}
113
114void setup() {
115 Serial.begin(115200);
116 delay(500);
117
118 Serial.println("\n\njust bef init part");
119
120
121 k_init(2, 2, 1); // 2 task, 2 semaphores, 0 messaegQueues */
122
123 msgQ = k_crt_send_Q(msgQSize, sizeof(unsigned long), dataBufForMsgQ);
124
127
128 isrSyncSem = k_crt_sem(0, 10); // 1: start value, 10: max value (clipping)
129 timedSem = k_crt_sem(0, 10); // 1: start value, 10: max value (clipping)
130
131 pinMode(2, INPUT_PULLUP); // for isr
132
133 // Select with or without debounce
134 //attachInterrupt(digitalPinToInterrupt(2), myISR_No_Debounce, FALLING); // install ISR
135 attachInterrupt(digitalPinToInterrupt(2), myISR_Debounce, FALLING); // install ISR
136
137 Serial.println("justr bef start");
138 lastRun = k_millis();
139 k_start(); /* start krnl timer speed 1 milliseconds*/
140
141 Serial.println("If you see this then krnl didnt start :-( ");
142}
143
144void loop() {}
#define STK
void task2()
void task1()
#define debounceTime
Definition k08isrsem.ino:29
struct k_t * pTask1
Definition k08isrsem.ino:17
struct k_t * pTask2
Definition k09msgq.ino:11
int dataBufForMsgQ[10]
Definition k09msgq.ino:15
struct k_msg_t * msgQ
Definition k09msgq.ino:13
char stkTask1[150]
Definition ka01isr.ino:48
volatile int localCpy
Definition ka01isr.ino:58
struct k_t * isrSyncSem
Definition ka01isr.ino:55
volatile int isrCount
Definition ka01isr.ino:58
struct k_t * timedSem
Definition ka01isr.ino:55
char stkTask2[150]
Definition ka01isr.ino:48
void myISR_Debounce()
Definition ka02isr.ino:65
unsigned long lastRun
Definition ka02isr.ino:62
void myISR_Debounce()
void setup()
const int msgQSize
void myISR_No_Debounce()
void task2()
void task1()
void loop()
unsigned long k_millis(void)
return no of millisec sincs start NB no leap seconds - its clean
Definition krnl.c:1247
unsigned long ki_millis(void)
returns no msec since start - no DI/EI lock Returns nr of milliseconds since krnl was started by k_st...
Definition krnl.c:1240
char k_receive(struct k_msg_t *pB, void *el, int timeout, int *lost_msg)
Definition krnl.c:991
char ki_send(struct k_msg_t *pB, void *el)
Definition krnl.c:908
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
struct k_msg_t * k_crt_send_Q(int nr_el, int el_size, void *pBuf)
Definition krnl.c:841
int k_start()
Definition krnl.c:1149
#define DI()
Definition krnl.h:478
#define EI()
Definition krnl.h:479
Definition krnl.h:323