int main (void) { int gameMode = MODE_INIT; int playerHand; int enemyHand; int records[RECORD_MAX][2]; int recordHeadIndex = 0; init(records, RECORD_MAX); while (1) { switch (gameMode) { case MODE_INIT: gameMode = taskInit(); break; case MODE_OP: gameMode = taskOp(&playerHand); break; case MODE_CHOICE: gameMode = taskChoiceHand(&enemyHand); saveHands(records, &recordHeadIndex, playerHand, enemyHand); break; case MODE_SHOW: gameMode = taskShow(playerHand, enemyHand); break; case MODE_SELECT: gameMode = taskSelect(); break; case MODE_RECORD: gameMode = taskRecord(records, recordHeadIndex, RECORD_MAX); break; case MODE_ED: gameMode = taskEd(records, recordHeadIndex, RECORD_MAX); return gameMode; } } }
int taskCreat( const char *name, unsigned priority, int options, unsigned stackSize, FUNCPTR func, ARG arg0, ARG arg1, ARG arg2, ARG arg3, ARG arg4, ARG arg5, ARG arg6, ARG arg7, ARG arg8, ARG arg9 ) { static char digits[] = "0123456789"; TCB_ID tcbId; char *pTaskMem; char *pStackBase; char newName[20]; int value; int nBytes; int nPreBytes; char *pBufStart; char *pBufEnd; char ch; /* Check if task lib is installed */ if (taskLibInstalled != TRUE) { errnoSet(S_taskLib_NOT_INSTALLED); tcbId = NULL; } else { /* If NULL name create default name for task */ if (name == NULL) { strcpy(newName, namelessPrefix); nBytes = strlen(newName); nPreBytes = nBytes; value = ++namelessCount; /* Do while value is non-zero */ do { newName[nBytes++] = digits[value % 10]; value /= 10; } while(value != 0); /* Calculate start/end positions in name */ pBufStart = newName + nPreBytes; pBufEnd = newName + nBytes - 1; /* While startbuffer lt. end buffer */ while (pBufStart < pBufEnd) { ch = *pBufStart; *pBufStart = *pBufEnd; *pBufEnd = ch; pBufStart++; pBufEnd--; } /* Terminate string */ newName[nBytes] = EOS; /* Use this a the name for the task */ name = newName; } /* Round up stack size */ stackSize = STACK_ROUND_UP(stackSize); /* Allocate new TCB plus stack */ pTaskMem = objAllocPad( taskClassId, (unsigned) stackSize, (void **) NULL ); if (pTaskMem == NULL) { /* errno set by objAllocPad() */ tcbId = NULL; } else { /* Setup stack vars */ #if (_STACK_DIR == _STACK_GROWS_DOWN) pStackBase = pTaskMem + stackSize; tcbId = (TCB_ID) pStackBase; #else /* _STACK_GROWS_UP */ tcbId = (TCB_ID) (pTaskMem + TASK_EXTRA_BYTES); pStackBase = STACK_ROUND_UP( pTaskMem + TASK_EXTRA_BYTES + sizeof(TCB) ); #endif /* _STACK_DIR */ /* Initialize task */ if (taskInit( tcbId, name, priority, options, pStackBase, stackSize, func, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 ) != OK) { /* errno set by taskInit() */ objFree(taskClassId, tcbId); tcbId = NULL; } } } return (int) tcbId; }
STATUS taskRestart( int taskId ) { TCB_ID tcbId; char *name, *rename; int len; unsigned priority; int options; char *pStackBase; unsigned stackSize; FUNCPTR entry; ARG args[MAX_TASK_ARGS]; STATUS status; if (INT_RESTRICT() != OK) { errnoSet(S_intLib_NOT_ISR_CALLABLE); return ERROR; } /* If self restart */ if ( (taskId == 0) || (taskId == (int) taskIdCurrent) ) { /* Task must be unsafe */ while (taskIdCurrent->safeCount > 0) taskSafe(); /* Spawn a task that will restart this task */ taskSpawn(restartTaskName, restartTaskPriority, restartTaskOptions, restartTaskStackSize, taskRestart, (ARG) taskIdCurrent, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0, (ARG) 0); /* Wait for restart */ while (1) taskSuspend(0); } /* End if self restart */ /* Get task context */ tcbId = taskTcb(taskId); if (tcbId == NULL) return ERROR; /* TASK_ID_VERIFY() already done by taskTcb() */ /* Copy task data */ priority = tcbId->priority; options = tcbId->options; entry = tcbId->entry; pStackBase = tcbId->pStackBase; stackSize = (tcbId->pStackEnd - tcbId->pStackBase) * _STACK_DIR; taskArgGet(tcbId, pStackBase, args); /* Copy name if needed */ name = tcbId->name; rename = NULL; if (name != NULL) { len = strlen(name) + 1; rename = malloc(len); if (rename != NULL) strcpy(rename, name); name = rename; } /* Prevent deletion */ taskSafe(); if (taskTerminate((int) tcbId) != OK) { taskUnsafe(); /* errno set by taskTerminate() */ return ERROR; } /* Initialize task with same data */ status = taskInit(tcbId, name, priority, options, pStackBase, stackSize, entry, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]); if (status != OK) { /* errno set by taskInit() */ return ERROR; } /* And start it */ status = taskActivate((int) tcbId); if (status != OK) { /* errno set by taskActivate() */ return ERROR; } /* Make me mortal */ taskUnsafe(); /* Free rename buffer if needed */ if (rename != NULL) free(rename); return OK; }
int offlineTaskInit (OfflineTask * rt, RcFile * cfg, struct PgmState_s *p) { Section *s; long tmp; char *cp; memset (rt, 0, sizeof (OfflineTask)); rt->p = p; debugMsg ("offlineTaskInit start\n"); s = rcfileFindSec (cfg, "OFFLINE"); rt->acq_duration = 120; rt->idle_duration = 24 * 3600; rt->adjust_interval = 0; rt->t_pos = 0; rt->t_freq = 0; if (s) { if (!rcfileFindSecValInt (s, "adjust_interval_hr", &tmp)) { rt->adjust_interval = tmp * 3600; } if (!rcfileFindSecValInt (s, "acq_duration_s", &tmp)) { rt->acq_duration = tmp; } if (!rcfileFindSecValInt (s, "idle_duration_hr", &tmp)) { rt->idle_duration = tmp * 3600; } if (!rcfileFindSecValInt (s, "t_pos", &tmp)) { rt->t_pos = tmp; } if (!rcfileFindSecValInt (s, "t_freq_khz", &tmp)) { rt->t_freq = tmp / 10; } if (!rcfileFindSecVal (s, "t_pol", &cp)) { switch (cp[0]) { case 'h': case 'H': rt->t_pol = 0; break; case 'v': case 'V': rt->t_pol = 1; break; case 'l': case 'L': rt->t_pol = 2; break; case 'r': case 'R': rt->t_pol = 3; break; default: break; } } } if (taskInit (&rt->t, rt, offline_task)) { errMsg ("task failed to init\n"); return 1; } return 0; }
static int __wind_task_init(struct task_struct *curr, struct pt_regs *regs) { xncompletion_t __user *u_completion; char name[XNOBJECT_NAME_LEN]; struct wind_arg_bulk bulk; int err = 0, prio, flags; WIND_TCB_PLACEHOLDER ph; WIND_TCB *task; if (!__xn_access_ok (curr, VERIFY_READ, __xn_reg_arg1(regs), sizeof(bulk))) return -EFAULT; if (!__xn_access_ok (curr, VERIFY_WRITE, __xn_reg_arg2(regs), sizeof(ph))) return -EFAULT; __xn_copy_from_user(curr, &bulk, (void __user *)__xn_reg_arg1(regs), sizeof(bulk)); if (bulk.a1) { if (!__xn_access_ok(curr, VERIFY_READ, bulk.a1, sizeof(name))) return -EFAULT; __xn_strncpy_from_user(curr, name, (const char __user *)bulk.a1, sizeof(name) - 1); name[sizeof(name) - 1] = '\0'; strncpy(curr->comm, name, sizeof(curr->comm)); curr->comm[sizeof(curr->comm) - 1] = '\0'; } else *name = '\0'; /* Task priority. */ prio = bulk.a2; /* Task flags. */ flags = bulk.a3 | VX_SHADOW; /* Completion descriptor our parent thread is pending on. */ u_completion = (xncompletion_t __user *)__xn_reg_arg3(regs); task = (WIND_TCB *)xnmalloc(sizeof(*task)); if (!task) { if (u_completion) xnshadow_signal_completion(u_completion, -ENOMEM); return -ENOMEM; } xnthread_clear_state(&task->threadbase, XNZOMBIE); /* Force FPU support in user-space. This will lead to a no-op if the platform does not support it. */ if (taskInit(task, name, prio, flags, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == OK) { /* Let the skin discard the TCB memory upon exit. */ task->auto_delete = 1; task->ptid = bulk.a4; /* Copy back the registry handle to the ph struct. */ ph.handle = xnthread_handle(&task->threadbase); __xn_copy_to_user(curr, (void __user *)__xn_reg_arg2(regs), &ph, sizeof(ph)); err = xnshadow_map(&task->threadbase, u_completion); } else { /* Unblock and pass back error code. */ err = wind_errnoget(); if (u_completion) xnshadow_signal_completion(u_completion, err); } if (err && !xnthread_test_state(&task->threadbase, XNZOMBIE)) xnfree(task); return err; }
int main( void ) { u32 i, ii, ulClock; u8 floatString[20]; // First test the descriptor page ( last page in FLASH ) // if configuration exists, use programmed ID, else query // Debug registers for ID. This is a workaround for silicon // issues in the F103 STM32s RCC->APB2ENR |= (RCC_IOPBEN|RCC_IOPCEN|RCC_IOPAEN); PYGMY_RCC_USART3_ENABLE; PYGMY_RCC_USART2_ENABLE; PYGMY_RCC_USART1_ENABLE; PYGMY_RCC_GPIOA_ENABLE; PYGMY_RCC_GPIOB_ENABLE; //globalID = fpecMCUID( ); globalID = 0; globalPLL = BIT16|BIT1; // First Init the Clocks if( globalID == 0x0416 ){ // L152 globalStrID = (u8*)STRID_L15X; globalXTAL = 16000000; globalFreq = 32000000; ulClock = globalFreq; FPEC->ACR = FPEC_ACR_PRFTBE | FPEC_ACR_LATENCY1; } else if( globalID == 0x0420 || globalID == 0x0428 ){ // F100 globalStrID = (u8*)STRID_F100; globalXTAL = 12000000; globalFreq = 24000000; ulClock = globalFreq; } else{ // F103 globalStrID = (u8*)STRID_F103; globalXTAL = 8000000; globalFreq = 72000000; //72022900; ulClock = 36000000; globalPLL = RCC_PLL_X9|BIT16|BIT15|BIT14|BIT1;//BIT10|BIT1; FPEC->ACR = FPEC_ACR_PRFTBE | FPEC_ACR_LATENCY2; } // else PYGMY_RCC_HSI_ENABLE; PYGMY_RCC_HSE_ENABLE; while( !PYGMY_RCC_HSE_READY ); RCC->CFGR2 = 0; RCC->CFGR = globalPLL; PYGMY_RCC_PLL_ENABLE; while( !PYGMY_RCC_PLL_READY ); taskInit(); streamInit(); streamSetPut( COM1, putsUSART1 ); streamSetPutc( COM1, putcUSART1 ); streamSetGet( COM1, bootGetUSART1 ); streamDisableDefaultGet( COM1 ); streamSetSTDIO( COM1 ); // Basic Port Init GPIOB->CRH &= ~( PIN10_CLEAR | PIN11_CLEAR ); GPIOB->CRH |= ( PIN10_OUT50_ALTPUSHPULL | PIN11_IN_FLOAT ); USART3->BRR = ( ( (ulClock >> 3 ) / BOOT_BAUDRATE ) << 4 ) + ( ( ( ulClock / BOOT_BAUDRATE ) ) & 0x0007 ); USART3->CR3 = USART_ONEBITE; USART3->CR1 = ( USART_OVER8 | USART_UE | USART_RXNEIE | USART_TE | USART_RE ); GPIOA->CRH &= ~( PIN9_CLEAR | PIN10_CLEAR ); GPIOA->CRH |= ( PIN9_OUT50_ALTPUSHPULL | PIN10_IN_FLOAT ); USART1->BRR = ( ( (ulClock >> 3 ) / BOOT_BAUDRATE ) << 4 ) + ( ( ( ulClock / BOOT_BAUDRATE ) ) & 0x0007 ); USART1->CR3 = USART_ONEBITE; USART1->CR1 = ( USART_OVER8 | USART_UE | USART_RXNEIE | USART_TE | USART_RE ); // End Basic Port Init // HSI Must be ON for Flash Program/Erase Operations // End Clock Init // Configure Interrupts // Do not read from NVIC LOAD or VAL!!! // Reading from Write only registers causes unpredictable behavior!!! NVIC->ISER[ 1 ] = 0x00000001 << 7; NVIC->ISER[ 1 ] = 0x00000001 << 6; NVIC->ISER[ 1 ] = 0x00000001 << 5; SYSTICK->VAL = globalFreq / 1000; SYSTICK->LOAD = globalFreq / 1000; // Based on ( 2X the System Clock ) / 1000 SYSTICK->CTRL = 0x07; // Enable system timer // End Configure Interrupts PYGMY_WATCHDOG_UNLOCK; PYGMY_WATCHDOG_PRESCALER( IWDT_PREDIV128 ); PYGMY_WATCHDOG_TIMER( 0x0FFF ); PYGMY_WATCHDOG_START; PYGMY_WATCHDOG_REFRESH; println( STDIO, "Pygmy Boot Lite V%s\rMCU ", version ); println( STDIO, "%s\rPage Size %d\rFLASH %dKB\rRAM %dKB", globalStrID, fpecFlashSectorSize(), fpecFlashSize(), fpecRAMSize() ); println( STDIO, "\rMCUID: 0x%08X", fpecMCUID() ); xmodemInit( &XModem ); while( 1 ){ // Wait for commands } }