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
00049 #ifndef __ESOS_TASK_H__
00050 #define __ESOS_TASK_H__
00051
00052 #include "lc.h"
00053
00054 struct stTask {
00055 lc_t lc;
00056 uint8 flags;
00057 uint8 (*pfn)(struct stTask *);
00058 uint32 u32_savedTick;
00059 uint32 u32_waitLen;
00060 };
00061
00071 typedef struct stTask* ESOS_TASK_HANDLE;
00072
00073
00074 #define ESOS_TASK_WAITING 0
00075 #define ESOS_TASK_ENDED 3
00076
00077
00078 #define __TASK_ENDED_MASK BIT3
00079
00080 #define __TASK_KILLED_MASK BIT2
00081
00082 #define __TASK_SLEEPING_MASK BIT1
00083
00084 #define __TASK_WAITING_MASK BIT0
00085
00086 #define __TASK_CALLED_MASK BIT7
00087
00088
00089 #define __ESOS_SET_TASK_SLEEPING_FLAG(TaskHandle) BIT_SET_MASK((TaskHandle)->flags, __TASK_SLEEPING_MASK)
00090 #define __ESOS_CLEAR_TASK_SLEEPING_FLAG(TaskHandle) BIT_CLEAR_MASK((TaskHandle)->flags, __TASK_SLEEPING_MASK)
00091 #define __ESOS_SET_TASK_KILLED_FLAG(TaskHandle) BIT_SET_MASK((TaskHandle)->flags, __TASK_KILLED_MASK)
00092 #define __ESOS_CLEAR_TASK_KILLED_FLAG(TaskHandle) BIT_CLEAR_MASK((TaskHandle)->flags, __TASK_KILLED_MASK)
00093 #define __ESOS_SET_TASK_WAITING_FLAG(TaskHandle) BIT_SET_MASK((TaskHandle)->flags, __TASK_WAITING_MASK)
00094 #define __ESOS_CLEAR_TASK_WAITING_FLAG(TaskHandle) BIT_CLEAR_MASK((TaskHandle)->flags, __TASK_WAITING_MASK)
00095 #define __ESOS_SET_TASK_CALLED_FLAG(TaskHandle) BIT_SET_MASK((TaskHandle)->flags, __TASK_CALLED_MASK)
00096 #define __ESOS_CLEAR_TASK_CALLED_FLAG(TaskHandle) BIT_CLEAR_MASK((TaskHandle)->flags, __TASK_CALLED_MASK)
00097 #define __ESOS_IS_TASK_CALLED(TaskHandle) IS_BIT_SET_MASK((TaskHandle)->flags, __TASK_CALLED_MASK)
00098 #define __ESOS_SET_TASK_ENDED_FLAG(TaskHandle) BIT_SET_MASK((TaskHandle)->flags, __TASK_ENDED_MASK)
00099 #define __ESOS_CLEAR_TASK_ENDED_FLAG(TaskHandle) BIT_CLEAR_MASK((TaskHandle)->flags, __TASK_ENDED_MASK)
00100
00101
00120 #define __ESOS_INIT_TASK(TaskHandle) \
00121 LC_INIT((TaskHandle)->lc);
00122
00144 #define ESOS_IS_TASK_INITED(TaskHandle) LC_IS_INITED((TaskHandle)->lc)
00145
00153 #define ESOS_IS_TASK_SLEEPING(TaskHandle) IS_BIT_SET_MASK((TaskHandle)->flags, __TASK_SLEEPING_MASK)
00154
00162 #define ESOS_IS_TASK_KILLED(TaskHandle) IS_BIT_SET_MASK((TaskHandle)->flags, __TASK_KILLED_MASK)
00163
00171 #define ESOS_IS_TASK_WAITING(TaskHandle) IS_BIT_SET_MASK((TaskHandle)->flags, __TASK_WAITING_MASK)
00172
00182 #define ESOS_IS_TASK_ENDED(TaskHandle) IS_BIT_SET_MASK((TaskHandle)->flags, __TASK_ENDED_MASK)
00183
00205 #define ESOS_USER_TASK(taskname) uint8 taskname(ESOS_TASK_HANDLE __pstSelf)
00206
00224 #define ESOS_CHILD_TASK(taskname, ...) uint8 taskname(ESOS_TASK_HANDLE __pstSelf, ##__VA_ARGS__)
00225
00237 #define ESOS_TASK_BEGIN() \
00238 { __ESOS_SET_TASK_CALLED_FLAG(__pstSelf); \
00239 LC_RESUME(__pstSelf->lc)
00240
00249 #define ESOS_TASK_END() \
00250 LC_END(__pstSelf->lc); \
00251 __pstSelf->flags = __TASK_ENDED_MASK; \
00252 __ESOS_INIT_TASK(__pstSelf); \
00253 return ESOS_TASK_ENDED; }
00254
00255
00267 #define ESOS_TASK_GET_TASK_HANDLE() __pstSelf
00268
00291 #define ESOS_SCHEDULE_TASK(pfnThread) ( (pfnThread) < ESOS_TASK_ENDED )
00292
00313 #define ESOS_TASK_WAIT_UNTIL(condition) \
00314 do { \
00315 LC_SET(__pstSelf->lc); \
00316 if(ESOS_IS_TASK_KILLED(__pstSelf)) { \
00317 __pstSelf->flags = __TASK_KILLED_MASK; \
00318 return ESOS_TASK_ENDED; \
00319 } \
00320 if((condition)) { \
00321 __ESOS_CLEAR_TASK_WAITING_FLAG(__pstSelf); \
00322 } \
00323 else { \
00324 __ESOS_SET_TASK_WAITING_FLAG(__pstSelf); \
00325 } \
00326 if(ESOS_IS_TASK_WAITING(__pstSelf)) { \
00327 return ESOS_TASK_WAITING; \
00328 } \
00329 } while(0)
00330
00341 #define ESOS_TASK_WAIT_WHILE(cond) ESOS_TASK_WAIT_UNTIL(!(cond))
00342
00353 #define ESOS_TASK_WAIT_TICKS(u32_duration) \
00354 do { \
00355 __pstSelf->u32_savedTick = esos_GetSystemTick(); \
00356 __pstSelf->u32_waitLen = (u32_duration); \
00357 ESOS_TASK_WAIT_UNTIL(__esos_hasTickDurationPassed(__pstSelf->u32_savedTick, __pstSelf->u32_waitLen) ); \
00358 } while(0);
00359
00362
00363 #define __ESOS_TASK_SPAWN(pstChild, fcnCallWithArgs) \
00364 do { \
00365 __ESOS_INIT_TASK((pstChild)); \
00366 ESOS_TASK_WAIT_THREAD((fcnCallWithArgs)); \
00367 } while(0)
00368
00396 #define ESOS_TASK_WAIT_THREAD(pfnChild, ...) ESOS_TASK_WAIT_WHILE(ESOS_SCHEDULE_TASK(pfnChild,##__VA_ARGS__ ))
00397
00413 #define ESOS_TASK_SPAWN_AND_WAIT(pstChild, pfnChild, ...) \
00414 __ESOS_TASK_SPAWN((pstChild), (pfnChild)( (pstChild), ##__VA_ARGS__) )
00415
00425 #define ESOS_ALLOCATE_CHILD_TASK(pstName) (pstName)=esos_GetFreeChildTaskStruct()
00426
00427
00428
00429
00430
00431
00432
00433
00456 #define ESOS_TASK_SLEEP() \
00457 do { \
00458 __ESOS_SET_TASK_SLEEPING_FLAG(__pstSelf); \
00459 ESOS_TASK_WAIT_WHILE(ESOS_IS_TASK_SLEEPING(__pstSelf)); \
00460 } while(0)
00461
00471 #define ESOS_TASK_RESTART() \
00472 do { \
00473 __pstSelf->flags = 0; \
00474 __ESOS_INIT_TASK(__pstSelf); \
00475 return ESOS_TASK_WAITING; \
00476 } while(0)
00477
00490 #define ESOS_TASK_EXIT() \
00491 do { \
00492 __pstSelf->flags = __TASK_ENDED_MASK; \
00493 return ESOS_TASK_ENDED; \
00494 } while(0)
00495
00496
00511 #define ESOS_WAKE_TASK(TaskHandle) __ESOS_TASK_CLEAR_SLEEPING_FLAG((TaskHandle))
00512
00524 #define ESOS_KILL_TASK(TaskHandle) __ESOS_TASK_SET_KILLED_FLAG((TaskHandle))
00525
00526
00542 #define ESOS_RESTART_TASK(TaskHandle) \
00543 do { \
00544 (TaskHandle)->flags = 0; \
00545 __ESOS_INIT_TASK((TaskHandle)); \
00546 } while(0)
00547
00548
00567 #define ESOS_TASK_YIELD() \
00568 do { \
00569 __ESOS_CLEAR_TASK_CALLED_FLAG(__pstSelf); \
00570 ESOS_TASK_WAIT_UNTIL(__ESOS_IS_TASK_CALLED(__pstSelf)); \
00571 } while(0)
00572
00582 struct stSemaphore {
00583 int16 i16_cnt;
00584 };
00585
00586
00587
00588
00589
00590
00591
00609 #define ESOS_SEMAPHORE(semaphoreName) struct stSemaphore (semaphoreName)
00610
00627 #define ESOS_INIT_SEMAPHORE(semaphoreName, i16_val) (semaphoreName).i16_cnt=(i16_val)
00628
00644 #define ESOS_TASK_WAIT_SEMAPHORE(semaphoreName, i16_val) \
00645 do { \
00646 ESOS_TASK_WAIT_UNTIL((semaphoreName).i16_cnt >= (i16_val) ); \
00647 (semaphoreName).i16_cnt -= (i16_val); \
00648 } while(0)
00649
00664 #define ESOS_SIGNAL_SEMAPHORE(semaphoreName, i16_val) (semaphoreName).i16_cnt+=(i16_val)
00665
00666
00667
00668
00669 #endif
00670
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711