osd-contiki/examples/sensinode/timer-test.c

138 lines
3.3 KiB
C
Raw Normal View History

2012-03-05 17:28:06 +01:00
/**
* \file
* Tests related to clocks and timers
* This is based on clock_test.c from the original sensinode port
2012-03-05 17:28:06 +01:00
*
* \author
* Zach Shelby <zach@sensinode.com> (Original)
* George Oikonomou - <oikonomou@users.sourceforge.net> (rtimer code)
*
*/
#include "contiki.h"
#include "sys/clock.h"
#include "sys/rtimer.h"
#include "dev/leds.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
#define TEST_CLOCK_DELAY 1
#define TEST_RTIMER 1
#define TEST_ETIMER 1
#define TEST_CLOCK_SECONDS 1
/*---------------------------------------------------------------------------*/
static struct etimer et;
#if TEST_CLOCK_DELAY
static rtimer_clock_t start_count, end_count, diff;
#endif
#if TEST_CLOCK_SECONDS
static unsigned long sec;
#endif
#if TEST_ETIMER
static clock_time_t count;
#endif
#if TEST_RTIMER
static struct rtimer rt;
rtimer_clock_t rt_now, rt_for;
static clock_time_t ct;
#endif
static uint8_t i;
/*---------------------------------------------------------------------------*/
2012-03-05 17:28:06 +01:00
PROCESS(clock_test_process, "Clock test process");
AUTOSTART_PROCESSES(&clock_test_process);
/*---------------------------------------------------------------------------*/
#if TEST_RTIMER
void
rt_callback(struct rtimer *t, void *ptr) {
rt_now = RTIMER_NOW();
ct = clock_time();
printf("Task called at %u (clock = %u)\n", rt_now, ct);
2012-03-05 17:28:06 +01:00
}
#endif
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(clock_test_process, ev, data)
{
PROCESS_BEGIN();
etimer_set(&et, 2 * CLOCK_SECOND);
PROCESS_YIELD();
2012-03-05 17:28:06 +01:00
#if TEST_CLOCK_DELAY
printf("Clock delay test, (10,000 x i) cycles:\n");
2012-03-05 17:28:06 +01:00
i = 1;
while(i < 7) {
start_count = RTIMER_NOW();
2012-03-05 17:28:06 +01:00
clock_delay(10000 * i);
end_count = RTIMER_NOW();
2012-03-05 17:28:06 +01:00
diff = end_count - start_count;
printf("Delayed %u = %u rtimer ticks = ~%u us\n", 10000 * i, diff,
diff * 64);
2012-03-05 17:28:06 +01:00
i++;
}
#endif
#if TEST_RTIMER
printf("Rtimer Test, 1 sec (%u rtimer ticks):\n", RTIMER_SECOND);
2012-03-05 17:28:06 +01:00
i = 0;
while(i < 5) {
2012-03-05 17:28:06 +01:00
etimer_set(&et, 2*CLOCK_SECOND);
printf("=======================\n");
ct = clock_time();
2012-03-05 17:28:06 +01:00
rt_now = RTIMER_NOW();
rt_for = rt_now + RTIMER_SECOND;
printf("Now=%u (clock = %u) - For=%u\n", rt_now, ct, rt_for);
2012-03-05 17:28:06 +01:00
if (rtimer_set(&rt, rt_for, 1,
(void (*)(struct rtimer *, void *))rt_callback, NULL) != RTIMER_OK) {
printf("Error setting\n");
}
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
i++;
}
#endif
#if TEST_ETIMER
printf("Clock tick and etimer test, 1 sec (%u clock ticks):\n", CLOCK_SECOND);
2012-03-05 17:28:06 +01:00
i = 0;
while(i < 10) {
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
etimer_reset(&et);
count = clock_time();
printf("%u ticks\n", count);
leds_toggle(LEDS_RED);
i++;
}
#endif
#if TEST_CLOCK_SECONDS
printf("Clock seconds test (5s):\n");
2012-03-05 17:28:06 +01:00
i = 0;
while(i < 10) {
etimer_set(&et, 5 * CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
etimer_reset(&et);
sec = clock_seconds();
printf("%lu seconds\n", sec);
2012-03-05 17:28:06 +01:00
leds_toggle(LEDS_GREEN);
i++;
}
#endif
printf("Done!\n");
2012-03-05 17:28:06 +01:00
PROCESS_END();
}
/*---------------------------------------------------------------------------*/