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