aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: 74dda7e01871bf4ffcf8658cadf52655c13f8074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* $Id$ */
/*
 * Copyright (c) 2010 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*
 * based on TinyRealTime by Dan Henriksson and Anton Cervin
 * http://www.control.lth.se/Publication/hen+04t.html
 */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "kernel.h"
#include "stack.h"
#include "queue.h"

#define LO8(x)			((uint8_t)((uint16_t)(x)))
#define HI8(x)			((uint8_t)((uint16_t)(x) >> 8))
#define NOW(hi, lo)		(((uint32_t)(hi) << 0x10) | (lo))
#define DISTANCE(from, to)	((int32_t)((to) - (from)))
#define SLICE			USEC(100)

struct task {
	uint32_t release;		/* release time */
	uint16_t sp;			/* stack pointer */
	uint8_t *stack;			/* stack area */
	uint8_t id;			/* task id */
	uint8_t prio;
	uint8_t defprio;
	struct queue *rq;
	TAILQ_ENTRY(task) r_link;
	TAILQ_ENTRY(task) t_link;
	TAILQ_ENTRY(task) w_link;
};

TAILQ_HEAD(queue, task);

struct kern {
	struct queue *rq;		/* run queue */
	struct queue *wq;		/* wait queues */
	struct queue tq;		/* time queue */
	struct task *idle;
	struct task *cur;		/* current task */
	uint16_t cycles;		/* clock high byte */
	uint8_t semaphore;		/* bitmap */
	uint8_t maxid;
	uint8_t reboot;
} kern;

NAKED(swtch)
{
	struct queue *rq;

	pusha();

	/* pick hightes rq, cannot fail */
	for (rq = kern.rq; TAILQ_EMPTY(rq); rq++)
		;

	/* switch context */
	kern.cur->sp = SP;
	kern.cur = TAILQ_FIRST(rq);
	SP = kern.cur->sp;

	/* set task slice timeout */
	OCR1B = TCNT1 + SLICE;

	popa();
	ret();
}

ISR(TIMER1_OVF_vect)
{
	if (!kern.reboot)
		wdt_reset();

	++kern.cycles;
}

ISR(TIMER1_COMPA_vect)
{
	struct task *tp, *tmp;
	uint32_t now;
	uint16_t nexthit;
	int32_t dist;

	/* grab time as early as possible */
	now = NOW(kern.cycles, TCNT1);
	nexthit = UINT16_MAX;

	/* release waiting tasks */
	TAILQ_FOREACH_SAFE(tp, &kern.tq, t_link, tmp) {
		dist = DISTANCE(now, tp->release);
		if (dist <= 0) {
			TAILQ_REMOVE(&kern.tq, tp, t_link);
			tp->prio = tp->defprio;
			tp->rq = &kern.rq[tp->prio];
			TAILQ_INSERT_TAIL(tp->rq, tp, r_link);
		} else if (dist < nexthit)
			nexthit = dist;
	}

	/* set next wakeup timer */
	OCR1A = TCNT1 + nexthit;
}

ISR(TIMER1_COMPB_vect)
{
	/* reschedule current task if it've used its time slice */
	TAILQ_REMOVE(kern.cur->rq, kern.cur, r_link);
	if (kern.cur->prio > RT && kern.cur->prio < RR)
		kern.cur->prio++;
	kern.cur->rq = &kern.rq[kern.cur->prio];
	TAILQ_INSERT_TAIL(kern.cur->rq, kern.cur, r_link);

	swtch();
}

void
init(uint8_t sema, uint8_t stack)
{
	uint8_t i;

	cli();

	/* disable watchdog */
	MCUSR = 0;
	wdt_disable();

	/* set clock prescale to 1 in case CKDIV8 fuse is on */
	clock_prescale_set(clock_div_1);

	/* Set up timer 1 */
	TCNT1 = 0;				/* reset timer */
	TCCR1A = 0;				/* normal operation */
	TCCR1B = TIMER_FLAGS;			/* prescale */
	TIMSK1 = _BV(OCIE1A)|_BV(OCIE1B)|_BV(TOIE1);	/* enable interrupts */
	OCR1A = 0;
	OCR1B = 0;

	/* init queues */

	kern.rq = calloc(nPrio, sizeof(struct queue));
	for (i = 0; i < nPrio; i++)
		TAILQ_INIT(&kern.rq[i]);

	kern.wq = calloc(sema, sizeof(struct queue));
	for (i = 0; i < sema; i++)
		TAILQ_INIT(&kern.wq[i]);

	TAILQ_INIT(&kern.tq);

	/* init idle task */
	kern.idle = calloc(1, sizeof(struct task));
	kern.idle->prio = Idle;
	kern.idle->defprio = Idle;
	kern.idle->id = 0;
	kern.idle->release = 0;
	kern.idle->sp = SP;			/* not really needed */
	kern.idle->stack = (uint8_t *)(RAMEND - stack + 1);
	kern.idle->rq = &kern.rq[kern.idle->prio];
	TAILQ_INSERT_TAIL(kern.idle->rq, kern.idle, r_link);
	kern.cur = TAILQ_FIRST(kern.idle->rq);

	kern.cycles = 0;
	kern.semaphore = 0;
	kern.maxid = 0;

	kern.reboot = 0;
	wdt_enable(WDTO_500MS);

	sei();
}

void
exec(void (*fun)(void *), void *args, uint8_t stack, uint8_t prio)
{
	struct task *tp;
	uint8_t *sp;

	cli();

	/* allocate task memory */
	tp = calloc(1, sizeof(struct task));
	tp->stack = calloc(stack, sizeof(uint8_t));
	sp = &tp->stack[stack - 1];

	/* initialize stack */
	*sp-- = LO8(fun);		/* PC(lo) */
	*sp-- = HI8(fun);		/* PC(hi) */

	sp -= 25;
	memset(sp, 0, 25);		/* r1, r0, SREG, r2-r23 */
	
	*sp-- = LO8(args);		/* r24 */
	*sp-- = HI8(args);		/* r25 */

	sp -= 6;
	memset(sp, 0, 6);		/* r26-r31 */

	tp->prio = prio;
	tp->defprio = prio;
	tp->id = ++kern.maxid;
	tp->release = 0;
	tp->sp = (uint16_t)sp;		/* SP */
	tp->rq = &kern.rq[tp->prio];
	TAILQ_INSERT_TAIL(tp->rq, tp, r_link);

	swtch();

	sei();
}

void
wait(uint8_t chan)
{
	cli();

	if (kern.semaphore & _BV(chan)) {
		/* semaphore busy, go into wait queue */
		TAILQ_REMOVE(kern.cur->rq, kern.cur, r_link);
		TAILQ_INSERT_TAIL(&kern.wq[chan], kern.cur, w_link);
		swtch();
	} else {
		/* occupy semaphore and continue */
		kern.semaphore |= _BV(chan);
	}

	sei();
}

void
signal(uint8_t chan)
{
	struct task *tp;

	cli();

	if ((tp = TAILQ_FIRST(&kern.wq[chan]))) {
		/* release first waiting task from wait queue */
		TAILQ_REMOVE(&kern.wq[chan], tp, w_link);
		kern.cur->prio = kern.cur->defprio;
		kern.cur->rq = &kern.rq[kern.cur->prio];
		TAILQ_INSERT_TAIL(kern.cur->rq, tp, r_link);
		swtch();
	} else {
		/* clear semaphore and continue */
		kern.semaphore &= ~_BV(chan);
	}

	sei();
}

void
sleep(uint32_t sec, uint32_t usec)
{
	cli();

	kern.cur->release += SEC(sec) + USEC(usec);
	TAILQ_REMOVE(kern.cur->rq, kern.cur, r_link);
	TAILQ_INSERT_TAIL(&kern.tq, kern.cur, t_link);
	swtch();

	sei();
}

void
yield(void)
{
	cli();

	TAILQ_REMOVE(kern.cur->rq, kern.cur, r_link);
	TAILQ_INSERT_TAIL(kern.cur->rq, kern.cur, r_link);
	swtch();

	sei();
}

void
suspend(void)
{
	cli();

	/* TODO: free memory */
	TAILQ_REMOVE(kern.cur->rq, kern.cur, r_link);
	swtch();

	sei();
}

uint32_t
now(void)
{
	return NOW(kern.cycles, TCNT1);
}

uint8_t
running(void)
{
	return kern.cur->id;
}

void
reboot(void)
{
	kern.reboot = 1;
}

void
idle(void)
{
	for (;;)
		sleep_mode();
}