nes-proj/examples/sensinode/timer-test.c

140 lines
3.3 KiB
C
Raw Normal View History

2012-03-05 16:28:06 +00:00
/**
* \file
* Tests related to clocks and timers
* This is based on clock_test.c from the original sensinode port
2012-03-05 16:28:06 +00: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_USEC 1
#define TEST_RTIMER 1
#define TEST_ETIMER 1
#define TEST_CLOCK_SECONDS 1
2012-03-05 16:28:06 +00:00
/*---------------------------------------------------------------------------*/
static struct etimer et;
#if TEST_CLOCK_DELAY_USEC
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 16:28:06 +00:00
PROCESS(clock_test_process, "Clock test process");
AUTOSTART_PROCESSES(&clock_test_process);
/*---------------------------------------------------------------------------*/
#if TEST_RTIMER
void
2012-12-16 15:29:45 +00:00
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 16:28:06 +00:00
}
#endif
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(clock_test_process, ev, data)
{
PROCESS_BEGIN();
etimer_set(&et, 2 * CLOCK_SECOND);
PROCESS_YIELD();
#if TEST_CLOCK_DELAY_USEC
printf("clock_delay_usec test, (10,000 x i) usec:\n");
2012-03-05 16:28:06 +00:00
i = 1;
while(i < 7) {
start_count = RTIMER_NOW();
clock_delay_usec(10000 * i);
end_count = RTIMER_NOW();
2012-03-05 16:28:06 +00:00
diff = end_count - start_count;
printf("Requested: %u usec, Real: %u rtimer ticks = ~%u us\n",
2012-12-16 15:29:45 +00:00
10000 * i, diff, diff * 64);
2012-03-05 16:28:06 +00:00
i++;
}
#endif
#if TEST_RTIMER
printf("Rtimer Test, 1 sec (%u rtimer ticks):\n", RTIMER_SECOND);
2012-03-05 16:28:06 +00:00
i = 0;
while(i < 5) {
2012-12-16 15:29:45 +00:00
etimer_set(&et, 2 * CLOCK_SECOND);
printf("=======================\n");
ct = clock_time();
2012-03-05 16:28:06 +00: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-12-16 15:29:45 +00:00
if(rtimer_set(&rt, rt_for, 1, (rtimer_callback_t) rt_callback, NULL) !=
RTIMER_OK) {
2012-03-05 16:28:06 +00:00
printf("Error setting\n");
}
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
i++;
}
#endif
#if TEST_ETIMER
2012-12-16 15:29:45 +00:00
printf("Clock tick and etimer test, 1 sec (%u clock ticks):\n",
CLOCK_SECOND);
2012-03-05 16:28:06 +00: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 16:28:06 +00: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 16:28:06 +00:00
leds_toggle(LEDS_GREEN);
i++;
}
#endif
printf("Done!\n");
2012-03-05 16:28:06 +00:00
PROCESS_END();
}
/*---------------------------------------------------------------------------*/