/* Blocks the thread until a message arrives in the mailbox, but does not block the thread longer than "timeout" milliseconds (similar to the sys_arch_sem_wait() function). The "msg" argument is a result parameter that is set by the function (i.e., by doing "*msg = ptr"). The "msg" parameter maybe NULL to indicate that the message should be dropped. The return values are the same as for the sys_arch_sem_wait() function: Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a timeout. Note that a function with a similar name, sys_mbox_fetch(), is implemented by lwIP. */ u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) { void *data; StatusType err; u32_t timeoutTick; u32_t startTick, endTick, ElapseTime; // Covert milliseconds to os ticks timeoutTick = (timeout * CFG_SYSTICK_FREQ) / 1000; if (timeoutTick == 0 && timeout != 0) timeoutTick = 1; startTick = CoGetOSTime(); data = CoPendQueueMail(*mbox, timeoutTick, &err); if (timeoutTick != 0 && E_OK != err) { if (msg) *msg = NULL; return SYS_ARCH_TIMEOUT; } endTick = CoGetOSTime(); if (msg) *msg = data; ElapseTime = ((endTick - startTick) * 1000) / CFG_SYSTICK_FREQ; return ElapseTime; }
/* Blocks the thread while waiting for the semaphore to be signaled. If the "timeout" argument is non-zero, the thread should only be blocked for the specified time (measured in milliseconds). If the timeout argument is non-zero, the return value is the number of milliseconds spent waiting for the semaphore to be signaled. If the semaphore wasn't signaled within the specified time, the return value is SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore (i.e., it was already signaled), the function may return zero. Notice that lwIP implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function. */ u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) { StatusType result; u32_t timeoutTick; u32_t startTick, endTick, elapseTime; //in lwip ,timeout is millisecond //in Coos ,timeout is timer tick! //chang timeout from millisecond to Coos tick timeoutTick = (timeout * CFG_SYSTICK_FREQ) / 1000; if(timeoutTick==0 && timeout != 0) { timeoutTick = 1; } if(timeoutTick > 0xFFFF) { timeoutTick = 0xFFFF; } startTick = CoGetOSTime(); result = CoPendSem(*sem, timeoutTick); if(timeoutTick != 0 && result != E_OK) { return SYS_ARCH_TIMEOUT; } endTick = CoGetOSTime(); //chang timeout from Coos tick to millisecond elapseTime = ((endTick - startTick) *1000) / CFG_SYSTICK_FREQ; return elapseTime; }
int16_t inqueue_get(queue_t *q, int timeout) { StatusType rc; uint64_t deadline = 0; uint8_t value; if (timeout > TIMEOUT_NOBLOCK) { deadline = timeout + CoGetOSTime(); } DISABLE_IRQ(); while (q->count == 0) { ENABLE_IRQ(); if (timeout == TIMEOUT_NOBLOCK) { return EERR_TIMEOUT; } rc = CoWaitForSingleFlag(q->flag, timeout == TIMEOUT_FOREVER ? 0 : (CoGetOSTime() - deadline)); if (rc != E_OK) { return EERR_TIMEOUT; } DISABLE_IRQ(); } q->count--; value = *q->p_read++; if (q->p_read == q->p_top) { q->p_read = q->p_bot; } ENABLE_IRQ(); return value; }
int16_t outqueue_put(queue_t *q, const uint8_t *value, uint16_t size, int timeout) { StatusType rc; uint64_t deadline = 0; if (timeout > TIMEOUT_NOBLOCK) { deadline = timeout + CoGetOSTime(); } DISABLE_IRQ(); while (size) { while (q->count == q->size) { /* Wake this thread up when the buffer has room for all remaining * bytes, or is empty. */ if (size > q->size) { q->wakeup = 0; } else { q->wakeup = q->size - size; } ENABLE_IRQ(); if (q->cb_func) { q->cb_func(q->cb_arg); } if (timeout == TIMEOUT_NOBLOCK) { return EERR_TIMEOUT; } rc = CoWaitForSingleFlag(q->flag, timeout == TIMEOUT_FOREVER ? 0 : (CoGetOSTime() - deadline)); if (rc != E_OK) { return EERR_TIMEOUT; } DISABLE_IRQ(); } size--; q->count++; *q->p_write++ = *value++; if (q->p_write == q->p_top) { q->p_write = q->p_bot; } } ENABLE_IRQ(); if (q->cb_func) { q->cb_func(q->cb_arg); } return EERR_OK; }
static void sem3_execute(void) { StatusType err; U64 expectTime; int i; sem1ID = CoCreateSem(0,5,EVENT_SORT_TYPE_FIFO); // Test 1 AcceptSem expectTime = CoGetOSTime(); err = CoAcceptSem(sem1ID); testAssert(err == E_SEM_EMPTY,"#5"); testAssertTimeWindow(expectTime,expectTime + ALLOWED_DELAY_TICK); // Test 2 PendSem with timeout //printf("#5"); expectTime = CoGetOSTime() + (MAX_SLAVE_TEST_TASKS * 50); for(i = 0; i < MAX_SLAVE_TEST_TASKS; i++) { err = CoPendSem(sem1ID,50); testAssert(err == E_TIMEOUT,"#6"); } testAssertTimeWindow(expectTime,expectTime + ALLOWED_DELAY_TICK); //printf("#6"); // Test 3 PendSem without timeout Task_Id[0] = CoCreateTask(task2, "A", MAINTEST_PRIMARY_PRIORITY - 3, &Task_Stack[0][SLAVE_TASK_STK_SIZE-1], SLAVE_TASK_STK_SIZE); err = CoPendSem(sem1ID,100); testAssert(err == E_OK,"#8"); // PostSem only inc 1 && PendSem dec 1 //printf("#7"); err = CoPendSem(sem1ID,100); testAssert(err == E_TIMEOUT,"#9"); err = CoDelSem(sem1ID,OPT_DEL_NO_PEND); testAssert(err == E_OK,"#7"); }
void menusTask(void * pdata) { opentxInit(); #if defined(PCBTARANIS) && defined(REV9E) while (1) { uint32_t pwr_check = pwrCheck(); if (pwr_check == e_power_off) { break; } else if (pwr_check == e_power_press) { continue; } #else while (pwrCheck() != e_power_off) { #endif U64 start = CoGetOSTime(); perMain(); // TODO remove completely massstorage from sky9x firmware U32 runtime = (U32)(CoGetOSTime() - start); // deduct the thread run-time from the wait, if run-time was more than // desired period, then skip the wait all together if (runtime < MENU_TASK_PERIOD_TICKS) { CoTickDelay(MENU_TASK_PERIOD_TICKS - runtime); } } #if defined(REV9E) topLcdOff(); #endif BACKLIGHT_OFF(); #if defined(PCBTARANIS) displaySleepBitmap(); #else lcd_clear(); displayPopup(STR_SHUTDOWN); #endif opentxClose(); boardOff(); // Only turn power off if necessary } extern void audioTask(void* pdata); void tasksStart() { CoInitOS(); #if defined(CLI) cliStart(); #endif #if defined(BLUETOOTH) btTaskId = CoCreateTask(btTask, NULL, 15, &btStack[BT_STACK_SIZE-1], BT_STACK_SIZE); #endif mixerTaskId = CoCreateTask(mixerTask, NULL, 5, &mixerStack[MIXER_STACK_SIZE-1], MIXER_STACK_SIZE); menusTaskId = CoCreateTask(menusTask, NULL, 10, &menusStack[MENUS_STACK_SIZE-1], MENUS_STACK_SIZE); audioTaskId = CoCreateTask(audioTask, NULL, 7, &audioStack[AUDIO_STACK_SIZE-1], AUDIO_STACK_SIZE); #if !defined(SIMU) audioMutex = CoCreateMutex(); mixerMutex = CoCreateMutex(); #endif CoStartOS(); }