Exemple #1
0
/*
 * Demonstrate Nut/OS one-shot timer.
 *
 * One-shot timers will call the callback function once and then stop
 * the timer automatically.
 *
 * Note that this function also demonstrates event timeouts.
 */
static void OneShotDemo(int s)
{
    HANDLE t;
    HANDLE e = NULL;
    int w;

    printf("Start %d s one-shot timer\n", s);
    t = NutTimerStart(s * 1000UL, TimerCallback, &e, TM_ONESHOT);
    for (w = 1; w < s + 1; w++) {
        printf("  Waiting %d s...", w);
        if (NutEventWait(&e, 1000UL) == 0) {
            puts("elapsed");
            break;
        }
        puts("timed out");
    }
}
Exemple #2
0
/*
 * Demonstrate Nut/OS periodic timer.
 *
 * Periodic timers will call the callback function and then restart
 * the timer automatically. They must be explicitly stopped, if no
 * longer needed.
 */
static void PeriodicDemo(int s)
{
    HANDLE t;
    HANDLE e = NULL;
    int i;
    uint32_t ms;

    printf("Start %d s periodic timer\n", s);
    t = NutTimerStart(s * 1000UL, TimerCallback, &e, 0);
    for (i = 0; i < 5; i++) {
        ms = NutGetMillis();
        printf("  Waiting...");
        NutEventWait(&e, NUT_WAIT_INFINITE);
        ms = NutGetMillis() - ms;
        printf("elapsed after %lu ms\n", ms);
    }
    puts("  Stop periodic timer");
    NutTimerStop(t);
}
Exemple #3
0
/*!
 * \brief Temporarily suspends the current thread.
 *
 * Causes the current thread to wait for a specified interval or,
 * if the specified interval is zero, to give up the CPU for
 * another thread with higher or same priority.
 *
 * This function may switch to another application thread, that
 * got the same or a higher priority and is ready to run.
 *
 * \note Threads may sleep longer than the specified number of
 *       milliseconds, depending on the number of threads
 *       with higher or equal priority, which are ready to run.
 *
 * \param ms Milliseconds to sleep. If 0, the current thread will not
 *           sleep, but may give up the CPU. The resolution is limited
 *           to the granularity of the system timer.
 *
 * \todo Code size can be reduced by trying to create the timer before
 *       removing the thread from the run queue.
 */
void NutSleep(uint32_t ms)
{
    if (ms) {

        /* remove running thread from runQueue */
        NutThreadRemoveQueue(runningThread, &runQueue);
        runningThread->td_state = TDS_SLEEP;

        if ((runningThread->td_timer = NutTimerStart(ms, NutThreadWake, runningThread, TM_ONESHOT)) != 0) {
#ifdef NUTTRACER
            TRACE_ADD_ITEM(TRACE_TAG_THREAD_SLEEP,(int)runningThread);
#endif
            NutThreadResume();
        } else
        {
            /* timer creation failed, restore queues */
            runningThread->td_queue = &runQueue;
            runningThread->td_qnxt  = runQueue;
            runningThread->td_state = TDS_RUNNING;
            runQueue = runningThread;
        }
    } else
        NutThreadYield();
}