krnl 1
Loading...
Searching...
No Matches
k11breakoutClip.ino
Go to the documentation of this file.
1
2/* (C) 2012,2013,2014,2015 *
3* *
4 Jens Dalsgaard Nielsen <jdn@es.aau.dk>
5 http://es.aau.dk/staff/jdn
6 Section of Automation & Control
7 Aalborg University,
8 Denmark
9* *
10 "THE BEER-WARE LICENSE" (frit efter PHK)
11 <jdn@es.aau.dk> wrote this file. As long as you
12 retain this notice you can do whatever you want
13 with this stuff. If we meet some day, and you think
14 this stuff is worth it ...
15 you can buy me a beer in return :-)
16 or if you are real happy then ...
17 single malt will be well received :-)
18* *
19 Use it at your own risk - no warranty
20* *
21 tested with duemilanove w/328, uno R3,
22 seeduino 1280 and mega2560
23*****************************************************/
24
25/*
26 Demonstrates krnl usage with semaphore clip detection and scheduler breakout
27 3 tasks - priority low,, mediaum and high
28 skeleton: cyclic execxution on timed semaphore and fixed conumation of time
29 Typical RMA setup
30
31 Semaphores are created with limit -1 which means that a signal will only be accepted if a
32 Task is waiting at the semahore (which is indicated by -1)
33
34 Two user supplied break out functions:
35 - k_sem_clip - called when semaphore is clippep
36 - k_break_out - called by scheduler every task shift
37 Dig out 81-3 is initialized an LEDs attached
38 LED 8 on . dummy is running
39 LED 9 on low prio is running (got nr 1 bq it is created first)
40 LED 10 on med prio running ( got 2 bq crt as nr 2 ...)
41 LED 11 on high prio is running (etc)
42 LED 13 on semaphore clip == low prio is lagging behind
43
44 You can play with RMA
45*/
46
47#include <krnl.h>
48
49struct k_t * pt1, *pt2, *pt3,
51
52#define TICKSPEED 1
53
54#define STK 100
55char st1[STK], st2[STK], st3[STK];
56
57volatile unsigned char led13 = 0;
58
60{
61 while (1) {
62 k_wait(sem1, 0);
63 k_eat_time(100); // lets eat 200 milliseconds
64 }
65}
66
67
68void medprio()
69{
70 while (1) {
71 k_wait(sem2, 0);
72 k_eat_time(800); // lets eat 5 milliseconds
73 }
74}
75
76void lowprio()
77{
78 while (1) {
79 k_wait(sem3, 0);
80 k_eat_time(1480); // approx max value before lagging behind
81 //led13 = 0; // reset
82 }
83}
84
85void initLED()
86{
87 int i;
88 // attach 6 LEDS to pin 8..13 for light show
89 for (i = 8; i <= 13; i++) { // init PORTB for driving LEDs
90 pinMode(i, OUTPUT);
91 digitalWrite(i, LOW);
92 }
93}
94
96{
97 pt3 = k_crt_task(lowprio, 30, st3, STK); //declared first so it get tasknr 1
98 pt2 = k_crt_task(medprio, 20, st2, STK); // 2 ...
99 pt1 = k_crt_task(highprio, 10, st1, STK); // 3
100}
101
103{
104 sem3 = k_crt_sem(0, 1); // get nr 3 - ... used to test if task i ready for next cycle or if its lagging behind
105 sem2 = k_crt_sem(0, 0); // get nr 2 - ... meaning at elast one task has to wait at the semaphore
106 sem1 = k_crt_sem(0, 0); // get nr 1 - 0 is highest acceptable value on a semaphore ...
107
111}
112
113void setup()
114{
115 initLED();
116 // LED13 is pin5 at PORTB
117 k_init(3, 3, 0); // dummy get tasknr 0
118
119 initTasks();
121
122 k_start(TICKSPEED); // 10 millisecond heartbeat
123}
124
125void loop() {}
126
127
128/************************ DEBUG CALLBACK BREAKOUT PART ****************/
129// must be extern C ! its not mandatory to supply with these functions - only if you need
130
131extern "C" {
132
133 // called when a semphore is clipping - nr is id of semaphore and i os nr of times clip has occured
134
135 void k_sem_clip(unsigned char nr, int i)
136 {
137 if (nr == 2)
138 led13 |= 0x20;
139 }
140
141 void k_sem_noclip(unsigned char nr)
142 {
143 if (nr == 2)
144 led13 = 0x00;
145 }
146
147
148 /* void k_send_Q_clip(unsigned char nr, int i) {} */
149
150 void k_breakout() // called every task shift from dispatcher
151 {
152 unsigned char c;
153 PORTB = (1 << pRun->nr) | led13; // arduino uno !! specific usage of PORTB
154 }
155 // for a MEGA you have to find another port :-)
156 // port K (adc8-15) seems feasible
157}
158
159
160
unsigned char led13
Definition k02.ino:59
#define STK
volatile int i
struct k_t * pt2
struct k_t * pt1
struct k_t * sem1
char st2[110]
char st1[110]
struct k_t * sem2
struct k_t * pRun
Definition krnl.c:148
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
char st3[110]
#define TICKSPEED
struct k_t * sem3
void highprio()
struct k_t * pt3
void medprio()
void lowprio()
void setup()
void initLED()
void k_breakout()
void highprio()
void initTasks()
void k_sem_noclip(unsigned char nr)
void k_sem_clip(unsigned char nr, int i)
void medprio()
void initSemaphores()
void lowprio()
void loop()
Definition krnl.h:323
unsigned char nr
Definition krnl.h:327