Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 #include <cfg/os.h>
00115 #include <cfg/memory.h>
00116
00117 #include <string.h>
00118
00119 #include <sys/atom.h>
00120 #include <sys/heap.h>
00121 #include <sys/thread.h>
00122
00123
00124 #include <dev/irqstack.h>
00125
00126 #ifdef NUTDEBUG
00127 #include <sys/osdebug.h>
00128 #endif
00129
00134
00135 #ifdef IRQSTACK_SIZE
00136
00143 uint16_t _irqstackdec = 128;
00144 #endif
00145
00152 typedef struct {
00153 uint8_t csf_r29;
00154 uint8_t csf_r28;
00155 uint8_t csf_r17;
00156 uint8_t csf_r16;
00157 uint8_t csf_r15;
00158 uint8_t csf_r14;
00159 uint8_t csf_r13;
00160 uint8_t csf_r12;
00161 uint8_t csf_r11;
00162 uint8_t csf_r10;
00163 uint8_t csf_r9;
00164 uint8_t csf_r8;
00165 uint8_t csf_r7;
00166 uint8_t csf_r6;
00167 uint8_t csf_r5;
00168 uint8_t csf_r4;
00169 uint8_t csf_r3;
00170 uint8_t csf_r2;
00171 #ifdef __AVR_3_BYTE_PC__
00172 uint8_t csf_pcex;
00173 #endif
00174 uint8_t csf_pchi;
00175 uint8_t csf_pclo;
00176 } SWITCHFRAME;
00177
00183 typedef struct {
00184 uint8_t cef_arghi;
00185 uint8_t cef_arglo;
00186 uint8_t cef_rampz;
00187 uint8_t cef_sreg;
00188 uint8_t cef_r1;
00189 #ifdef __AVR_3_BYTE_PC__
00190 uint8_t cef_pcex;
00191 #endif
00192 uint8_t cef_pchi;
00193 uint8_t cef_pclo;
00194 } ENTERFRAME;
00195
00196 #define LONG_PTR_P(lp, mem_p) \
00197 __asm__ __volatile__("ldi %A0, lo8("#mem_p ")" "\n\t" \
00198 "ldi %B0, hi8("#mem_p ")" "\n\t" \
00199 "ldi %C0, hh8("#mem_p ")" "\n\t" \
00200 "clr %D0" \
00201 :"=d" (lp))
00202
00203
00204
00205
00206 static void NutThreadEntry(void) __attribute__ ((naked));
00207 static void NutThreadEntry(void)
00208 {
00209 __asm__ __volatile__("pop r25" "\n\t"
00210 "pop r24" "\n\t"
00211 "pop __tmp_reg__" "\n\t"
00212 "out %0, __tmp_reg__" "\n\t"
00213 "pop __tmp_reg__" "\n\t"
00214 "out %1, __tmp_reg__" "\n\t"
00215 "pop __zero_reg__" "\n\t"
00216 "reti" "\n\t"
00217 ::"I" _SFR_IO_ADDR(RAMPZ), "I" _SFR_IO_ADDR(SREG)
00218 );
00219 }
00220
00221
00233 void NutThreadSwitch(void) __attribute__ ((noinline)) __attribute__ ((naked));
00234 void NutThreadSwitch(void)
00235 {
00236
00237
00238
00239 asm volatile ("push r2" "\n\t"
00240 "push r3" "\n\t"
00241 "push r4" "\n\t"
00242 "push r5" "\n\t"
00243 "push r6" "\n\t"
00244 "push r7" "\n\t"
00245 "push r8" "\n\t"
00246 "push r9" "\n\t"
00247 "push r10" "\n\t"
00248 "push r11" "\n\t"
00249 "push r12" "\n\t"
00250 "push r13" "\n\t"
00251 "push r14" "\n\t"
00252 "push r15" "\n\t"
00253 "push r16" "\n\t"
00254 "push r17" "\n\t"
00255 "push r28" "\n\t"
00256 "push r29" "\n\t"
00257 "in %A0, %1" "\n\t"
00258 "in %B0, %2" "\n\t"
00259 :"=r" (runningThread->td_sp)
00260 :"I" _SFR_IO_ADDR(SPL),
00261 "I" _SFR_IO_ADDR(SPH)
00262 );
00263
00264
00265
00266
00267
00268 asm volatile (".global thread_start\n"
00269 "thread_start:\n\t"::);
00270
00271
00272
00273
00274 runningThread = runQueue;
00275 runningThread->td_state = TDS_RUNNING;
00276 asm volatile ("out %1, %A0" "\n\t"
00277 "out %2, %B0" "\n\t"
00278 "pop r29" "\n\t"
00279 "pop r28" "\n\t"
00280 "pop r17" "\n\t"
00281 "pop r16" "\n\t"
00282 "pop r15" "\n\t"
00283 "pop r14" "\n\t"
00284 "pop r13" "\n\t"
00285 "pop r12" "\n\t"
00286 "pop r11" "\n\t"
00287 "pop r10" "\n\t"
00288 "pop r9" "\n\t"
00289 "pop r8" "\n\t"
00290 "pop r7" "\n\t"
00291 "pop r6" "\n\t"
00292 "pop r5" "\n\t"
00293 "pop r4" "\n\t"
00294 "pop r3" "\n\t"
00295 "pop r2" "\n\t"
00296 "ret" "\n\t"
00297 ::"r" (runningThread->td_sp),
00298 "I" _SFR_IO_ADDR(SPL),
00299 "I" _SFR_IO_ADDR(SPH)
00300 );
00301 }
00302
00321 HANDLE NutThreadCreate(char * name, void (*fn) (void *), void *arg, size_t stackSize)
00322 {
00323 uint8_t *threadMem;
00324 SWITCHFRAME *sf;
00325 ENTERFRAME *ef;
00326 NUTTHREADINFO *td;
00327 #ifdef IRQSTACK_SIZE
00328 if (stackSize > _irqstackdec + 128) stackSize -= _irqstackdec;
00329 #endif
00330
00331
00332
00333
00334 if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
00335 return 0;
00336 }
00337
00338 td = (NUTTHREADINFO *) (threadMem + stackSize);
00339 ef = (ENTERFRAME *) ((uint16_t) td - sizeof(ENTERFRAME));
00340 sf = (SWITCHFRAME *) ((uint16_t) ef - sizeof(SWITCHFRAME));
00341
00342
00343 memcpy(td->td_name, name, sizeof(td->td_name) - 1);
00344 td->td_name[sizeof(td->td_name) - 1] = 0;
00345 td->td_sp = (uint16_t) sf - 1;
00346 td->td_memory = threadMem;
00347
00348
00349
00350
00351
00352 #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
00353 {
00354 uint32_t *fip = (uint32_t *)threadMem;
00355 while (fip < (uint32_t *)sf) {
00356 *fip++ = DEADBEEF;
00357 }
00358 }
00359 #else
00360 *((uint32_t *) threadMem) = DEADBEEF;
00361 *((uint32_t *) (threadMem + 4)) = DEADBEEF;
00362 *((uint32_t *) (threadMem + 8)) = DEADBEEF;
00363 *((uint32_t *) (threadMem + 12)) = DEADBEEF;
00364 #endif
00365 td->td_priority = 64;
00366
00367
00368
00369
00370 #ifdef __AVR_3_BYTE_PC__
00371 ef->cef_pcex = 0;
00372 #endif
00373 ef->cef_pchi = (uint8_t) (((uint16_t) fn) >> 8);
00374 ef->cef_pclo = (uint8_t) (((uint16_t) fn) & 0xff);
00375 ef->cef_sreg = 0x80;
00376 ef->cef_rampz = 0;
00377 ef->cef_r1 = 0;
00378
00379 ef->cef_arglo = (uint8_t) (((uint16_t) arg) & 0xff);
00380 ef->cef_arghi = (uint8_t) (((uint16_t) arg) >> 8);
00381
00382 #ifdef __AVR_3_BYTE_PC__
00383 sf->csf_pcex = 0;
00384 #endif
00385 sf->csf_pchi = (uint8_t) (((uint16_t) NutThreadEntry) >> 8);
00386 sf->csf_pclo = (uint8_t) (((uint16_t) NutThreadEntry) & 0xff);
00387
00388
00389
00390
00391
00392 td->td_next = nutThreadList;
00393 nutThreadList = td;
00394 td->td_state = TDS_READY;
00395 td->td_timer = 0;
00396 td->td_queue = 0;
00397 #ifdef NUTDEBUG
00398 if (__os_trf)
00399 fprintf(__os_trs, "Cre<%04x>", (uintptr_t) td);
00400 #endif
00401
00402 NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
00403
00404 #ifdef NUTDEBUG
00405 if (__os_trf) {
00406 NutDumpThreadList(__os_trs);
00407
00408 }
00409 #endif
00410
00411
00412
00413
00414 if (runningThread == 0) {
00415 NutEnterCritical();
00416 asm volatile ("rjmp thread_start\n\t"::);
00417
00418 }
00419
00420
00421
00422
00423
00424
00425 if (runningThread != runQueue) {
00426 runningThread->td_state = TDS_READY;
00427 #ifdef NUTDEBUG
00428 if (__os_trf)
00429 fprintf(__os_trs, "New<%04x %04x>", (uintptr_t) runningThread, (uintptr_t) runQueue);
00430 #endif
00431 NutEnterCritical();
00432 NutThreadSwitch();
00433 NutExitCritical();
00434 }
00435
00436 return td;
00437 }
00438