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