2007-05-22 22:58:38 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup rt
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of the architecture-agnostic parts of the real-time timer module.
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-03-19 01:16:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2007-10-23 22:33:19 +02:00
|
|
|
* @(#)$Id: rtimer.c,v 1.5 2007/10/23 20:33:19 adamdunkels Exp $
|
2007-03-19 01:16:13 +01:00
|
|
|
*/
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
#include "sys/rtimer.h"
|
2007-03-19 01:16:13 +01:00
|
|
|
#include "contiki.h"
|
|
|
|
|
2007-05-22 22:58:38 +02:00
|
|
|
#ifdef RTIMER_CONF_NUM
|
|
|
|
#define LIST_SIZE RTIMER_CONF_NUM
|
|
|
|
#else
|
2007-05-17 02:23:58 +02:00
|
|
|
#define LIST_SIZE 4
|
2007-05-22 22:58:38 +02:00
|
|
|
#endif
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
static struct rtimer *rtimers[LIST_SIZE];
|
2007-03-19 01:16:13 +01:00
|
|
|
static u8_t next, firstempty;
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-25 19:10:30 +02:00
|
|
|
rtimer_init(void)
|
2007-03-19 01:16:13 +01:00
|
|
|
{
|
|
|
|
next = 0;
|
|
|
|
firstempty = 0;
|
2007-03-25 19:10:30 +02:00
|
|
|
rtimer_arch_init();
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-10-23 22:33:19 +02:00
|
|
|
rtimer_set(struct rtimer *rtimer, rtimer_clock_t time,
|
|
|
|
rtimer_clock_t duration,
|
|
|
|
rtimer_callback_t func, void *ptr)
|
2007-03-19 01:16:13 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
PRINTF("rtimer_set time %d\n", time);
|
2007-03-28 22:28:22 +02:00
|
|
|
|
|
|
|
rtimer->func = func;
|
|
|
|
rtimer->ptr = ptr;
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Check if rtimer queue is full. */
|
2007-03-19 01:16:13 +01:00
|
|
|
if(firstempty == (next - 1) % LIST_SIZE) {
|
2007-03-25 19:10:30 +02:00
|
|
|
PRINTF("rtimer_set: next %d firstempty %d full\n", next, firstempty);
|
|
|
|
return RTIMER_ERR_FULL;
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Check if it is possible to run this rtimer at the requested
|
2007-03-19 01:16:13 +01:00
|
|
|
time. */
|
|
|
|
for(i = next; i != firstempty;
|
|
|
|
i = (i + 1) % LIST_SIZE) {
|
|
|
|
|
2007-05-17 02:23:58 +02:00
|
|
|
if(rtimers[i] == rtimer) {
|
|
|
|
/* Check if timer is already scheduled. If so, we do not
|
|
|
|
schedule it again. */
|
|
|
|
return RTIMER_ERR_ALREADY_SCHEDULED;
|
|
|
|
|
|
|
|
}
|
2007-03-19 01:16:13 +01:00
|
|
|
/* XXX: should check a range of time not just the same precise
|
|
|
|
moment. */
|
2007-03-25 19:10:30 +02:00
|
|
|
if(rtimers[i]->time == time) {
|
|
|
|
PRINTF("rtimer_set: next %d firstempty %d time %d == %d\n",
|
|
|
|
next, firstempty, rtimers[i]->time, time);
|
|
|
|
return RTIMER_ERR_TIME;
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
}
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Put the rtimer at the end of the rtimer list. */
|
|
|
|
rtimer->time = time;
|
|
|
|
rtimers[firstempty] = rtimer;
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_post: putting rtimer %p as %d\n", rtimer, firstempty);
|
2007-03-19 01:16:13 +01:00
|
|
|
|
|
|
|
firstempty = (firstempty + 1) % LIST_SIZE;
|
|
|
|
|
2007-03-28 22:28:22 +02:00
|
|
|
/* PRINTF("rtimer_post: next %d firstempty %d scheduling soon\n",
|
2007-03-19 01:16:13 +01:00
|
|
|
next, firstempty);*/
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Check if this is the first rtimer on the list. If so, we need to
|
2007-03-28 22:28:22 +02:00
|
|
|
run the rtimer_arch_schedule() function to get the ball rolling. */
|
2007-03-19 01:16:13 +01:00
|
|
|
if(firstempty == (next + 1) % LIST_SIZE) {
|
|
|
|
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_set scheduling %d %p (%d)\n",
|
|
|
|
next, rtimers[next], rtimers[next]->time);
|
2007-03-28 22:28:22 +02:00
|
|
|
rtimer_arch_schedule(rtimers[next]->time);
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
return RTIMER_OK;
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-25 19:10:30 +02:00
|
|
|
rtimer_run_next(void)
|
2007-03-19 01:16:13 +01:00
|
|
|
{
|
|
|
|
int i, n;
|
2007-03-25 19:10:30 +02:00
|
|
|
struct rtimer *t;
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-05-17 02:23:58 +02:00
|
|
|
/* Do not run timer if list is empty. */
|
|
|
|
if(next == firstempty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
t = rtimers[next];
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Increase the pointer to the next rtimer. */
|
2007-03-19 01:16:13 +01:00
|
|
|
next = (next + 1) % LIST_SIZE;
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Run the rtimer. */
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_run_next running %p\n", t);
|
2007-03-19 01:16:13 +01:00
|
|
|
t->func(t, t->ptr);
|
|
|
|
|
|
|
|
if(next == firstempty) {
|
2007-03-25 19:10:30 +02:00
|
|
|
PRINTF("rtimer_run_next: empty rtimer list\n");
|
|
|
|
/* The list is empty, no more rtimers to schedule. */
|
2007-03-19 01:16:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Find the next rtimer to run. */
|
2007-03-19 01:16:13 +01:00
|
|
|
n = next;
|
|
|
|
for(i = next; i != firstempty; i = (i + 1) % LIST_SIZE) {
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_run_next checking %p (%d) against %p (%d)\n",
|
|
|
|
rtimers[i], rtimers[i]->time,
|
|
|
|
rtimers[n], rtimers[n]->time);
|
2007-03-28 22:28:22 +02:00
|
|
|
if(RTIMER_CLOCK_LT(rtimers[i]->time, rtimers[n]->time)) {
|
2007-03-19 01:16:13 +01:00
|
|
|
n = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_run_next next rtimer is %d %p (%d)\n",
|
|
|
|
n, rtimers[n], rtimers[n]->time);
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
/* Put the next rtimer first in the rtimer list. */
|
|
|
|
t = rtimers[next];
|
|
|
|
rtimers[next] = rtimers[n];
|
|
|
|
rtimers[n] = t;
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-05-17 02:23:58 +02:00
|
|
|
PRINTF("rtimer_run_next scheduling %d %p (%d)\n",
|
|
|
|
next, rtimers[next], rtimers[next]->time);
|
2007-03-19 01:16:13 +01:00
|
|
|
|
2007-03-25 19:10:30 +02:00
|
|
|
rtimer_arch_schedule(rtimers[next]->time);
|
2007-03-19 01:16:13 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|