Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

timers/timers.c

This sample demonstrates the usage of Nut/OS timer functions.




#include <dev/uartavr.h>
#include <sys/thread.h>
#include <sys/print.h>
#include <sys/timer.h>

static NUTDEVICE *uart;

/*
 * Timer callback routine.
 *
 * This function is called by the system timer thread.
 * It is executed at a very high priority and must 
 * return as soon as possible and must not call any 
 * potentially blocking function.
 *
 * To keep this example as simple as possible, we
 * break the above rule and call NutPrint functions.
 * However, this is not really a problem, because
 * the UART output queue won't overflow on our few
 * characters und NutPrintFlush returns immediately
 * after starting transmit interrupts, which are
 * running in the background.
 */
void TimerCallback(HANDLE timer, void *arg)
{
    int iarg = *((int *)arg);

    /*
     * Print the sequence number. Its pointer has been
     * passed to us as an argument.
     */
    NutPrintFormat(uart, "Callback sequence %d\r\n", iarg);
    NutPrintFlush(uart);
}

/*
 * Main application routine. 
 *
 * Nut/OS automatically calls this entry after initialization.
 */
THREAD(NutMain, arg)
{
    int i;
    int seq;
    u_long baud = 115200;
    u_long sleep_ms = 10000;
    u_long cpu_crystal;
    int one_shot;
    HANDLE timer;

    /*
     * Register the UART device, open it and
     * set the baudrate.
     */
    NutRegisterDevice(&devUart0, 0, 0);
    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);

    /*
     * The timer functions automatically determine the
     * CPU speed during system initialization. Query that
     * value and print it on the console.
     */
    cpu_crystal = NutGetCpuClock();
    NutPrintFormat(uart, "Timer sample running on %u.%u MHz CPU\r\n",
                   (int)(cpu_crystal / 1000000UL),
                   (int)((cpu_crystal - (cpu_crystal / 1000000UL) * 1000000UL) / 100)
                   );
    NutPrintFlush(uart);

    /*
     * Endless application loop.
     */
    for(seq = 0;; seq++) {

        /*
         * Predefine the one-shot option flag for the
         * timer started below. Each odd sequence starts
         * a one-shot timer, each even sequence a
         * priodical one.
         */
        if(seq & 1)
            one_shot = TM_ONESHOT;
        else
            one_shot = 0;

        /*
         * Start a timer with 1 second timer intervals.
         * This timer will call TimerCallback exactly one
         * time, if it's a one-shot timer or periodically,
         * if not a one-shot timer.
         *
         * We pass a pointer to the sequence counter,
         * which in turn is passed to the callback
         * function.
         */
        timer = NutTimerStart(1000, TimerCallback, &seq, one_shot);

        /*
         * Sleep for a number of seconds.
         */
        NutPrintFormat(uart, "Sleeping %u seconds\r\n", (int)(sleep_ms / 1000UL));
        NutPrintFlush(uart);
        NutSleep(sleep_ms);

        /*
         * Woken up after a sleep. Call NutDelay
         * of 250 milliseconds for 16 times. This
         * will completely block the CPU, so we
         * won't see any callback function calls
         * during this period.
         */
        NutPrintFormat(uart, "Woken up, now blocking CPU\r\n");
        NutPrintFlush(uart);
        for(i = 0; i < 16; i++)
            NutDelay(250);

        /*
         * Stop periodical timer. One-shot timers
         * are automatically stopped by Nut/OS.
         */
        if(one_shot == 0)
            NutTimerStop(timer);
    }
}

© 2000-2001 by egnite Software GmbH - visit http://www.ethernut.de/