Fiddled more with the timing: the phase seems stable now at least. Still bugs left though...

This commit is contained in:
adamdunkels 2010-04-04 12:28:29 +00:00
parent 1fa81eac59
commit 7d6ff0289e

View File

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: contikimac.c,v 1.25 2010/04/04 07:49:31 adamdunkels Exp $ * $Id: contikimac.c,v 1.26 2010/04/04 12:28:29 adamdunkels Exp $
*/ */
/** /**
@ -51,7 +51,7 @@
#include "sys/pt.h" #include "sys/pt.h"
#include "sys/rtimer.h" #include "sys/rtimer.h"
/*#include "cooja-debug.h"*/ #include "cooja-debug.h"
#include "contiki-conf.h" #include "contiki-conf.h"
#ifdef EXPERIMENT_SETUP #ifdef EXPERIMENT_SETUP
@ -212,7 +212,7 @@ off(void)
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static rtimer_clock_t cycle_start; static volatile rtimer_clock_t cycle_start;
static char powercycle(struct rtimer *t, void *ptr); static char powercycle(struct rtimer *t, void *ptr);
static void static void
schedule_powercycle(struct rtimer *t, rtimer_clock_t time) schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
@ -221,18 +221,10 @@ schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
if(contikimac_is_on) { if(contikimac_is_on) {
if(RTIMER_CLOCK_LT(RTIMER_TIME(t) + time, RTIMER_NOW())) { if(RTIMER_CLOCK_LT(RTIMER_TIME(t) + time, RTIMER_NOW() + 1)) {
time = RTIMER_NOW() - RTIMER_TIME(t); time = RTIMER_NOW() - RTIMER_TIME(t) + 1;
} }
while(RTIMER_TIME(t) + time == RTIMER_NOW() ||
RTIMER_TIME(t) + time == RTIMER_NOW() + 1 ||
RTIMER_TIME(t) + time == RTIMER_NOW() + 2 ||
RTIMER_TIME(t) + time == RTIMER_NOW() + 3 ||
RTIMER_TIME(t) + time == RTIMER_NOW() + 4) {
time++;
}
#if NURTIMER #if NURTIMER
r = rtimer_reschedule(t, time, 1); r = rtimer_reschedule(t, time, 1);
#else #else
@ -245,6 +237,28 @@ schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
} }
} }
static void static void
schedule_powercycle_fixed(struct rtimer *t, rtimer_clock_t fixed_time)
{
int r;
if(contikimac_is_on) {
if(RTIMER_CLOCK_LT(fixed_time, RTIMER_NOW() + 1)) {
fixed_time = RTIMER_NOW() + 1;
}
#if NURTIMER
r = rtimer_reschedule(t, RTIMER_TIME(t) - time, 1);
#else
r = rtimer_set(t, fixed_time, 1,
(void (*)(struct rtimer *, void *))powercycle, NULL);
#endif
if(r != RTIMER_OK) {
printf("schedule_powercycle: could not set rtimer\n");
}
}
}
static void
powercycle_turn_radio_off(void) powercycle_turn_radio_off(void)
{ {
if(we_are_sending == 0) { if(we_are_sending == 0) {
@ -261,22 +275,6 @@ powercycle_turn_radio_on(void)
static char static char
powercycle(struct rtimer *t, void *ptr) powercycle(struct rtimer *t, void *ptr)
{ {
rtimer_clock_t start;
if(is_streaming) {
start = RTIMER_NOW();
#if NURTIMER
if(!RTIMER_CLOCK_LT(start, RTIMER_NOW(), stream_until))
#else
if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until))
#endif
{
is_streaming = 0;
rimeaddr_copy(&is_streaming_to, &rimeaddr_null);
rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null);
}
}
PT_BEGIN(&pt); PT_BEGIN(&pt);
while(1) { while(1) {
@ -286,6 +284,19 @@ powercycle(struct rtimer *t, void *ptr)
cycle_start = RTIMER_NOW(); cycle_start = RTIMER_NOW();
if(WITH_STREAMING && is_streaming) {
#if NURTIMER
if(!RTIMER_CLOCK_LT(cycle_start, RTIMER_NOW(), stream_until))
#else
if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until))
#endif
{
is_streaming = 0;
rimeaddr_copy(&is_streaming_to, &rimeaddr_null);
rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null);
}
}
packet_seen = 0; packet_seen = 0;
do { do {
@ -313,6 +324,7 @@ powercycle(struct rtimer *t, void *ptr)
powercycle_turn_radio_off(); powercycle_turn_radio_off();
} }
schedule_powercycle(t, CCA_SLEEP_TIME); schedule_powercycle(t, CCA_SLEEP_TIME);
// COOJA_DEBUG_STR("yield\n");
PT_YIELD(&pt); PT_YIELD(&pt);
} }
@ -394,7 +406,8 @@ powercycle(struct rtimer *t, void *ptr)
leds_on(LEDS_RED); leds_on(LEDS_RED);
} }
if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME)) { if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME)) {
schedule_powercycle(t, CYCLE_TIME - (RTIMER_NOW() - cycle_start) + 1); /* schedule_powercycle(t, CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/
schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start);
/* printf("cycle_start 0x%02x now 0x%02x wait 0x%02x\n", /* printf("cycle_start 0x%02x now 0x%02x wait 0x%02x\n",
cycle_start, RTIMER_NOW(), CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/ cycle_start, RTIMER_NOW(), CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/
PT_YIELD(&pt); PT_YIELD(&pt);
@ -962,6 +975,7 @@ cycle_announcement(void *ptr)
static void static void
listen_callback(int periods) listen_callback(int periods)
{ {
printf("Snoop\n");
is_snooping = periods + 1; is_snooping = periods + 1;
} }
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */ #endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
@ -1047,6 +1061,9 @@ const struct rdc_driver contikimac_driver = {
uint16_t uint16_t
contikimac_debug_print(void) contikimac_debug_print(void)
{ {
static rtimer_clock_t one_cycle_start;
printf("Drift %d\n", (one_cycle_start - cycle_start) % CYCLE_TIME);
one_cycle_start = cycle_start;
return 0; return 0;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/