Contiki 2.6

rtimer.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rt
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  *         Implementation of the architecture-agnostic parts of the real-time timer module.
00009  * \author
00010  *         Adam Dunkels <adam@sics.se>
00011  *
00012  */
00013 
00014 
00015 /*
00016  * Copyright (c) 2005, Swedish Institute of Computer Science
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms, with or without
00020  * modification, are permitted provided that the following conditions
00021  * are met:
00022  * 1. Redistributions of source code must retain the above copyright
00023  *    notice, this list of conditions and the following disclaimer.
00024  * 2. Redistributions in binary form must reproduce the above copyright
00025  *    notice, this list of conditions and the following disclaimer in the
00026  *    documentation and/or other materials provided with the distribution.
00027  * 3. Neither the name of the Institute nor the names of its contributors
00028  *    may be used to endorse or promote products derived from this software
00029  *    without specific prior written permission.
00030  *
00031  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00032  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00034  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00035  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00036  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00037  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00038  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00039  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00040  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00041  * SUCH DAMAGE.
00042  *
00043  * This file is part of the Contiki operating system.
00044  *
00045  * @(#)$Id: rtimer.c,v 1.7 2010/01/19 13:08:24 adamdunkels Exp $
00046  */
00047 
00048 #include "sys/rtimer.h"
00049 #include "contiki.h"
00050 
00051 #define DEBUG 0
00052 #if DEBUG
00053 #include <stdio.h>
00054 #define PRINTF(...) printf(__VA_ARGS__)
00055 #else
00056 #define PRINTF(...)
00057 #endif
00058 
00059 static struct rtimer *next_rtimer;
00060 
00061 /*---------------------------------------------------------------------------*/
00062 void
00063 rtimer_init(void)
00064 {
00065   rtimer_arch_init();
00066 }
00067 /*---------------------------------------------------------------------------*/
00068 int
00069 rtimer_set(struct rtimer *rtimer, rtimer_clock_t time,
00070            rtimer_clock_t duration,
00071            rtimer_callback_t func, void *ptr)
00072 {
00073   int first = 0;
00074 
00075   PRINTF("rtimer_set time %d\n", time);
00076 
00077   if(next_rtimer == NULL) {
00078     first = 1;
00079   }
00080 
00081   rtimer->func = func;
00082   rtimer->ptr = ptr;
00083 
00084   rtimer->time = time;
00085   next_rtimer = rtimer;
00086 
00087   if(first == 1) {
00088     rtimer_arch_schedule(time);
00089   }
00090   return RTIMER_OK;
00091 }
00092 /*---------------------------------------------------------------------------*/
00093 void
00094 rtimer_run_next(void)
00095 {
00096   struct rtimer *t;
00097   if(next_rtimer == NULL) {
00098     return;
00099   }
00100   t = next_rtimer;
00101   next_rtimer = NULL;
00102   t->func(t, t->ptr);
00103   if(next_rtimer != NULL) {
00104     rtimer_arch_schedule(next_rtimer->time);
00105   }
00106   return;
00107 }
00108 /*---------------------------------------------------------------------------*/