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
00039 #include "esos.h"
00040
00041
00042
00043
00057 #define MAX_NUM_USER_TASKS 16
00058 #define MAX_NUM_CHILD_TASKS MAX_NUM_USER_TASKS
00059
00060 #define REMOVE_IDX 0xFE
00061
00062
00063
00064
00065 struct stTask __astUserTaskPool[MAX_NUM_USER_TASKS];
00066 uint8 __au8UserTaskStructIndex[MAX_NUM_USER_TASKS];
00067 struct stTask __astChildTaskPool[MAX_NUM_CHILD_TASKS];
00068 uint8 __u8UserTasksRegistered;
00069 uint8 __u8ChildTasksRegistered;
00070
00071 struct stTimer __astTmrSvcs[MAX_NUM_TMRS];
00072 uint8 __esos_u8TmrSvcsRegistered;
00073 uint16 __esos_u16TmrActiveFlags;
00074
00075 #ifdef ESOS_USE_BULK_CDC_USB
00076 static struct stTask __stUsbCommSystem;
00077 #endif
00078
00079 uint16 __esos_u16UserFlags, __esos_u16SystemFlags;
00080 uint32 __u32_esos_PRNG_Seed;
00081
00082
00083
00084
00094 ESOS_TASK_HANDLE esos_RegisterTask( uint8 (*taskname)(ESOS_TASK_HANDLE pstTask) ) {
00095 uint8 u8_i;
00096 uint8 u8_FoundFcn = FALSE;
00097 uint8 u8_IndexFcn;
00098 uint8 u8_IndexFree=0;
00099 uint8 u8_FoundFree = FALSE;
00100
00101 if (__u8UserTasksRegistered < MAX_NUM_USER_TASKS) {
00102 for (u8_i=0; u8_i<MAX_NUM_USER_TASKS; u8_i++) {
00103 if (__astUserTaskPool[u8_i].pfn == taskname) {
00104 u8_FoundFcn = TRUE;
00105 u8_IndexFcn = u8_i;
00106 break;
00107 }
00108 if ((!u8_FoundFree) && (__astUserTaskPool[u8_i].pfn == NULLPTR)) {
00109 u8_FoundFree = TRUE;
00110 u8_IndexFree = u8_i;
00111 }
00112 }
00113 if (u8_FoundFcn) {
00114 __ESOS_INIT_TASK( &__astUserTaskPool[u8_IndexFcn]);
00115 __astUserTaskPool[u8_IndexFcn].flags = 0;
00116 __au8UserTaskStructIndex[__u8UserTasksRegistered] = u8_IndexFcn;
00117 __u8UserTasksRegistered++;
00118 return &__astUserTaskPool[u8_IndexFcn];
00119 }
00120
00121 if (u8_FoundFree) {
00122 __astUserTaskPool[u8_IndexFree].pfn = taskname;
00123 __ESOS_INIT_TASK(&__astUserTaskPool[u8_IndexFree]);
00124 __astUserTaskPool[u8_IndexFree].flags = 0;
00125 __au8UserTaskStructIndex[__u8UserTasksRegistered] = u8_IndexFree;
00126 __u8UserTasksRegistered++;
00127 return &__astUserTaskPool[u8_IndexFree];
00128 }
00129
00130
00131
00132
00133 return NULLPTR;
00134 } else {
00135 return NULLPTR;
00136 }
00137 }
00138
00147 uint8 esos_UnregisterTask( uint8 (*taskname)(ESOS_TASK_HANDLE pstTask) ) {
00148 uint8 u8Status=FALSE;
00149 uint8 u8_i, u8_z;
00150 ESOS_TASK_HANDLE pstNowTask;
00151
00152 for (u8_i=0; u8_i<__u8UserTasksRegistered; u8_i++) {
00153
00154 u8_z = __au8UserTaskStructIndex[u8_i];
00155
00156 if ((u8_z != NULLIDX) & (u8_z != REMOVE_IDX)) {
00157 pstNowTask = &__astUserTaskPool[u8_z];
00158 if (pstNowTask->pfn == taskname) {
00159 __au8UserTaskStructIndex[u8_i] = REMOVE_IDX;
00160
00161
00162 __esos_SetSystemFlag( __ESOS_SYS_FLAG_PACK_TASKS );
00163 u8Status=TRUE;
00164 break;
00165 }
00166 }
00167 }
00168 return u8Status;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00186 ESOS_TASK_HANDLE esos_GetFreeChildTaskStruct() {
00187 uint16 u16_i = 0;
00188
00189 while (u16_i < MAX_NUM_CHILD_TASKS) {
00190 if (ESOS_IS_TASK_INITED( &__astChildTaskPool[u16_i]) )
00191 return &__astChildTaskPool[u16_i];
00192 u16_i++;
00193 }
00194 return NULLPTR;
00195 }
00196
00197
00210 void esos_SetRandomUint32Seed(uint32 u32_in) {
00211 __u32_esos_PRNG_Seed = u32_in;
00212 }
00213
00220 uint32 esos_GetRandomUint32(void) {
00221 uint32 hi, lo;
00222
00223 lo = 16807 * ( __u32_esos_PRNG_Seed * 0xFFFF );
00224 hi = 16807 * ( __u32_esos_PRNG_Seed >> 16 );
00225 lo += (hi & 0x7FFF) << 16;
00226 lo += (hi >> 15);
00227 if (lo > 0x7FFFFFFF) lo -= 0x7FFFFFFF;
00228 return (__u32_esos_PRNG_Seed = (uint32) lo );
00229 }
00230
00235 uint8 esos_GetMaxNumberTasks(void) {
00236 return MAX_NUM_USER_TASKS;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 uint16 __esos_hasTickDurationPassed(uint32 u32_startTick, uint32 u32_period) {
00249 uint32 u32_delta, u32_current;
00250
00251 u32_current = esos_GetSystemTick();
00252 u32_delta = u32_current - u32_startTick;
00253 if (u32_current < u32_startTick)
00254 u32_delta += 0xFFFFFFFF;
00255 if (u32_delta > u32_period)
00256 return TRUE;
00257 else
00258 return FALSE;
00259 }
00260
00261
00262
00263
00264
00265
00266 void __esos_tmrSvcsExecute(void) {
00267 uint8 u8_cnt, u8_index;
00268
00269 u8_cnt = __esos_u8TmrSvcsRegistered;
00270 u8_index = 0;
00271 while (u8_cnt) {
00272
00273 if (esos_IsTimerRunning(u8_index)) {
00274 __astTmrSvcs[u8_index].u32_cntDown--;
00275 if (__astTmrSvcs[u8_index].u32_cntDown == 0 ) {
00276 __astTmrSvcs[u8_index].u32_cntDown = __astTmrSvcs[u8_index].u32_period;
00277 __astTmrSvcs[u8_index].pfn();
00278 }
00279 u8_cnt--;
00280 }
00281 u8_index++;
00282 }
00283 }
00284
00302 ESOS_TMR_HANDLE esos_RegisterTimer( void (*timername)(void), uint32 u32_period ) {
00303 uint8 u8_i;
00304
00305 if ( esos_GetNumberRunningTimers() < MAX_NUM_TMRS) {
00306 for (u8_i=0; u8_i<MAX_NUM_TMRS; u8_i++ ) {
00307 if (!esos_IsTimerRunning(u8_i)) {
00308 __astTmrSvcs[u8_i].pfn = timername;
00309 __astTmrSvcs[u8_i].u32_period = u32_period;
00310 __astTmrSvcs[u8_i].u32_cntDown = u32_period;
00311 __esos_u8TmrSvcsRegistered++;
00312 __esos_MarkTimerRunning( u8_i );
00313 return u8_i;
00314 }
00315 }
00316 return ESOS_TMR_FAILURE;
00317 }
00318 else
00319 return ESOS_TMR_FAILURE;
00320 }
00321
00331 uint8 esos_UnregisterTimer( ESOS_TMR_HANDLE hnd_timer ) {
00332
00333 if ( esos_IsTimerRunning(hnd_timer) ) {
00334 __astTmrSvcs[hnd_timer].pfn = NULLPTR;
00335 __esos_u8TmrSvcsRegistered--;
00336 __esos_MarkTimerStopped(hnd_timer);
00337 return TRUE;
00338 } else
00339 return FALSE;
00340 }
00341
00352 ESOS_TMR_HANDLE esos_GetTimerHandle( void (*pfnTmrFcn)(void) ) {
00353 uint8 u8_i=0;
00354 uint8 u8_cnt;
00355
00356 u8_cnt = esos_GetNumberRunningTimers();
00357 while (u8_cnt) {
00358 if (esos_IsTimerRunning(u8_i) ) {
00359 if ( __astTmrSvcs[u8_i].pfn == pfnTmrFcn ) return u8_i;
00360 u8_cnt--;
00361 }
00362 u8_i++;
00363 }
00364 return ESOS_TMR_FAILURE;
00365 }
00366
00367
00379 uint8 esos_ChangeTimerPeriod( ESOS_TMR_HANDLE hnd_timer, uint32 u32_period ) {
00380
00381 if (esos_IsTimerRunning(hnd_timer) ) {
00382 __astTmrSvcs[hnd_timer].u32_period = u32_period;
00383 return TRUE;
00384 } else return FALSE;
00385 }
00386
00387 void __esosInit(void) {
00388 uint8 i;
00389
00390
00391 for (i=0; i<MAX_NUM_USER_TASKS; i++) {
00392 __astUserTaskPool[i].pfn = NULLPTR;
00393 __au8UserTaskStructIndex[i] = NULLIDX;
00394 __astChildTaskPool[i].pfn = NULLPTR;
00395 }
00396 __esos_u16TmrActiveFlags = 0;
00397 for (i=0; i<MAX_NUM_TMRS; i++) {
00398 __astTmrSvcs[i].pfn = NULLPTR;
00399 }
00400
00401
00402 __u8UserTasksRegistered = 0;
00403
00404 __u8ChildTasksRegistered = 0;
00405
00406 __esos_u8TmrSvcsRegistered = 0;
00407
00408
00409 __u32_esos_PRNG_Seed = MAX_NUM_USER_TASKS;
00410
00411
00412
00413
00414 __esos_hw_InitSystemTick();
00415
00416
00417
00418
00419
00420 #ifdef ESOS_USE_IRQS
00421 _esos_hw_InitUserInterrupts();
00422 #endif
00423
00424
00425
00426
00427
00428
00429
00430
00431 #ifdef ESOS_USE_BULK_CDC_USB
00432 __esos_InitCommSystem();
00433 #endif
00434 #ifdef ESOS_USE_SERIAL_PORT
00435 __esos_InitCommSystem();
00436 #endif
00437
00438 user_init();
00439
00440 }
00441
00442 main_t main(void) {
00443 uint8 u8TaskReturnedVal=0;
00444 uint8 u8i ,u8j, u8NumRegdTasksTemp;
00445 ESOS_TASK_HANDLE pstNowTask;
00446
00447 __esosInit();
00448
00449
00450
00451
00452
00453 while (TRUE) {
00454
00455
00456
00457
00458
00459 u8i = 0;
00460
00461
00462
00463
00464 u8NumRegdTasksTemp = __u8UserTasksRegistered;
00465 while ( u8i < u8NumRegdTasksTemp ) {
00466 pstNowTask = &__astUserTaskPool[__au8UserTaskStructIndex[u8i]];
00467 u8TaskReturnedVal = pstNowTask->pfn( pstNowTask );
00468 if (u8TaskReturnedVal == ESOS_TASK_ENDED) {
00469
00470 esos_UnregisterTask( pstNowTask->pfn );
00471 }
00472 u8i++;
00473 OS_ITERATE;
00474 }
00475
00476
00477 if (__esos_IsSystemFlagSet( __ESOS_SYS_FLAG_PACK_TASKS) ) {
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 u8i = __u8UserTasksRegistered;
00490 while(TRUE) {
00491 pstNowTask = &__astUserTaskPool[u8i];
00492 if (__au8UserTaskStructIndex[u8i] == REMOVE_IDX) {
00493 for ( u8j=u8i+1; u8j<u8NumRegdTasksTemp; u8j++) {
00494 __au8UserTaskStructIndex[u8j-1] = __au8UserTaskStructIndex[u8j];
00495 }
00496 __au8UserTaskStructIndex[u8NumRegdTasksTemp-1] = NULLIDX;
00497 u8NumRegdTasksTemp--;
00498 }
00499
00500 if (u8i) u8i--; else break;
00501 }
00502 __u8UserTasksRegistered=u8NumRegdTasksTemp;
00503 __esos_ClearSystemFlag( __ESOS_SYS_FLAG_PACK_TASKS );
00504 }
00505 }
00506
00507 OS_END;
00508
00509 }