00001
00005 #ifndef TASK__H
00006 #define TASK__H
00007
00008 #include "pq.h"
00009
00019
00020 #define LO(x) (x & 0x0ff)
00021 #define HI(x) ((x >> 8) & 0x0ff)
00023 #define STACK_SIZE 96
00025 #define EVENT_NOERROR 0
00026 #define EVENT_NOTASKS -1
00027 #define EVENT_OVERFLOW -2
00028 #define EVENT_TIMEOUT -3
00029 #define EVENT_DELETED -4
00051 typedef struct tcb TCB;
00052
00053 struct tcb {
00054 char *stack;
00055 char *stacktop;
00056 int stacksize;
00057 int priority;
00058 int status;
00059 unsigned misc;
00060 unsigned TimeStamp;
00061 unsigned TcbSwaps;
00062 int timeout;
00063 char *name;
00064 TCB *next;
00065 TCB *list;
00066 };
00067
00071 typedef struct ecb {
00072 TCB *task_h;
00073 TCB *task_t;
00074 struct ecb *Tnext,*Tprev;
00075 int SemaphoreMode;
00076 int EventCount;
00077 char name[20];
00078 } ECB;
00080 #define SEMAPHORE_MODE_BLOCKING 0
00081 #define SEMAPHORE_MODE_TIMEOUT 1
00083 //-------------------------------------------------------------------
00084
00085
00086
00087 #define THREAD(func,arg)\
00088 void func(void *arg) __attribute__((noreturn)); \
00089 void func(void *arg)
00092
00093
00094
00095
00096 extern ECB *MasterList;
00097 extern ECB *TimeoutList;
00098 extern PQ ActiveTasks;
00099 extern TCB *CurrentTask,*NextTask;
00100 extern ECB *LCDBlocker;
00101 extern int OsRunning;
00102 extern volatile int TStamp;
00103
00104 extern volatile int InterruptCount;
00105 extern volatile int Blocking;
00106
00107 extern void CreateTask(TCB *t, void (*task)(void *), int stacksize,int priority,char *name,void *arg);
00108 extern void IrqSwap(void);
00109 extern void OSInit(void);
00110 extern void Yeild(void);
00111 extern ECB *NewSemaphore(int InitCount,int Mode,char *name);
00112 extern void CreateSemaphore(ECB *e,int InitCount,int Mode,char *n);
00113 extern void DeleteSemaphore(ECB *e);
00114 extern int PendSemaphore(ECB *e, int Timeout);
00115 extern int PostSemaphore(ECB *e, int Value);
00116 extern int PostSemaphoreWithData(ECB *e, int Value,unsigned data);
00117 extern int PostSemaphoreIrq(ECB *e, int Value);
00118 extern void TimerTicker(void);
00119 extern void vPortYieldProcessor(void);
00120 extern void vPortStartFirstTask(void);
00121 extern int TimeDelay(int mSec);
00122 extern void Enable(char sr);
00123 extern char Disable(void);
00124 extern void StartOS(void);
00125 extern void DoSwap(void);
00126 extern void ExitInterrupt(void);
00127 extern unsigned MeasureStackUsage(char *s,unsigned max);
00128 extern unsigned StackHeapAvailiable(void);
00129 extern void RegisterTickerPost(ECB *pS);
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00149 #define SAVE_IRQ() \
00150 asm volatile ( "push r31 \n\t" \
00151 "in r31, __SREG__ \n\t" \
00152 "ori r31,0x80 \n\t" \
00153 "push r31 \n\t" \
00154 "push r30 \n\t" \
00155 "push r29 \n\t" \
00156 "push r28 \n\t" \
00157 "push r27 \n\t" \
00158 "push r26 \n\t" \
00159 "push r25 \n\t" \
00160 "push r24 \n\t" \
00161 "push r23 \n\t" \
00162 "push r22 \n\t" \
00163 "push r21 \n\t" \
00164 "push r20 \n\t" \
00165 "push r19 \n\t" \
00166 "push r18 \n\t" \
00167 "push r17 \n\t" \
00168 "push r16 \n\t" \
00169 "push r15 \n\t" \
00170 "push r14 \n\t" \
00171 "push r13 \n\t" \
00172 "push r12 \n\t" \
00173 "push r11 \n\t" \
00174 "push r10 \n\t" \
00175 "push r9 \n\t" \
00176 "push r8 \n\t" \
00177 "push r7 \n\t" \
00178 "push r6 \n\t" \
00179 "push r5 \n\t" \
00180 "push r4 \n\t" \
00181 "push r3 \n\t" \
00182 "push r2 \n\t" \
00183 "push r1 \n\t" \
00184 "clr r1 \n\t" \
00185 "push r0 \n\t" \
00186 );
00187
00188 #define RESTORE_IRQ() \
00189 asm volatile ( "pop r0 \n\t" \
00190 "pop r1 \n\t" \
00191 "pop r2 \n\t" \
00192 "pop r3 \n\t" \
00193 "pop r4 \n\t" \
00194 "pop r5 \n\t" \
00195 "pop r6 \n\t" \
00196 "pop r7 \n\t" \
00197 "pop r8 \n\t" \
00198 "pop r9 \n\t" \
00199 "pop r10 \n\t" \
00200 "pop r11 \n\t" \
00201 "pop r12 \n\t" \
00202 "pop r13 \n\t" \
00203 "pop r14 \n\t" \
00204 "pop r15 \n\t" \
00205 "pop r16 \n\t" \
00206 "pop r17 \n\t" \
00207 "pop r18 \n\t" \
00208 "pop r19 \n\t" \
00209 "pop r20 \n\t" \
00210 "pop r21 \n\t" \
00211 "pop r22 \n\t" \
00212 "pop r23 \n\t" \
00213 "pop r24 \n\t" \
00214 "pop r25 \n\t" \
00215 "pop r26 \n\t" \
00216 "pop r27 \n\t" \
00217 "pop r28 \n\t" \
00218 "pop r29 \n\t" \
00219 "pop r30 \n\t" \
00220 "pop r31 \n\t" \
00221 "out __SREG__, r31 \n\t" \
00222 "pop r31 \n\t" \
00223 "reti \n\t" \
00224 );
00225
00226
00227
00228
00229 #define SAVE_CONTEXT() \
00230 asm volatile ( "push r31 \n\t" \
00231 "in r31, __SREG__ \n\t" \
00232 "cli \n\t" \
00233 "push r31 \n\t" \
00234 "push r30 \n\t" \
00235 "push r29 \n\t" \
00236 "push r28 \n\t" \
00237 "push r27 \n\t" \
00238 "push r26 \n\t" \
00239 "push r25 \n\t" \
00240 "push r24 \n\t" \
00241 "push r23 \n\t" \
00242 "push r22 \n\t" \
00243 "push r21 \n\t" \
00244 "push r20 \n\t" \
00245 "push r19 \n\t" \
00246 "push r18 \n\t" \
00247 "push r17 \n\t" \
00248 "push r16 \n\t" \
00249 "push r15 \n\t" \
00250 "push r14 \n\t" \
00251 "push r13 \n\t" \
00252 "push r12 \n\t" \
00253 "push r11 \n\t" \
00254 "push r10 \n\t" \
00255 "push r9 \n\t" \
00256 "push r8 \n\t" \
00257 "push r7 \n\t" \
00258 "push r6 \n\t" \
00259 "push r5 \n\t" \
00260 "push r4 \n\t" \
00261 "push r3 \n\t" \
00262 "push r2 \n\t" \
00263 "push r1 \n\t" \
00264 "clr r1 \n\t" \
00265 "push r0 \n\t" \
00266 "lds r26, CurrentTask \n\t" \
00267 "lds r27, CurrentTask + 1 \n\t" \
00268 "in r0, 0x3d \n\t" \
00269 "st x+, r0 \n\t" \
00270 "in r0, 0x3e \n\t" \
00271 "st x+, r0 \n\t" \
00272 );
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 #define RESTORE_CONTEXT_OLD() \
00324 asm volatile ( "lds r26, NextTask \n\t" \
00325 "lds r27, NextTask + 1 \n\t" \
00326 "ld r28, x+ \n\t" \
00327 "out __SP_L__, r28 \n\t" \
00328 "ld r29, x+ \n\t" \
00329 "out __SP_H__, r29 \n\t" \
00330 "pop r0 \n\t" \
00331 "pop r1 \n\t" \
00332 "pop r2 \n\t" \
00333 "pop r3 \n\t" \
00334 "pop r4 \n\t" \
00335 "pop r5 \n\t" \
00336 "pop r6 \n\t" \
00337 "pop r7 \n\t" \
00338 "pop r8 \n\t" \
00339 "pop r9 \n\t" \
00340 "pop r10 \n\t" \
00341 "pop r11 \n\t" \
00342 "pop r12 \n\t" \
00343 "pop r13 \n\t" \
00344 "pop r14 \n\t" \
00345 "pop r15 \n\t" \
00346 "pop r16 \n\t" \
00347 "pop r17 \n\t" \
00348 "pop r18 \n\t" \
00349 "pop r19 \n\t" \
00350 "pop r20 \n\t" \
00351 "pop r21 \n\t" \
00352 "pop r22 \n\t" \
00353 "pop r23 \n\t" \
00354 "pop r24 \n\t" \
00355 "pop r25 \n\t" \
00356 "pop r26 \n\t" \
00357 "pop r27 \n\t" \
00358 "pop r28 \n\t" \
00359 "pop r29 \n\t" \
00360 "pop r30 \n\t" \
00361 "pop r31 \n\t" \
00362 "tst r31 \n\t" \
00363 "brmi irqen \n\t" \
00364 "out __SREG__, r31 \n\t" \
00365 "pop r31 \n\t" \
00366 "ret \n\t" \
00367 "irqen: cbr r31, 128 \n\t" \
00368 "out __SREG__, r31 \n\t" \
00369 "pop r31 \n\t" \
00370 "reti \n\t" \
00371 );
00372
00373
00374 #define RESTORE_CONTEXT() \
00375 asm volatile ( "lds r26, NextTask \n\t" \
00376 "lds r27, NextTask + 1 \n\t" \
00377 "ld r28, x+ \n\t" \
00378 "out __SP_L__, r28 \n\t" \
00379 "ld r29, x+ \n\t" \
00380 "out __SP_H__, r29 \n\t" \
00381 "pop r0 \n\t" \
00382 "pop r1 \n\t" \
00383 "pop r2 \n\t" \
00384 "pop r3 \n\t" \
00385 "pop r4 \n\t" \
00386 "pop r5 \n\t" \
00387 "pop r6 \n\t" \
00388 "pop r7 \n\t" \
00389 "pop r8 \n\t" \
00390 "pop r9 \n\t" \
00391 "pop r10 \n\t" \
00392 "pop r11 \n\t" \
00393 "pop r12 \n\t" \
00394 "pop r13 \n\t" \
00395 "pop r14 \n\t" \
00396 "pop r15 \n\t" \
00397 "pop r16 \n\t" \
00398 "pop r17 \n\t" \
00399 "pop r18 \n\t" \
00400 "pop r19 \n\t" \
00401 "pop r20 \n\t" \
00402 "pop r21 \n\t" \
00403 "pop r22 \n\t" \
00404 "pop r23 \n\t" \
00405 "pop r24 \n\t" \
00406 "pop r25 \n\t" \
00407 "pop r26 \n\t" \
00408 "pop r27 \n\t" \
00409 "pop r28 \n\t" \
00410 "pop r29 \n\t" \
00411 "pop r30 \n\t" \
00412 "pop r31 \n\t" \
00413 "sbrc r31,7 \n\t" \
00414 "rjmp irqen \n\t" \
00415 "out __SREG__, r31 \n\t" \
00416 "pop r31 \n\t" \
00417 "ret \n\t" \
00418 "irqen: cbr r31, 128 \n\t" \
00419 "out __SREG__, r31 \n\t" \
00420 "pop r31 \n\t" \
00421 "reti \n\t" \
00422 );
00423
00426
00427
00428
00429
00430 #endif