krnl 1
Loading...
Searching...
No Matches
isem.ino
Go to the documentation of this file.
1#include <krnl.h>
2
3// External triggered ISR
4// An Interrupt Service Routine is attached to pin 2
5// So when pin2 is drived to ground (by a wire) an interrupt is generated.
6// The ISR increment a counter and send it to a message Q
7// naming ki_send .... "i" indicates it can be used in an ISR and demand interrupt to be disabled prio to call
8// and that no task shift takes place in the call
9// demonstrates ISR with message Q and preemption(task shift) in the ISR
10// NB Take a look on the ISR. For 1280 and 2560 it is INT4 but for 168,328,.. it's INTO
11// It is taken care by a compile flag
12// (c) beerware license JDN 2013
13// AS USUAL Serial.print is not krnl safe !!! cant break your program
14
15struct k_t * p_t1, *sem1;
16
17#define STK_SIZE 400
18
19char s1[STK_SIZE]; // stak for t1 ... and t2
20
21
22volatile int icnt=0;
23
24void doBlink(void) {
25 static char flag = 0;
26 flag = ! flag;
27 digitalWrite(13,flag);
28}
29
30
31
32void t1(void) {
33
34 while (1) {
35 int i;
36
37 k_wait(sem1,0); // wait forever
38 doBlink();
39 }
40
41}
42
43
44#if defined (__AVR_ATmega2560__) || defined (__AVR_ATmega1280__)
45ISR(INT4_vect,ISR_NAKED) {
46#else
47ISR(INT0_vect, ISR_NAKED) {
48#endif
49
50 // no local vars ?!? ! I think
51 PUSHREGS();
52 if (!k_running)
53 goto exitt ;
54
55 icnt++;
57 K_CHG_STAK();
58exitt:
59
60 POPREGS();
61 RETI();
62}
63
64void initISR()
65{
66 DI();
67 pinMode(2,INPUT); // set som input
68 digitalWrite(2,HIGH); // enable pullup resistor
69#if defined (__AVR_ATmega2560__) || defined (__AVR_ATmega1280__)
70 // 1280/2560 mega pin2 intr:4, pin5 intr:5
71 EIMSK |= (1 << INT4); // enable external intr
72 EICRB |= (1 << ISC41); // trigger INT4 on falling edge
73#else
74 EIMSK |= (1 << INT0); // enable external intr
75 EICRA |= (1 << ISC01); // trigger INT0 on falling edge
76#endif
77
78 EI();
79}
80
81void setup() {
82
83 Serial.begin(9600);
84 pinMode(13,OUTPUT);
85
86 k_init(1,1,0); // from now you can crt task,sem etc
87
88 sem1 = k_crt_sem(0,10); //
89
90 p_t1 = k_crt_task(t1, 10, s1, STK_SIZE);
91
92 initISR();
93
94 Serial.println("just bef");
95 k_start(10); // now we are runnning with timer 10 msev
96 //Serial.println("oev");
97
98 // main will not come back and will sleep rest of life
99}
100
101void loop(void) {
102 // just for compilation - will never be called
103}
104
105/* QED :-) */
106
107
108
109
volatile int i
unsigned char s1[110]
Definition k03asleep.ino:25
struct k_t * sem1
int ki_signal(struct k_t *sem)
signal a semaphore from AN ISR - no task shift , do not enable intr,...
Definition krnl.c:605
volatile char k_running
Definition krnl.c:156
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
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
#define RETI()
Definition krnl.h:480
#define K_CHG_STAK()
Definition krnl.h:488
void loop(void)
Definition isem.ino:101
void doBlink(void)
Definition isem.ino:24
#define STK_SIZE
Definition isem.ino:17
void setup()
Definition isem.ino:81
struct k_t * p_t1
Definition isem.ino:15
void initISR()
Definition isem.ino:64
void t1(void)
Definition isem.ino:32
ISR(INT0_vect, ISR_NAKED)
Definition isem.ino:47
volatile int icnt
Definition isem.ino:22
struct k_t * t1
Definition krnlgen.ino:3
PUSHREGS()
Definition krnl.h:323